diff options
Diffstat (limited to 'lasertext/src/ofApp.h')
| -rw-r--r-- | lasertext/src/ofApp.h | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/lasertext/src/ofApp.h b/lasertext/src/ofApp.h new file mode 100644 index 0000000..e8c7da9 --- /dev/null +++ b/lasertext/src/ofApp.h @@ -0,0 +1,166 @@ +#pragma once + +#include "ofMain.h" +#include "ofxSvg.h" + +#include "ofxHelios.h" +#include "colourPolyline.h" + +class glyph{ +public: + glyph(char c,float w,vector<ofPolyline> lines){ + glyph(c,w,lines,ofColor(0,0,0)); + } + glyph(char c,float w,vector<ofPolyline> 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<ofPolyline> outline; + ofColor colour; +}; + +class glyphbanner{ + ofXml SVGFont; + vector<glyph> glyphs; + vector<colourPolyline> outlines; + ofVec2f centre; +public: + glyphbanner(){}; + void init(vector<string> message){ + for (auto& word: message){ + for (auto& c: word){ + addGlyph(c,ofColor::fromHsb(ofRandom(255.0),225,255)); + } + addGlyph(' '); + } + } + 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<ofColor> colours(){ + vector<ofColor> c; + for (auto& i:glyphs) c.push_back(i.colour); + return c; + } + void loadFont(filesystem::path path){ + if( SVGFont.load(path) ){ + vector<glyph> g=glyphs; + clear(); + createGlyphs(g); + ofLog()<<"loaded "<<path.stem().string(); + }else{ + ofLog()<<"unable to load "<<path<<" check data/ folder"; + } + + } + void createGlyphs(string text){ + for (auto& c:text) addGlyph(c); + } + void createGlyphs(vector<glyph> _g){ + for (auto& g:_g) addGlyph(g.code,g.colour); + } + void addGlyph(char c,ofColor col=ofColor(255,255,255)){ + vector<ofPolyline> 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<string> 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<splitGlyphPath.size(); i+=3){ + if(splitGlyphPath[i] == "M"){ + if (shape.size()) { + shapes.push_back(shape); + shape.clear(); + } + shape.addVertex(ofToFloat(splitGlyphPath[i+1]), ofToFloat(splitGlyphPath[i+2])); + }else if(splitGlyphPath[i] == "L"){ + shape.lineTo(ofToFloat(splitGlyphPath[i+1]), ofToFloat(splitGlyphPath[i+2])); + } + } + if (shape.size()) shapes.push_back(shape); + glyphs.push_back(glyph(c,charWidth,shapes,col)); + } + void removeGlyph(){glyphs.pop_back();} + void clear(){glyphs.clear();} + void randomiseColours(); + void draw(){ + float p=(-width())/2; + ofPushMatrix(); + ofTranslate(ofGetWidth()/2,ofGetHeight()/2); + ofScale(0.05,-0.05,0.05); + for (auto& i:glyphs){ + i.draw(p,0); + p+=i.width; + } + ofPopMatrix(); + } + vector<colourPolyline>& 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; + +}; |
