summaryrefslogtreecommitdiff
path: root/lasertext/src/ofApp.h
blob: 46f6bbc21fbbe5db346b66410819a29d39bacb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#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;
};

//how to deal with words

class glyphbanner{
	ofXml SVGFont;
	vector<glyph> glyphs;
	vector<colourPolyline> outlines;
	ofVec2f centre;
	float lastUpdateTime;
	vector<string> split(string s) {
	    size_t pos_start = 0, pos_end;
	    string token;
	    vector<string> 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<string> 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<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();
	}
	void update(){
		//calculate params for word/letter anim

	}
	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;

};