-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayout-array.html
300 lines (227 loc) · 7.7 KB
/
layout-array.html
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<html>
<head>
<title>Paragraph Layouts (p5js)</title>
<script src="lib/p5.js"></script>
<!--script src="lib/rita.js"></script-->
<script src="/Projects/RiTaJS/src/rita.js"></script>
<script src="lib/rita-wordnet.js"></script>
<script>
var txt = ['A raw memory. Church.', 'A loud room with children playing, thoughtlessly.','Wandering wildly. I stand small and young'];
var rs, tx = 20, ty = 50, tw = 500, th = 95, fsize = 28;
function preload()
{
font = loadFont('LondonBetween.ttf');
}
function setup()
{
createCanvas(800, 600);
background(245);
textFont(font, fsize);
rs = RiString.createLines({
font: font, fontSize: fsize, text: txt, x: tx, y: ty, h: th
});
}
function draw()
{
background(245);
rect(tx, ty, tw, th);
for (var i = 0; i < rs.length; i++) {
text(rs[i].text(), rs[i].get('x'), rs[i].get('y'));
}
}
// ------------------------------------------------------------------
// TODO: test html tags
var E = '', SP = ' ', paragraphIndent = 30, indentFirstParagraph = false;
var NON_BREAKING_SPACE = "<sp/>";
var PARAGRAPH_BREAK = "<p/>";
var LINE_BREAK = "<br/>";
RiString.createLines = function(options) {
var opts = parseOpts(options, {
fontSize: 12, x: 0, y: 0, w: Number.MAX_VALUE , h: Number.MAX_VALUE
});
var p5font = opts.font, txt = opts.text,
fsize = opts.fontSize, x = opts.x, y = opts.y, w = opts.w, h = opts.h,
leading = opts.leading || opts.fontSize * 1.2, // make const
ascent, descent, startX = x, currentX, currentY, rlines = [],
sb = E, words = [], next, yPos = 0, rt = undefined,
newParagraph = 0, forceBreak = 0, firstLine = 1,
maxW = x + w, maxH = y + h;
if (!txt || !txt.length) return [];
// 1. get font ascent and descent
ascent = p5font._textAscent(fsize);
descent = p5font._textDescent(fsize);
//console.log('ascent: ',ascent,'descent: ',descent);
if (Array.isArray(txt)) {
return layoutArray(txt, x, y, h, ascent, descent, leading);
}
// remove line breaks & add spaces around html
txt = txt.replace(/>/g, '>').replace(/</g, '<');
txt = txt.replace(/ ?(<[^>]+>) ?/g, " $1 ");
txt = txt.replace(/[\r\n]+/g, SP);
// split into reversed array of words
RiString._addToStack(txt, words);
if (!words.length) return [];
//log("txt.len="+txt.length+" x="+x+" y="+y+" w="+w+" h="+h+" lead="+leading);log(p5font);
currentY = y + ascent;
if (indentFirstParagraph)
startX += paragraphIndent;
while (words.length > 0) {
next = words.pop();
if (!next.length) continue;
// check for HTML-style tags
if (/<[^>]+>/.test(next)) {
if (next === NON_BREAKING_SPACE) {
sb += SP;
}
else if (next === PARAGRAPH_BREAK) {
newParagraph = true;
}
else if (next === LINE_BREAK) {
forceBreak = true;
}
continue;
}
// re-calculate our X position
currentX = startX + font._textWidth(sb + next, fontSize);
// check it against the line-width
if (!newParagraph && !forceBreak && currentX < maxW) {
sb += next + SP;
// add-word
}
else {
// check yPosition to see if its ok for another line?
if (RiString._withinBoundsY(currentY, leading, maxH, descent, firstLine)) {
yPos = firstLine ? currentY : currentY + leading;
rt = RiString._newRiTextLine(sb, p5font, startX, yPos);
rlines.push(rt);
currentY = newParagraph ? rt.get('y') + paragraphLeading : rt.get('y');
startX = x;
// reset
if (newParagraph)
startX += paragraphIndent;
sb = next + SP;
// reset with next word
newParagraph = false;
forceBreak = false;
firstLine = false;
}
else {
// we've run out of y-space, break the loop and finish
words.push(next);
// not needed
break;
}
}
}
// check if leftover words can make a new line
if (RiString._withinBoundsY(currentY, leading, maxH, descent, firstLine)) {
// TODO: is it possible that there are tags in here?
yPos = firstLine ? currentY : currentY + leading;
rlines.push(RiString._newRiTextLine(sb, p5font, x, yPos));
sb = E;
}
else {
RiString._addToStack(sb, words);
// save for next (not needed?)
}
return rlines;
};
RiString._withinBoundsY = function(currentY, leading, maxY, descent, firstLine) {
return firstLine ? (currentY + descent) <= maxY :
(currentY + leading + descent) <= maxY;
};
RiString._addToStack = function(txt, words) {
var tmp = txt.split(SP);
for ( var i = tmp.length - 1; i >= 0; i--)
words.push(tmp[i]);
};
RiString._newRiTextLine = function(s, pf, xPos, nextY) {
return new RiString(s.replace(/ *$/,'')).set('x', xPos).set('y', nextY);/// pf);
};
function endsWith(str, ending) {
if (typeof s != 'string') return false;
return new RegExp(ending+'$').test(str);
//return str.slice(-ending.length) == ending;
}
RiString.createWords = function(options) {
options.push({splitter: RiString.prototype.splitWords});
return RiString._createRiStrings(options);
};
RiString.createLetters = function(options) {
options.push({splitter: RiString.prototype.splitLetters});
return RiString._createRiStrings(options);
};
RiString._createRiStrings = function(options) {
var rlines = RiString.createLines(options);
if (!rlines || rlines.length < 1) return [];
var result = [];
for (var i = 0; i < rlines.length; i++) {
var rs = options.splitter.call(rlines[i]);
for (var j = 0; j < rs.length; j++) {
result.push(rs[j]);
}
//RiString.dispose(rlines[i]);
}
return result;
};
RiString.prototype.splitWords = function(regex) {
regex = regex || SP;
(typeof regex == S) && (regex = new RegExp(regex));
var l = [];
var txt = this._rs._text;
var words = txt.split(regex);
for ( var i = 0; i < words.length; i++) {
if (words[i].length < 1) continue;
var tmp = this.copy();
tmp.text(words[i]);
var mx = RiString._wordOffsetFor(this, words, i);
tmp.position(mx, this.y);
l.push(tmp);
}
return l;
};
RiString.prototype.splitLetters = function() {
var l = [];
var chars = [];
var txt = this.text();
var len = txt.length;
for (var t = 0; t < len; t++) {
chars[t] = txt.charAt(t);
}
for ( var i = 0; i < chars.length; i++) {
if (chars[i] == SP) continue;
var tmp = this.copy();
tmp.text(chars[i]);
var mx = this.charOffset(i);
tmp.position(mx, this.y);
l.push(tmp);
}
return l;
};
function layoutArray(lines, x, y, h, ascent, descent, leading) {
// sets x,y for those that fit within the height
var lastOk, currentY = y + ascent, result = [];
for (var k = 0; k < lines.length; k++)
{
result.push(RiString(lines[k]).set('x', x).set('y', currentY));
if (!RiString._withinBoundsY(currentY, leading, y + h, descent))
break;
currentY += leading;
}
return result;
};
function parseOpts(options, defaults) {
if (typeof options !== 'object') {
options = defaults;
}
else {
for (var key in defaults) {
if (typeof options[key] === 'undefined')
options[key] = defaults[key];
}
}
return options;
}
</script>
</head>
</html>