- Alignment
- FontProps
- FontStyle
- FontVariant
- HAlignment
- LayoutOptions
- Line
- Lines
- LinesProps
- MeasureOptions
- MinimalCanvas
- MinimalCanvasContext
- NumberFunc
- NumberLineFunc
- Overflow
- OverflowWrap
- RotatorOptions
- VAlignment
Renames and re-exports Textbox
A convenience utility class to assist rotating text.
new Rotator(opts: RotatorOptions): Rotator;| Parameter | Type |
|---|---|
opts |
RotatorOptions |
Rotator
get origin(): [number, number];Origin point of the rotation
[number, number]
align(): HAlignment;Controls the horizontal alignment of the text.
By default this will be set to "left".
align(align: HAlignment): this;Controls the horizontal alignment of the text.
By default this will be set to "left".
| Parameter | Type |
|---|---|
align |
HAlignment |
this
anchor(): string;A convenience method to set or get a rotation anchor.
A rotation anchor controls the origin point of the rotation relative to textbox. It is a string of one or more of alignment keywords separated by spaces.
Keywords: [ top, middle, bottom, left, center, right ].
By default it is set to "top left"
string
anchor(anchor: string): this;A convenience method to set or get a rotation anchor.
A rotation anchor controls the origin point of the rotation relative to textbox. It is a string of one or more of alignment keywords separated by spaces.
Keywords: [ top, middle, bottom, left, center, right ].
By default it is set to "top left"
| Parameter | Type |
|---|---|
anchor |
string |
this
createElement(): CreateElementFunc;The element factory function to use when constructing SVG elements within the SVG renderer.
The interface conforms to React's React.createElement so you may simply set that function as
the factory if you want to use the rendered text in a React render tree.
CreateElementFunc
createElement(factory: CreateElementFunc): this;The element factory function to use when constructing SVG elements within the SVG renderer.
The interface conforms to React's React.createElement so you may simply set that function as
the factory if you want to use the rendered text in a React render tree.
| Parameter | Type |
|---|---|
factory |
CreateElementFunc |
this
height(): number;Controls the height of the rotation box in pixels.
By default this will be set to Infinity.
number
height(height: number): this;Controls the height of the rotation box in pixels.
By default this will be set to Infinity.
| Parameter | Type |
|---|---|
height |
number |
this
render(contentOrCallback: SVGElement): SVGElement;Render onto a Canvas or as an SVG element based on whether a canvas based target was supplied or not.
| Parameter | Type |
|---|---|
contentOrCallback |
SVGElement |
SVGElement
render(contentOrCallback: () => void, ctx: OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D): void;Render onto a Canvas or as an SVG element based on whether a canvas based target was supplied or not.
| Parameter | Type |
|---|---|
contentOrCallback |
() => void |
ctx |
OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D |
void
renderCanvas(callback: (ctx: CanvasRenderingContext2D) => void, target: OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D): void;Set up a rotated context for additional rendering.
The method will set up a rotated context, call the supplied callback argument with a CanvasRenderingContext2D as its argument, and then clean up.
| Parameter | Type |
|---|---|
callback |
(ctx: CanvasRenderingContext2D) => void |
target |
OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D |
void
renderSVG(content: SVGElement): SVGElement;Render an SVG element that is rotated around its origin point and place the given content inside it.
| Parameter | Type |
|---|---|
content |
SVGElement |
SVGElement
rotate(): number;Controls the angle of rotation in degrees.
By default this will be set to 0.
number
rotate(degrees: number): this;Controls the angle of rotation in degrees.
By default this will be set to 0.
| Parameter | Type |
|---|---|
degrees |
number |
this
valign(): VAlignment;Controls the vertical anchor point of the rotation.
By default this will be set to "top".
valign(align: VAlignment): this;Controls the vertical anchor point of the rotation.
By default this will be set to "top".
| Parameter | Type |
|---|---|
align |
VAlignment |
this
width(): number;Controls the width of the rotation box in pixels.
By default this will be set to Infinity.
number
width(width: number): this;Controls the width of the rotation box in pixels.
By default this will be set to Infinity.
| Parameter | Type |
|---|---|
width |
number |
this
A Textbox formatter that allows flowing rich text into a preset area.
new Textbox(options?: Partial<LayoutOptions>): Textbox;| Parameter | Type |
|---|---|
options? |
Partial<LayoutOptions> |
Textbox
| Property | Modifier | Type | Default value | Description |
|---|---|---|---|---|
createElement |
static |
(name: string, props?: | Record<string, string | number | boolean | null> | null, ...children: (string | SVGElement | null | undefined)[]) => SVGElement |
createElement |
Default factory function to use when constructing SVG elements. |
defaultparser |
static |
(text?: string | null) => Token[] |
textparser |
Default parser to use when consuming text. |
measureText |
static |
(token: string | Token, font: string | FontProps, options: MeasureOptions) => number |
measureText |
Measure a string of text as printed with a specified font and return its width. |
setMeasureCanvas |
static |
(canvas: MinimalCanvas | null) => void |
setMeasureCanvas |
Set the canvas to use when measuring text (in a non-browser environment). |
align(): Alignment;Controls the horizontal alignment of the text.
By default this will be set to "left".
align(align: Alignment): this;Controls the horizontal alignment of the text.
By default this will be set to "left".
| Parameter | Type |
|---|---|
align |
Alignment |
this
createElement(): CreateElementFunc;The element factory function to use when constructing SVG elements within the SVG renderer.
The interface conforms to React's React.createElement so you may simply set that function as
the factory if you want to use the rendered text in a React render tree.
CreateElementFunc
createElement(factory: CreateElementFunc): this;The element factory function to use when constructing SVG elements within the SVG renderer.
The interface conforms to React's React.createElement so you may simply set that function as
the factory if you want to use the rendered text in a React render tree.
| Parameter | Type |
|---|---|
factory |
CreateElementFunc |
this
font(): string;Set or get a default font for the text. Parts of the text may override the default font, but this defines the font used when nothing else is specified.
The method assumes a [CSS-style font declaration])(eb/CSS/Reference/Properties/font) string.
By default this will be set to "12px/14px sans-serif".
string
font(font: string): this;Set or get a default font for the text. Parts of the text may override the default font, but this defines the font used when nothing else is specified.
The method assumes a [CSS-style font declaration])(eb/CSS/Reference/Properties/font) string.
By default this will be set to "12px/14px sans-serif".
| Parameter | Type |
|---|---|
font |
string |
this
height(): number | NumberFunc;Controls the height of the textbox in which to fit the text.
By default this will be set to Infinity which mean text will
not be subject to overflow.
number | NumberFunc
height(height: number | NumberFunc): this;Controls the height of the textbox in which to fit the text.
By default this will be set to Infinity which mean text will
not be subject to overflow.
| Parameter | Type |
|---|---|
height |
number | NumberFunc |
this
linebreak(text: string): Lines;Parses text, flows it into the set dimensions and returns a list of the lines. The returned object can then be passed on to the renderer.
As well as a list of lines of tokens, the lines object has a height property which is useful if you want to set the render destination to the fit the text.
The lines object also includes a .hasOverflow property which indicates if the
text was able to fit a designated height. It will be true if the text did not
fit the defined space.
| Parameter | Type |
|---|---|
text |
string |
overflow(): string;The symbol to use when indicating text overflow (text that is cut off because it doesn't fit within the set boundaries).
May be set to any string or the keyword "ellipsis".
By default this will be set to "ellipsis" which prints ….
string
overflow(overflow: string): this;The symbol to use when indicating text overflow (text that is cut off because it doesn't fit within the set boundaries).
May be set to any string or the keyword "ellipsis".
By default this will be set to "ellipsis" which prints ….
| Parameter | Type |
|---|---|
overflow |
string |
this
overflowLine(): string;The symbol to use when indicating a single line text overflow.
May be set to any string or "" to turn off line overflow cutoff.
By default this will be set to "" which will allow words longer
than the width of the textbox to print in full.
string
overflowLine(overflow: string): this;The symbol to use when indicating a single line text overflow.
May be set to any string or "" to turn off line overflow cutoff.
By default this will be set to "" which will allow words longer
than the width of the textbox to print in full.
| Parameter | Type |
|---|---|
overflow |
string |
this
overflowWrap(): OverflowWrap;Controls whether line breaks should be inserted within an otherwise unbreakable string to prevent text from overflowing the textbox.
- Set this to
"break-word"to allow extra breaks mid-word. - Set this to
"normal"to only allow natural breaks at whitespace, punctuation, or hyphens.
By default this will be set to "normal".
overflowWrap(wrap: OverflowWrap): this;Controls whether line breaks should be inserted within an otherwise unbreakable string to prevent text from overflowing the textbox.
- Set this to
"break-word"to allow extra breaks mid-word. - Set this to
"normal"to only allow natural breaks at whitespace, punctuation, or hyphens.
By default this will be set to "normal".
| Parameter | Type |
|---|---|
wrap |
OverflowWrap |
this
parser(): (text: string) => Token[];The parser to use when parsing text input.
- Set it to
"text"for plaintext parsing. - Set it to
"html"for basic HTML parsing (it understands such things as<b>,<sup>,<tt>, ...). - Set it to
"latex"for a very basic LaTeX parser (it understands basic text commands and punctuation - no math mode).
An alternative parser may be provided as a callback, as long as it conforms to the parser interface.
By default the parser is set to "text".
(text: string): Token[];| Parameter | Type |
|---|---|
text |
string |
Token[]
parser(parser:
| "text"
| (text: string) => Token[]
| "html"
| "latex"): this;The parser to use when parsing text input.
- Set it to
"text"for plaintext parsing. - Set it to
"html"for basic HTML parsing (it understands such things as<b>,<sup>,<tt>, ...). - Set it to
"latex"for a very basic LaTeX parser (it understands basic text commands and punctuation - no math mode).
An alternative parser may be provided as a callback, as long as it conforms to the parser interface.
By default the parser is set to "text".
| Parameter | Type |
|---|---|
parser |
| "text" | (text: string) => Token[] | "html" | "latex" |
this
render(text: string | Lines): SVGElement;Render text or linebreak-output onto a Canvas or as an SVG element.
This method determines which render output is desired based on whether a canvas based target was supplied or not.
| Parameter | Type |
|---|---|
text |
string | Lines |
SVGElement
render(text: string | Lines, target: OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D): void;Render text or linebreak-output onto a Canvas or as an SVG element.
This method determines which render output is desired based on whether a canvas based target was supplied or not.
| Parameter | Type |
|---|---|
text |
string | Lines |
target |
OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D |
void
renderCanvas(text: string | Lines, target: OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D): void;Render text or linebreak-output onto a Canvas.
| Parameter | Type |
|---|---|
text |
string | Lines |
target |
OffscreenCanvas | HTMLCanvasElement | CanvasRenderingContext2D |
void
renderSVG(text: string | Lines): SVGElement;Render text or linebreak-output as an SVG element.
The output will be a single root text element with the lines and sections contained within.
| Parameter | Type |
|---|---|
text |
string | Lines |
SVGElement
valign(): VAlignment;Controls the vertical alignment of the text.
By default this will be set to "top".
valign(align: VAlignment): this;Controls the vertical alignment of the text.
By default this will be set to "top".
| Parameter | Type |
|---|---|
align |
VAlignment |
this
width(): number | NumberLineFunc;Controls the width of the textbox in which to fit the text.
This can be a callback function if you want runaround text layout, or to flow the text into irregular space.
By default this will be set to Infinity which prints a text
without linebreaks into a single line.
number | NumberLineFunc
width(width: number | NumberLineFunc): this;Controls the width of the textbox in which to fit the text.
This can be a callback function if you want runaround text layout, or to flow the text into irregular space.
By default this will be set to Infinity which prints a text
without linebreaks into a single line.
| Parameter | Type |
|---|---|
width |
number | NumberLineFunc |
this
x(): number | NumberLineFunc;Controls the indentation of text lines.
This is useful for fitting text into non-rectanglular shapes. A callback provided as an argument this will be called every line with the line number as a parameter.
By default this will be set to 0.
number | NumberLineFunc
x(x: number | NumberLineFunc): this;Controls the indentation of text lines.
This is useful for fitting text into non-rectanglular shapes. A callback provided as an argument this will be called every line with the line number as a parameter.
By default this will be set to 0.
| Parameter | Type |
|---|---|
x |
number | NumberLineFunc |
this
function htmlparser(text?: string | null): Token[];Parse a very small subset of HTML.
This parser can handle linebreaks, as well as inline text
instruction tags such as <b>, <tt>, <em>, <sup>.
| Parameter | Type |
|---|---|
text? |
string | null |
Token[]
function latexparser(text?: string | null): Token[];Parse a very small subset of LaTeX.
This parser can handle linebreaks, verbatim, as well as inline text instructions for non-math text. Math mode is not supported at all.
| Parameter | Type |
|---|---|
text? |
string | null |
Token[]
function measureText(
token: string | Token,
font: string | FontProps,
options: MeasureOptions): number;Measure a string of text as printed with a specified font and return its width.
Be careful that in non-browser environments you may want to supply an alternative canvas using setMeasureCanvas or this method will default to a crude "best guess" method.
| Parameter | Type | Default value | Description |
|---|---|---|---|
token |
string | Token |
undefined |
A string or token of text to measure. |
font |
string | FontProps |
undefined |
A CSS font shorthand string or a collection of font properties. |
options |
MeasureOptions |
defaultOptions |
Text handling options. |
number
function setMeasureCanvas(canvas: MinimalCanvas | null): void;Set the canvas to use when measuring text. This will be needed when using measureText in a non-browser environment.
| Parameter | Type | Description |
|---|---|---|
canvas |
MinimalCanvas | null |
The canvas interface to use. |
void
function textparser(text?: string | null): Token[];A basic text parser that sections texts into words but also deals with inserting appropriate breaks at correct points.
| Parameter | Type |
|---|---|
text? |
string | null |
Token[]
Represents a unit of text, typically a single word.
| Property | Type | Inherited from |
|---|---|---|
font |
FontProps |
Token.font |
line? |
number |
Token.line |
value |
string |
Token.value |
whitespace |
boolean | undefined |
Token.whitespace |
width |
number |
Token.width |
toJSON(): any;any
Represents a unit of text, typically a single word.
| Property | Type | Inherited from |
|---|---|---|
font |
FontProps |
Token.font |
line? |
number |
Token.line |
value |
string |
Token.value |
whitespace |
boolean | undefined |
Token.whitespace |
width |
number |
Token.width |
toJSON(): any;any
Represents a unit of text, typically a single word.
| Property | Type | Inherited from |
|---|---|---|
font |
FontProps |
Token.font |
line? |
number |
Token.line |
value |
string |
Token.value |
whitespace |
boolean | undefined |
Token.whitespace |
width |
number |
Token.width |
toJSON(): any;any
Represents a unit of text, typically a single word.
| Property | Type |
|---|---|
font |
FontProps |
line? |
number |
value |
string |
whitespace |
boolean | undefined |
width |
number |
toJSON(): any;any
type Alignment = "left" | "center" | "right" | "justify";Text alignment.
type FontProps = {
baseline?: number;
class?: string;
color?: string;
family?: string;
height?: number;
href?: string;
rel?: string;
size?: number;
sizeAdjust?: number;
style?: FontStyle;
target?: string;
tracking?: number;
variant?: FontVariant;
weight?: number;
};A collection of font properties or styles.
| Property | Type | Description |
|---|---|---|
baseline? |
number |
Baseline adjustment factor (such as for sub-/superscript). |
class? |
string |
Class identifier to be applied to emitted elements. |
color? |
string |
Color of the text. |
family? |
string |
Name of the font's type family. |
height? |
number |
Height of the leading (line-height). |
href? |
string |
HREF URL property for the text. |
rel? |
string |
Rel identifier to be applied to emitted link elements (use with href). |
size? |
number |
Size of the font in pixels. |
sizeAdjust? |
number |
Size adjustment multiplier (such as for sub-/superscript). |
style? |
FontStyle |
Style of the font (italics or not). |
target? |
string |
Link target identifier to be applied to emitted link elements (use with href). |
tracking? |
number |
Tracking (multiplier) for the text. |
variant? |
FontVariant |
Variant of the font (Small-Caps or not). |
weight? |
number |
Weight of the font as a number (400 is regular). |
type FontStyle = "normal" | "italic";Font style.
type FontVariant = "normal" | "small-caps";Font variant.
type HAlignment = "left" | "center" | "right";Horizontal alignment.
type LayoutOptions = {
align: Alignment;
createElement: CreateElementFunc;
font: string;
height: number | NumberFunc;
overflow: Overflow;
overflowLine: Overflow;
overflowWrap: OverflowWrap;
parser: (text: string) => Token[];
valign: VAlignment;
width: number | NumberLineFunc;
x: number | NumberLineFunc;
};Options for a textbox and how text flows inside it.
| Property | Type | Default value | Description |
|---|---|---|---|
align |
Alignment |
"top" |
Horizontal alignment of the text. |
createElement |
CreateElementFunc |
Infinity |
Factory function to use when constructing SVG elements. |
font |
string |
Infinity |
Default font to use (as CSS shorthand). |
height |
number | NumberFunc |
Infinity |
Height of the textbox in pixels. |
overflow |
Overflow |
"ellipsis" |
The symbol to use when indicating text overflow ("…"). |
overflowLine |
Overflow |
"" |
Symbol to use when indicating line overflow, or "" when line overflow is off. |
overflowWrap |
OverflowWrap |
"normal" |
Whether breaks should be inserted within an otherwise unbreakable word to prevent overflowing the textbox. |
parser |
(text: string) => Token[] |
Textbox.defaultparser |
Which parser to use when parsing text input. |
valign |
VAlignment |
"left" |
Vertical alignment of the text. |
width |
number | NumberLineFunc |
Infinity |
Width of the textbox in pixels. |
x |
number | NumberLineFunc |
0 |
Line horizontal offset factor or per-line callback to control horizontal offset. |
type Line = Token[] & {
width: number;
};A (linebroken) line of Tokens
width: number;type Lines = Line[] & LinesProps;A collection of flowed text lines & their properties.
type LinesProps = {
font: FontProps;
hasLineOverflow: boolean;
hasOverflow: boolean;
height: number;
width: number;
};Additional properties for linebroken text.
| Property | Type | Description |
|---|---|---|
font |
FontProps |
Default font properties used. |
hasLineOverflow |
boolean |
Does any line have line-overflow? |
hasOverflow |
boolean |
Did the text overflow? |
height |
number |
Height of the layed-out text. |
width |
number |
Width of the layed-out text. |
type MeasureOptions = {
collapse?: boolean;
trim?: boolean;
};Options for text measuring.
| Property | Type | Default value | Description |
|---|---|---|---|
collapse? |
boolean |
true |
Should whitespace in the text get collapsed (like in HTML)? |
trim? |
boolean |
true |
Should the text be trimmed before measuring? |
type MinimalCanvas = {
getContext: (contextId: "2d") => MinimalCanvasContext | null;
};Minimal Canvas interface needed.
| Property | Type |
|---|---|
getContext |
(contextId: "2d") => MinimalCanvasContext | null |
type MinimalCanvasContext = {
font: string;
measureText: (text: string) => {
width: number;
};
};Minimal CanvasRenderingContext2D interface needed.
| Property | Type |
|---|---|
font |
string |
measureText |
(text: string) => { width: number; } |
type NumberFunc = () => number;A function that emits a width or height in pixels.
number
type NumberLineFunc = (line?: number) => number;A function accepting a line number that emits a width or height in pixels.
| Parameter | Type |
|---|---|
line? |
number |
number
type Overflow = "ellipsis" | string;Overflow indication symbol.
type OverflowWrap = "break-word" | "normal";Overflow wrap behavior.
type RotatorOptions = {
anchor: string;
createElement: CreateElementFunc;
height: number;
rotate: number;
width: number;
};Options for the Rotator class
type VAlignment = "top" | "middle" | "bottom";Vertical alignment.