attempt wrap text [debug]

This commit is contained in:
jrkb 2023-05-05 22:03:51 +02:00
parent 29d9b44140
commit 24273912cb
3 changed files with 123 additions and 1 deletions

View file

@ -1,4 +1,5 @@
#include "GPUFontAtlasLayerCombo.h" #include "GPUFontAtlasLayerCombo.h"
#include "GPUFontLayer.h"
#include "Utils.h" #include "Utils.h"
#include "font.hpp" #include "font.hpp"
#include "ofAppRunner.h" #include "ofAppRunner.h"
@ -67,7 +68,17 @@ void GPUFontAtlasLayerCombo::update(){
std::vector <ofxGPUFont::GlyphIdentity> _variationText; std::vector <ofxGPUFont::GlyphIdentity> _variationText;
for(const auto & layer : layers){ for(const auto & layer : layers){
std::string layerText = layer->getProps().text; std::string layerText = layer->getProps().text;
totalCharacters += layerText.length(); int mirrorMultiplier = 1;
if(layer->getProps().mirror_x){
mirrorMultiplier += 1;
}
if(layer->getProps().mirror_y){
mirrorMultiplier += 1;
}
if(layer->getProps().mirror_xy){
mirrorMultiplier += 1;
}
totalCharacters += layerText.length() * mirrorMultiplier;
_variationText.insert(_variationText.end(), _variationText.insert(_variationText.end(),
layer->getVariationText().begin(), layer->getVariationText().begin(),
layer->getVariationText().end()); layer->getVariationText().end());
@ -211,6 +222,19 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
bb, bbs, advanceY, bb, bbs, advanceY,
true, layer->getProps().fontSize_px, true, layer->getProps().fontSize_px,
layer->getProps().lineHeight); layer->getProps().lineHeight);
float lineHeight = font->getLineHeight(layer->getProps().fontSize_px) * layer->getProps().lineHeight;
int n_wraps = wrapBoundingBoxes(bbs,
layer->getVariationText(),
layer->getProps().width,
lineHeight);
if(ofGetFrameNum() % 30 == 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;
glm::vec4 transformOrigin; glm::vec4 transformOrigin;
getAndApplyTransformOrigin(transformOrigin, getAndApplyTransformOrigin(transformOrigin,
layer->getOuterNode(), layer->getOuterNode(),
@ -219,6 +243,7 @@ void GPUFontAtlasLayerCombo::draw(int width, int height){
ascender, ascender,
advanceY, advanceY,
layer->getProps()); layer->getProps());
auto bb_mirror_none = bb; auto bb_mirror_none = bb;
bb_mirror_none.multiply(layer->getInnerNode().getGlobalTransformMatrix()); bb_mirror_none.multiply(layer->getInnerNode().getGlobalTransformMatrix());
@ -418,5 +443,89 @@ void GPUFontAtlasLayerCombo::getAndApplyTransformOrigin(glm::vec4 & transformOri
momNode.rotateDeg(props.rotation, glm::vec3(0, 0, 1)); momNode.rotateDeg(props.rotation, glm::vec3(0, 0, 1));
momNode.setPosition(transformOrigin); momNode.setPosition(transformOrigin);
} }
int GPUFontAtlasLayerCombo::wrapBoundingBoxes(std::vector <ofxGPUFont::Font::BoundingBox> & bbs,
const std::vector <ofxGPUFont::GlyphIdentity> & _variationText,
int width,
float advanceY){
int i = 0;
int wrapIndex = -1;
int n_wraps = 0;
bool firstToBreak = true;
vector <int> breakPoints;
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;
wrapIndex = -1;
i++;
}else{
hasGoodCharacter = true;
}
}
if( //!hadAlwaysGoodCharacter ||
(variationText[i].charcode == '-'
|| variationText[i].charcode == ' ')){
wrapIndex = i + 1;
cout << "WRAP INDEX " << ofToString(wrapIndex) << endl;
}
if(bb.p1.x > 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;
}else{
if(firstToBreak){
breakPoints.push_back(wrapIndex);
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;
}
}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;
}
}
n_wraps++;
firstToBreak = false;
}
}
i++;
}
return n_wraps;
}
//void GPUFontAtlasLayerCombo::cullBoundingBoxes(std::vector <ofxGPUFont::Font::BoundingBox> & bbs,
//int width, int height){
//}
//bool GPUFontAtlasLayerCombo::isInside(const ofxGPUFont::Font::BoundingBox & bb,
//int width, int height){
//}
//bool GPUFontAtlasLayerCombo::isInside(const glm::vec4 & vertex,
//int width, int height){
//}
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Utils.h" #include "Utils.h"
#include "font.hpp"
#include "ofMain.h" #include "ofMain.h"
#include "AtlasLayerCombo.h" #include "AtlasLayerCombo.h"
#include "GPUFontLayer.h" #include "GPUFontLayer.h"
@ -83,6 +84,16 @@ class GPUFontAtlasLayerCombo : public AtlasLayerCombo {
bool isDirty = false; bool isDirty = false;
private: private:
int wrapBoundingBoxes(std::vector <ofxGPUFont::Font::BoundingBox> & bbs,
const std::vector <ofxGPUFont::GlyphIdentity> & _variationText,
int width,
float advanceY);
//void cullBoundingBoxes(std::vector <ofxGPUFont::Font::BoundingBox> & bbs,
//int width, int height);
//bool isInside(const ofxGPUFont::Font::BoundingBox & bb,
//int width, int height);
//bool isInside(const glm::vec4 & vertex,
//int width, int height);
GPUFontAtlasLayerComboSettings settings; GPUFontAtlasLayerComboSettings settings;
FT_Library library; FT_Library library;
vector <shared_ptr <GPUFontLayer> > layers; vector <shared_ptr <GPUFontLayer> > layers;

View file

@ -30,6 +30,8 @@ class Layer {
struct Props { struct Props {
float x = 200; float x = 200;
float y = 200; float y = 200;
float width = 0;
float height = 0;
float rotation = 0; float rotation = 0;
float fontSize_px = 42; float fontSize_px = 42;
float letterSpacing = 0; float letterSpacing = 0;