render2d/text.rs#
Source: lib/render2d/text.rs
text -- fonts, text styles and the layout of text (G6c; docs/RENDER2D.md "Text"): what a scene draws of a text, and
what the CPU path of G6e (lib/gfx outlines of the same layout) and picking in G6d (each text's extent) take from it.
Fonts are TrueType or OpenType (also a collection's face) from their bytes: lib/text parses them (outlines, metrics),
lib/text_shape shapes (HarfRust: kerning, ligatures, marks, Arabic joining, Indic reordering; Unicode bidi and script
runs; fallback fonts per run that the first font cannot show). A text style is DXF's STYLE: a font, fallback fonts,
a fixed height or none, a width factor, an oblique angle; heights in drawing units, or in pixels for annotation that
keeps its size on the screen.
A layout is in em units of the text's frame (x along the baseline, y up; one em = the font's units per em): its lines
(baseline origin, glyphs in visual order with their pens), the bars small text is drawn as, its ink box; and the frame
itself: origin + m · e maps a point e of the frame to the drawing (or, for a style in pixels, to pixels from the origin).
Heights are cap heights, as DXF's TEXT and MTEXT measure them (the em is height / cap height).
struct FontId(pub u32)a font of a scene (Scene::add_font)struct TextStyleId(pub u32)a text style of a scene (Scene::add_text_style)
/// the most fallback fonts of a text style
FALLBACK_MAX: usize = 4
/// AutoCAD's single line spacing: baselines 5/3 of the text height apart (MText::line_spacing multiplies it)
LINE_SPACING: f64 = 5.0 / 3.0
struct TextStyleDXF's STYLE: how texts of it look. Made withnewand changed field by field (it may grow)font: FontIdfallback: [FontId; FALLBACK_MAX]-- fonts for whatfontlacks, tried in this order per run of one script (the firstfallbacks)fallbacks: u32height: f64-- a fixed cap height (0: each text says its own), inunitswidth: f64-- how much wider than the font (1: as it is)oblique: f64-- how far the glyphs lean, radians from upright (positive: to the right)unit: Unit-- Drawing: heights in drawing units, the text zooms with the drawing; Px: pixels, the text keeps its size on the screen
impl TextStyle
const fn new(font: FontId) -> TextStylea font upright, its own width, heights per text, in drawing unitsfn with_fallbacks(mut self, fonts: &[FontId]) -> TextStylethe same with fallback fonts (at most FALLBACK_MAX)enum HAlignDXF TEXT's horizontal alignment (group 72)LeftCenterRightAligned-- betweenatandat2: the height follows the width (the width factor stays)Middle-- the centre of the baseline and the middle of all the text's ink, descenders tooFit-- betweenatandat2: the width factor follows (the height stays)
enum VAlignDXF TEXT's vertical alignment (group 73): the baseline, the font's descender, half the cap height, the cap heightBaselineBottomMiddleTop
enum AttachMTEXT's attachment point (group 71, 1 to 9): whereatlies on the text's blockTopLeftTopCenterTopRightMiddleLeftMiddleCenterMiddleRightBottomLeftBottomCenterBottomRight
struct Text<'a>a single line of text (DXF's TEXT). Made withnewand changed field by field (it may grow)text: &'a str-- one line (control characters are left out)style: TextStyleIdat: [f64; 2]-- the alignment point: the baseline's start for Left / Baseline (DXF 10), else the point the alignment names (DXF 11); Aligned and Fit run the baseline fromat(DXF 10) toat2(DXF 11)at2: [f64; 2]height: Option<f64>-- the cap height (None: the style's), in the style's unitrotation: f64-- radians, counter-clockwise (Aligned and Fit: the direction from at to at2)width: Option<f64>-- the width factor and the oblique angle (None: the style's; DXF 41, 51)oblique: Option<f64>halign: HAlignvalign: VAlignbackward: bool-- DXF 71: mirrored in x (backward), mirrored in y (upside down)upside_down: bool
impl<'a> Text<'a>
const fn new(text: &'a str, style: TextStyleId, at: [f64; 2]) -> Text<'a>left on the baseline atat, unturned, the style's height, width and oblique anglestruct MText<'a>text of several lines (the basics of DXF's MTEXT: explicit breaks, a wrapping width, line spacing, the nine attachment points, rotation; its inline formatting codes are G6e's, which maps them onto runs). Made withnewand changed field by field (it may grow: runs with their own font, height and colour)text: &'a str-- lines broken at '\n'style: TextStyleIdat: [f64; 2]height: Option<f64>-- the cap height (None: the style's), in the style's unitwidth: f64-- wrap lines longer than this (0: no wrapping), in the style's unitrotation: f64attach: Attachline_spacing: f64-- times LINE_SPACING times the height between baselines (DXF 44)
impl<'a> MText<'a>
const fn new(text: &'a str, style: TextStyleId, at: [f64; 2]) -> MText<'a>top left atat, no wrapping, single spacingstruct Placeda glyph of a layoutfont: FontIdglyph: u32pen: [f64; 2]-- its origin, em units from its line's originadvance: f64-- its advance (em)cluster: u32-- the byte where its cluster starts in the textmark: bool-- a mark (no advance: an accent or vowel sign over its base): it has no cell of its ownblank: bool-- whitespace or no outline: no ink, no bar
struct Linea line of a layout: its baseline's origin (em, in the frame), its glyphs (a range of Layout::glyphs) and bars, its advance width (em)origin: [f64; 2]glyphs: core::ops::Range<u32>bars: core::ops::Range<u32>width: f64
struct Bara word drawn as one bar when its text is small (em, from its line's origin): from its ink's left to its right, from the baseline to the x-height,inkthe share of that box its glyphs coverx0: f64x1: f64y0: f64y1: f64ink: f64
struct Layouta text laid out: in em units of its frame, and the frameorigin: [f64; 2]-- where the frame's origin lies (drawing units) and its map from em units: origin + (m[0] x + m[1] y, m[2] x + m[3] y) (a style in pixels: pixels from the origin, y up, before the camera's rotation)m: [f64; 4]px: boolglyphs: Vec<Placed>lines: Vec<Line>bars: Vec<Bar>ink: Option<[f64; 4]>-- the box of the glyphs' ink (em, in the frame; empty text: None)cell: [f64; 4]-- the box of the lines: advances, descender to cap height (em, in the frame)em: f64-- the em in drawing units (or pixels): what the zoom decides byx_height: f64-- the x-height (em) the bars stand on
impl Layout
fn to_drawing(&self, e: [f64; 2]) -> [f64; 2]a point of the frame (em) in the drawing (a style in pixels: pixels from the origin)fn quad_of(&self, b: [f64; 4]) -> [[f64; 2]; 4]the corners of a box of the frame in the drawing (counter-clockwise from its lower left in the frame)fn quad(&self) -> [[f64; 2]; 4]the text's extent in the drawing: its ink box's corners (picking, G6d), or the cell's where it has no inkfn bounds(&self) -> [f64; 4]the axis-aligned box of quad() (drawing units)struct GlyphShapewhat a scene knows of a glyph: its contours (em), the box of its control hulls, the area it fillscontours: Vec<Vec<Seg>>bbox: Option<[f64; 4]>area: f64
struct Fontsthe fonts of a scene (or of anyone laying out text: the CPU path of G6e)struct FontMetricsa font's metrics in em units (Fonts::metrics)units_per_em: f64cap_height: f64x_height: f64ascender: f64descender: f64
impl Fonts
fn new() -> Fontsfn add(&mut self, data: &[u8], face: u32) -> Result<FontId>a font from TrueType or OpenType bytes (face: the font of a collection, else 0)fn has(&self, f: FontId) -> boolfn len(&self) -> usizefn is_empty(&self) -> boolfn metrics(&self, f: FontId) -> Option<FontMetrics>fn glyph_of(&self, f: FontId, c: char) -> u32the glyph for a character (0: the font lacks it)fn shape(&mut self, f: FontId, g: u32) -> Option<&GlyphShape>a glyph's contours in em units (y up) and what is known of them (cached)fn layout_text(fonts: &mut Fonts, styles: &[TextStyle], t: &Text) -> Result<Layout>a single line laid out (DXF TEXT)fn layout_mtext(fonts: &mut Fonts, styles: &[TextStyle], t: &MText) -> Result<Layout>text of several lines laid out (the basics of DXF MTEXT)