go-text/typesetting

repository·main·Indexed 41 days ago

https://github.com/go-text/typesetting

A pure Go library providing typesetting capabilities for GUI applications, serving as a core component for Go UI toolkits such as Fyne, Gio, and Ebitengine. It includes packages for text shaping, OpenType font loading and parsing, and a bi-directional (BiDi) text utility.

Tokens
893
Snippets
0
Records
8
Agent score
51%

What's inside go-text/typesetting

  1. What is the di library?

    main
    The di library is a specialized utility designed to convert bi-directional (BiDi) text into uni-directional text. This is useful for processing text that contains mixed directions (such as combining Left-to-Right and Right-to-Left scripts) into a single, consistent direction.
  2. What is the typesetting library?

    main
    The typesetting library provides typesetting capabilities implemented in pure Go. It is designed for use in GUI applications and is a shared dependency for several Go UI toolkits, including Fyne, Gio, and Ebitengine.
  3. Overview of the font package

    main

    The font package is a library designed for loading and utilizing OpenType fonts. It is structured into two layers:

    1. font/opentype: Provides low-level parsing of font files and their internal tables.
    2. font: Provides a high-level API intended for use by font shapers and renderers.
  4. How the system font index works

    main

    To enable fast and accurate matching of fonts against family, aspect, and rune coverage queries, FontMap uses a system font index composed of lightweight font summaries.

    Performance and Caching:

    • Initial Scan: The first time UseSystemFonts is called, the library performs a filesystem scan to build the index. This can take between 0.2 and 0.5 seconds on a typical laptop.
    • On-disk Cache: The index is saved to disk. Subsequent application launches are fast because the library loads the cached index and performs a quick integrity check against the current filesystem to detect if fonts have been added or removed.
  5. Select fonts using SetQuery

    main

    After initializing a FontMap, use SetQuery to select fonts that match specific requirements.

    A query consists of:

    • Families: One or several font family names.
    • Aspect: Describes the style, weight, and stretchiness of the desired font.

    You can also call SetScript to influence how fallback fonts are resolved for specific writing systems.

    Matching Logic:

    • Family Substitutions: The library uses a substitution system (inspired by fontconfig) to expand requested families with similar known families to ensure better fallback coverage. For example, requesting Arial might automatically include Arimo, sans-serif, and others based on hard-coded substitution rules.
    • Style Matching: If an exact match for the requested Aspect (e.g., ExtraBold Italic) is not found, the library applies CSS font selection rules to return the closest available match (e.g., Bold Oblique).
  6. Understand the versioning and stability of typesetting

    main
    The library is currently in a rapid evolution phase and uses unstable versions (v0.x.y). Developers should be aware that breaking changes will occur and will be indicated by a bump in the minor version number (x). Bug fixes and performance improvements will be indicated by a bump in the patch number (y).
  7. Initialize a FontMap for font discovery and loading

    main

    The FontMap is the primary entry point for locating and loading font.Font objects. You should create a new FontMap for each text shaping task.

    To populate the map, you can use one or both of the following methods:

    • UseSystemFonts: Scans and includes fonts installed on the host system. The first call triggers a filesystem scan to build a font index, which is then cached on disk for faster subsequent launches.
    • AddFont / AddFace: Adds specific user-provided font files or faces to the map.

    Once initialized, the FontMap satisfies the shaping.Fontmap interface, allowing it to be used with shaping.SplitByFace.