From 207e2537b13f8249bc8891a69f32f02c184f724a Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Thu, 30 Mar 2023 16:22:35 +0200 Subject: [PATCH] interpolate glyph pairs --- src/Layer.cpp | 161 +++++++++++++++++++-------------------- src/Layer.h | 3 +- src/LayerComposition.cpp | 6 +- 3 files changed, 81 insertions(+), 89 deletions(-) diff --git a/src/Layer.cpp b/src/Layer.cpp index 981e360..b112fc5 100644 --- a/src/Layer.cpp +++ b/src/Layer.cpp @@ -26,15 +26,16 @@ void MsdfLayer::draw(glm::vec3 position) const { const ofImage & atlasImage = atlas->getAtlasImage(); ofDisableAlphaBlending(); ofEnableDepthTest(); - shader->begin(); - shader->setUniformTexture("msdf", atlasImage.getTexture(), 0); - shader->setUniform4f("fontColor", ofFloatColor::white); float pixelRange = 2; int atlas_w = atlasImage.getWidth(); int atlas_h = atlasImage.getHeight(); int atlas_x = 0; int atlas_y = 0; glm::vec2 unitRange = glm::vec2(pixelRange) / glm::vec2(atlas_w, atlas_h); + + shader->begin(); + shader->setUniformTexture("msdf", atlasImage.getTexture(), 0); + shader->setUniform4f("fontColor", ofFloatColor::white); shader->setUniform2f("unitRange", unitRange); shader->end(); @@ -46,116 +47,108 @@ void MsdfLayer::draw(glm::vec3 position) const { ofPopStyle(); char previous_c; bool firstLetter = true; + + // set variation axis + const float swing_sin = sin(ofGetElapsedTimef() * 1); + const float swing_01 = ofMap(swing_sin, -1, 1, 0, 1); + const vector & variationAxisAvailable = atlas->getVariationAxesAvailable(); + const string var_name = variationAxisAvailable[0].name; + const float var_value = ofMap(swing_01, 0, 1, + variationAxisAvailable[0].minValue, + variationAxisAvailable[0].maxValue); + + int i = 0; for(const char c : propsBuffer[0].text){ - ofxMsdfgen::GlyphGeometry gg = atlas->getGlyphGeometry(c); - int x, y, w, h; - gg.getBoxRect(x, y, w, h); - //y = atlas_h - (y + h); - if(y < 0){ + float mix = -1; + ofxMsdfgen::GlyphGeometry gg_a; + ofxMsdfgen::GlyphGeometry gg_b; + + bool success = atlas->getGlyphGeometryPair(c, + {var_name, var_value}, + gg_a, + gg_b, + mix); + if(!success){ + ofLogError("MsdfLayer::draw") << "could not get glyphGeometryPair" << endl; + } + int x_a, y_a, w_a, h_a; + int x_b, y_b, w_b, h_b; + gg_a.getBoxRect(x_a, y_a, w_a, h_a); + gg_b.getBoxRect(x_b, y_b, w_b, h_b); + if(y_a < 0 || y_b < 0){ // FIXME: make sure this does not happen ofLogError("MsdfLayer::draw") << "y smaller 0, this is not good" << endl; } ofPushStyle(); - double pl, pb, pr, pt, - il, ib, ir, it; - gg.getQuadPlaneBounds(pl, pb, pr, pt); - gg.getQuadAtlasBounds(il, ib, ir, it); + double pl_a, pb_a, pr_a, pt_a; + double pl_b, pb_b, pr_b, pt_b; + gg_a.getQuadPlaneBounds(pl_a, pb_a, pr_a, pt_a); + gg_b.getQuadPlaneBounds(pl_b, pb_b, pr_b, pt_b); + + double advance_a = gg_a.getAdvance(); + double advance_b = gg_b.getAdvance(); + + float x = glm::mix(float(x_a), float(x_b), mix); + float y = glm::mix(float(y_a), float(y_b), mix); + float w = glm::mix(float(w_a), float(w_b), mix); + float h = glm::mix(float(h_a), float(h_b), mix); + float pl = glm::mix(float(pl_a), float(pl_b), mix); + //float pb = glm::mix(float(pb_a), float(pb_b), mix); + //float pr = glm::mix(float(pr_a), float(pr_b), mix); + float pt = glm::mix(float(pt_a), float(pt_b), mix); + double advance = glm::mix(advance_a, advance_b, mix); ofPushMatrix(); - float magic = ofMap(sin(ofGetElapsedTimeMicros() * 0.000001), -1, 1, 1, 200); - magic = atlas->settings.scale; - auto fontMetrics = atlas->getFontGeometry().getMetrics(); - float cender = (fontMetrics.ascenderY - fontMetrics.descenderY); - //magic *= cender; - //if(ofGetFrameNum() % 30 == 0){ - //cout << "drawing letter " << c << endl - //<< "\tpl: " << ofToString(pl) - //<< "\tpb: " << ofToString(pb) - //<< "\tpr: " << ofToString(pr) - //<< "\tpt: " << ofToString(pt) - //<< endl; - //} - - //ofPushMatrix(); - //for(int i = 0; i < 2; i++){ - //if(i == 1){ - //ofRotateDeg(90); - //ofTranslate(400, -150, 0); - //} - //ofNoFill(); - //ofSetColor(ofColor::white); - //ofDrawLine(glm::vec2(0, 90), glm::vec2(0, 140)); - //ofSetColor(ofColor::lightGray); - //for(int i = 1; i < 3; i++){ - //ofDrawLine(glm::vec2(i * 20, 90), glm::vec2(i * 20, 140)); - //} - //ofFill(); - //if(i == 0){ - //ofSetColor(ofColor::red); - //ofDrawRectangle(0, 100, pl * 50, 10); - //ofSetColor(ofColor::lightBlue); - //ofDrawRectangle(0, 120, pr * 50, 10); - //ofSetColor(ofColor::green); - //ofDrawRectangle(0, 130, gg.getAdvance() * 50, 10); - //ofDrawBitmapStringHighlight(ofToString(gg.getAdvance()), 0, 150); - //ofDrawBitmapStringHighlight(ofToString(pl), 0, 170); - //ofDrawBitmapStringHighlight(ofToString(pb), 0, 190); - //ofDrawBitmapStringHighlight(ofToString(pr), 0, 210); - //ofDrawBitmapStringHighlight(ofToString(pt), 0, 230); - //}else{ - //ofSetColor(ofColor::green); - //ofDrawRectangle(0, 110, pb * 50, 10); - //ofSetColor(ofColor::yellow); - //ofDrawRectangle(0, 130, pt * 50, 10); - //} - //} - //ofPopMatrix(); - shader->begin(); + float magic = atlas->settings.scale; ofSetColor(ofFloatColor(1, 1, 1, 1)); ofTranslate(pl * magic, 0, 0); ofTranslate(0, -1 * pt * magic, 0); - msdf_atlas::unicode_t a = static_cast (previous_c); - msdf_atlas::unicode_t b = static_cast (c); - double advance = gg.getAdvance(); + //msdf_atlas::unicode_t a = static_cast (previous_c); + //msdf_atlas::unicode_t b = static_cast (c); if(!firstLetter){ //cout << "this is being printed" << endl; auto & fontGeometry = atlas->getFontGeometry(); const std::map , double> kerning = fontGeometry.getKerning(); ofxMsdfgen::GlyphGeometry gg_previous = atlas->getGlyphGeometry(c); - std::map , double>::const_iterator it = kerning.find(std::make_pair (gg_previous.getIndex(), gg.getIndex())); + std::map , double>::const_iterator it = kerning.find(std::make_pair (gg_previous.getIndex(), gg_a.getIndex())); if(it != kerning.end()){ advance += it->second; ofDrawBitmapStringHighlight(ofToString(it->second), 0, 200, ofColor::black, ofColor::pink); } - ofPushStyle(); - ofFill(); - ofSetColor(ofColor::green); - ofDrawRectangle(0, 0, 400, 400); - ofPopStyle(); - //fontGeometry.getAdvance(advance, 'a', 'b'); - //cout << "first worked" << endl; - //fontGeometry.getAdvance(advance, a, b); - //cout << "this is maybeh being printed" << endl; - //bool gotAdvance = atlas->getFontGeometry().getAdvance(advance, a, b); - //cout << "this is being not printed" << endl; - //if(gotAdvance){ - //ofTranslate(advance * magic, 0, 0); - //} - }else{ - firstLetter = false; } - msdf_atlas::GlyphBox gb; - atlasImage.drawSubsection(0, 0, w, h, x, y, w, h); + glm::vec2 translation_a = glm::vec2(float(x_a) / float(atlas_w), + float(y_a) / float(atlas_h)); + glm::vec2 scale_a = glm::vec2(float(w_a) / float(atlas_w), + float(h_a) / float(atlas_h)); + glm::vec2 translation_b = glm::vec2(float(x_b) / float(atlas_w), + float(y_b) / float(atlas_h)); + glm::vec2 scale_b = glm::vec2(float(w_b) / float(atlas_w), + float(h_b) / float(atlas_h)); + //if(firstLetter){ + //cout << "translation_a: " << ofToString(translation_a) << endl; + //cout << "scale_a: " << ofToString(scale_a) << endl; + //} + shader->begin(); + shader->setUniform4f("fontColor", ofFloatColor::yellow); + shader->setUniform2f("translation_a", translation_a); + shader->setUniform2f("scale_a", scale_a); + shader->setUniform2f("translation_b", translation_b); + shader->setUniform2f("scale_b", scale_b); + shader->setUniform1f("msdf_mix", mix); + atlasImage.draw(0, 0, w, h); shader->end(); ofPopMatrix(); ofPopStyle(); ofTranslate(advance * magic, 0, 0); ofTranslate(-1 * pl * magic, 0, 0); previous_c = c; + if(firstLetter){ + firstLetter = false; + } + i++; } ofPopMatrix(); - //atlasImage.draw(x, y); ofDisableDepthTest(); ofEnableAlphaBlending(); } diff --git a/src/Layer.h b/src/Layer.h index 50a926b..0754a97 100644 --- a/src/Layer.h +++ b/src/Layer.h @@ -25,7 +25,6 @@ class Layer { float x = 0; float y = 0; float rotation = 0; - float wght = 100; float fontSize_px = 24; float * color[4]; TransformOrigin transformOrigin = TransformOrigin::CENTER; @@ -34,7 +33,7 @@ class Layer { bool mirror_y = false; float mirror_y_distance = 0; float letterDelay = 0; - string text = "ASYA is the best"; + string text = "Variable Font Editor"; }; struct Settings { uint32_t maxBufferSize = 100; diff --git a/src/LayerComposition.cpp b/src/LayerComposition.cpp index 38b29af..714f8de 100644 --- a/src/LayerComposition.cpp +++ b/src/LayerComposition.cpp @@ -19,9 +19,9 @@ void LayerComposition::setup(){ msdfShader = make_shared (); #ifdef TARGET_EMSCRIPTEN - msdfShader->load("ofxMsdfgen/shaders/unitRange/ES3/shader"); + msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader"); #else - msdfShader->load("ofxMsdfgen/shaders/unitRange/GL3/shader"); + msdfShader->load("ofxMsdfgen/shaders/mix/GL3/shader"); #endif auto layer = make_unique (); @@ -45,7 +45,7 @@ void LayerComposition::update(){ void LayerComposition::draw() const { for(const auto & layer : layers){ - layer->draw(glm::vec3(0, 200, 0)); + layer->draw(glm::vec3(400, 200, 0)); } }