1 /**
2 * @author zz85 / http://www.lab4games.net/zz85/blog
3 * @author alteredq / http://alteredqualia.com/
4 *
5 * For Text operations in three.js (See TextGeometry)
6 *
7 * It uses techniques used in:
8 *
9 * typeface.js and canvastext
10 * For converting fonts and rendering with javascript
11 * http://typeface.neocracy.org
12 *
13 * Triangulation ported from AS3
14 * Simple Polygon Triangulation
15 * http://actionsnippet.com/?p=1462
16 *
17 * A Method to triangulate shapes with holes
18 * http://www.sakri.net/blog/2009/06/12/an-approach-to-triangulating-polygons-with-holes/
19 *
20 */
21
22 /**@namespace*/
23 THREE.FontUtils = {
24
25 faces : {},
26
27 // Just for now. face[weight][style]
28
29 face : "helvetiker",
30 weight: "normal",
31 style : "normal",
32 size : 150,
33 divisions : 10,
34
35 getFace : function() {
36
37 return this.faces[ this.face ][ this.weight ][ this.style ];
38
39 },
40
41 loadFace : function( data ) {
42
43 var family = data.familyName.toLowerCase();
44
45 var ThreeFont = this;
46
47 ThreeFont.faces[ family ] = ThreeFont.faces[ family ] || {};
48
49 ThreeFont.faces[ family ][ data.cssFontWeight ] = ThreeFont.faces[ family ][ data.cssFontWeight ] || {};
50 ThreeFont.faces[ family ][ data.cssFontWeight ][ data.cssFontStyle ] = data;
51
52 var face = ThreeFont.faces[ family ][ data.cssFontWeight ][ data.cssFontStyle ] = data;
53
54 return data;
55
56 },
57
58 drawText : function( text ) {
59
60 var characterPts = [], allPts = [];
61
62 // RenderText
63
64 var i, p,
65 face = this.getFace(),
66 scale = this.size / face.resolution,
67 offset = 0,
68 chars = String( text ).split( '' ),
69 length = chars.length;
70
71 var fontPaths = [];
72
73 for ( i = 0; i < length; i ++ ) {
74
75 var path = new THREE.Path();
76
77 var ret = this.extractGlyphPoints( chars[ i ], face, scale, offset, path );
78 offset += ret.offset;
79
80 fontPaths.push( ret.path );
81
82 }
83
84 // get the width
85
86 var width = offset / 2;
87 //
88 // for ( p = 0; p < allPts.length; p++ ) {
89 //
90 // allPts[ p ].x -= width;
91 //
92 // }
93
94 //var extract = this.extractPoints( allPts, characterPts );
95 //extract.contour = allPts;
96
97 //extract.paths = fontPaths;
98 //extract.offset = width;
99
100 return { paths : fontPaths, offset : width };
101
102 },
103
104
105
106
107 extractGlyphPoints : function( c, face, scale, offset, path ) {
108
109 var pts = [];
110
111 var i, i2, divisions,
112 outline, action, length,
113 scaleX, scaleY,
114 x, y, cpx, cpy, cpx0, cpy0, cpx1, cpy1, cpx2, cpy2,
115 laste,
116 glyph = face.glyphs[ c ] || face.glyphs[ '?' ];
117
118 if ( !glyph ) return;
119
120 if ( glyph.o ) {
121
122 outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
123 length = outline.length;
124
125 scaleX = scale;
126 scaleY = scale;
127
128 for ( i = 0; i < length; ) {
129
130 action = outline[ i ++ ];
131
132 //console.log( action );
133
134 switch( action ) {
135
136 case 'm':
137
138 // Move To
139
140 x = outline[ i++ ] * scaleX + offset;
141 y = outline[ i++ ] * scaleY;
142
143 path.moveTo( x, y );
144 break;
145
146 case 'l':
147
148 // Line To
149
150 x = outline[ i++ ] * scaleX + offset;
151 y = outline[ i++ ] * scaleY;
152 path.lineTo(x,y);
153 break;
154
155 case 'q':
156
157 // QuadraticCurveTo
158
159 cpx = outline[ i++ ] * scaleX + offset;
160 cpy = outline[ i++ ] * scaleY;
161 cpx1 = outline[ i++ ] * scaleX + offset;
162 cpy1 = outline[ i++ ] * scaleY;
163
164 path.quadraticCurveTo(cpx1, cpy1, cpx, cpy);
165
166 laste = pts[ pts.length - 1 ];
167
168 if ( laste ) {
169
170 cpx0 = laste.x;
171 cpy0 = laste.y;
172
173 for ( i2 = 1, divisions = this.divisions; i2 <= divisions; i2 ++ ) {
174
175 var t = i2 / divisions;
176 var tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
177 var ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
178 }
179
180 }
181
182 break;
183
184 case 'b':
185
186 // Cubic Bezier Curve
187
188 cpx = outline[ i++ ] * scaleX + offset;
189 cpy = outline[ i++ ] * scaleY;
190 cpx1 = outline[ i++ ] * scaleX + offset;
191 cpy1 = outline[ i++ ] * -scaleY;
192 cpx2 = outline[ i++ ] * scaleX + offset;
193 cpy2 = outline[ i++ ] * -scaleY;
194
195 path.bezierCurveTo( cpx, cpy, cpx1, cpy1, cpx2, cpy2 );
196
197 laste = pts[ pts.length - 1 ];
198
199 if ( laste ) {
200
201 cpx0 = laste.x;
202 cpy0 = laste.y;
203
204 for ( i2 = 1, divisions = this.divisions; i2 <= divisions; i2 ++ ) {
205
206 var t = i2 / divisions;
207 var tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
208 var ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
209
210 }
211
212 }
213
214 break;
215
216 }
217
218 }
219 }
220
221
222
223 return { offset: glyph.ha*scale, path:path};
224 }
225
226 };
227
228
229 THREE.FontUtils.generateShapes = function( text, parameters ) {
230
231 // Parameters
232
233 parameters = parameters || {};
234
235 var size = parameters.size !== undefined ? parameters.size : 100;
236 var curveSegments = parameters.curveSegments !== undefined ? parameters.curveSegments: 4;
237
238 var font = parameters.font !== undefined ? parameters.font : "helvetiker";
239 var weight = parameters.weight !== undefined ? parameters.weight : "normal";
240 var style = parameters.style !== undefined ? parameters.style : "normal";
241
242 THREE.FontUtils.size = size;
243 THREE.FontUtils.divisions = curveSegments;
244
245 THREE.FontUtils.face = font;
246 THREE.FontUtils.weight = weight;
247 THREE.FontUtils.style = style;
248
249 // Get a Font data json object
250
251 var data = THREE.FontUtils.drawText( text );
252
253 var paths = data.paths;
254 var shapes = [];
255
256 for ( var p = 0, pl = paths.length; p < pl; p ++ ) {
257
258 Array.prototype.push.apply( shapes, paths[ p ].toShapes() );
259
260 }
261
262 return shapes;
263
264 };
265
266
267 /**
268 * This code is a quick port of code written in C++ which was submitted to
269 * flipcode.com by John W. Ratcliff // July 22, 2000
270 * See original code and more information here:
271 * http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
272 *
273 * ported to actionscript by Zevan Rosser
274 * www.actionsnippet.com
275 *
276 * ported to javascript by Joshua Koo
277 * http://www.lab4games.net/zz85/blog
278 *
279 */
280
281
282 ( function( namespace ) {
283
284 var EPSILON = 0.0000000001;
285
286 // takes in an contour array and returns
287
288 var process = function( contour, indices ) {
289
290 var n = contour.length;
291
292 if ( n < 3 ) return null;
293
294 var result = [],
295 verts = [],
296 vertIndices = [];
297
298 /* we want a counter-clockwise polygon in verts */
299
300 var u, v, w;
301
302 if ( area( contour ) > 0.0 ) {
303
304 for ( v = 0; v < n; v++ ) verts[ v ] = v;
305
306 } else {
307
308 for ( v = 0; v < n; v++ ) verts[ v ] = ( n - 1 ) - v;
309
310 }
311
312 var nv = n;
313
314 /* remove nv - 2 vertices, creating 1 triangle every time */
315
316 var count = 2 * nv; /* error detection */
317
318 for( v = nv - 1; nv > 2; ) {
319
320 /* if we loop, it is probably a non-simple polygon */
321
322 if ( ( count-- ) <= 0 ) {
323
324 //** Triangulate: ERROR - probable bad polygon!
325
326 //throw ( "Warning, unable to triangulate polygon!" );
327 //return null;
328 // Sometimes warning is fine, especially polygons are triangulated in reverse.
329 console.log( "Warning, unable to triangulate polygon!" );
330
331 if ( indices ) return vertIndices;
332 return result;
333
334 }
335
336 /* three consecutive vertices in current polygon, <u,v,w> */
337
338 u = v; if ( nv <= u ) u = 0; /* previous */
339 v = u + 1; if ( nv <= v ) v = 0; /* new v */
340 w = v + 1; if ( nv <= w ) w = 0; /* next */
341
342 if ( snip( contour, u, v, w, nv, verts ) ) {
343
344 var a, b, c, s, t;
345
346 /* true names of the vertices */
347
348 a = verts[ u ];
349 b = verts[ v ];
350 c = verts[ w ];
351
352 /* output Triangle */
353
354 /*
355 result.push( contour[ a ] );
356 result.push( contour[ b ] );
357 result.push( contour[ c ] );
358 */
359 result.push( [ contour[ a ],
360 contour[ b ],
361 contour[ c ] ] );
362
363
364 vertIndices.push( [ verts[ u ], verts[ v ], verts[ w ] ] );
365
366 /* remove v from the remaining polygon */
367
368 for( s = v, t = v + 1; t < nv; s++, t++ ) {
369
370 verts[ s ] = verts[ t ];
371
372 }
373
374 nv--;
375
376 /* reset error detection counter */
377
378 count = 2 * nv;
379
380 }
381
382 }
383
384 if ( indices ) return vertIndices;
385 return result;
386
387 };
388
389 // calculate area of the contour polygon
390
391 var area = function ( contour ) {
392
393 var n = contour.length;
394 var a = 0.0;
395
396 for( var p = n - 1, q = 0; q < n; p = q++ ) {
397
398 a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
399
400 }
401
402 return a * 0.5;
403
404 };
405
406 // see if p is inside triangle abc
407
408 var insideTriangle = function( ax, ay,
409 bx, by,
410 cx, cy,
411 px, py ) {
412
413 var aX, aY, bX, bY;
414 var cX, cY, apx, apy;
415 var bpx, bpy, cpx, cpy;
416 var cCROSSap, bCROSScp, aCROSSbp;
417
418 aX = cx - bx; aY = cy - by;
419 bX = ax - cx; bY = ay - cy;
420 cX = bx - ax; cY = by - ay;
421 apx= px -ax; apy= py - ay;
422 bpx= px - bx; bpy= py - by;
423 cpx= px - cx; cpy= py - cy;
424
425 aCROSSbp = aX*bpy - aY*bpx;
426 cCROSSap = cX*apy - cY*apx;
427 bCROSScp = bX*cpy - bY*cpx;
428
429 return ( (aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0) );
430
431 };
432
433
434 var snip = function ( contour, u, v, w, n, verts ) {
435
436 var p;
437 var ax, ay, bx, by;
438 var cx, cy, px, py;
439
440 ax = contour[ verts[ u ] ].x;
441 ay = contour[ verts[ u ] ].y;
442
443 bx = contour[ verts[ v ] ].x;
444 by = contour[ verts[ v ] ].y;
445
446 cx = contour[ verts[ w ] ].x;
447 cy = contour[ verts[ w ] ].y;
448
449 if ( EPSILON > (((bx-ax)*(cy-ay)) - ((by-ay)*(cx-ax))) ) return false;
450
451 for ( p = 0; p < n; p++ ) {
452
453 if( (p == u) || (p == v) || (p == w) ) continue;
454
455 px = contour[ verts[ p ] ].x
456 py = contour[ verts[ p ] ].y
457
458 if ( insideTriangle( ax, ay, bx, by, cx, cy, px, py ) ) return false;
459
460 }
461
462 return true;
463
464 };
465
466
467 namespace.Triangulate = process;
468 namespace.Triangulate.area = area;
469
470 return namespace;
471
472 })(THREE.FontUtils);
473
474 // To use the typeface.js face files, hook up the API
475 self._typeface_js = { faces: THREE.FontUtils.faces, loadFace: THREE.FontUtils.loadFace };
nike free rn
new balance hombre baratas
cinturones gucci
ugg rebajas
cinturon gucci
ray ban baratas
nike cortez
peuterey mujer
christian louboutin madrid
mbt zapatos
gafas ray ban baratas
mbt ofertas
air max blancas
mbt barcelona
nike air max 90
woolrich barcelona
nike mujer
botas ugg
gafas de sol carrera aratas
air max 2016 baratas
oakley baratas
nike air max 2016