ttf_loader: changing the reference point for text

After the change, the reference point for text is the left
edge of the baseline.
This commit is contained in:
Mira Grudzinska 2025-05-15 01:09:10 +02:00
parent 1e692f223c
commit 0b82bf0dbd
5 changed files with 21 additions and 21 deletions

View file

@ -62,12 +62,13 @@ struct UserExample : tvgexam::Example
text->font("Arial", 80);
text->text("THORVG Text");
text->fill(255, 255, 255);
text->translate(0, 100); //text baseline at y=100
canvas->push(text);
auto text2 = tvg::Text::gen();
text2->font("Arial", 30, "italic");
text2->text("Font = \"Arial\", Size = 40, Style=Italic");
text2->translate(0, 150);
text2->translate(0, 180); //text baseline at y=180
text2->fill(255, 255, 255);
canvas->push(text2);
@ -75,43 +76,42 @@ struct UserExample : tvgexam::Example
text3->font(nullptr, 40); //Use any font
text3->text("Kerning Test: VA, AV, TJ, JT");
text3->fill(255, 255, 255);
text3->translate(0, 225);
text3->translate(0, 275);
canvas->push(text3);
auto text4 = tvg::Text::gen();
text4->font("Arial", 25);
text4->text("Purple Text");
text4->fill(255, 0, 255);
text4->translate(0, 310);
text4->translate(0, 330);
canvas->push(text4);
auto text5 = tvg::Text::gen();
text5->font("Arial", 25);
text5->text("Gray Text");
text5->fill(150, 150, 150);
text5->translate(220, 310);
text5->translate(220, 330);
canvas->push(text5);
auto text6 = tvg::Text::gen();
text6->font("Arial", 25);
text6->text("Yellow Text");
text6->fill(255, 255, 0);
text6->translate(400, 310);
text6->translate(400, 330);
canvas->push(text6);
auto text7 = tvg::Text::gen();
text7->font("Arial", 15);
text7->text("Transformed Text - 30'");
text7->text("Transformed Text - 0'");
text7->fill(0, 0, 0);
text7->translate(600, 400);
text7->rotate(30);
text7->translate(600, 410);
canvas->push(text7);
auto text8 = tvg::Text::gen();
text8->font("Arial", 15);
text8->fill(0, 0, 0);
text8->text("Transformed Text - 90'");
text8->translate(600, 400);
text8->translate(600, 410);
text8->rotate(90);
canvas->push(text8);
@ -119,7 +119,7 @@ struct UserExample : tvgexam::Example
text9->font("Arial", 15);
text9->fill(0, 0, 0);
text9->text("Transformed Text - 180'");
text9->translate(800, 400);
text9->translate(600, 410);
text9->rotate(180);
canvas->push(text9);
@ -143,7 +143,7 @@ struct UserExample : tvgexam::Example
fill->colorStops(colorStops, 3);
text10->fill(fill);
text10->translate(0, 350);
text10->translate(0, 400);
canvas->push(text10);
@ -166,7 +166,7 @@ struct UserExample : tvgexam::Example
text11->fill(fill2);
text11->translate(0, 450);
text11->translate(0, 500);
canvas->push(text11);
@ -174,7 +174,7 @@ struct UserExample : tvgexam::Example
text12->font("SentyCloud", 50);
text12->fill(255, 25, 25);
text12->text("\xe4\xb8\x8d\xe5\x88\xb0\xe9\x95\xbf\xe5\x9f\x8e\xe9\x9d\x9e\xe5\xa5\xbd\xe6\xb1\x89\xef\xbc\x81");
text12->translate(0, 525);
text12->translate(0, 600);
canvas->push(text12);
return true;

View file

@ -1576,6 +1576,8 @@ public:
*
* @param[in] text The multi-byte text encoded with utf8 string to be rendered.
*
* @note The reference point for text is the left edge of the baseline. Unless the text is explicitly translated,
* this point will be placed at (0, 0) of the canvas which, by default, lies outside the visible rendering area.
* @since 1.0
*/
Result text(const char* text) noexcept;

View file

@ -900,7 +900,7 @@ static void _fontText(TextDocument& doc, Scene* scene)
strcpy(buf, doc.text);
auto token = std::strtok(buf, delim);
auto cnt = 0;
auto cursorY = 0.0f;
while (token) {
auto txt = Text::gen();
if (txt->font(doc.name, size) != Result::Success) {
@ -913,14 +913,12 @@ static void _fontText(TextDocument& doc, Scene* scene)
float width;
txt->bounds(nullptr, nullptr, &width, nullptr);
txt->translate(width * doc.justify, cursorY);
auto cursorX = width * doc.justify;
auto cursorY = lineHeight * cnt;
txt->translate(cursorX, -lineHeight + cursorY);
scene->push(txt);
token = std::strtok(nullptr, delim);
scene->push(txt);
cnt++;
cursorY += lineHeight;
}
}

View file

@ -867,7 +867,7 @@ static Paint* _textBuildHelper(SvgLoaderData& loaderData, const SvgNode* node, c
if (node->transform) textTransform = *node->transform;
else textTransform = tvg::identity();
translateR(&textTransform, {node->node.text.x, node->node.text.y - textNode->fontSize});
translateR(&textTransform, {node->node.text.x, node->node.text.y});
text->transform(textTransform);
//TODO: handle def values of font and size as used in a system?

View file

@ -288,7 +288,7 @@ bool TtfLoader::read(Shape* shape, char* text, FontMetrics& out)
//TODO: optimize with the texture-atlas?
TtfGlyphMetrics gmetrics;
Point offset = {0.0f, reader.metrics.hhea.ascent};
Point offset = {0.0f, 0.0f};
Point kerning = {0.0f, 0.0f};
auto lglyph = INVALID_GLYPH;
auto loadMinw = true;