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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#pragma once
#include "ofMain.h"
#include "ofxSvg.h"
#include "ofxGui.h"
#include "ofxXmlSettings.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();
}
void randomiseColour(){
colour=ofColor::fromHsb(ofRandom(255.0),225,255);
}
char code;
float width;
vector<ofPolyline> outline;
ofColor colour;
};
class glyphWord{
public:
glyphWord(){amount=1.0f;}
vector<glyph> glyphs;
float amount;
};
class glyphbanner{
ofXml SVGFont;
vector<glyphWord> words;
vector<colourPolyline> outlines;
ofVec2f centre;
float lastUpdateTime;
float playhead;
float enspace;
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){
createWords(message);
lastUpdateTime=ofGetElapsedTimef();
playhead=0.0f;
}
int length(){
int l=0;
for (auto& w:words) {
l+=w.glyphs.size();
}
return l+max(0,(int)words.size()-1);
}
float width(){
float _w=0.0f;
for (auto& w:words) {
for (auto& g:w.glyphs) _w+=g.width;
}
return _w+max((float)(words.size()-1)*enspace,0.0f);
}
int glyphCount(){
int c=0;
for (auto& w:words){
c+=w.glyphs.size();
}
return c;
}
string text(){
string s;
for (auto& w:words) {
for (auto& g:w.glyphs) s+=ofToString(g.code);
}
return s;
}
void loadFont(filesystem::path path){
if( SVGFont.load(path) ){
vector<glyphWord> w=words;
clear();
createWords(w);
enspace=getGlyph(' ').width;
ofLog()<<"loaded "<<path.stem().string();
}else{
ofLog()<<"unable to load "<<path<<" check data/ folder";
}
}
void createWords(string message){
clear();
vector<string> m=split(message);
for (auto& word: m){
glyphWord w;
for (auto& c: word){
w.glyphs.push_back(getGlyph(c,ofColor::fromHsb(ofRandom(255.0),225,255)));
}
words.push_back(w);
}
}
void createWords(vector<glyphWord> _words){
clear();
for (auto& _w:_words) {
glyphWord w;
for (auto& g: _w.glyphs){
w.glyphs.push_back(getGlyph(g.code,g.colour));
}
words.push_back(w);
}
}
glyph getGlyph(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);
return glyph(c,charWidth,shapes,col);
}
void addGlyph(char g,ofColor c){
if (g==' ') words.push_back(glyphWord());
else {
words[words.size()-1].glyphs.push_back(getGlyph(g,c));
}
}
void removeGlyph(){
if (words.size()){
glyphWord lw=words[words.size()-1];
lw.glyphs.pop_back();
if (!lw.glyphs.size()){
words.pop_back();
}
}
}
void clear(){words.clear();}
void update(float speed=1.0f){
float delta=ofGetElapsedTimef()-lastUpdateTime;
lastUpdateTime=ofGetElapsedTimef();
playhead+=delta*speed;
int theword=0;
float segment=0.0f;
if (false){ //1 word per second
theword=int(playhead)%words.size();
segment=playhead-int(playhead);
}
else { //proportional to #of letters
int theletter=int(playhead)%glyphCount();
theword=0;
while((theletter-=words[theword].glyphs.size())>0){
theword++;
}
segment=(((float)theletter+words[theword].glyphs.size()+playhead-int(playhead))/words[theword].glyphs.size());
}
//calculate params for word/letter anim
for (int i=0;i<words.size();i++){
words[i].amount=(i==theword?sin(segment*3.1415):0);
for (auto& g:words[i].glyphs){
if (ofRandom(100)<speed) {
g.randomiseColour();
}
}
}
}
vector<colourPolyline>& getOutlines(float s=1.0f){
outlines.clear();
float p=(-width())/2;
for (auto& w:words){
for (auto& g:w.glyphs){
if (w.amount>0.0f){
for (auto& o:g.outline){
auto q=o;
q.scale(s,-s);
q.translate(glm::vec3(p*s,0,0));
outlines.push_back(colourPolyline(q,g.colour*w.amount));
}
}
p+=g.width;
}
p+=enspace;
}
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;
ofxPanel textgui;
ofParameter<float> text_scale;
ofParameter<float> text_speed;
//======= laser gui
ofxPanel lasergui;
ofParameter<bool> laser_power;
ofParameter<int> laser_intensity;
ofParameter<int> laser_points;
ofParameter<int> laser_subdivide;
ofParameter<int> laser_blank_num;
ofParameter<float> laser_max_angle;
ofxXmlSettings XML;
};
|