Skip to content

Commit

Permalink
Changed getFontInfo function to be parseFont and moved it to Label.
Browse files Browse the repository at this point in the history
Rather than keeping a fontInfoCache in LabelCollection the font
properties are stored on the Label itself and the parseFont function is
called when the font string changes.
  • Loading branch information
jasonbeverage committed Jul 1, 2019
1 parent ab2fec5 commit 07b7d89
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
22 changes: 22 additions & 0 deletions Source/Scene/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ define([
label._repositionAllGlyphs = true;
}

function getCSSValue(element, property) {
return document.defaultView.getComputedStyle(element, null).getPropertyValue(property);
}

function parseFont(label) {
var div = document.createElement('div');
div.style.position = 'absolute';
div.style.opacity = 0;
div.style.font = label._font;
document.body.appendChild(div);

label._fontFamily = getCSSValue(div,'font-family');
label._fontSize = getCSSValue(div,'font-size').replace('px', '');
label._fontStyle = getCSSValue(div,'font-style');
label._fontWeight = getCSSValue(div,'font-weight');

document.body.removeChild(div);
}

/**
* A Label draws viewport-aligned text positioned in the 3D scene. This constructor
* should not be used directly, instead create labels by calling {@link LabelCollection#add}.
Expand Down Expand Up @@ -165,6 +184,8 @@ define([

this._relativeSize = 1.0;

parseFont(this);

this._updateClamping();
}

Expand Down Expand Up @@ -326,6 +347,7 @@ define([
if (this._font !== value) {
this._font = value;
rebindAllGlyphs(this);
parseFont(this);
}
}
},
Expand Down
49 changes: 5 additions & 44 deletions Source/Scene/LabelCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,43 +83,6 @@ define([
});
}

function getCSSValue(element, property) {
return document.defaultView.getComputedStyle(element, null).getPropertyValue(property);
}

var fontInfoCache = {};
/**
* Given a CSS font string return an object containing the fontFamily, fontSize, fontStyle and fontWeight.
*/
function getFontInfo(font) {
var fontInfo = fontInfoCache[font];
if (defined(fontInfo)) {
return fontInfo;
}

var div = document.createElement('div');
div.style.position = 'absolute';
div.style.opacity = 0;
div.style.font = font;
document.body.appendChild(div);

var fontFamily = getCSSValue(div,'font-family');
var fontSize = getCSSValue(div,'font-size').replace('px', '');
var fontStyle = getCSSValue(div,'font-style');
var fontWeight = getCSSValue(div,'font-weight');

document.body.removeChild(div);
fontInfo = {
fontFamily : fontFamily,
fontSize : fontSize,
fontStyle : fontStyle,
fontWeight : fontWeight
};
fontInfoCache[font] = fontInfo;

return fontInfo;
}

// reusable object for calling writeTextToCanvas
var writeTextToCanvasParameters = {};
function createGlyphCanvas(character, font, fillColor, outlineColor, outlineWidth, style, verticalOrigin) {
Expand Down Expand Up @@ -179,10 +142,8 @@ define([
var glyphIndex;
var textIndex;

var font = label._font;
var fontInfo = getFontInfo(font);
// Compute a font size scale relative to the sdf font generated size.
label._relativeSize = fontInfo.fontSize / SDFSettings.FONT_SIZE;
label._relativeSize = label._fontSize / SDFSettings.FONT_SIZE;

// if we have more glyphs than needed, unbind the extras.
if (textLength < glyphsLength) {
Expand Down Expand Up @@ -240,16 +201,16 @@ define([

var id = JSON.stringify([
character,
fontInfo.fontFamily,
fontInfo.fontStyle,
fontInfo.fontWeight,
label._fontFamily,
label._fontStyle,
label._fontWeight,
+verticalOrigin
]);

var glyphTextureInfo = glyphTextureCache[id];
if (!defined(glyphTextureInfo)) {

var glyphFont = fontInfo.fontStyle + ' ' + fontInfo.fontWeight + ' ' + SDFSettings.FONT_SIZE + 'px ' + fontInfo.fontFamily;
var glyphFont = label._fontStyle + ' ' + label._fontWeight + ' ' + SDFSettings.FONT_SIZE + 'px ' + label._fontFamily;

var canvas = createGlyphCanvas(character, glyphFont, Color.WHITE, Color.WHITE, 0.0, LabelStyle.FILL, verticalOrigin);

Expand Down

0 comments on commit 07b7d89

Please sign in to comment.