Skip to content

Commit

Permalink
fixed up bugs with xtend
Browse files Browse the repository at this point in the history
  • Loading branch information
atlmtw committed Jul 20, 2022
1 parent 8fa064b commit 16be3b0
Show file tree
Hide file tree
Showing 8 changed files with 1,088 additions and 65 deletions.
30 changes: 14 additions & 16 deletions dist/three-bmfont-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ var TextLayout = /*#__PURE__*/function () {
function TextLayout(opt) {
_classCallCheck(this, TextLayout);

//getters for the private vars
['width', 'height', 'descender', 'ascender', 'xHeight', 'baseline', 'capHeight', 'lineHeight'].forEach(function (name) {
Object.defineProperty(TextLayout, name, {
get: wrapper(name),
configurable: true
});
});
this.glyphs = [];
this._measure = this.computeMetrics.bind(this);
this.update(opt);
Expand All @@ -195,6 +202,8 @@ var TextLayout = /*#__PURE__*/function () {
_createClass(TextLayout, [{
key: "update",
value: function update(opt) {
var _this = this;

opt = Object.assign({
measure: this._measure
}, opt);
Expand All @@ -207,7 +216,6 @@ var TextLayout = /*#__PURE__*/function () {

this._setupSpaceGlyphs(font);

console.log(font);
var lines = makeLines(text, opt);
var minWidth = opt.width || 0; //clear glyphs

Expand Down Expand Up @@ -237,7 +245,6 @@ var TextLayout = /*#__PURE__*/function () {
this._lineHeight = lineHeight;
this._ascender = lineHeight - descender - this._xHeight; //layout each glyph

var self = this;
lines.forEach(function (line, lineIndex) {
var start = line.start;
var end = line.end;
Expand All @@ -246,7 +253,8 @@ var TextLayout = /*#__PURE__*/function () {

for (var i = start; i < end; i++) {
var id = text.charCodeAt(i);
var glyph = self.getGlyph(font, id);

var glyph = _this.getGlyph(font, id);

if (glyph) {
if (lastGlyph) x += getKerning(font, lastGlyph.id, glyph.id);
Expand Down Expand Up @@ -281,7 +289,8 @@ var TextLayout = /*#__PURE__*/function () {
//then fall back to the 'm' or 'w' glyphs
//then fall back to the first glyph available

var space = getGlyphById(font, SPACE_ID) || getMGlyph(font) || font.chars[0]; //and create a fallback for tab
var space = getGlyphById(font, SPACE_ID) || getMGlyph(font) || font.chars[0];
var space = JSON.parse(JSON.stringify(space)); //and create a fallback for tab

var tabWidth = this._opt.tabSize * space.xadvance;
this._fallbackSpaceGlyph = space;
Expand Down Expand Up @@ -357,17 +366,7 @@ var TextLayout = /*#__PURE__*/function () {
}]);

return TextLayout;
}(); //getters for the private vars


['width', 'height', 'descender', 'ascender', 'xHeight', 'baseline', 'capHeight', 'lineHeight'].forEach(addGetter);

function addGetter(name) {
Object.defineProperty(TextLayout, name, {
get: wrapper(name),
configurable: true
});
} //create lookups for private vars
}(); //create lookups for private vars


function wrapper(name) {
Expand Down Expand Up @@ -691,7 +690,6 @@ var TextGeometry = /*#__PURE__*/function (_BufferGeometry) {
createTextGeometry_classCallCheck(this, TextGeometry);

_this = _super.call(this);
console.log(opt);

if (typeof opt === 'string') {
opt = {
Expand Down
2 changes: 1 addition & 1 deletion dist/three-bmfont-text.min.js

Large diffs are not rendered by default.

41 changes: 18 additions & 23 deletions dist/three-bmfont-text.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ function number(num, def) {

class TextLayout {
constructor(opt) {
//getters for the private vars
['width', 'height',
'descender', 'ascender',
'xHeight', 'baseline',
'capHeight',
'lineHeight'
].forEach((name) => {
Object.defineProperty(TextLayout, name, {
get: wrapper(name),
configurable: true
})
})

this.glyphs = []
this._measure = this.computeMetrics.bind(this)
this.update(opt)
Expand All @@ -203,8 +216,6 @@ class TextLayout {
var font = opt.font
this._setupSpaceGlyphs(font)

console.log(font);

var lines = makeLines(text, opt)
var minWidth = opt.width || 0

Expand Down Expand Up @@ -240,8 +251,7 @@ class TextLayout {
this._ascender = lineHeight - descender - this._xHeight

//layout each glyph
var self = this
lines.forEach(function (line, lineIndex) {
lines.forEach((line, lineIndex) => {
var start = line.start
var end = line.end
var lineWidth = line.width
Expand All @@ -250,7 +260,7 @@ class TextLayout {
//for each glyph in that line...
for (var i = start; i < end; i++) {
var id = text.charCodeAt(i)
var glyph = self.getGlyph(font, id)
var glyph = this.getGlyph(font, id)
if (glyph) {
if (lastGlyph)
x += getKerning(font, lastGlyph.id, glyph.id)
Expand Down Expand Up @@ -296,6 +306,8 @@ class TextLayout {
var space = getGlyphById(font, SPACE_ID) ||
getMGlyph(font) ||
font.chars[0]

var space = JSON.parse(JSON.stringify(space));

//and create a fallback for tab
var tabWidth = this._opt.tabSize * space.xadvance
Expand Down Expand Up @@ -378,21 +390,6 @@ class TextLayout {
}
}

//getters for the private vars
['width', 'height',
'descender', 'ascender',
'xHeight', 'baseline',
'capHeight',
'lineHeight'
].forEach(addGetter)

function addGetter(name) {
Object.defineProperty(TextLayout, name, {
get: wrapper(name),
configurable: true
})
}

//create lookups for private vars
function wrapper(name) {
return (new Function([
Expand All @@ -405,7 +402,7 @@ function wrapper(name) {
function getGlyphById(font, id) {
if (!font.chars || font.chars.length === 0)
return null

var glyphIdx = findChar(font.chars, id)
if (glyphIdx >= 0)
return font.chars[glyphIdx]
Expand Down Expand Up @@ -708,7 +705,6 @@ function createTextGeometry (opt) {
class TextGeometry extends external_three_namespaceObject.BufferGeometry {
constructor(opt){
super();
console.log(opt);

if (typeof opt === 'string') {
opt = { text: opt }
Expand All @@ -726,7 +722,6 @@ class TextGeometry extends external_three_namespaceObject.BufferGeometry {
if (typeof opt === 'string') {
opt = { text: opt }
}

// use constructor defaults
opt = Object.assign({}, this._opt, opt)

Expand Down
2 changes: 1 addition & 1 deletion dist/three-bmfont-text.module.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"scripts": {
"build": "webpack",
"dev": "webpack serve",
"browser" : "browserify test.js -o bundle.js",
"browser" : "npm run build; browserify test.js -o bundle.js;",
"build-babel": "npx babel src --out-dir dist",
"test-2d": "budo test/test-2d.js:bundle.js --dir test --live",
"test-3d": "budo test/test-3d.js:bundle.js --dir test --live",
Expand Down
2 changes: 0 additions & 2 deletions src/lib/createTextGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function createTextGeometry (opt) {
class TextGeometry extends BufferGeometry {
constructor(opt){
super();
console.log(opt);

if (typeof opt === 'string') {
opt = { text: opt }
Expand All @@ -31,7 +30,6 @@ class TextGeometry extends BufferGeometry {
if (typeof opt === 'string') {
opt = { text: opt }
}

// use constructor defaults
opt = Object.assign({}, this._opt, opt)

Expand Down
39 changes: 18 additions & 21 deletions src/lib/layout-bmfont-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ function number(num, def) {

class TextLayout {
constructor(opt) {
//getters for the private vars
['width', 'height',
'descender', 'ascender',
'xHeight', 'baseline',
'capHeight',
'lineHeight'
].forEach((name) => {
Object.defineProperty(TextLayout, name, {
get: wrapper(name),
configurable: true
})
})

this.glyphs = []
this._measure = this.computeMetrics.bind(this)
this.update(opt)
Expand All @@ -42,8 +55,6 @@ class TextLayout {
var font = opt.font
this._setupSpaceGlyphs(font)

console.log(font);

var lines = makeLines(text, opt)
var minWidth = opt.width || 0

Expand Down Expand Up @@ -79,8 +90,7 @@ class TextLayout {
this._ascender = lineHeight - descender - this._xHeight

//layout each glyph
var self = this
lines.forEach(function (line, lineIndex) {
lines.forEach((line, lineIndex) => {
var start = line.start
var end = line.end
var lineWidth = line.width
Expand All @@ -89,7 +99,7 @@ class TextLayout {
//for each glyph in that line...
for (var i = start; i < end; i++) {
var id = text.charCodeAt(i)
var glyph = self.getGlyph(font, id)
var glyph = this.getGlyph(font, id)
if (glyph) {
if (lastGlyph)
x += getKerning(font, lastGlyph.id, glyph.id)
Expand Down Expand Up @@ -135,6 +145,8 @@ class TextLayout {
var space = getGlyphById(font, SPACE_ID) ||
getMGlyph(font) ||
font.chars[0]

var space = JSON.parse(JSON.stringify(space));

//and create a fallback for tab
var tabWidth = this._opt.tabSize * space.xadvance
Expand Down Expand Up @@ -217,21 +229,6 @@ class TextLayout {
}
}

//getters for the private vars
['width', 'height',
'descender', 'ascender',
'xHeight', 'baseline',
'capHeight',
'lineHeight'
].forEach(addGetter)

function addGetter(name) {
Object.defineProperty(TextLayout, name, {
get: wrapper(name),
configurable: true
})
}

//create lookups for private vars
function wrapper(name) {
return (new Function([
Expand All @@ -244,7 +241,7 @@ function wrapper(name) {
function getGlyphById(font, id) {
if (!font.chars || font.chars.length === 0)
return null

var glyphIdx = findChar(font.chars, id)
if (glyphIdx >= 0)
return font.chars[glyphIdx]
Expand Down
Loading

0 comments on commit 16be3b0

Please sign in to comment.