Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for text displacement on pxTextCanvas #2021

Merged
merged 1 commit into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions examples/pxScene2d/src/pxTextCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ void pxTextCanvas::sendPromise()
}
}

rtError pxTextCanvas::setPixelSize(uint32_t v)
{
mPixelSize = v;
setNeedsRecalc(true);
return RT_OK;
}

void pxTextCanvas::renderText(bool render)
{
if (render && !mTextLines.empty()) {
Expand All @@ -336,8 +343,8 @@ void pxTextCanvas::renderTextLine(const pxTextLine& textLine)
float sx = 1.0;
float sy = 1.0;

float charW = 0, charH = 0;
uint32_t size = textLine.pixelSize;

if (mFont != textLine.font)
{
setFont(textLine.font);
Expand All @@ -350,16 +357,12 @@ void pxTextCanvas::renderTextLine(const pxTextLine& textLine)
);

// TODO: calc alignment

if (getFontResource() != nullptr)
{
getFontResource()->measureTextInternal(cStr, size, sx, sy, charW, charH);
//TODO: add clipping support here
mw = charW > mw ? charW : mw;
//getFontResource()->measureTextInternal(cStr, size, sx, sy, charW, charH);
}

mh += charH; // TODO: hack, remove. For some reason canvas size calculated in js is smaller than expected
// this allows to account for height changes when drawing multiple lines

// Now, render the text
if( getFontResource() != nullptr)
{
Expand Down Expand Up @@ -443,7 +446,8 @@ rtError pxTextCanvas::fillText(rtString text, uint32_t x, uint32_t y)
, mw
, mh
);
pxTextLine textLine(text, x, y);
pxTextLine textLine(text, x, y - mPixelSize); // subtract pixelSize from y in order to get y = 0 positioned
// at the text baseline like HTML canvas does
rtValue color;
textColor(color);
textLine.setStyle(mFont, mPixelSize, color.toInt32());
Expand All @@ -455,8 +459,6 @@ rtError pxTextCanvas::fillText(rtString text, uint32_t x, uint32_t y)
rtError pxTextCanvas::clear()
{
mTextLines.clear();
mw = 0;
mh = 0;
mTranslateX = 0;
mTranslateY = 0;
setNeedsRecalc(true);
Expand Down
2 changes: 2 additions & 0 deletions examples/pxScene2d/src/pxTextCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class pxTextCanvas : public pxText {
rtError setSX(float v) override { setNeedsRecalc(true); return pxObject::setSX(v);}
rtError setSY(float v) override { setNeedsRecalc(true); return pxObject::setSY(v);}

rtError setPixelSize(uint32_t v) override ;

void renderText(bool render);
void setNeedsRecalc(bool recalc);
virtual float getFBOWidth();
Expand Down