#pragma once #include "ofMain.h" #include "ofxSvg.h" #include "ofxHelios.h" #include "colourPolyline.h" class glyph{ public: glyph(char c,float w,vector lines){ glyph(c,w,lines,ofColor(0,0,0)); } glyph(char c,float w,vector lines,ofColor col){ code=c; width=w; outline=lines; colour=col; } void draw(float x, float y){ ofSetColor(colour); ofPushMatrix(); ofTranslate(x,y); for (auto& v:outline) v.draw(); ofPopMatrix(); } char code; float width; vector outline; ofColor colour; }; //how to deal with words class glyphbanner{ ofXml SVGFont; vector glyphs; vector outlines; ofVec2f centre; float lastUpdateTime; vector split(string s) { size_t pos_start = 0, pos_end; string token; vector res; while ((pos_end = s.find (" ", pos_start)) != string::npos) { token = s.substr (pos_start, pos_end - pos_start); pos_start = pos_end + 1; res.push_back (token); } res.push_back (s.substr (pos_start)); return res; } public: glyphbanner(){}; void init(string message){ vector m=split(message); for (auto& word: m){ for (auto& c: word){ addGlyph(c,ofColor::fromHsb(ofRandom(255.0),225,255)); } addGlyph(' '); } lastUpdateTime=ofGetElapsedTimef(); } int length(){return glyphs.size();} float width(){ float w=0.0f; for (auto& i:glyphs) w+=i.width; return w; } string text(){ string s; for (auto& i:glyphs) s+=ofToString(i.code); return s; } vector colours(){ vector c; for (auto& i:glyphs) c.push_back(i.colour); return c; } void loadFont(filesystem::path path){ if( SVGFont.load(path) ){ vector g=glyphs; clear(); createGlyphs(g); ofLog()<<"loaded "< _g){ for (auto& g:_g) addGlyph(g.code,g.colour); } void addGlyph(char c,ofColor col=ofColor(255,255,255)){ vector shapes; ofPolyline shape; string elementPath = "/svg/defs/font/glyph[@unicode='"+ofToString(c)+"']"; if(SVGFont.findFirst(elementPath) == 0 ){ elementPath = "/svg/defs/font/glyph[@unicode='?']"; } ofXml xmlElement = SVGFont.findFirst(elementPath); float charWidth = ofToFloat(xmlElement.getAttribute("horiz-adv-x").getValue()); vector splitGlyphPath = ofSplitString(xmlElement.getAttribute("d").getValue(), " ");//glyph path data in SVG looks like this: "M 139 -9.45 L 230 18.9 L 299 22.1 L 227 25.2" for(int i=0; i& getOutlines(){ outlines.clear(); float p=(-width())/2; for (auto& i:glyphs){ for (auto& o:i.outline){ auto q=o; q.scale(.05,-.05); q.translate(glm::vec3(p*.05,0,0)); outlines.push_back(colourPolyline(q,i.colour)); } p+=i.width; } return outlines; } }; class ofApp : public ofBaseApp{ public: void setup(); void update(); void draw(); void exit(); void keyPressed(ofKeyEventArgs &keyargs); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void mouseEntered(int x, int y); void mouseExited(int x, int y); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); ofxHelios laser; ofDirectory fonts; int currentFont; string displaytext; glyphbanner banner; };