1212var p5 = require ( '../core/main' ) ;
1313var constants = require ( '../core/constants' ) ;
1414
15- /*
16- * TODO:
17- * -- kerning
18- * -- alignment: justified?
19- */
20-
2115/**
2216 * Base class for font handling
2317 * @class p5.Font
@@ -35,11 +29,6 @@ p5.Font = function(p) {
3529 this . font = undefined ;
3630} ;
3731
38- p5 . Font . prototype . list = function ( ) {
39- // TODO
40- throw new Error ( 'not yet implemented' ) ;
41- } ;
42-
4332/**
4433 * Returns a tight bounding box for the given text string using this
4534 * font (currently only supports single lines)
@@ -85,21 +74,28 @@ p5.Font.prototype.list = function() {
8574 *words Lorem ipsum dol go off canvas and contained by white bounding box
8675 *
8776 */
88- p5 . Font . prototype . textBounds = function ( str , x , y , fontSize , options ) {
77+ p5 . Font . prototype . textBounds = function ( str , x , y , fontSize , opts ) {
8978 x = x !== undefined ? x : 0 ;
9079 y = y !== undefined ? y : 0 ;
91- fontSize = fontSize || this . parent . _renderer . _textSize ;
9280
9381 // Check cache for existing bounds. Take into consideration the text alignment
9482 // settings. Default alignment should match opentype's origin: left-aligned &
9583 // alphabetic baseline.
96- var p =
97- ( options && options . renderer && options . renderer . _pInst ) || this . parent ,
98- renderer = p . _renderer ,
99- alignment = renderer . _textAlign || constants . LEFT ,
100- baseline = renderer . _textBaseline || constants . BASELINE ,
101- key = cacheKey ( 'textBounds' , str , x , y , fontSize , alignment , baseline ) ,
84+ var p = ( opts && opts . renderer && opts . renderer . _pInst ) || this . parent ,
85+ ctx = p . _renderer . drawingContext ,
86+ alignment = ctx . textAlign || constants . LEFT ,
87+ baseline = ctx . textBaseline || constants . BASELINE ,
88+ cacheResults = false ,
89+ result ,
90+ key ;
91+
92+ fontSize = fontSize || p . _renderer . _textSize ;
93+
94+ // NOTE: cache disabled for now pending further discussion of #3436
95+ if ( cacheResults ) {
96+ key = cacheKey ( 'textBounds' , str , x , y , fontSize , alignment , baseline ) ;
10297 result = this . cache [ key ] ;
98+ }
10399
104100 if ( ! result ) {
105101 var minX ,
@@ -111,7 +107,7 @@ p5.Font.prototype.textBounds = function(str, x, y, fontSize, options) {
111107 yCoords = [ ] ,
112108 scale = this . _scale ( fontSize ) ;
113109
114- this . font . forEachGlyph ( str , x , y , fontSize , options , function (
110+ this . font . forEachGlyph ( str , x , y , fontSize , opts , function (
115111 glyph ,
116112 gX ,
117113 gY ,
@@ -139,7 +135,7 @@ p5.Font.prototype.textBounds = function(str, x, y, fontSize, options) {
139135
140136 // Bounds are now calculated, so shift the x & y to match alignment settings
141137 pos = this . _handleAlignment (
142- renderer ,
138+ p . _renderer ,
143139 str ,
144140 result . x ,
145141 result . y ,
@@ -149,9 +145,9 @@ p5.Font.prototype.textBounds = function(str, x, y, fontSize, options) {
149145 result . x = pos . x ;
150146 result . y = pos . y ;
151147
152- this . cache [
153- cacheKey ( 'textBounds' , str , x , y , fontSize , alignment , baseline )
154- ] = result ;
148+ if ( cacheResults ) {
149+ this . cache [ key ] = result ;
150+ }
155151 }
156152
157153 return result ;
@@ -494,8 +490,7 @@ function pathToPoints(cmds, options) {
494490 }
495491
496492 if ( opts . simplifyThreshold ) {
497- /*var count = */ simplify ( pts , opts . simplifyThreshold ) ;
498- //console.log('Simplify: removed ' + count + ' pts');
493+ simplify ( pts , opts . simplifyThreshold ) ;
499494 }
500495
501496 return pts ;
@@ -1238,8 +1233,7 @@ function base3(t, p1, p2, p3, p4) {
12381233function cacheKey ( ) {
12391234 var hash = '' ;
12401235 for ( var i = arguments . length - 1 ; i >= 0 ; -- i ) {
1241- var v = arguments [ i ] ;
1242- hash += v === Object ( v ) ? JSON . stringify ( v ) : v ;
1236+ hash += '?' + arguments [ i ] ;
12431237 }
12441238 return hash ;
12451239}
0 commit comments