Skip to content

Commit cbc68ab

Browse files
committed
Merge pull request phaserjs#1950 from jdnichollsc/dev
Added Text.addFontStyle and Text.addFontWeight
2 parents d23e5d6 + 655013d commit cbc68ab

2 files changed

Lines changed: 97 additions & 3 deletions

File tree

src/gameobjects/Text.js

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ Phaser.Text = function (game, x, y, text, style) {
9090
*/
9191
this.strokeColors = [];
9292

93+
/**
94+
* @property {array} fontStyles - An array of the font styles values as specified by {@link Phaser.Text#addFontStyle addFontStyle}.
95+
*/
96+
this.fontStyles = [];
97+
98+
/**
99+
* @property {array} fontWeights - An array of the font weights values as specified by {@link Phaser.Text#addFontWeight addFontWeight}.
100+
*/
101+
this.fontWeights = [];
102+
93103
/**
94104
* Should the linePositionX and Y values be automatically rounded before rendering the Text?
95105
* You may wish to enable this if you want to remove the effect of sub-pixel aliasing from text.
@@ -472,7 +482,7 @@ Phaser.Text.prototype.updateText = function () {
472482
linePositionY = Math.round(linePositionY);
473483
}
474484

475-
if (this.colors.length > 0 || this.strokeColors.length > 0)
485+
if (this.colors.length > 0 || this.strokeColors.length > 0 || this.fontWeights.length > 0 || this.fontStyles.length > 0)
476486
{
477487
this.updateLine(lines[i], linePositionX, linePositionY);
478488
}
@@ -603,7 +613,7 @@ Phaser.Text.prototype.updateShadow = function (state) {
603613
};
604614

605615
/**
606-
* Updates a line of text, applying fill and stroke per-character colors if applicable.
616+
* Updates a line of text, applying fill and stroke per-character colors or style and weight per-character font if applicable.
607617
*
608618
* @method Phaser.Text#updateLine
609619
* @private
@@ -614,6 +624,21 @@ Phaser.Text.prototype.updateLine = function (line, x, y) {
614624
{
615625
var letter = line[i];
616626

627+
if(this.fontWeights.length > 0 || this.fontStyles.length > 0){
628+
629+
var components = this.fontToComponents(this.context.font);
630+
631+
if(this.fontStyles[this._charCount]){
632+
components.fontStyle = this.fontStyles[this._charCount];
633+
}
634+
635+
if(this.fontWeights[this._charCount]){
636+
components.fontWeight = this.fontWeights[this._charCount];
637+
}
638+
639+
this.context.font = this.componentsToFont(components);
640+
}
641+
617642
if (this.style.stroke && this.style.strokeThickness)
618643
{
619644
if (this.strokeColors[this._charCount])
@@ -659,6 +684,22 @@ Phaser.Text.prototype.clearColors = function () {
659684

660685
};
661686

687+
/**
688+
* Clears any text styles or weights font that were set by `addFontStyle` or `addFontWeight`.
689+
*
690+
* @method Phaser.Text#clearFontValues
691+
* @return {Phaser.Text} This Text instance.
692+
*/
693+
Phaser.Text.prototype.clearFontValues = function () {
694+
695+
this.fontStyles = [];
696+
this.fontWeights = [];
697+
this.dirty = true;
698+
699+
return this;
700+
701+
};
702+
662703
/**
663704
* Set specific colors for certain characters within the Text.
664705
*
@@ -709,6 +750,54 @@ Phaser.Text.prototype.addStrokeColor = function (color, position) {
709750

710751
};
711752

753+
/**
754+
* Set specific font styles for certain characters within the Text.
755+
*
756+
* It works by taking a font style value, which is a typical string such as `normal`, `italic` or `oblique`.
757+
* The position value is the index of the character in the Text string to start applying this font style to.
758+
* Once set the font style remains in use until either another font style or the end of the string is encountered.
759+
* For example if the Text was `Photon Storm` and you did `Text.addFontStyle('italic', 6)` it would font style in the word `Storm` in italic.
760+
*
761+
* If you wish to change the text font weight see addFontWeight instead.
762+
*
763+
* @method Phaser.Text#addFontStyle
764+
* @param {string} style - A canvas font-style that will be used on the text style eg `normal`, `italic`, `oblique`.
765+
* @param {number} position - The index of the character in the string to start applying this font style value from.
766+
* @return {Phaser.Text} This Text instance.
767+
*/
768+
Phaser.Text.prototype.addFontStyle = function (style, position) {
769+
770+
this.fontStyles[position] = style;
771+
this.dirty = true;
772+
773+
return this;
774+
775+
};
776+
777+
/**
778+
* Set specific font weights for certain characters within the Text.
779+
*
780+
* It works by taking a font weight value, which is a typical string such as `normal`, `bold`, `bolder`, etc.
781+
* The position value is the index of the character in the Text string to start applying this font weight to.
782+
* Once set the font weight remains in use until either another font weight or the end of the string is encountered.
783+
* For example if the Text was `Photon Storm` and you did `Text.addFontWeight('bold', 6)` it would font weight in the word `Storm` in bold.
784+
*
785+
* If you wish to change the text font style see addFontStyle instead.
786+
*
787+
* @method Phaser.Text#addFontWeight
788+
* @param {string} style - A canvas font-weight that will be used on the text weight eg `normal`, `bold`, `bolder`, `lighter`, etc.
789+
* @param {number} position - The index of the character in the string to start applying this font weight value from.
790+
* @return {Phaser.Text} This Text instance.
791+
*/
792+
Phaser.Text.prototype.addFontWeight = function (weight, position) {
793+
794+
this.fontWeights[position] = weight;
795+
this.dirty = true;
796+
797+
return this;
798+
799+
};
800+
712801
/**
713802
* Greedy wrapping algorithm that will wrap words as the line grows longer than its horizontal bounds.
714803
*

typescript/phaser.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4564,9 +4564,11 @@ declare module Phaser {
45644564
fixedToCamera: boolean;
45654565
font: string;
45664566
fontSize: number|string;
4567-
fontWeight: string;
45684567
fontStyle: string;
4568+
fontStyles: string[];
45694569
fontVariant: string;
4570+
fontWeight: string;
4571+
fontWeights: string[];
45704572
game: Phaser.Game;
45714573
input: Phaser.InputHandler;
45724574
inputEnabled: boolean;
@@ -4601,8 +4603,11 @@ declare module Phaser {
46014603
z: number;
46024604

46034605
addColor(color: string, position: number): Phaser.Text;
4606+
addFontStyle(style: string, position: number): Phaser.Text;
4607+
addFontWeight(weight: string, position: number): Phaser.Text;
46044608
addStrokeColor(color: string, position: number): Phaser.Text;
46054609
clearColors(): Phaser.Text;
4610+
clearFontValues(): Phaser.Text;
46064611
componentsToFont(components: any): string;
46074612
destroy(destroyChildren?: boolean): void;
46084613
fontToComponents(font: string): any;

0 commit comments

Comments
 (0)