std.svg.text
Text ausgeben: einzeilig mit SvgTextAt, mehrzeilig über SvgTextBegin + SvgTspan, dazu Text entlang eines Pfads.
Wie überall in std.svg gibt es keinen Style-String: Schrift, Gewicht, Anker und Ausrichtung werden vorab am Dokument gesetzt und gelten für das nächste Element. Ein einzeiliger Text wird mit SvgApply abgeschlossen; ein Textblock mit SvgTextEnd.
→ std.svg · std.svg.style · std.svg.defs
Verwendung
import std.io;
import std.svg.builder;
import std.svg.elements;
import std.svg.style;
import std.svg.text;
fn main(): int64 {
var doc: int64 := SvgNew(800.0, 400.0);
SvgSetPrettyPrint(doc, 1);
SvgSetFont(doc, "DejaVu Sans", 24.0);
SvgSetFillHex(doc, "black");
SvgTextAt(doc, 100.0, 50.0, "Hallo Welt");
SvgApply(doc);
SvgSetTextAnchor(doc, SVG_ANCHOR_MIDDLE);
SvgSetFontWeight(doc, SVG_WEIGHT_BOLD);
SvgTextAt(doc, 400.0, 150.0, "Zentriert und fett");
SvgApply(doc);
// Mehrzeilig: dy je Zeile in Benutzereinheiten
SvgSetFont(doc, "DejaVu Sans", 18.0);
SvgTextBegin(doc, 50.0, 250.0);
SvgTspan(doc, "Erste Zeile", 0.0, 0.0);
SvgTspan(doc, "Zweite Zeile", 0.0, 22.0);
SvgTspanStyle(doc, "Dritte fett", SVG_WEIGHT_BOLD, 0.0, 22.0);
SvgTextEnd(doc);
Print(SvgToString(doc)); Print("\n");
SvgFree(doc);
return 0;
}
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="800" height="400">
<text x="100" y="50" font-family="DejaVu Sans" font-size="24" fill="black">Hallo Welt</text>
<text x="400" y="150" font-weight="bold" text-anchor="middle">Zentriert und fett</text>
<text x="50" y="250" font-family="DejaVu Sans" font-size="18">
<tspan>Erste Zeile</tspan>
<tspan dy="22">Zweite Zeile</tspan>
<tspan font-weight="bold" dy="22">Dritte fett</tspan>
</text>
</svg>
Am zweiten Text sieht man die Regel: Die Schriftangabe des ersten Aufrufs ist mit dessen SvgApply verbraucht und muss für den Block erneut gesetzt werden.
Konstanten
| Name | Wert | Verwendung |
|---|---|---|
SVG_ANCHOR_START / SVG_ANCHOR_MIDDLE / SVG_ANCHOR_END | 0–2 | SvgSetTextAnchor |
SVG_WEIGHT_NORMAL / SVG_WEIGHT_BOLD | 0 / 1 | SvgSetFontWeight, SvgTspanStyle |
SVG_STYLE_NORMAL / SVG_STYLE_ITALIC | 0 / 1 | SvgSetFontStyle |
SVG_DECO_NONE / SVG_DECO_UNDERLINE / SVG_DECO_STRIKE / SVG_DECO_OVERLINE | 0–3 | SvgSetTextDecoration |
SVG_BASELINE_AUTO / SVG_BASELINE_MIDDLE / SVG_BASELINE_HANGING | 0–2 | SvgSetDominantBaseline |
Funktionen
| Signatur | Sichtbarkeit | Beschreibung |
|---|---|---|
SvgSetFont(doc: int64, family: pchar, size: f64): void | pub | Schriftfamilie und -größe |
SvgSetFontWeight(doc: int64, weight: int64): void | pub | Gewicht, SVG_WEIGHT_* |
SvgSetFontStyle(doc: int64, style: int64): void | pub | Stil, SVG_STYLE_* |
SvgSetTextAnchor(doc: int64, anchor: int64): void | pub | Ausrichtung, SVG_ANCHOR_* |
SvgSetDominantBaseline(doc: int64, baseline: int64): void | pub | Grundlinie, SVG_BASELINE_* |
SvgSetLetterSpacing(doc: int64, spacing: f64): void | pub | Zeichenabstand |
SvgSetWordSpacing(doc: int64, spacing: f64): void | pub | Wortabstand |
SvgSetTextDecoration(doc: int64, deco: int64): void | pub | Unterstreichung usw., SVG_DECO_* |
SvgTextAt(doc: int64, x: f64, y: f64, text: pchar): void | pub | Einzeiliger Text, danach SvgApply |
SvgTextBegin(doc: int64, x: f64, y: f64): int64 | pub | Textblock beginnen |
SvgTspan(doc: int64, text: pchar, dx: f64, dy: f64): void | pub | Zeile im Block |
SvgTspanStyle(doc: int64, text: pchar, weight: int64, dx: f64, dy: f64): void | pub | Zeile mit eigenem Gewicht |
SvgTextEnd(doc: int64): void | pub | Textblock schließen |
SvgTextOnPath(doc: int64, pathId: pchar, text: pchar, offset: f64): void | pub | Text entlang eines Pfads |
Nicht vorhanden: SvgTextRotated, SvgTextSpan und die SvgTextBlock-Familie — für gedrehten Text die Transformationen aus std.svg.style vor dem Textaufruf setzen.
Letzte Aktualisierung: 2026-08-19 — Seite gegen die echten Signaturen neu geschrieben (kein Style-Parameter, Konstanten statt Zeichenketten, SvgTextBegin/SvgTspan/SvgTextEnd statt Textblock-Typ). Beispiel mit lyxc 1.1.3I übersetzt und ausgeführt, Ausgabe übernommen.
