Docs menu: Text Documentation home
On this page Where text renders Baselines Text and the roundtrip

Text

The text element, its style keys, and where it renders.

{ type = "text", x = 300, y = 390, content = "MSX Basic Shapes",
  style = { fill = #ffffff, font_size = 14, text_anchor = "middle" } }
text_demo rendered
examples/text_demo.msx · 34 lines · open in playground · all samples
Source
@CONFIG( version -> "1.0.0" )
@DATA(
  scene = { width = 640, height = 400, background = #14161f }
  defs::
    { type = "linear_gradient", id = "title_fill",
      x1 = 0.0, y1 = 0.0, x2 = 1.0, y2 = 0.0,
      stops = [
        { offset = 0.0, color = #f7971e, opacity = 1.0 },
        { offset = 1.0, color = #e94560, opacity = 1.0 }
      ] }
  elements::
    { type = "text", x = 320, y = 70, content = "MSX Text",
      style = { fill = "url(#title_fill)", font_size = 56, font_weight = "bold", text_anchor = "middle" } }
    { type = "text", x = 30, y = 125, content = "Regular: quick brown fox",
      style = { fill = #ffffff, font_size = 22 } }
    { type = "text", x = 30, y = 160, content = "Bold: quick brown fox",
      style = { fill = #ffffff, font_size = 22, font_weight = "bold" } }
    { type = "text", x = 360, y = 125, content = "Accents: é ñ ü ß Å",
      style = { fill = #4facfe, font_size = 22 } }
    { type = "text", x = 360, y = 160, content = "Weight 700, size 22",
      style = { fill = #4facfe, font_size = 22, font_weight = "700" } }
    { type = "line", x1 = 320, y1 = 195, x2 = 320, y2 = 295,
      style = { stroke = #555566, stroke_width = 1 } }
    { type = "text", x = 320, y = 220, content = "start",
      style = { fill = #ffd200, font_size = 22, text_anchor = "start" } }
    { type = "text", x = 320, y = 255, content = "middle",
      style = { fill = #ffd200, font_size = 22, text_anchor = "middle" } }
    { type = "text", x = 320, y = 290, content = "end",
      style = { fill = #ffd200, font_size = 22, text_anchor = "end" } }
    { type = "text", x = 30, y = 365, content = "Outline",
      style = { fill = "none", stroke = #e94560, stroke_width = 2, font_size = 72 } }
    { type = "text", x = 340, y = 365, content = "Gradient",
      style = { fill = "url(#title_fill)", font_size = 64 } }
)
Field Meaning Required
x, y Anchor position. y is the baseline yes
content The string to draw yes

Text takes the usual id and transform fields and a style. The keys that shape the text are font_size, font_family, font_weight, text_anchor and dominant_baseline. text_anchor chooses which part of the string sits at x: start, middle or end. font_weight takes normal, bold or a number such as 600. The fill color comes from fill.

Where text renders

All three renderers draw text.

The CPU and GPU renderers draw text as glyph outlines from one embedded face, DejaVu Sans, in regular and bold. The outlines go through the same path pipeline as any other shape, so fills, strokes and gradients all apply. The GPU renderer does not antialias, so its text edges are harder than the CPU renderer's.

  • font_family is ignored. Every text element uses DejaVu Sans.
  • Bold applies when font_weight is bold or a number of 600 or more.
  • Spacing comes from advance widths and the legacy kerning table. There is no OpenType shaping, so ligatures and complex scripts are not supported.
  • The embedded subset covers Latin, Latin Extended-A, Greek, Cyrillic, common punctuation, arrows and a few math symbols. A character outside that set draws the font's missing glyph box.
  • The browser draws SVG text with its own fonts, so the same string differs slightly in width between SVG and PNG output.

A gradient on text maps onto the text box, which is the advance width of the string by the height from ascender to descender. This matches the box SVG uses.

Baselines

y is the alphabetic baseline unless dominant_baseline says otherwise. The CPU and GPU renderers read these values: middle, central, hanging, text-before-edge, text-top, text-after-edge, text-bottom and ideographic. Any other value keeps the alphabetic baseline.

From binary version 3 the compiled file keeps dominant_baseline, and a scene that sets it is written as version 3 (see Binary format).

Text and the roundtrip

Text survives the binary format unchanged. A compiled file renders the same SVG text as its source, which is what msx roundtrip checks.