Skip to content

Commit

Permalink
Better caching for vertical text metrics for the hybrid method. See p…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Nov 17, 2015
1 parent dcf1ea9 commit c12edfd
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions js/nodes/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ define( function( require ) {
// See https://github.com/phetsims/scenery/issues/455 for more information.
var useSVGTextLengthAdjustments = !platform.ie && !platform.edge;

// Maps CSS {string} => {Bounds2}, so that we can cache the vertical font sizes outside of the Font objects themselves.
var hybridFontVerticalCache = {};

function Text( text, options ) {
this._text = ''; // filled in with mutator
this._font = scenery.Font.DEFAULT; // default font, usually 10px sans-serif
Expand Down Expand Up @@ -319,13 +322,14 @@ define( function( require ) {
return Bounds2.NOTHING; // we are the hybridTextNode, ignore us
}

if ( this._font._cachedSVGBounds === undefined ) {
var css = this._font.toCSS();
var verticalBounds = hybridFontVerticalCache[ css ];
if ( !verticalBounds ) {
hybridTextNode.setFont( this._font );
this._font._cachedSVGBounds = hybridTextNode.getBounds().copy();
verticalBounds = hybridFontVerticalCache[ css ] = hybridTextNode.getBounds().copy();
}

var canvasWidth = this.approximateCanvasWidth();
var verticalBounds = this._font._cachedSVGBounds;

// it seems that SVG bounds generally have x=0, so we hard code that here
return new Bounds2( 0, verticalBounds.minY, canvasWidth, verticalBounds.maxY );
Expand Down

0 comments on commit c12edfd

Please sign in to comment.