Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/components/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var cache = new PromiseCache();
var fontWidthFactors = {};
var textures = {};

// Regular expression for detecting a URLs with a protocol prefix.
var protocolRe = /^\w+:/;

/**
* SDF-based text component.
* Based on https://github.com/Jam3/three-bmfont-text.
Expand Down Expand Up @@ -95,13 +98,12 @@ module.exports.Component = registerComponent('text', {
update: function (oldData) {
var data = coerceData(this.data);
var font = this.currentFont;
var fontImage = this.getFontImageSrc();

if (textures[fontImage]) {
this.texture = textures[fontImage];
if (textures[data.font]) {
this.texture = textures[data.font];
} else {
// Create texture per font.
this.texture = textures[fontImage] = new THREE.Texture();
this.texture = textures[data.font] = new THREE.Texture();
this.texture.anisotropy = MAX_ANISOTROPY;
}

Expand Down Expand Up @@ -234,7 +236,7 @@ module.exports.Component = registerComponent('text', {
var texture = self.texture;
texture.image = image;
texture.needsUpdate = true;
textures[fontImgSrc] = texture;
textures[data.font] = texture;
self.texture = texture;
self.mesh.visible = true;
el.emit('textfontset', {font: data.font, fontObj: font});
Expand All @@ -249,8 +251,15 @@ module.exports.Component = registerComponent('text', {
},

getFontImageSrc: function () {
if (this.data.fontImage) { return this.data.fontImage; }
var fontSrc = this.lookupFont(this.data.font || DEFAULT_FONT) || this.data.font;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if we're following the vars-on-top style...it was insisted a while back and sometimes it's not followed very strictly, and sometimes just results in a mess of unordered var declarations / initializations at the top. And then when you make small code changes, need to shuffle the function to match the rule. Just ranting 😛

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No preference, I don't use it strictly in my own code. ¯_(ツ)_/¯

return this.data.fontImage || fontSrc.replace(/(\.fnt)|(\.json)/, '.png');
var imageSrc = this.currentFont.pages[0];
// If the image URL contains a non-HTTP(S) protocol, assume it's an absolute
// path on disk and try to infer the path from the font source instead.
if (imageSrc.match(protocolRe) && imageSrc.indexOf('http') !== 0) {
return fontSrc.replace(/(\.fnt)|(\.json)/, '.png');
}
return THREE.LoaderUtils.extractUrlBase(fontSrc) + imageSrc;
},

/**
Expand Down