JFLib

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 TextStyle DXF's STYLE: how texts of it look. Made with new and changed field by field (it may grow)
    • font: FontId
    • fallback: [FontId; FALLBACK_MAX] -- fonts for what font lacks, tried in this order per run of one script (the first fallbacks)
    • fallbacks: u32
    • height: f64 -- a fixed cap height (0: each text says its own), in units
    • width: 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) -> TextStyle a font upright, its own width, heights per text, in drawing units

  • fn with_fallbacks(mut self, fonts: &[FontId]) -> TextStyle the same with fallback fonts (at most FALLBACK_MAX)

  • enum HAlign DXF TEXT's horizontal alignment (group 72)

    • Left
    • Center
    • Right
    • Aligned -- between at and at2: 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 too
    • Fit -- between at and at2: the width factor follows (the height stays)
  • enum VAlign DXF TEXT's vertical alignment (group 73): the baseline, the font's descender, half the cap height, the cap height

    • Baseline
    • Bottom
    • Middle
    • Top
  • enum Attach MTEXT's attachment point (group 71, 1 to 9): where at lies on the text's block

    • TopLeft
    • TopCenter
    • TopRight
    • MiddleLeft
    • MiddleCenter
    • MiddleRight
    • BottomLeft
    • BottomCenter
    • BottomRight
  • struct Text<'a> a single line of text (DXF's TEXT). Made with new and changed field by field (it may grow)

    • text: &'a str -- one line (control characters are left out)
    • style: TextStyleId
    • at: [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 from at (DXF 10) to at2 (DXF 11)
    • at2: [f64; 2]
    • height: Option<f64> -- the cap height (None: the style's), in the style's unit
    • rotation: 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: HAlign
    • valign: VAlign
    • backward: 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 at at, unturned, the style's height, width and oblique angle

  • struct 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 with new and changed field by field (it may grow: runs with their own font, height and colour)

    • text: &'a str -- lines broken at '\n'
    • style: TextStyleId
    • at: [f64; 2]
    • height: Option<f64> -- the cap height (None: the style's), in the style's unit
    • width: f64 -- wrap lines longer than this (0: no wrapping), in the style's unit
    • rotation: f64
    • attach: Attach
    • line_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 at at, no wrapping, single spacing

  • struct Placed a glyph of a layout

    • font: FontId
    • glyph: u32
    • pen: [f64; 2] -- its origin, em units from its line's origin
    • advance: f64 -- its advance (em)
    • cluster: u32 -- the byte where its cluster starts in the text
    • mark: bool -- a mark (no advance: an accent or vowel sign over its base): it has no cell of its own
    • blank: bool -- whitespace or no outline: no ink, no bar
  • struct Line a 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 Bar a 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, ink the share of that box its glyphs cover

    • x0: f64
    • x1: f64
    • y0: f64
    • y1: f64
    • ink: f64
  • struct Layout a text laid out: in em units of its frame, and the frame

    • origin: [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: bool
    • glyphs: 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 by
    • x_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 ink

  • fn bounds(&self) -> [f64; 4] the axis-aligned box of quad() (drawing units)

  • struct GlyphShape what a scene knows of a glyph: its contours (em), the box of its control hulls, the area it fills

    • contours: Vec<Vec<Seg>>
    • bbox: Option<[f64; 4]>
    • area: f64
  • struct Fonts the fonts of a scene (or of anyone laying out text: the CPU path of G6e)

  • struct FontMetrics a font's metrics in em units (Fonts::metrics)

    • units_per_em: f64
    • cap_height: f64
    • x_height: f64
    • ascender: f64
    • descender: f64

impl Fonts

  • fn new() -> Fonts

  • fn 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) -> bool

  • fn len(&self) -> usize

  • fn is_empty(&self) -> bool

  • fn metrics(&self, f: FontId) -> Option<FontMetrics>

  • fn glyph_of(&self, f: FontId, c: char) -> u32 the 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)

Source: docs/API.md in the JFLib repository.