### Interactive Program Notice for GPL Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/licenses/Saab.txt Display this short notice when your program starts in interactive mode to inform users about its free software status and warranty. ```text Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. ``` -------------------------------- ### Check Harfbuzz Version Source: https://github.com/yeslogic/allsorts/blob/master/tests/myanmar/README.md Verify the installed version of the Harfbuzz shaping tool. ```bash $ hb-shape --version hb-shape (HarfBuzz) 9.0.0 ``` -------------------------------- ### Generate test-font.ttf with fonttools Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/opentype/README.md Generates a test font file from its TTX description using the fonttools command-line interface. Ensure fonttools is installed. ```bash ttx -o test-font.ttf test-font.ttx ``` -------------------------------- ### Clone and enter AOTS repository Source: https://github.com/yeslogic/allsorts/blob/master/tests/aots/README.md Initial steps to acquire the AOTS source code. ```bash git clone https://github.com/adobe-type-tools/aots.git cs aots ``` -------------------------------- ### Standard Copyright Notice for GPL Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/licenses/Saab.txt Include this notice at the beginning of each source file to convey the exclusion of warranty and specify licensing terms under the GNU GPL. ```text Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ``` -------------------------------- ### Copy test data to project directory Source: https://github.com/yeslogic/allsorts/blob/master/tests/aots/README.md Synchronizes the generated test files to the local project font directory. ```bash rsync -az tests/ /path/to/prince/src/fonts/fontcode-rust/tests/aots/ ``` -------------------------------- ### Load and Parse Fonts Source: https://context7.com/yeslogic/allsorts/llms.txt Reads a font file from disk and initializes a Font instance for further operations. Automatically detects TTF, OTF, WOFF, and WOFF2 formats. ```rust use allsorts::binary::read::ReadScope; use allsorts::font_data::FontData; use allsorts::Font; fn main() -> Result<(), Box> { // Read font file into memory let buffer = std::fs::read("font.ttf")?; // Parse the font file - automatically detects format (TTF, OTF, WOFF, WOFF2) let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; // Get a table provider for font index 0 (use different index for TTC collections) let provider = font_file.table_provider(0)?; // Create the Font instance for shaping and glyph operations let font = Font::new(provider)?; println!("Font has {} glyphs", font.num_glyphs()); println!("Is variable font: {}", font.is_variable()); Ok(()) } ``` -------------------------------- ### Build AOTS library and test data Source: https://github.com/yeslogic/allsorts/blob/master/tests/aots/README.md Compiles the library and generates the necessary test data files. ```bash make ``` -------------------------------- ### Convert Variable Font to Static Instance Source: https://context7.com/yeslogic/allsorts/llms.txt Creates a static font file from a variable font by specifying axis values. The axis order in the user tuple must match the font's fvar table definition. ```rust use allsorts::binary::read::ReadScope; use allsorts::font_data::FontData; use allsorts::tables::Fixed; use allsorts::variations::instance; fn main() -> Result<(), Box> { let buffer = std::fs::read("variable-font.ttf")?; let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; let provider = font_file.table_provider(0)?; // Define axis values (order must match font's fvar axis order) // Example for wght (weight) and wdth (width) axes let user_tuple = [ Fixed::from(700.0), // Bold weight Fixed::from(100.0), // Normal width ]; // Create static instance let (instance_font, normalized_tuple) = instance(&provider, &user_tuple)?; // Write the instanced font std::fs::write("instance-bold.ttf", &instance_font)?; println!("Created static font instance: {} bytes", instance_font.len()); Ok(()) } ``` -------------------------------- ### Copyright Disclaimer for Proprietary Software Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/licenses/Saab.txt Use this sample disclaimer to disclaim copyright interest in a program, particularly when developed by an employee. ```text Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice ``` -------------------------------- ### Generate Rust test cases Source: https://github.com/yeslogic/allsorts/blob/master/tests/aots/README.md Uses the Saxon XSLT processor to transform OpenType XML specifications into Rust test case source code. ```bash java -jar jars/saxon9he.jar -s:src/opentype.xml -xsl:../../Work/prince/src/fonts/fontcode-rust/tests/aots/aots2testrust.xsl -o:../../Work/prince/src/fonts/fontcode-rust/tests/aots/testcases.rs ``` -------------------------------- ### Generate SFNT-TTF-Composite WOFF2 Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/woff2/README.md Converts the SFNT-TTF-Composite TTX file into a WOFF2 font file. ```bash ttx --flavor woff2 -o SFNT-TTF-Composite.woff2 SFNT-TTF-Composite.ttx ``` -------------------------------- ### Dump 'post' table with allsorts-tools Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/opentype/README.md Extracts the 'post' table from a TTF font file and saves it to a binary file using the allsorts dump command. This is useful for inspecting post-script table data. ```bash allsorts dump -t post Caudex-Regular.ttf > tests/opentype/post.bin ``` -------------------------------- ### Subset a font in Rust Source: https://context7.com/yeslogic/allsorts/llms.txt Creates a subset font containing only specified glyph IDs, suitable for PDF embedding. Requires a font file and a list of glyph indices. ```rust use allsorts::binary::read::ReadScope; use allsorts::font_data::FontData; use allsorts::subset::{subset, CmapTarget, SubsetProfile}; fn main() -> Result<(), Box> { let buffer = std::fs::read("font.otf")?; let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; let provider = font_file.table_provider(0)?; // Glyph IDs to include (0 = .notdef must always be first) let glyph_ids = [0, 36, 72, 82, 82, 88]; // Example: glyphs for "Hello" // Create subset font let subset_font = subset( &provider, &glyph_ids, &SubsetProfile::Pdf, // Minimal tables for PDF embedding CmapTarget::Unrestricted, // Auto-select best cmap format )?; // Write subset font to file std::fs::write("subset.otf", &subset_font)?; println!("Subset font size: {} bytes", subset_font.len()); Ok(()) } ``` -------------------------------- ### Dump 'CBLC' and 'CBDT' tables with allsorts-tools Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/opentype/README.md Extracts the 'CBLC' and 'CBDT' tables from a TTF font file and saves them to binary files using the allsorts dump command. These tables are related to color glyphs. ```bash allsorts dump -t CBLC NotoColorEmoji.ttf > allsorts/tests/fonts/opentype/CBLC.bin ``` ```bash allsorts dump -t CBDT NotoColorEmoji.ttf > allsorts/tests/fonts/opentype/CBDT.bin ``` -------------------------------- ### Retrieve Glyph Names from Font Source: https://context7.com/yeslogic/allsorts/llms.txt Fetches PostScript names for specified glyph IDs from a font file. Requires a font file and the 'allsorts' crate. ```rust use allsorts::binary::read::ReadScope; use allsorts::font_data::FontData; use allsorts::Font; fn main() -> Result<(), Box> { let buffer = std::fs::read("font.ttf")?; let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; let provider = font_file.table_provider(0)?; let font = Font::new(provider)?; // Get names for specific glyph IDs let glyph_ids = vec![0, 1, 36, 37, 72]; let names = font.glyph_names(&glyph_ids); for (id, name) in glyph_ids.iter().zip(names.iter()) { println!("Glyph {}: {}", id, name); } // Output example: // Glyph 0: .notdef // Glyph 1: .null // Glyph 36: A // Glyph 37: B // Glyph 72: a Ok(()) } ``` -------------------------------- ### Reconstruct Font from Selected Tables Source: https://context7.com/yeslogic/allsorts/llms.txt Converts WOFF to OTF by reconstructing a font from a specified set of tables. Useful for creating optimized font files for embedding. ```rust use allsorts::binary::read::ReadScope; use allsorts::font_data::FontData; use allsorts::subset::whole_font; use allsorts::tag; fn main() -> Result<(), Box> { let buffer = std::fs::read("font.woff")?; let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; let provider = font_file.table_provider(0)?; // Specify tables to include in output font let tags = [ tag::CMAP, tag::HEAD, tag::HHEA, tag::HMTX, tag::MAXP, tag::NAME, tag::OS_2, tag::POST, tag::GLYF, tag::LOCA, // or tag::CFF for CFF fonts ]; // Reconstruct font with specified tables let font_data = whole_font(&provider, &tags)?; std::fs::write("output.otf", &font_data)?; println!("Reconstructed font: {} bytes", font_data.len()); Ok(()) } ``` -------------------------------- ### Read and Inspect WOFF/WOFF2 Fonts Source: https://context7.com/yeslogic/allsorts/llms.txt Handles compressed web font formats (WOFF, WOFF2) with automatic decompression. Provides access to font metadata and unified API for font operations. ```rust use allsorts::binary::read::ReadScope; use allsorts::font_data::FontData; use allsorts::woff::WoffFont; use allsorts::Font; fn main() -> Result<(), Box> { // WOFF files are handled automatically via FontData let buffer = std::fs::read("webfont.woff")?; let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; match &font_file { FontData::Woff(woff) => { println!("WOFF font with {} tables", woff.table_directory.len()); println!("Original sfnt flavor: 0x{:08X}", woff.flavor()); // Access extended metadata if present if let Some(metadata) = woff.extended_metadata()? { println!("Metadata: {}", metadata); } } FontData::Woff2(_) => println!("WOFF2 font"), FontData::OpenType(_) => println!("OpenType font"), } // Use unified API regardless of format let provider = font_file.table_provider(0)?; let font = Font::new(provider)?; println!("Font has {} glyphs", font.num_glyphs()); Ok(()) } ``` -------------------------------- ### Generate Expected Indices with hb-shape Source: https://github.com/yeslogic/allsorts/blob/master/tests/myanmar/README.md Use this command to generate expected indices for good inputs using the Harfbuzz shaping tool. ```bash xargs -d '\n' -n1 hb-shape --shapers ot,fallback --no-glyph-names --no-clusters --no-positions --remove-default-ignorables ../fonts/myanmar/Padauk-Regular.ttf < good > harfbuzz/good-padauk ``` -------------------------------- ### Generate WOFF2 test-font Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/woff2/README.md Uses fonttools to convert an OpenType TTX file into a WOFF2 format font file. ```bash ttx --flavor woff2 -o test-font.woff2 ../opentype/test-font.ttx ``` -------------------------------- ### Perform Text Shaping Source: https://context7.com/yeslogic/allsorts/llms.txt Converts text into positioned glyphs by applying GSUB and GPOS tables. Requires specifying script and language tags for correct substitution and kerning. ```rust use allsorts::binary::read::ReadScope; use allsorts::font::MatchingPresentation; use allsorts::font_data::FontData; use allsorts::gsub::{FeatureMask, Features}; use allsorts::{tag, Font}; fn main() -> Result<(), Box> { let buffer = std::fs::read("tests/fonts/opentype/Klei.otf")?; let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; let provider = font_file.table_provider(0)?; let mut font = Font::new(provider)?; // Define script and language tags let script = tag::LATN; // Latin script let lang = tag::DFLT; // Default language // Map text to glyphs (handles Unicode normalization for script) let glyphs = font.map_glyphs( "Shaping in a jiffy.", script, MatchingPresentation::NotRequired, ); // Shape the glyphs - applies GSUB substitutions and GPOS positioning let glyph_infos = font.shape( glyphs, script, Some(lang), Features::default_mask(), // Enable default features (liga, kern, etc.) &[], // No custom features None, // No variation tuple for variable fonts true, // Enable kerning )?; // Iterate through shaped glyphs with positioning info for info in &glyph_infos { println!( "Glyph ID: {}, X advance: {}, X offset: {}, Y offset: {}", info.glyph.glyph_index, info.horizontal_advance, info.placement.x, info.placement.y ); } Ok(()) } ``` -------------------------------- ### Font Specimen CSS Styles Source: https://github.com/yeslogic/allsorts/blob/master/src/font_specimen/head.html Defines the @font-face rule and layout styles for font specimen pages. Requires template variables for font family and source path. ```css @font-face { src: url("{{ font_src }}"); font-family: "{{ family_name }}-{{ subfamily_name }}"; } body { font-family: sans-serif, "{{ family_name }}-{{ subfamily_name }}"; margin: 1em; } dt { font-weight: bold; text-transform: uppercase; font-size: 12px; letter-spacing: 0.3px; margin: 1.5em 0 0.25em; color: #444; } dd { margin: 0; } dd, li { line-height: 1.5; } .specimen-font { font-family: "{{ family_name }}-{{ subfamily_name }}", sans-serif; } .sample-text { margin-top: 1em; } .sample { margin: 0.5em 0 0; overflow-wrap: break-word; } .column-list { padding-left: 20px; margin-left: 0; columns: 250px; column-gap: 50px; } .count { float: right; margin-left: 1em; } ``` -------------------------------- ### Dump 'head' table with allsorts-tools Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/opentype/README.md Extracts the 'head' table from a TTF font file and saves it to a binary file using the allsorts dump command. This is useful for inspecting specific font table data. ```bash allsorts dump -t head tests/opentype/test-font.ttf > tests/opentype/head.bin ``` -------------------------------- ### Dump 'hmtx' table with allsorts-tools Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/opentype/README.md Extracts the 'hmtx' table from a TTF font file and saves it to a binary file using the allsorts dump command. This is useful for inspecting horizontal metrics. ```bash allsorts dump -t hmtx ../../../data/fonts/Ubuntu-R.ttf > tests/opentype/hmtx.bin ``` -------------------------------- ### Dump 'name' table with allsorts-tools Source: https://github.com/yeslogic/allsorts/blob/master/tests/fonts/opentype/README.md Extracts the 'name' table from a TTF font file and saves it to a binary file using the allsorts dump command. This is useful for inspecting font naming information. ```bash allsorts dump -t name ../../../data/fonts/test-font.ttf > tests/opentype/name.bin ``` -------------------------------- ### Extract glyph outlines in Rust Source: https://context7.com/yeslogic/allsorts/llms.txt Extracts vector path commands from glyphs by implementing the OutlineSink trait. Supports both CFF and TrueType (glyf) font formats. ```rust use std::fmt::Write; use allsorts::binary::read::ReadScope; use allsorts::cff::outline::CFFOutlines; use allsorts::cff::CFF; use allsorts::font::{GlyphTableFlag, MatchingPresentation}; use allsorts::font_data::FontData; use allsorts::outline::{OutlineBuilder, OutlineSink}; use allsorts::pathfinder_geometry::line_segment::LineSegment2F; use allsorts::pathfinder_geometry::vector::Vector2F; use allsorts::tables::glyf::{GlyfVisitorContext, LocaGlyf}; use allsorts::tables::loca::{owned, LocaTable}; use allsorts::tables::SfntVersion; use allsorts::{tag, Font}; // Implement OutlineSink to receive drawing commands struct PathBuilder { path: String, } impl OutlineSink for PathBuilder { fn move_to(&mut self, to: Vector2F) { writeln!(&mut self.path, "M {} {}", to.x(), to.y()).unwrap(); } fn line_to(&mut self, to: Vector2F) { writeln!(&mut self.path, "L {} {}", to.x(), to.y()).unwrap(); } fn quadratic_curve_to(&mut self, ctrl: Vector2F, to: Vector2F) { writeln!(&mut self.path, "Q {} {} {} {}", ctrl.x(), ctrl.y(), to.x(), to.y()).unwrap(); } fn cubic_curve_to(&mut self, ctrl: LineSegment2F, to: Vector2F) { writeln!(&mut self.path, "C {} {} {} {} {} {}", ctrl.from_x(), ctrl.from_y(), ctrl.to_x(), ctrl.to_y(), to.x(), to.y()).unwrap(); } fn close(&mut self) { writeln!(&mut self.path, "Z").unwrap(); } } fn main() -> Result<(), Box> { let buffer = std::fs::read("tests/fonts/opentype/Klei.otf")?; let scope = ReadScope::new(&buffer); let font_file = scope.read::>()?; let provider = font_file.table_provider(0)?; let mut font = Font::new(provider)?; // Map a character to get its glyph let glyphs = font.map_glyphs("+", tag::LATN, MatchingPresentation::NotRequired); let glyph_id = glyphs[0].glyph_index; let mut sink = PathBuilder { path: String::new() }; // Handle CFF or glyf outlines based on font type if font.glyph_table_flags.contains(GlyphTableFlag::CFF) && font.font_table_provider.sfnt_version() == tag::OTTO { let cff_data = font.font_table_provider.read_table_data(tag::CFF)?; let cff = ReadScope::new(&cff_data).read::>()?; let mut cff_outlines = CFFOutlines { table: &cff }; cff_outlines.visit(glyph_id, None, &mut sink)?; } else if font.glyph_table_flags.contains(GlyphTableFlag::GLYF) { let loca_data = font.font_table_provider.read_table_data(tag::LOCA)?; let loca = ReadScope::new(&loca_data).read_dep::>(( font.maxp_table.num_glyphs, font.head_table.index_to_loc_format, ))?; let glyf_data = font.font_table_provider.read_table_data(tag::GLYF).map(Box::from)?; let mut loca_glyf = LocaGlyf::loaded(owned::LocaTable::from(&loca), glyf_data); let mut ctx = GlyfVisitorContext::new(&mut loca_glyf, None); ctx.visit(glyph_id, None, &mut sink)?; } println!("Path commands for glyph {}:\n{}", glyph_id, sink.path); Ok(()) } ```