Class Glyph

Element implements a generic base class for VexFlow, with implementations of general functions and properties that can be inherited by all VexFlow elements.

The Element is an abstract class that needs to be subclassed to work. It handles style and text-font properties for the Element and any child elements, along with working with the Registry to create unique ids, but does not have any tools for formatting x or y positions or connections to a Stave.

Hierarchy

Constructors

Properties

bbox: BoundingBox = ...
botGlyphs: Glyph[] = []
code: string
metrics: GlyphMetrics
scale: number = 1
topGlyphs: Glyph[] = []
CURRENT_CACHE_KEY: string = ''
MUSIC_FONT_STACK: Font[] = []
TEXT_FONT: Required<FontInfo> = ...

Default font for text. This is not related to music engraving. Instead, see Flow.setMusicFont(...fontNames) to customize the font for musical symbols placed on the score.

Accessors

  • get font(): string
  • Returns the CSS compatible font string for the text font.

    Returns string

  • set font(f): void
  • Provide a CSS compatible font string (e.g., 'bold 16px Arial') that will be applied to text (not glyphs).

    Parameters

    • f: string

    Returns void

  • get fontInfo(): Required<FontInfo>
  • Return a copy of the current FontInfo object.

    Returns Required<FontInfo>

  • set fontInfo(fontInfo): void
  • Parameters

    Returns void

  • get fontSize(): string
  • Returns string

    a CSS font-size string (e.g., '18pt', '12px', '1em').

  • set fontSize(size): void
  • The size is 1) a string of the form '10pt' or '16px', compatible with the CSS font-size property. or 2) a number, which is interpreted as a point size (i.e. 12 == '12pt').

    Parameters

    • size: string | number

    Returns void

  • get fontSizeInPixels(): number
  • Returns number

    the font size in px.

  • get fontSizeInPoints(): number
  • Returns number

    the font size in pt.

  • get fontStyle(): string
  • Returns string

    a CSS font-style string (e.g., 'italic').

  • set fontStyle(style): void
  • Parameters

    • style: string

    Returns void

  • get fontWeight(): string
  • Returns string

    a CSS font-weight string (e.g., 'bold'). As in CSS, font-weight is always returned as a string, even if it was set as a number.

  • set fontWeight(weight): void
  • Parameters

    • weight: string | number

    Returns void

  • get CATEGORY(): string
  • Returns string

