-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds letterSpacing property to BitmapText (#4642)
- Loading branch information
1 parent
e9faee4
commit a05e210
Showing
5 changed files
with
204 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { BitmapText } = require('../'); | ||
const { Texture, BaseTexture } = require('@pixi/core'); | ||
|
||
describe('PIXI.BitmapText', function () | ||
{ | ||
before(function (done) | ||
{ | ||
this.fontXML = null; | ||
this.fontImage = null; | ||
this.font = null; | ||
|
||
const resolveURL = (url) => path.resolve(this.resources, url); | ||
const loadXML = (url) => new Promise((resolve) => | ||
fs.readFile(resolveURL(url), 'utf8', (err, data) => | ||
{ | ||
expect(err).to.be.null; | ||
resolve((new window.DOMParser()).parseFromString(data, 'text/xml')); | ||
})); | ||
|
||
const loadImage = (url) => new Promise((resolve) => | ||
{ | ||
const image = new Image(); | ||
|
||
image.onload = () => resolve(image); | ||
image.src = resolveURL(url); | ||
}); | ||
|
||
this.resources = path.join(__dirname, 'resources'); | ||
Promise.all([ | ||
loadXML('font1.fnt'), | ||
loadImage('font1.png'), | ||
]).then(([ | ||
fontXML, | ||
fontImage, | ||
]) => | ||
{ | ||
this.fontXML = fontXML; | ||
this.fontImage = fontImage; | ||
const texture = new Texture(new BaseTexture(this.fontImage, null, 1)); | ||
|
||
this.font = BitmapText.registerFont(this.fontXML, texture); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('text', function () | ||
{ | ||
it('should render text even if there are unsupported characters', function () | ||
{ | ||
const text = new BitmapText('ABCDEFG', { | ||
font: this.font.font, | ||
}); | ||
|
||
expect(text.children.length).to.equal(4); | ||
}); | ||
it('should break line on space', function () | ||
{ | ||
const bmpText = new BitmapText('', { | ||
font: this.font.font, | ||
size: 24, | ||
}); | ||
|
||
bmpText.maxWidth = 40; | ||
bmpText.text = 'A A A A A A A '; | ||
bmpText.updateText(); | ||
|
||
expect(bmpText.textWidth).to.lessThan(bmpText.maxWidth); | ||
|
||
bmpText.maxWidth = 40; | ||
bmpText.text = 'A A A A A A A'; | ||
bmpText.updateText(); | ||
|
||
expect(bmpText.textWidth).to.lessThan(bmpText.maxWidth); | ||
}); | ||
it('letterSpacing should add extra space between characters', function () | ||
{ | ||
const text = 'ABCD zz DCBA'; | ||
const bmpText = new BitmapText(text, { | ||
font: this.font.font, | ||
}); | ||
const positions = []; | ||
const renderedChars = bmpText.children.length; | ||
|
||
for (let x = 0; x < renderedChars; ++x) | ||
{ | ||
positions.push(bmpText.children[x].x); | ||
} | ||
for (let space = 1; space < 20; ++space) | ||
{ | ||
bmpText.letterSpacing = space; | ||
bmpText.updateText(); | ||
let prevPos = bmpText.children[0].x; | ||
|
||
for (let char = 1; char < renderedChars; ++char) | ||
{ | ||
expect(bmpText.children[char].x).to.equal(prevPos + space + positions[char] - positions[char - 1]); | ||
prevPos = bmpText.children[char].x; | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
require('./BitmapText'); | ||
require('./BitmapFontLoader'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<font> | ||
<info face="font" size="24" bold="0" italic="0" charset="" unicode="" stretchH="100" smooth="1" aa="1" padding="2,2,2,2" spacing="0,0" outline="0"/> | ||
<common lineHeight="27" base="18" scaleW="46" scaleH="201" pages="1" packed="0"/> | ||
<pages> | ||
<page id="0" file="font.png"/> | ||
</pages> | ||
<chars count="15"> | ||
<char id="65" x="2" y="2" width="19" height="20" xoffset="0" yoffset="0" xadvance="16" page="0" chnl="15"/> | ||
<char id="66" x="2" y="24" width="15" height="20" xoffset="2" yoffset="0" xadvance="16" page="0" chnl="15"/> | ||
<char id="67" x="2" y="46" width="18" height="20" xoffset="1" yoffset="0" xadvance="17" page="0" chnl="15"/> | ||
<char id="68" x="19" y="24" width="17" height="20" xoffset="2" yoffset="0" xadvance="17" page="0" chnl="15"/> | ||
<char id="91" x="2" y="68" width="7" height="24" xoffset="2" yoffset="0" xadvance="7" page="0" chnl="15"/> | ||
<char id="45" x="23" y="2" width="9" height="4" xoffset="1" yoffset="10" xadvance="8" page="0" chnl="15"/> | ||
<char id="92" x="34" y="2" width="10" height="20" xoffset="-1" yoffset="0" xadvance="7" page="0" chnl="15"/> | ||
<char id="47" x="2" y="94" width="10" height="20" xoffset="-1" yoffset="0" xadvance="7" page="0" chnl="15"/> | ||
<char id="46" x="23" y="8" width="5" height="5" xoffset="2" yoffset="15" xadvance="7" page="0" chnl="15"/> | ||
<char id="44" x="11" y="68" width="5" height="8" xoffset="2" yoffset="15" xadvance="7" page="0" chnl="15"/> | ||
<char id="63" x="2" y="116" width="13" height="20" xoffset="1" yoffset="0" xadvance="13" page="0" chnl="15"/> | ||
<char id="33" x="14" y="78" width="5" height="20" xoffset="2" yoffset="0" xadvance="7" page="0" chnl="15"/> | ||
<char id="59" x="2" y="138" width="5" height="18" xoffset="2" yoffset="5" xadvance="7" page="0" chnl="15"/> | ||
<char id="58" x="2" y="158" width="5" height="15" xoffset="2" yoffset="5" xadvance="7" page="0" chnl="15"/> | ||
<char id="93" x="2" y="175" width="7" height="24" xoffset="0" yoffset="0" xadvance="7" page="0" chnl="15"/> | ||
<char id="32" x="0" y="0" width="0" height="0" xoffset="0" yoffset="0" xadvance="7" page="0" chnl="15"/> | ||
</chars> | ||
<kernings count="2"> | ||
<kerning first="32" second="65" amount="-1"/> | ||
<kerning first="65" second="32" amount="-1"/> | ||
</kernings> | ||
</font> |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.