GeezerVison is showing maxgraph.github.io — TextShape | @maxgraph/core
Extends Shape to implement a text shape.
This shape is NOT registered in ShapeRegistry when using Graph or calling registerDefaultShapes.
It is the label shape: it is created by CellRenderer.createLabel and stored in CellState.text, next to
the shape of the cell itself. It is not usable as a value of the shape style, because shapes coming from
ShapeRegistry are constructed without any argument whereas this one requires a value and bounds, and because
getTextRotation would then recurse endlessly on its own state.
Rotation of vertical labels
The rotation applied to a vertical label (a cell whose horizontal style is false) does not come from this shape.
getTextRotation delegates to the shape of the cell, CellState.shape, so the
verticalTextRotation declared here is never read while the label belongs to a cell state. To change vertical
text from "bottom to top" (the -90 degrees default) to "top to bottom", set Shape.verticalTextRotation on the
shape of the cell. No global configuration object covers it, so the change goes through a CellRenderer
subclass:
import { CellRenderer, Graph } from '@maxgraph/core'; import type { CellState, Shape } from '@maxgraph/core';
class TopToBottomCellRenderer extends CellRenderer { override createShape(state: CellState): Shape { const shape = super.createShape(state); shape.verticalTextRotation = 90; return shape; } }
class TopToBottomGraph extends Graph { override createCellRenderer(): CellRenderer { return new TopToBottomCellRenderer(); } }
BaseGraph takes the renderer as a collaborator instead of a factory method:
new BaseGraph({ container, cellRenderer: new TopToBottomCellRenderer() }).
Beware that 0 does not mean "no extra rotation": Shape.getTextRotation computes
this.verticalTextRotation || -90, so a value of 0 falls back to -90 degrees.
Constructors
constructor
-
new TextShape( value: string | HTMLElement | SVGGElement, bounds: Rectangle, align?: AlignValue, valign?: VAlignValue, color?: string, family?: string, size?: number, fontStyle?: number, spacing?: number, spacingTop?: number, spacingRight?: number, spacingBottom?: number, spacingLeft?: number, horizontal?: boolean, background?: string, border?: string, wrap?: boolean, clipped?: boolean, overflow?: OverflowValue, labelPadding?: number, textDirection?: TextDirectionValue,): TextShape
Parameters
- value: string | HTMLElement | SVGGElement
- bounds: Rectangle
- align: AlignValue = 'center'
- valign: VAlignValue = 'middle'
- color: string = 'black'
- family: string = StyleDefaultsConfig.fontFamily
- size: number = StyleDefaultsConfig.fontSize
- fontStyle: number = DEFAULT_FONTSTYLE
- spacing: number = 2
- spacingTop: number = 0
- spacingRight: number = 0
- spacingBottom: number = 0
- spacingLeft: number = 0
- horizontal: boolean = true
- background: string = NONE
- border: string = NONE
- wrap: boolean = false
- clipped: boolean = false
- overflow: OverflowValue = 'visible'
- labelPadding: number = 0
- textDirection: TextDirectionValue = DEFAULT_TEXT_DIRECTION
Returns TextShape
Properties
antiAlias
antiAlias: boolean = true
Rendering hint for configuring the canvas.
background
background: string
baseSpacingBottom
baseSpacingBottom: number = 0
Specifies the spacing to be added to the bottom spacing. Default is 0. Use the
value 1 here to get the same label positions as in mxGraph 1.x.
baseSpacingLeft
baseSpacingLeft: number = 0
Specifies the spacing to be added to the left spacing. Default is 0.
baseSpacingRight
baseSpacingRight: number = 0
Specifies the spacing to be added to the right spacing. Default is 0.
baseSpacingTop
baseSpacingTop: number = 0
Specifies the spacing to be added to the top spacing. Default is 0. Use the
value 5 here to get the same label positions as in mxGraph 1.x.
boundingBox
Contains the bounding box of the shape, that is, the smallest rectangle
that includes all pixels of the shape.
bounds
Holds the Rectangle that specifies the bounds of this shape.
cacheEnabled
cacheEnabled: boolean = true
Specifies if caching for HTML labels should be enabled. Default is true.
cursor
cursor: string = ''
dialect
Holds the dialect in which the shape is to be painted.
endArrow
endArrow: string = NONE
endSize
endSize: number = 1
fillOpacity
fillOpacity: number = 100
flipH
flipH: boolean = false
flipV
flipV: boolean = false
fontStyle
fontStyle: number
glass
glass: boolean = false
gradient
gradient: string = NONE
horizontal
horizontal: boolean
ignoreClippedStringSize
ignoreClippedStringSize: boolean = true
Specifies if the string size should be measured in if
the label is clipped and the label position is center and middle. If this is
true, then the bounding box will be set to . Default is true.
has precedence over this switch.
ignoreStringSize
ignoreStringSize: boolean = false
Specifies if the actual string size should be measured. If disabled the
boundingBox will not ignore the actual size of the string, otherwise
will be used instead. Default is false.
imageSrc
imageSrc: string | null = null
indicator
indicator: Shape | null = null
indicatorColor
indicatorColor: string = NONE
indicatorGradientColor
indicatorGradientColor: string = NONE
indicatorImageSrc
indicatorImageSrc: string | null = null
indicatorShape
indicatorShape: typeof Shape | null = null
indicatorStrokeColor
indicatorStrokeColor: string = NONE
isDashed
isDashed: boolean = false
isRounded
isRounded: boolean = false
isShadow
isShadow: boolean = false
labelPadding
labelPadding: number
lastValue
lastValue: string | HTMLElement | SVGGElement | null = null
Contains the last rendered text value. Used for caching.
margin
margin: Point | null = null
minSvgStrokeWidth
minSvgStrokeWidth: number = 1
Minimum stroke width for SVG output.
node
node: SVGGElement
Holds the outermost DOM node that represents this shape.
opacity
opacity: number = 100
originalPointerEvents
originalPointerEvents: boolean | null = null
outline
outline: boolean = false
Specifies if the shape should be drawn as an outline. This disables all
fill colors and can be used to disable other drawing states that should
not be painted for outlines. Default is false. This should be set before
calling .
pointerEvents
pointerEvents: boolean = true
Specifies if pointer events should be handled. Default is true.
points
points: ( Point | null) [] = []
Holds the array of that specify the points of this shape.
preserveImageAspect
preserveImageAspect: boolean = false
Switch to preserve image aspect.
replaceLinefeeds
replaceLinefeeds: boolean = true
Specifies if linefeeds in HTML labels should be replaced with BR tags.
Default is true.
rotation
rotation: number = 0
scale
scale: number = 1
Holds the scale in which the shape is being painted.
shapePointerEvents
shapePointerEvents: boolean = false
Specifies if pointer events outside of shape should be handled. Default
is false.
spacingBottom
spacingBottom: number
spacingLeft
spacingLeft: number
spacingRight
spacingRight: number
spacingTop
spacingTop: number
startArrow
startArrow: string = NONE
startSize
startSize: number = 1
state
Optional reference to the corresponding .
stencilPointerEvents
stencilPointerEvents: boolean = false
Specifies if pointer events outside of stencils should be handled. Default
is false. Set this to true for backwards compatibility with the 1.x branch.
stroke
stroke: string = NONE
strokeOpacity
strokeOpacity: number = 100
strokeWidth
strokeWidth: number = 1
style
Optional reference to the style of the corresponding .
svgPointerEvents
svgPointerEvents: string = 'all'
Specifies if pointer events should be handled. Default is true.
svgStrokeTolerance
svgStrokeTolerance: number = 8
unrotatedBoundingBox
unrotatedBoundingBox: Rectangle | null = null
useSvgBoundingBox
useSvgBoundingBox: boolean = true
Allows to use the SVG bounding box in SVG. Default is false for performance
reasons.
value
value: string | HTMLElement | SVGGElement
verticalTextRotation
verticalTextRotation: number = -90
Rotation for vertical text. Default is -90 (bottom to top).
This value is not used to rotate the label of a cell: getTextRotation delegates to
CellState.shape, so the value that applies is the Shape.verticalTextRotation of the shape of the
cell. See the documentation of this class for the way to change it.
visible
visible: boolean = true
Specifies if the shape is visible. Default is true.
Methods
addPoints
-
addPoints( c: AbstractCanvas2D, pts: Point[], rounded?: boolean, arcSize: number, close?: boolean, exclude?: number[], initialMove?: boolean,): void
Paints the given points with rounded corners.
Parameters
- c: AbstractCanvas2D
- pts: Point[]
- rounded: boolean = false
- arcSize: number
- close: boolean = false
- exclude: number[] = []
- initialMove: boolean = true
Returns void
afterPaint
-
Invokes after paint was called.
Returns void
apply
-
Extends mxShape to update the text styles.
Returns void
augmentBoundingBox
-
Augments the bounding box with the strokewidth and shadow offsets.
Returns void
beforePaint
-
Invoked before paint is called.
Returns void
checkBounds
-
Returns true if the bounds are not null and all of its variables are numeric.
Returns boolean
clear
-
Removes all child nodes and resets all CSS.
Returns void
configureCanvas
-
configureCanvas( c: AbstractCanvas2D, x: number, y: number, w: number, h: number,): void
Sets the state of the canvas for drawing the shape.
Returns void
create
-
Creates and returns the DOM node for the shape.
This implementation assumes that maxGraph produces SVG elements.
Returns SVGGElement
createBoundingBox
-
Returns a new rectangle that represents the bounding box of the bare shape
with no shadows or strokewidths.
createCanvas
-
Creates a new canvas for drawing this shape. May return null.
createSvgCanvas
-
Creates and returns an SvgCanvas2D for rendering this shape.
createTransparentSvgRectangle
-
createTransparentSvgRectangle( x: number, y: number, w: number, h: number,): SVGRectElement
Adds a transparent rectangle that catches all events.
Parameters
- x: number
- y: number
- w: number
- h: number
Returns SVGRectElement
destroy
-
Destroys the shape by removing it from the DOM and releasing the DOM
node associated with the shape using Event#release.
Returns void
destroyCanvas
-
Destroys the given canvas which was used for drawing. This implementation
increments the reference counts on all shared gradients used in the canvas.
Returns void
getArcSize
-
getArcSize(w: number, h: number): number
Returns the arc size for the given dimension.
Returns number
getAutoDirection
-
Used to determine the automatic text direction.
Returns 'ltr' or 'rtl' depending on the contents of value.
This is not invoked for HTML, wrapped content or if value is a DOM node.
ProtectedgetBaseArcSize
-
Base arc size for the shape, taken from the style.
Returns number
getContentNode
-
getContentNode(): SVGGElement
Returns the node that contains the rendered input.
Returns SVGGElement
getCursor
-
Returns the current cursor.
Returns string
getGradientBounds
-
Returns the bounding box for the gradient box for this shape.
getHtmlValue
-
Private helper function to create SVG elements
Returns string
getLabelBounds
-
Returns the Rectangle for the label bounds of this shape, based on the
given scaled and translated bounds of the shape. This method should not
change the rectangle in-place. This implementation returns the given rect.
getLabelMargins
-
Returns the scaled top, left, bottom and right margin to be used for
computing the label bounds as an Rectangle, where the bottom and right
margin are defined in the width and height of the rectangle, respectively.
getRotation
-
Returns the rotation from the style.
Returns number
getShapeRotation
-
getShapeRotation(): number
Returns 0 to avoid using rotation in the canvas via updateTransform.
Returns number
getSpacing
-
Returns the spacing as an Point.
getSvgScreenOffset
-
getSvgScreenOffset(): number
Disables offset in IE9 for crisper image output.
Returns number
getTextCss
-
Private helper function to create SVG elements
Returns string
getTextRotation
-
getTextRotation(): number
Returns the rotation for the text label of the corresponding shape.
Returns number
init
-
init(container: HTMLElement | SVGElement): void
Initializes the shape by adding it into the given container if the node of the shape doesn't already have a parent.
Parameters
- container: HTMLElement | SVGElement
DOM node that will contain the shape.
Returns void
initStyles
-
Sets the styles to their default values.
Returns void
isHtmlAllowed
-
Returns true if HTML is allowed for this shape. This implementation always returns false.
Returns boolean
isPaintBoundsInverted
-
isPaintBoundsInverted(): boolean
Inverts the bounds if Shape#isBoundsInverted returns true or if the
horizontal style is false.
Returns boolean
isRoundable
-
isRoundable( c: AbstractCanvas2D, x: number, y: number, w: number, h: number,): boolean
Hook for subclassers. This implementation returns false.
Returns boolean
paintBackground
-
paintBackground( c: AbstractCanvas2D, x: number, y: number, w: number, h: number,): void
Hook for subclassers. This implementation is empty.
Returns void
paintEdgeShape
-
Hook for subclassers. This implementation is empty.
Returns void
paintForeground
-
paintForeground( c: AbstractCanvas2D, x: number, y: number, w: number, h: number,): void
Hook for subclassers. This implementation is empty.
Returns void
paintGlassEffect
-
paintGlassEffect( c: AbstractCanvas2D, x: number, y: number, w: number, h: number, arc: number,): void
Paints the glass gradient effect.
Returns void
paintVertexShape
-
paintVertexShape( c: AbstractCanvas2D, x: number, y: number, w: number, h: number,): void
redraw
-
Renders the text using the given DOM nodes.
Returns void
redrawHtmlShape
-
Updates the HTML node(s) to reflect the latest bounds and scale.
Returns void
redrawShape
-
Updates the SVG or VML shape.
Returns void
setCursor
-
setCursor(cursor: string): void
Sets the cursor on the given shape.
Returns void
setTransparentBackgroundImage
-
setTransparentBackgroundImage(node: SVGElement): void
Sets a transparent background CSS style to catch all events.
Paints the line shape.
Returns void
updateBoundingBox
-
updateBoundingBox(): void
Updates the for this shape using the given node and position.
Returns void
updateBoundsFromPoints
-
updateBoundsFromPoints(): void
Updates the bounds based on the points.
Returns void
updateFont
-
updateFont(node: HTMLElement | SVGGElement): void
Updates the HTML node(s) to reflect the latest bounds and scale.
Parameters
- node: HTMLElement | SVGGElement
Returns void
updateInnerHtml
-
updateInnerHtml(elt: HTMLElement): void
Sets the inner HTML of the given element to the .
Returns void
updateMargin
-
Returns the spacing as an Point.
Returns void
updateSize
-
updateSize(node: HTMLElement, enableWrap?: boolean): void
Updates the HTML node(s) to reflect the latest bounds and scale.
Parameters
- node: HTMLElement
- enableWrap: boolean = false
Returns void
updateTransform
-
updateTransform( c: AbstractCanvas2D, x: number, y: number, w: number, h: number,): void
Sets the scale and rotation on the given canvas.
Returns void
updateValue
-
Updates the HTML node(s) to reflect the latest bounds and scale.
Returns void
|