Font.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { ShapeUtils } from '../ShapeUtils';
  2. import { ShapePath } from './Path';
  3. /**
  4. * @author zz85 / http://www.lab4games.net/zz85/blog
  5. * @author mrdoob / http://mrdoob.com/
  6. */
  7. function Font( data ) {
  8. this.data = data;
  9. }
  10. Object.assign( Font.prototype, {
  11. isFont: true,
  12. generateShapes: function ( text, size, divisions ) {
  13. function createPaths( text ) {
  14. var chars = String( text ).split( '' );
  15. var scale = size / data.resolution;
  16. var offset = 0;
  17. var paths = [];
  18. for ( var i = 0; i < chars.length; i ++ ) {
  19. var ret = createPath( chars[ i ], scale, offset );
  20. offset += ret.offset;
  21. paths.push( ret.path );
  22. }
  23. return paths;
  24. }
  25. function createPath( c, scale, offset ) {
  26. var glyph = data.glyphs[ c ] || data.glyphs[ '?' ];
  27. if ( ! glyph ) return;
  28. var path = new ShapePath();
  29. var pts = [], b2 = ShapeUtils.b2, b3 = ShapeUtils.b3;
  30. var x, y, cpx, cpy, cpx0, cpy0, cpx1, cpy1, cpx2, cpy2, laste;
  31. if ( glyph.o ) {
  32. var outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
  33. for ( var i = 0, l = outline.length; i < l; ) {
  34. var action = outline[ i ++ ];
  35. switch ( action ) {
  36. case 'm': // moveTo
  37. x = outline[ i ++ ] * scale + offset;
  38. y = outline[ i ++ ] * scale;
  39. path.moveTo( x, y );
  40. break;
  41. case 'l': // lineTo
  42. x = outline[ i ++ ] * scale + offset;
  43. y = outline[ i ++ ] * scale;
  44. path.lineTo( x, y );
  45. break;
  46. case 'q': // quadraticCurveTo
  47. cpx = outline[ i ++ ] * scale + offset;
  48. cpy = outline[ i ++ ] * scale;
  49. cpx1 = outline[ i ++ ] * scale + offset;
  50. cpy1 = outline[ i ++ ] * scale;
  51. path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
  52. laste = pts[ pts.length - 1 ];
  53. if ( laste ) {
  54. cpx0 = laste.x;
  55. cpy0 = laste.y;
  56. for ( var i2 = 1; i2 <= divisions; i2 ++ ) {
  57. var t = i2 / divisions;
  58. b2( t, cpx0, cpx1, cpx );
  59. b2( t, cpy0, cpy1, cpy );
  60. }
  61. }
  62. break;
  63. case 'b': // bezierCurveTo
  64. cpx = outline[ i ++ ] * scale + offset;
  65. cpy = outline[ i ++ ] * scale;
  66. cpx1 = outline[ i ++ ] * scale + offset;
  67. cpy1 = outline[ i ++ ] * scale;
  68. cpx2 = outline[ i ++ ] * scale + offset;
  69. cpy2 = outline[ i ++ ] * scale;
  70. path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
  71. laste = pts[ pts.length - 1 ];
  72. if ( laste ) {
  73. cpx0 = laste.x;
  74. cpy0 = laste.y;
  75. for ( var i2 = 1; i2 <= divisions; i2 ++ ) {
  76. var t = i2 / divisions;
  77. b3( t, cpx0, cpx1, cpx2, cpx );
  78. b3( t, cpy0, cpy1, cpy2, cpy );
  79. }
  80. }
  81. break;
  82. }
  83. }
  84. }
  85. return { offset: glyph.ha * scale, path: path };
  86. }
  87. //
  88. if ( size === undefined ) size = 100;
  89. if ( divisions === undefined ) divisions = 4;
  90. var data = this.data;
  91. var paths = createPaths( text );
  92. var shapes = [];
  93. for ( var p = 0, pl = paths.length; p < pl; p ++ ) {
  94. Array.prototype.push.apply( shapes, paths[ p ].toShapes() );
  95. }
  96. return shapes;
  97. }
  98. } );
  99. export { Font };