From 20ad951c4625ea565014971df74a48c65450f4bb Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Wed, 6 Sep 2023 18:17:53 +0200 Subject: [PATCH] fix text alignment --- src/GPUFontAtlasLayerCombo.cpp | 36 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/GPUFontAtlasLayerCombo.cpp b/src/GPUFontAtlasLayerCombo.cpp index 8ea1f36..8121cde 100644 --- a/src/GPUFontAtlasLayerCombo.cpp +++ b/src/GPUFontAtlasLayerCombo.cpp @@ -794,12 +794,9 @@ int GPUFontAtlasLayerCombo::wrapBoundingBoxes(std::vector lineBreaks; - std::vector goodCharacters; int lastGoodCharacter = -1; //textAlignment = ofMap(sin(ofGetElapsedTimef()), -1, 1, 0, 1); - std::vector vis; std::vector wrapIndices; { // collect existing linebreaks, wrapindices and good characters @@ -824,7 +821,6 @@ int GPUFontAtlasLayerCombo::wrapBoundingBoxes(std::vector 0 && bb.p1.x > width){ if(wrapIndex >= 0 && std::find(lineBreaks.begin(), lineBreaks.end(), wrapIndex - 1) == lineBreaks.end()){ - lastBrokenWrapIndex = wrapIndex; wrapWidth = bbs[wrapIndex].p0.x; int nextLineBreak = INT_MAX; for(int lb = 0; lb < lineBreaks.size(); lb++){ @@ -855,6 +850,7 @@ int GPUFontAtlasLayerCombo::wrapBoundingBoxes(std::vector 0){ lineBreaks.push_back(lastGoodCharacter); } - //float w = width > 0 ? width : maxX; - float w = maxX; + std::sort(lineBreaks.begin(), lineBreaks.end()); + + // following may seem overcomplicated, though: + // if width is set, we use width to shift text alignment + // if not, we calculate maximum x and use that + // the syntax makes it possible to only calculate + // when we need it + float w = width > 0 ? width : + std::accumulate(bbs.begin(), + bbs.end(), + -1 * FLT_MAX, + [](float a, + const ofxGPUFont::Font::BoundingBox & bb) + { + return max(a, bb.p1.x); + }); int startFromCharacter = 0; for(int lineBreak : lineBreaks){ ofxGPUFont::Font::BoundingBox & bbb = bbs[lineBreak]; - int vi = vis[lineBreak]; - int vi2 = vis[startFromCharacter]; - variationTextAppearance[vi].color[0] = 1.0; - variationTextAppearance[vi2].color[1] = 1.0; + float shiftX = (w - bbb.p1.x) * textAlignment; for(int i = startFromCharacter; i <= lineBreak; i++){ auto & bb = bbs[i];