wrapping with slight error on side

This commit is contained in:
jrkb 2023-05-06 22:12:40 +02:00
parent 24273912cb
commit 976e309a54

View file

@ -210,6 +210,7 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
indices.resize(totalCharacters * 6);
std::vector <ofVboMesh> outerBoundingBoxes;
ofRectangle r;
for(const auto & layer : layers){
OFX_PROFILER_SCOPE("draw layer");
float ascender = font->getAscender(layer->getProps().fontSize_px);
@ -227,14 +228,14 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
layer->getVariationText(),
layer->getProps().width,
lineHeight);
if(ofGetFrameNum() % 30 == 0){
if(ofGetFrameNum() % 600 == 0){
cout
<< "n_wraps: " << ofToString(n_wraps) << endl
<< "layer->getProps().width: " << ofToString(layer->getProps().width) << endl
;
}
bb.p2.y += n_wraps * lineHeight;
bb.p3.y += n_wraps * lineHeight;
//bb.p2.y += n_wraps * lineHeight;
//bb.p3.y += n_wraps * lineHeight;
glm::vec4 transformOrigin;
getAndApplyTransformOrigin(transformOrigin,
layer->getOuterNode(),
@ -347,6 +348,8 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
float width = max_x - min_x;
float height = max_y - min_y;
layer->setBoundingBox(Layer::BoundingBox{min_x, min_y, width, height});
r = ofRectangle(layer->getProps().x, layer->getProps().y, layer->getProps().width, 800);
}
{
@ -355,6 +358,11 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
}
glUseProgram(currentProgram);
ofPushStyle();
ofSetColor(ofColor::green);
ofNoFill();
ofDrawRectangle(r);
ofPopStyle();
//ofPushStyle();
//ofSetColor(ofColor::white);
//for(const auto & b : outerBoundingBoxes){
@ -449,71 +457,88 @@ int GPUFontAtlasLayerCombo::wrapBoundingBoxes(std::vector <ofxGPUFont::Font::Bou
float advanceY){
int i = 0;
int vi = 0;
int wrapIndex = -1;
int n_wraps = 0;
bool firstToBreak = true;
vector <int> breakPoints;
float collectedWrapWidth = 0;
for(ofxGPUFont::Font::BoundingBox & bb : bbs){
bool hasGoodCharacter = false;
bool hadAlwaysGoodCharacter = true;
while(!hasGoodCharacter && i < variationText.size() - 1){
if(variationText[i].charcode == '\0'
|| variationText[i].charcode == '\r'
|| variationText[i].charcode == '\n'){
hadAlwaysGoodCharacter = false;
while(!hasGoodCharacter && vi < variationText.size() - 1){
if(variationText[vi].charcode == '\0'
|| variationText[vi].charcode == '\r'
|| variationText[vi].charcode == '\n'){
wrapIndex = -1;
i++;
collectedWrapWidth = 0;
vi++;
}else if(variationText[vi].charcode == ' '){ // TODO: any whitespace
wrapIndex = i;
vi++;
}else{
hasGoodCharacter = true;
}
}
if( //!hadAlwaysGoodCharacter ||
(variationText[i].charcode == '-'
|| variationText[i].charcode == ' ')){
wrapIndex = i + 1;
cout << "WRAP INDEX " << ofToString(wrapIndex) << endl;
if(variationText[vi].charcode == '-'){
wrapIndex = i;
}
if(bb.p1.x > width){
float xxx = bb.p1.x - collectedWrapWidth;
if(ofGetFrameNum() % 600 == 0){
cout << "WRAP INDEX " << char(variationText[vi].charcode) << ":" << ofToString(wrapIndex) << " xxx: " << ofToString(xxx) << " cww: " << ofToString(collectedWrapWidth) << endl;
}
if(bb.p1.x - collectedWrapWidth > width){
if(wrapIndex >= 0){
if(wrapIndex == i){
breakPoints.push_back(i);
bb.p0.x = 0;
bb.p1.x -= bb.p0.x;
bb.p2.x -= bb.p0.x;
bb.p3.x -= bb.p0.x;
bb.p0.y += (n_wraps + 1) * advanceY;
bb.p1.y += (n_wraps + 1) * advanceY;
bb.p2.y += (n_wraps + 1) * advanceY;
bb.p3.y += (n_wraps + 1) * advanceY;
float wrapWidth = bb.p0.x;
collectedWrapWidth = wrapWidth;
n_wraps++;
bb.p0.x -= wrapWidth;
bb.p1.x -= wrapWidth;
bb.p2.x -= wrapWidth;
bb.p3.x -= wrapWidth;
bb.p0.y += (n_wraps) * advanceY;
bb.p1.y += (n_wraps) * advanceY;
bb.p2.y += (n_wraps) * advanceY;
bb.p3.y += (n_wraps) * advanceY;
}else{
if(firstToBreak){
breakPoints.push_back(wrapIndex);
collectedWrapWidth = bbs[wrapIndex].p0.x;
n_wraps++;
for(int w = wrapIndex; w <= i; w++){
bbs[w].p0.x -= bbs[wrapIndex].p0.x;
bbs[w].p1.x -= bbs[wrapIndex].p0.x;
bbs[w].p2.x -= bbs[wrapIndex].p0.x;
bbs[w].p3.x -= bbs[wrapIndex].p0.x;
bbs[w].p0.y += (n_wraps + 1) * advanceY;
bbs[w].p1.y += (n_wraps + 1) * advanceY;
bbs[w].p2.y += (n_wraps + 1) * advanceY;
bbs[w].p3.y += (n_wraps + 1) * advanceY;
bbs[w].p0.x -= collectedWrapWidth;
bbs[w].p1.x -= collectedWrapWidth;
bbs[w].p2.x -= collectedWrapWidth;
bbs[w].p3.x -= collectedWrapWidth;
bbs[w].p0.y += (n_wraps) * advanceY;
bbs[w].p1.y += (n_wraps) * advanceY;
bbs[w].p2.y += (n_wraps) * advanceY;
bbs[w].p3.y += (n_wraps) * advanceY;
}
}else{
bb.p0.x -= bbs[wrapIndex].p0.x;
bb.p1.x -= bbs[wrapIndex].p0.x;
bb.p2.x -= bbs[wrapIndex].p0.x;
bb.p3.x -= bbs[wrapIndex].p0.x;
bb.p0.y += (n_wraps + 1) * advanceY;
bb.p1.y += (n_wraps + 1) * advanceY;
bb.p2.y += (n_wraps + 1) * advanceY;
bb.p3.y += (n_wraps + 1) * advanceY;
bb.p0.x -= collectedWrapWidth;
bb.p1.x -= collectedWrapWidth;
bb.p2.x -= collectedWrapWidth;
bb.p3.x -= collectedWrapWidth;
bb.p0.y += (n_wraps) * advanceY;
bb.p1.y += (n_wraps) * advanceY;
bb.p2.y += (n_wraps) * advanceY;
bb.p3.y += (n_wraps) * advanceY;
}
}
n_wraps++;
firstToBreak = false;
}
}else{
bb.p0.x -= collectedWrapWidth;
bb.p1.x -= collectedWrapWidth;
bb.p2.x -= collectedWrapWidth;
bb.p3.x -= collectedWrapWidth;
bb.p0.y += (n_wraps) * advanceY;
bb.p1.y += (n_wraps) * advanceY;
bb.p2.y += (n_wraps) * advanceY;
bb.p3.y += (n_wraps) * advanceY;
}
vi++;
i++;
}
return n_wraps;