#include "Layer.h" #include "conversion.h" #include "ofAppRunner.h" #include "ofGraphics.h" #include "ofUtils.h" namespace ofxVariableLab { Layer::Layer(){ } Layer::~Layer(){ } MsdfLayer::MsdfLayer(){ } MsdfLayer::~MsdfLayer(){ } void Layer::update(){ if(isDirty){ isDirty = false; } } 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->setUniform2f("unitRange", unitRange); shader->end(); ofPushMatrix(); ofTranslate(position); ofPushStyle(); ofSetColor(ofColor::pink); ofDrawLine(glm::vec2(0, 0), glm::vec2(ofGetWidth(), 0)); ofPopStyle(); char previous_c; bool firstLetter = true; 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){ // 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); 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(); 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(); 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())); 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); shader->end(); ofPopMatrix(); ofPopStyle(); ofTranslate(advance * magic, 0, 0); ofTranslate(-1 * pl * magic, 0, 0); previous_c = c; } ofPopMatrix(); //atlasImage.draw(x, y); ofDisableDepthTest(); ofEnableAlphaBlending(); } void Layer::setProps(const Props & props){ while(propsBuffer.size() > max(0, int(settings.maxBufferSize - 1))){ propsBuffer.pop_back(); } propsBuffer.push_front(props); } const Layer::Props & Layer::getProps() const { return propsBuffer[0]; } void Layer::clearPropsBuffer(){ propsBuffer.clear(); } void Layer::setId(const string & id){ this->id = id; } const string & Layer::getId(){ return id; } }