add: more font

This commit is contained in:
Simon Hardt
2022-03-05 21:58:04 +01:00
parent 0553927dad
commit 0fe1509c6c
9 changed files with 149 additions and 52 deletions

View File

@@ -1,7 +1,10 @@
#include "Image.hpp"
#include "Color.hpp"
#include <stdint.h>
namespace frame
{
@@ -36,6 +39,11 @@ namespace frame
return mBuffer.at(toInternal(x, y));
}
void Image::Clear(Color color)
{
memset(mBuffer.data(), color, mBuffer.size());
}
void Image::operator=(Image const& image)
{
mHeight = image.mHeight;

View File

@@ -1,9 +1,11 @@
#pragma once
#include "Color.hpp"
#include "Size.hpp"
#include <cstdint>
#include <vector>
namespace frame
{
@@ -25,6 +27,8 @@ namespace frame
uint8_t& at(uint32_t x, uint32_t y);
uint8_t const& at(uint32_t x, uint32_t y) const;
void Clear(Color color);
auto getWidth() const
{
return mWidth;

View File

@@ -24,6 +24,47 @@ namespace frame::font
}
}
Glyph const& Font::getGlyph(uint32_t size, char c) const
{
return sizes.at(size).at(c);
}
uint32_t Font::getLength(std::string_view text, uint32_t size) const
{
uint32_t length = 0;
for(auto c : text)
{
auto const& g = getGlyph(size, c);
length += g.advance;
}
return length;
}
uint32_t Font::getHeight(std::string_view text, uint32_t size) const
{
uint32_t height = 0;
for(auto c : text)
{
auto const& g = getGlyph(size, c);
height = std::max((uint32_t)g.height, height);
}
return height;
}
int32_t Font::getMaxYOffset(std::string_view text, uint32_t size) const
{
int32_t y_offset = 0;
for(auto c : text)
{
auto const& g = getGlyph(size, c);
y_offset = std::min(g.y_offset, y_offset);
}
return y_offset;
}
void Font::LoadFromStream(std::istream& os)
{
json file_data = json::parse(os);
@@ -46,9 +87,4 @@ namespace frame::font
}
}
Glyph const& Font::getGlyph(uint32_t size, char c) const
{
return sizes.at(size).at(c);
}
} // namespace frame::font

View File

@@ -9,12 +9,12 @@
namespace frame::font
{
using Glyphs = std::map<char, Glyph>;
using LineSpacing = std::map<uint8_t, uint32_t>;
using LineSpacing = std::map<uint32_t, uint32_t>;
class Font
{
std::string name;
std::map<uint8_t, Glyphs> sizes;
std::map<uint32_t, Glyphs> sizes;
LineSpacing lineSpacing;
public:
@@ -23,6 +23,15 @@ namespace frame::font
Glyph const& getGlyph(uint32_t size, char c) const;
uint32_t getLength(std::string_view text, uint32_t size) const;
uint32_t getHeight(std::string_view text, uint32_t size) const;
int32_t getMaxYOffset(std::string_view text, uint32_t size) const;
uint32_t getLineSpacing(uint32_t size) const
{
return lineSpacing.at(size);
}
protected:
void LoadFromStream(std::istream& os);
};

View File

@@ -13,9 +13,9 @@ namespace frame::font
Image image;
uint8_t height;
uint8_t width;
int8_t x_offset;
int8_t y_offset;
int8_t advance;
int32_t x_offset;
int32_t y_offset;
int32_t advance;
public:
Glyph() = default;

View File

@@ -65,12 +65,32 @@ int main()
{110, (int)display->getSize().height - 10},
5);
target.DrawText("abcdefghijklmnopqrstuvwxyz", {100, 50}, *font, 14);
target.DrawText("Hallo Welt!", {100, 50}, *font, 14);
target.DrawText("Hallo Welt!", {50, 100}, *font, 30);
display->Display(target.getImage());
std::this_thread::sleep_for(10s);
target.Clear();
frame::Vector pos = display->getSize() / 2;
auto text = "10:30";
pos.x -= font->getLength(text, 250) / 2;
pos.y += font->getHeight(text, 250) / 2;
pos.y -= font->getMaxYOffset(text, 250) + font->getHeight(text, 250);
target.DrawText(text, pos, *font, 250);
target.DrawTextGlyphBounds(text, pos, *font, 250);
target.DrawLine({0, (int32_t)pos.y},
{(int32_t)display->getSize().width, pos.y});
display->Display(target.getImage());
std::this_thread::sleep_for(10s);
display->Clear(frame::display::Color::WHITE);
}

View File

@@ -173,7 +173,7 @@ namespace frame::render
void RenderTarget::DrawText(std::string_view pText,
Vector pCenterLineStart,
font::Font const& pFont,
uint8_t size)
uint32_t size)
{
auto pos = pCenterLineStart;
for(auto c : pText)
@@ -182,6 +182,31 @@ namespace frame::render
}
}
void RenderTarget::DrawTextGlyphBounds(std::string_view pText,
Vector pCenterLineStart,
font::Font const& pFont,
uint32_t size)
{
auto pos = pCenterLineStart;
auto lineSpacing = pFont.getLineSpacing(size);
auto _3 = lineSpacing / 3;
for(auto c : pText)
{
auto const& g = pFont.getGlyph(size, c);
DrawRect({int32_t(pos.x), int32_t(pos.y - _3 * 2)},
{int32_t(pos.x + g.advance), int32_t(pos.y + _3)},
1);
pos.x += g.advance;
}
}
void RenderTarget::Clear(Color color)
{
image.Clear(color);
}
void RenderTarget::DrawPointsMirrorCircle(Vector const& center,
Vector const& pos,
Color color)

View File

@@ -43,7 +43,14 @@ namespace frame::render
void DrawText(std::string_view pText,
Vector pCenterLineStart,
font::Font const& pFont,
uint8_t size);
uint32_t size);
void DrawTextGlyphBounds(std::string_view pText,
Vector pCenterLineStart,
font::Font const& pFont,
uint32_t size);
void Clear(Color color = WHITE);
Image const& getImage()
{