pixi-glyphs 4.0.2 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,7 +48,17 @@ The style objects are modified versions (supersets) of `PIXI.TextStyle` (referre
48
48
 
49
49
  (By the way, there is an excellent [TextStyle visual editor](https://pixijs.io/pixi-text-style/) that you can use to preview how your style will appear. The style objects generated can be used in this component.)
50
50
 
51
- - Everything in [`PIXI.TextStyle`](https://pixijs.download/dev/docs/PIXI.TextStyle.html)
51
+ - Everything in [`PIXI.TextStyle`](https://pixijs.download/dev/docs/PIXI.TextStyle.html) including:
52
+ - `fontFamily` - The font family. e.g. `"Arial"` or `"Arial, Helvetica, sans-serif"`
53
+ - `fontStyle` - Font style. Options are `"normal"`, `"italic"`, `"oblique"`. Default is `"normal"`.
54
+ - `fontWeight` - Font weight. Options include `"normal"`, `"bold"`, `"bolder"`, `"lighter"`, `"100"` through `"900"`. Default is `"normal"`.
55
+ - `stroke` - Stroke color for the text. Default is `0x000000` (black).
56
+ - `strokeThickness` - Thickness of the stroke. Default is `0` (no stroke).
57
+ - `dropShadow` - Enable drop shadow. Default is `false`.
58
+ - `dropShadowColor` - Drop shadow color. Default is `0x000000`.
59
+ - `dropShadowBlur`, `dropShadowAngle`, `dropShadowDistance` - Drop shadow properties.
60
+ - `padding` - Padding around the text. Default is `0`.
61
+ - `trim` - Trim transparent borders. Default is `false`.
52
62
  - `align` - Has all the options from `PIXI.TextStyle` plus additional options for justified alignment:
53
63
  - `"justify-left"`, `"justify-right"`, and `"justify-center"` are all types of jutified alignment and they only differ in how they treat the last line of text.
54
64
  - `"justify-all"` justifies all lines of text even the last line.
@@ -58,11 +68,18 @@ The style objects are modified versions (supersets) of `PIXI.TextStyle` (referre
58
68
  - `fontSize` - Has the added ability to use percentage-based sizes which are based on the `fontSize` in the parent tag. In the example: `<small fontSize="10">small<big fontSize="300%">big</big></small>`, "small" will be 10px and "big" will be 30px. The default `fontSize` is `26`px.
59
69
  - `fontScaleWidth` - Percentage to scale the font e.g. `0.5` = 50%
60
70
  - `fontScaleHeight` - Percentage to scale the font e.g. `1.25` = 125%
71
+ - `letterSpacing` - Additional horizontal spacing between each character in pixels. Can be positive to spread letters apart or negative to bring them closer together. Default is `0`.
72
+ - `lineSpacing` - Additional vertical spacing between lines of text in pixels. This spacing is added between all lines within a paragraph. Default is `0`.
61
73
  - `paragraphSpacing` - Additional spacing between paragraphs that is added when you use an explicit carriage return rather than letting the text wrap at the end of a line. Default is `0`. Can also be negative.
62
74
  - `breakLines` - When `breakLines` is `false`, the text in the tag will ignore the `wordWrapWidth` property and never wrap to the next line unless you explicitly include a newline character. It essentially treats the whole tag as a single word. If a nested tag overrides this, only the text inside the nested tag will wrap. Default is `true`.
63
75
  - `imgSrc` - ID of image to include in this tag (see `imgMap` under Options section)
64
- - `imgDisplay` - How should the image be displayed. `"block"` is no scaling, `"icon"` scales the image to match the text-size and appear inline.
76
+ - `imgDisplay` - How should the image be displayed. `"block"` is no scaling, `"icon"` scales the image to match the text-size and appear inline, `"inline"` displays at original size inline. Default is `"inline"`.
65
77
  - `iconScale` - If you use `imgDisplay="icon"`, this value will scale the size of the icon relative to the text size. Default is `1.0` (or 100%)
78
+ - `imgScale` - Scales both width and height of an image by a percentage. e.g. `"50%"` makes the image half size.
79
+ - `imgScaleX` - Scales only the width of an image by a percentage. e.g. `"200%"` makes the image twice as wide.
80
+ - `imgScaleY` - Scales only the height of an image by a percentage. e.g. `"75%"` makes the image 3/4 of its original height.
81
+ - `imgWidth` - Sets the width of an image in pixels or as a percentage. e.g. `100` or `"50%"`.
82
+ - `imgHeight` - Sets the height of an image in pixels or as a percentage. e.g. `50` or `"150%"`.
66
83
  - `textDecoration` - (i.e. underlines) Adds lines under, over, or through your text. Possible values are either `"normal"` or one or more of `"underline"`, `"overline"`, `"line-through"` (as a space separated string). Can also be set using the more fine-grained properties below. By default, decorations have `color` that matches the `fill` color of the text, `thickness` of `1` and `offset` `0`. **Note: You may need to enabled the `drawWhitespace` option in `options` to avoid seeing gaps in your text decorations between words.**
67
84
  - `decorationColor` - overrides the default color (`fill`) for all decorations.
68
85
  - `decorationThickness` - overrides the default thickness (`1`) for all decorations.
@@ -76,6 +93,7 @@ The style objects are modified versions (supersets) of `PIXI.TextStyle` (referre
76
93
  - `lineThroughThickness` - Sets the thickness of the line-through. Default is `1`.
77
94
  - `lineThroughOffset` - Positions the line-through above or below the default location. Default is `0`.
78
95
  - `adjustBaseline` - Adjusts the position of the text above or below the baseline. Default is `0`. Also see the `adjustFontBaseline` property in the options.
96
+ - `highlightColor` - Adds a background highlight color behind the text. Can be a hex number like `0xFFEB3B` or a hex string like `"#FFEB3B"`. The highlight appears as a continuous solid color box behind the text with no borders, spanning across spaces between words for a seamless highlight effect. When the same highlight color is applied to consecutive text segments, they will be rendered as a single continuous highlight box. Perfect for emphasizing important text, creating visual hierarchy, inline code blocks, or implementing syntax highlighting. Example: `<highlight>This entire phrase is highlighted</highlight>` will create one continuous highlight box.
79
97
  - `color` - An alias for `fill`. It's recommended you just use either `fill` or `color`, but if both are set, `fill` will be used. If tags are nested, `color` on an inner tag can override `fill` in an outer tag.
80
98
 
81
99
  Additionally, the following changes have been made to the default style values:
@@ -86,7 +104,7 @@ Additionally, the following changes have been made to the default style values:
86
104
 
87
105
  ##### 'Default' `default` styles
88
106
 
89
- Some styles (`fontSize`, `color`, etc.) are set by default when you call `new Glyphs()` but they can all be overridden. The most important default styles are:
107
+ Some styles (`fontSize`, `color`, etc.) are set by default when you call `new Glyphs()` but they can all be overridden. The complete default styles are:
90
108
 
91
109
  ```javascript
92
110
  {
@@ -95,6 +113,12 @@ Some styles (`fontSize`, `color`, etc.) are set by default when you call `new Gl
95
113
  wordWrap: true,
96
114
  wordWrapWidth: 500,
97
115
  fill: 0x000000,
116
+ fontSize: 26,
117
+ stroke: 0x000000,
118
+ dropShadowColor: 0x000000,
119
+ imgDisplay: "inline",
120
+ iconScale: 1.0,
121
+ breakLines: true
98
122
  }
99
123
  ```
100
124
 
@@ -117,6 +141,8 @@ The third parameter in the Glyphs constructor is a set of options.
117
141
  - `supressConsole` - When `true`, prevents warnings and other messages from being logged to the console, however, this does **not** affect `debugConsole`.
118
142
  - `errorHandler` - A handler function that will receive non-fatal warnings and errors generated by the component. **This will not catch any exceptions**, only internal messaging such as when you try use an unknown tag. The handler should be in the format `(e:ErrorMessage) => void` where `ErrorMessage` is an object with the shape `{ code: string, message: string, type: "warning" | "error" }`.
119
143
  - `overdrawDecorations` - When using text decorations (e.g. `underline`) an additional length in pixels equal to `overdrawDecorations` will be added to _both_ sides of the line. Default is `0`.
144
+ - `lineSpacing` - Sets the default line spacing for all text. Can also be set per-tag in styles. Default is `0`.
145
+ - `paragraphSpacing` - Sets the default paragraph spacing for all text. Can also be set per-tag in styles. Default is `0`.
120
146
 
121
147
  To see a list of the default options, you can view the static property `Glyphs.defaultOptions` or look in the source code for `DEFAULT_OPTIONS`
122
148
 
package/dist/Glyphs.d.ts CHANGED
@@ -39,6 +39,8 @@ export default class Glyphs<TextType extends PixiTextTypes = PIXI.Text> extends
39
39
  private _debugGraphics;
40
40
  private _textContainer;
41
41
  get textContainer(): PIXI.Container;
42
+ private _highlightContainer;
43
+ get highlightContainer(): PIXI.Container;
42
44
  private _decorationContainer;
43
45
  get decorationContainer(): PIXI.Container;
44
46
  private _spriteContainer;
@@ -58,6 +60,7 @@ export default class Glyphs<TextType extends PixiTextTypes = PIXI.Text> extends
58
60
  draw(): void;
59
61
  protected createDrawingForTextDecoration(textDecoration: TextDecorationMetrics): PIXI.Graphics;
60
62
  protected createTextField(text: string, style: TextStyleExtended): TextType;
63
+ protected createHighlightBox(highlightColor: any, x: number, y: number, width: number, height: number): PIXI.Graphics | null;
61
64
  protected createTextFieldForToken(token: TextSegmentToken): TextType;
62
65
  toDebugString(): string;
63
66
  drawDebug(): void;
@@ -1492,6 +1492,9 @@ class Glyphs extends PIXI__namespace.Container {
1492
1492
  get textContainer() {
1493
1493
  return this._textContainer;
1494
1494
  }
1495
+ get highlightContainer() {
1496
+ return this._highlightContainer;
1497
+ }
1495
1498
  get decorationContainer() {
1496
1499
  return this._decorationContainer;
1497
1500
  }
@@ -1515,12 +1518,14 @@ class Glyphs extends PIXI__namespace.Container {
1515
1518
  this._spriteTemplates = {};
1516
1519
  this._debugGraphics = void 0;
1517
1520
  this._textContainer = void 0;
1521
+ this._highlightContainer = void 0;
1518
1522
  this._decorationContainer = void 0;
1519
1523
  this._spriteContainer = void 0;
1520
1524
  this._debugContainer = void 0;
1521
1525
  this.logWarning = (code, message) => logWarning(this.options.errorHandler, this.options.supressConsole, this)(code, message);
1522
1526
  this._textContainer = new PIXI__namespace.Container();
1523
1527
  this._spriteContainer = new PIXI__namespace.Container();
1528
+ this._highlightContainer = new PIXI__namespace.Container();
1524
1529
  this._decorationContainer = new PIXI__namespace.Container();
1525
1530
  this._debugContainer = new PIXI__namespace.Container();
1526
1531
  this._debugGraphics = new PIXI__namespace.Graphics();
@@ -1541,9 +1546,18 @@ class Glyphs extends PIXI__namespace.Container {
1541
1546
  ...userStyles
1542
1547
  };
1543
1548
  }
1549
+ const styleOverrides = {
1550
+ ...tagStyles.default
1551
+ };
1552
+ if (options.lineSpacing !== undefined) {
1553
+ styleOverrides.lineSpacing = options.lineSpacing;
1554
+ }
1555
+ if (options.paragraphSpacing !== undefined) {
1556
+ styleOverrides.paragraphSpacing = options.paragraphSpacing;
1557
+ }
1544
1558
  const mergedDefaultStyles = {
1545
1559
  ...DEFAULT_STYLE,
1546
- ...tagStyles.default
1560
+ ...styleOverrides
1547
1561
  };
1548
1562
  tagStyles.default = mergedDefaultStyles;
1549
1563
  this.tagStyles = tagStyles;
@@ -1588,6 +1602,12 @@ class Glyphs extends PIXI__namespace.Container {
1588
1602
  this._options = {};
1589
1603
  }
1590
1604
  resetChildren() {
1605
+ if (this._highlightContainer) {
1606
+ this._highlightContainer.removeChildren();
1607
+ this.removeChild(this._highlightContainer);
1608
+ }
1609
+ this._highlightContainer = new PIXI__namespace.Container();
1610
+ this.addChild(this._highlightContainer);
1591
1611
  if (this._textContainer) {
1592
1612
  this._textContainer.removeChildren();
1593
1613
  this.removeChild(this._textContainer);
@@ -1703,16 +1723,47 @@ class Glyphs extends PIXI__namespace.Container {
1703
1723
  }
1704
1724
  draw() {
1705
1725
  this.resetChildren();
1706
- if (this.textContainer === null || this.spriteContainer === null) {
1707
- throw new Error("Somehow the textContainer or spriteContainer is null. This shouldn't be possible. Perhaps you've destroyed this object?");
1726
+ if (this.textContainer === null || this.spriteContainer === null || this.highlightContainer === null) {
1727
+ throw new Error("Somehow the textContainer, spriteContainer, or highlightContainer is null. This shouldn't be possible. Perhaps you've destroyed this object?");
1708
1728
  }
1709
1729
  const textContainer = this.textContainer;
1710
1730
  const spriteContainer = this.spriteContainer;
1731
+ const highlightContainer = this.highlightContainer;
1711
1732
  const {
1712
1733
  drawWhitespace
1713
1734
  } = this.options;
1714
1735
  const tokensFlat = this.tokensFlat;
1715
1736
  const tokens = drawWhitespace ? tokensFlat : tokensFlat.filter(isNotWhitespaceToken);
1737
+ let i = 0;
1738
+ while (i < tokensFlat.length) {
1739
+ const token = tokensFlat[i];
1740
+ if (isTextToken(token) && token.style.highlightColor !== undefined) {
1741
+ const highlightColor = token.style.highlightColor;
1742
+ let startX = token.bounds.x;
1743
+ let minY = token.bounds.y;
1744
+ let maxY = token.bounds.y + token.bounds.height;
1745
+ let endX = token.bounds.x + token.bounds.width;
1746
+ let j = i + 1;
1747
+ while (j < tokensFlat.length) {
1748
+ const nextToken = tokensFlat[j];
1749
+ if (isTextToken(nextToken) && nextToken.style.highlightColor === highlightColor && Math.abs(nextToken.bounds.y - token.bounds.y) < 5) {
1750
+ endX = nextToken.bounds.x + nextToken.bounds.width;
1751
+ minY = Math.min(minY, nextToken.bounds.y);
1752
+ maxY = Math.max(maxY, nextToken.bounds.y + nextToken.bounds.height);
1753
+ j++;
1754
+ } else {
1755
+ break;
1756
+ }
1757
+ }
1758
+ const highlight = this.createHighlightBox(highlightColor, startX, minY, endX - startX, maxY - minY);
1759
+ if (highlight) {
1760
+ highlightContainer.addChild(highlight);
1761
+ }
1762
+ i = j;
1763
+ } else {
1764
+ i++;
1765
+ }
1766
+ }
1716
1767
  let drewDecorations = false;
1717
1768
  let displayObject;
1718
1769
  tokens.forEach((t, index) => {
@@ -1816,6 +1867,28 @@ class Glyphs extends PIXI__namespace.Container {
1816
1867
  });
1817
1868
  return textField;
1818
1869
  }
1870
+ createHighlightBox(highlightColor, x, y, width, height) {
1871
+ if (!highlightColor || width <= 0 || height <= 0) {
1872
+ return null;
1873
+ }
1874
+ const highlight = new PIXI__namespace.Graphics();
1875
+ let color = highlightColor;
1876
+ if (typeof color === "string") {
1877
+ if (color.indexOf("#") === 0) {
1878
+ color = "0x" + color.substring(1);
1879
+ color = parseInt(color, 16);
1880
+ } else {
1881
+ this.logWarning("invalid-highlight-color", "Sorry, at this point, only hex colors are supported for highlightColor. Please use either a hex number like 0x66FF33 or a string like '#66FF33'");
1882
+ color = 0xFFFF00;
1883
+ }
1884
+ }
1885
+ highlight.rect(0, 0, width, height).fill({
1886
+ color: color
1887
+ });
1888
+ highlight.x = x;
1889
+ highlight.y = y;
1890
+ return highlight;
1891
+ }
1819
1892
  createTextFieldForToken(token) {
1820
1893
  const {
1821
1894
  textTransform = ""