Methods

  • Adds a child Element to the Element, which lets it inherit the same style as the parent when setGroupStyle() is called.

    Examples of children are noteheads and stems. Modifiers such as Accidentals are generally not set as children.

    Note that StaveNote calls setGroupStyle() when setStyle() is called.

    Parameters

    Returns Glyph

  • Add a class label (An element can have multiple class labels).

    Parameters

    • className: string

    Returns Glyph

  • Draw an element.

    Parameters

    • Rest ...args: any[]

    Returns void

  • Draw the element and all its sub-elements (ie.: Modifiers in a Stave) with the element's style (see getStyle() and setStyle())

    Returns void

  • Return an attribute, such as 'id', 'type' or 'class'.

    Parameters

    • name: string

    Returns any

  • Returns string

  • Get the css string describing this Element's text font. e.g., 'bold 10pt Arial'.

    Returns string

  • Returns string

    a CSS font-size string (e.g., '18pt', '12px', '1em'). See Element.fontSizeInPixels or Element.fontSizeInPoints if you need to get a number for calculation purposes.

  • Return associated SVGElement.

    Parameters

    • suffix: string = ''

    Returns undefined | SVGElement

  • Returns number

  • Returns number

  • Check if it has a class label (An element can have multiple class labels).

    Parameters

    • className: string

    Returns boolean

  • Return the rendered status.

    Returns boolean

  • Remove a class label (An element can have multiple class labels).

    Parameters

    • className: string

    Returns Glyph

  • Parameters

    Returns void

  • Parameters

    • x: number

    Returns void

  • Returns void

  • Reset the text font to the style indicated by the static TEXT_FONT property. Subclasses can call this to initialize textFont for the first time.

    Returns void

  • Set an attribute such as 'id', 'class', or 'type'.

    Parameters

    • name: string
    • value: undefined | string

    Returns Glyph

  • Set the element's text font family, size, weight, style (e.g., Arial, 10pt, bold, italic).

    This attribute does not determine the font used for musical Glyphs like treble clefs.

    Parameters

    • Optional font: string | FontInfo

      is 1) a FontInfo object or 2) a string formatted as CSS font shorthand (e.g., 'bold 10pt Arial') or 3) a string representing the font family (at least one of size, weight, or style must also be provided).

    • Optional size: string | number

      a string specifying the font size and unit (e.g., '16pt'), or a number (the unit is assumed to be 'pt').

    • Optional weight: string | number

      is a string (e.g., 'bold', 'normal') or a number (100, 200, ... 900).

    • Optional style: string

      is a string (e.g., 'italic', 'normal'). If no arguments are provided, then the font is set to the default font. Each Element subclass may specify its own default by overriding the static TEXT_FONT property.

    Returns Glyph

  • Change the font size, while keeping everything else the same.

    Parameters

    • Optional size: string | number

    Returns Glyph

  • Parameters

    • options: any

    Returns void

  • Parameters

    • x: number
    • y: number

    Returns void

  • Parameters

    • x: number

    Returns void

  • Parameters

    • y: number

    Returns void

  • Parameters

    • point: number

    Returns Glyph

  • Set the rendered status.

    Parameters

    • rendered: boolean = true

    Returns Glyph

  • Set the element style used to render.

    Example:

    element.setStyle({ fillStyle: 'red', strokeStyle: 'red' });
    element.draw();

    Note: If the element draws additional sub-elements (ie.: Modifiers in a Stave), the style can be applied to all of them by means of the context:

    element.setStyle({ fillStyle: 'red', strokeStyle: 'red' });
    element.getContext().setFillStyle('red');
    element.getContext().setStrokeStyle('red');
    element.draw();

    or using drawWithStyle:

    element.setStyle({ fillStyle: 'red', strokeStyle: 'red' });
    element.drawWithStyle();

    Parameters

    Returns Glyph

  • Parameters

    • x_shift: number

    Returns Glyph

  • Parameters

    • y_shift: number

    Returns Glyph

  • Parameters

    • outline: number[]
    • scale: number
    • x_pos: number
    • y_pos: number

    Returns BoundingBox

  • Parameters

    • code: string
    • point: number
    • Optional category: string

    Returns number

  • Parameters

    • fontStack: Font[]
    • code: string
    • Optional category: string

    Returns GlyphMetrics

  • Pass a key of the form glyphs.{category}.{code}.{key} to Font.lookupMetric(). If the initial lookup fails, try again with the path glyphs.{category}.{key}. If the second lookup fails, return the defaultValue.

    Parameters

    • font: Font
    • category: string

      any metric path under 'glyphs', so 'stem.up' could resolve to glyphs.stem.up.shiftX, glyphs.stem.up.shiftY, etc.

    • code: string
    • key: string
    • defaultValue: number

    Returns number

  • Parameters

    • fontStack: Font[]
    • code: string

    Returns {
        font: Font;
        glyph: FontGlyph;
    }

  • Renders glyphs from the default font stack.

    Parameters

    • ctx: RenderContext

      Canvas or SVG context

    • x_pos: number

      x coordinate

    • y_pos: number

      y coordinate

    • point: number

      the point size of the font

    • code: string

      the glyph code in font.getGlyphs()

    • Optional options: {
          category?: string;
          scale?: number;
      }
      • Optional category?: string
      • Optional scale?: number

    Returns GlyphMetrics

  • Parameters

    • ctx: RenderContext
    • outline: number[]
    • scale: number
    • x_pos: number
    • y_pos: number

    Returns void