On this page:
parse-font-family
parse-font-weight
parse-font-style
parse-text-anchor
parse-number-list
collapse-whitespace

9 Text🔗ℹ

svg/svg renders <text>/<tspan> by converting text to path geometry via dc-path%’s own text-outline (not dc<%>’s draw-text), so text gets the same gradient/pattern/dasharray/opacity support as any other shape, for free. <textPath> (laying text along a referenced shape’s own geometry, via an arc-length parameterization of the path) and xml:space="preserve" (disabling the default whitespace-collapsing behavior, with correct XML-style inheritance down the tree) are both supported as well. None of this rendering logic is exported directly, though — there is no single "parse/render a whole <text> element" function in this API, since it involves rendering-time context (pen position or arc-length position along a path, inherited font state) beyond what a standalone parser can usefully capture. The functions in this group are the small, self-contained parsers that turn SVG/CSS text-related attribute values into the symbols and structures font% and the rest of that internal pipeline expect.

parse-number-list and collapse-whitespace, while used elsewhere in the file for text-specific purposes (<text>’s x/y/dx/dy attributes, which accept a list of numbers — one per character — and collapsing runs of whitespace in text content per SVG’s whitespace-handling rules), are general-purpose enough to be useful outside text too; parse-number-list in particular is reused for filter primitive attributes like stdDeviation and radius elsewhere in the file.

procedure

(parse-font-family s)  
(or/c string? #f) symbol?
  s : (or/c string? #f)
Parses a font-family value — a comma-separated list such as "Georgia, 'Times New Roman', serif" into two values: a specific font face (the first non-generic name in the list, quotes stripped, or #f if the list is all generic keywords), and a family (one of 'roman, 'swiss, 'modern, 'script, 'decorative, or 'default) — the two pieces font%’s constructor wants separately, since it distinguishes an actual installed font name from an abstract fallback bucket. The family is taken from the first CSS generic keyword found anywhere in the list ("serif"'roman, "sans-serif"'swiss, "monospace"'modern, "cursive"'script, "fantasy"'decorative), defaulting to 'default if none appears.

Examples:
> (parse-font-family "Georgia, serif")

"Georgia"

'roman

> (parse-font-family "sans-serif")

#f

'swiss

> (parse-font-family #f)

#f

'default

Text rendered with the results of parse-font-family/ parse-font-weight/parse-font-style normal, bold, and italic:

the words Normal, Bold, and Italic, each rendered in its own named style

procedure

(parse-font-weight s)  (or/c 'normal 'bold exact-integer?)

  s : (or/c string? #f)
Parses a font-weight value: "bold"/"normal" give the matching symbol; a numeric CSS weight (e.g. "600") is passed through as an integer, since racket/draw’s font% accepts an integer weight directly rather than only the two named levels. Anything else (including an absent value) gives 'normal.

procedure

(parse-font-style s)  (or/c 'normal 'italic 'slant)

  s : (or/c string? #f)
Parses a font-style value: "italic" gives 'italic, "oblique" gives 'slant (the symbol font% uses for the same concept), anything else gives 'normal.

procedure

(parse-text-anchor s)  (or/c 'start 'middle 'end)

  s : (or/c string? #f)
Parses a text-anchor value ("start", "middle", or "end") into the matching symbol; anything else (including an absent value) gives 'start, SVG’s default.

The same text, anchored 'start, 'middle, and 'end relative to the same reference point (the black dot; the dashed line marks its x position):

three lines of text, each positioned differently relative to an identical reference dot -- starting at it, centered on it, and ending at it

procedure

(parse-number-list s)  (listof real?)

  s : (or/c string? #f)
Parses a comma-or-whitespace-separated list of numbers — the format used by <text>’s x/y/dx/dy attributes, and by filter-primitive numeric attributes like stdDeviation and radius (which accept either one number or two, for separate x/y values). Non-numeric tokens are silently dropped rather than raising; #f or an empty/whitespace-only string give '().

Examples:
> (parse-number-list "1, 2  3,4")

'(1 2 3 4)

> (parse-number-list "5")

'(5)

> (parse-number-list #f)

'()

procedure

(collapse-whitespace s)  string?

  s : string?
Collapses every run of one or more spaces, tabs, carriage returns, or newlines in s into a single space — SVG’s default xml:space="default" whitespace handling for text content — without trimming leading or trailing whitespace (trimming is handled separately, only at the true start of a whole <text> subtree, since a leading space on an inner <tspan> still needs to be preserved as the word-separator it is when concatenated after a previous run of text).

Example:
> (collapse-whitespace "a   b\n\tc")

"a b c"