diff --git a/fontConverter/src/main.cpp b/fontConverter/src/main.cpp index 60bec8c..f4d24b7 100644 --- a/fontConverter/src/main.cpp +++ b/fontConverter/src/main.cpp @@ -26,7 +26,7 @@ int main() export_font["sizes"] = json::object(); std::vector sizes = - {12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 100, 150, 200, 250, 300}; + {12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 100, 250}; for(auto& size : sizes) { diff --git a/frame/src/Rect.hpp b/frame/src/Rect.hpp new file mode 100644 index 0000000..98c31fd --- /dev/null +++ b/frame/src/Rect.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace frame +{ + + struct Rect + { + int32_t top; + int32_t left; + int32_t width; + int32_t height; + }; +} // namespace frame \ No newline at end of file diff --git a/frame/src/main.cpp b/frame/src/main.cpp index 9506d98..9f573cd 100644 --- a/frame/src/main.cpp +++ b/frame/src/main.cpp @@ -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); } diff --git a/frame/src/render/RenderTarget.cpp b/frame/src/render/RenderTarget.cpp index 42e2164..057b29f 100644 --- a/frame/src/render/RenderTarget.cpp +++ b/frame/src/render/RenderTarget.cpp @@ -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, diff --git a/frame/src/render/RenderTarget.hpp b/frame/src/render/RenderTarget.hpp index aba08ae..91713a2 100644 --- a/frame/src/render/RenderTarget.hpp +++ b/frame/src/render/RenderTarget.hpp @@ -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,