add: Rect font rendering
This commit is contained in:
15
frame/src/Rect.hpp
Normal file
15
frame/src/Rect.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace frame
|
||||
{
|
||||
|
||||
struct Rect
|
||||
{
|
||||
int32_t top;
|
||||
int32_t left;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
};
|
||||
} // namespace frame
|
||||
@@ -82,14 +82,88 @@ int main()
|
||||
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});
|
||||
|
||||
target.DrawTextGlyphBounds(text, pos, *font, 250);
|
||||
target.DrawLine({0, (int32_t)pos.y},
|
||||
{(int32_t)display->getSize().width, pos.y});
|
||||
auto rect = frame::Rect{0,
|
||||
0,
|
||||
(int)display->getSize().width,
|
||||
(int)display->getSize().height};
|
||||
|
||||
target.DrawText(rect, text, *font, 250);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
std::this_thread::sleep_for(10s);
|
||||
target.Clear();
|
||||
target.DrawText(rect, text, *font, 250, frame::AlignHorizontal::CENTER);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
target.Clear();
|
||||
target.DrawText(rect, text, *font, 250, frame::AlignHorizontal::RIGHT);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
target.Clear();
|
||||
target.DrawText(rect,
|
||||
text,
|
||||
*font,
|
||||
250,
|
||||
frame::AlignHorizontal::LEFT,
|
||||
frame::AlignVertical::CENTER);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
target.Clear();
|
||||
target.DrawText(rect,
|
||||
text,
|
||||
*font,
|
||||
250,
|
||||
frame::AlignHorizontal::CENTER,
|
||||
frame::AlignVertical::CENTER);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
target.Clear();
|
||||
target.DrawText(rect,
|
||||
text,
|
||||
*font,
|
||||
250,
|
||||
frame::AlignHorizontal::RIGHT,
|
||||
frame::AlignVertical::CENTER);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
target.Clear();
|
||||
target.DrawText(rect,
|
||||
text,
|
||||
*font,
|
||||
250,
|
||||
frame::AlignHorizontal::LEFT,
|
||||
frame::AlignVertical::BOTTOM);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
target.Clear();
|
||||
target.DrawText(rect,
|
||||
text,
|
||||
*font,
|
||||
250,
|
||||
frame::AlignHorizontal::CENTER,
|
||||
frame::AlignVertical::BOTTOM);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
target.Clear();
|
||||
target.DrawText(rect,
|
||||
text,
|
||||
*font,
|
||||
250,
|
||||
frame::AlignHorizontal::RIGHT,
|
||||
frame::AlignVertical::BOTTOM);
|
||||
display->Display(target.getImage());
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
display->Clear(frame::display::Color::WHITE);
|
||||
}
|
||||
|
||||
@@ -182,6 +182,48 @@ namespace frame::render
|
||||
}
|
||||
}
|
||||
|
||||
void RenderTarget::DrawText(Rect rect,
|
||||
std::string pText,
|
||||
font::Font const& pFont,
|
||||
uint32_t size,
|
||||
AlignHorizontal alignHorizontal,
|
||||
AlignVertical alignVertical)
|
||||
{
|
||||
auto const lineSpacing = pFont.getLineSpacing(size);
|
||||
auto const lineSpacing_3 = lineSpacing / 3;
|
||||
|
||||
Vector start{rect.top, rect.left};
|
||||
auto text_height = pFont.getHeight(pText, size);
|
||||
auto length = pFont.getLength(pText, size);
|
||||
auto y_offset = pFont.getMaxYOffset(pText, size);
|
||||
|
||||
switch(alignVertical)
|
||||
{
|
||||
case AlignVertical::TOP: start.y += lineSpacing_3 * 2; break;
|
||||
case AlignVertical::CENTER:
|
||||
start.y += rect.height / 2;
|
||||
start.y += text_height / 2;
|
||||
start.y -= y_offset + text_height;
|
||||
break;
|
||||
case AlignVertical::BOTTOM:
|
||||
start.y += rect.height;
|
||||
start.y += text_height + y_offset;
|
||||
break;
|
||||
}
|
||||
|
||||
switch(alignHorizontal)
|
||||
{
|
||||
case AlignHorizontal::CENTER:
|
||||
start.x += rect.width / 2;
|
||||
start.x -= length / 2;
|
||||
break;
|
||||
|
||||
case AlignHorizontal::RIGHT: start.x += rect.width - length; break;
|
||||
}
|
||||
|
||||
DrawText(pText, start, pFont, size);
|
||||
}
|
||||
|
||||
void RenderTarget::DrawTextGlyphBounds(std::string_view pText,
|
||||
Vector pCenterLineStart,
|
||||
font::Font const& pFont,
|
||||
|
||||
@@ -2,9 +2,16 @@
|
||||
|
||||
#include "../Color.hpp"
|
||||
#include "../Image.hpp"
|
||||
#include "../Rect.hpp"
|
||||
#include "../Vector.hpp"
|
||||
#include "../font/Font.hpp"
|
||||
|
||||
namespace frame
|
||||
{
|
||||
enum class AlignVertical { TOP, CENTER, BOTTOM };
|
||||
enum class AlignHorizontal { LEFT, CENTER, RIGHT };
|
||||
} // namespace frame
|
||||
|
||||
namespace frame::render
|
||||
{
|
||||
|
||||
@@ -45,6 +52,13 @@ namespace frame::render
|
||||
font::Font const& pFont,
|
||||
uint32_t size);
|
||||
|
||||
void DrawText(Rect rect,
|
||||
std::string pText,
|
||||
font::Font const& pFont,
|
||||
uint32_t size,
|
||||
AlignHorizontal alignHorizontal = AlignHorizontal::LEFT,
|
||||
AlignVertical alignVertical = AlignVertical::TOP);
|
||||
|
||||
void DrawTextGlyphBounds(std::string_view pText,
|
||||
Vector pCenterLineStart,
|
||||
font::Font const& pFont,
|
||||
|
||||
Reference in New Issue
Block a user