This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1, text align does not work while it's being moving. 2, TextMetrics.wordWrap from official code adds extra useless blank to the head of the first text line, see issue: pixijs/pixijs#4251, and this is the hot fix for it. I've pushed this patch to the official Guithub as a pull-request already, once they merge it, I'll clear my code again. fix bug for GTextInput.ts 1, text align does not work.
- Loading branch information
Showing
3 changed files
with
135 additions
and
9 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,106 @@ | ||
/*this class is temporarily for the bug fixing purpose only, so once PIXI releases a new version, this class will be removed */ | ||
|
||
namespace PIXI.extras { | ||
|
||
export class Text extends PIXI.Text { | ||
|
||
private static __init:boolean = false; | ||
|
||
public constructor(text?:string, style?:PIXI.TextStyle, canvas?:HTMLCanvasElement) { | ||
super(text, style, canvas); | ||
|
||
if(!PIXI.extras.Text.__init) { | ||
PIXI.extras.Text.__init = true; | ||
|
||
//override | ||
PIXI.TextMetrics.wordWrap = function(text, style, canvas) | ||
{ | ||
if(!canvas) canvas = (PIXI.TextMetrics as any)["_canvas"]; | ||
|
||
const context = canvas.getContext('2d'); | ||
|
||
// Greedy wrapping algorithm that will wrap words as the line grows longer | ||
// than its horizontal bounds. | ||
let result = ''; | ||
let firstChar = text.charAt(0); | ||
const lines = text.split('\n'); | ||
const wordWrapWidth = style.wordWrapWidth; | ||
const characterCache:{ [char:string] : number } = {}; | ||
|
||
for (let i = 0; i < lines.length; i++) | ||
{ | ||
let spaceLeft = wordWrapWidth; | ||
const words = lines[i].split(' '); | ||
|
||
for (let j = 0; j < words.length; j++) | ||
{ | ||
const wordWidth = context.measureText(words[j]).width; | ||
|
||
if (style.breakWords && wordWidth > wordWrapWidth) | ||
{ | ||
// Word should be split in the middle | ||
const characters = words[j].split(''); | ||
|
||
for (let c = 0; c < characters.length; c++) | ||
{ | ||
const character = characters[c]; | ||
let characterWidth = characterCache[character]; | ||
|
||
if (characterWidth === undefined) | ||
{ | ||
characterWidth = context.measureText(character).width; | ||
characterCache[character] = characterWidth; | ||
} | ||
|
||
if (characterWidth > spaceLeft) | ||
{ | ||
result += `\n${character}`; | ||
spaceLeft = wordWrapWidth - characterWidth; | ||
} | ||
else | ||
{ | ||
if (c === 0 && (j > 0 || firstChar == ' ')) | ||
{ | ||
result += ' '; | ||
} | ||
|
||
result += character; | ||
spaceLeft -= characterWidth; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
const wordWidthWithSpace = wordWidth + context.measureText(' ').width; | ||
|
||
if (j === 0 || wordWidthWithSpace > spaceLeft) | ||
{ | ||
// Skip printing the newline if it's the first word of the line that is | ||
// greater than the word wrap width. | ||
if (j > 0) | ||
{ | ||
result += '\n'; | ||
} | ||
result += words[j]; | ||
spaceLeft = wordWrapWidth - wordWidth; | ||
} | ||
else | ||
{ | ||
spaceLeft -= wordWidthWithSpace; | ||
result += ` ${words[j]}`; | ||
} | ||
} | ||
} | ||
|
||
if (i < lines.length - 1) | ||
{ | ||
result += '\n'; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
} | ||
} |
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