Skip to main content

Crate glyphslib

Crate glyphslib 

Source
Expand description

§glyphslib-rs

A Rust library for reading, writing, and manipulating Glyphs font source files (.glyphs and .glyphspackage).

This crate provides full support for both Glyphs 2 and Glyphs 3 file formats, allowing you to:

  • Load and parse Glyphs files
  • Access and modify font data (masters, instances, glyphs, layers, etc.)
  • Serialize modified data back to Glyphs format
  • Convert between Glyphs 2 and Glyphs 3 formats

§File Format Support

  • Glyphs 2: The original file format using OpenStep Property Lists
  • Glyphs 3: The modern file format with enhanced features and better structure

The library automatically detects which format is being used and provides a unified interface through the Font enum.

§Examples

§Loading a Glyphs file

use glyphslib::Font;
use std::path::Path;

// Load a .glyphs or .glyphspackage file
let font = Font::load(Path::new("MyFont.glyphs")).unwrap();

// Check the format version
match &font {
    Font::Glyphs2(g2) => println!("Loaded Glyphs 2 file: {}", g2.family_name),
    Font::Glyphs3(g3) => println!("Loaded Glyphs 3 file: {}", g3.family_name),
}

§Examining font data

use glyphslib::{Font, GlyphsFile};
use std::path::Path;

let font = Font::load(Path::new("MyFont.glyphs")).unwrap();

// Access common properties regardless of version
let font = font.as_glyphs3().unwrap();
println!("Family: {}", font.family_name());
println!("Number of glyphs: {}", font.glyphs().len());

// Iterate through glyphs
for glyph in font.glyphs() {
    println!("Glyph: {}", glyph.name());
}

// Access masters
for master in font.masters() {
    println!("Master: {}", master.name());
}

§Modifying font data

use glyphslib::Font;
use std::path::Path;
use glyphslib::glyphs3::{Property, SingularPropertyKey};

let mut font = Font::load(Path::new("MyFont.glyphs")).unwrap();

// Modify properties based on version
match &mut font {
    Font::Glyphs2(g2) => {
        g2.designer = Some("Jane Doe".to_string());
        // Modify a glyph
        if let Some(glyph) = g2.glyphs.iter_mut().find(|g| g.name == "A") {
            glyph.export = false;
        }
    }
    Font::Glyphs3(g3) => {
        g3.properties.push(Property::singular(SingularPropertyKey::Designer, "Jane Doe".to_string()));
        // Modify a glyph
        if let Some(glyph) = g3.glyphs.iter_mut().find(|g| g.name == "A") {
            glyph.export = false;
        }
    }
}

§Serializing back to Glyphs format

use glyphslib::Font;
use std::path::Path;
use std::fs;

let font = Font::load(Path::new("MyFont.glyphs")).unwrap();

// Serialize to string
let output = match &font {
    Font::Glyphs2(g2) => openstep_plist::to_string(g2).unwrap(),
    Font::Glyphs3(g3) => openstep_plist::to_string(g3).unwrap(),
};

// Write to file
fs::write("MyFont_modified.glyphs", output).unwrap();

§Converting between formats

use glyphslib::Font;
use std::path::Path;

let font = Font::load(Path::new("MyFont.glyphs")).unwrap();

// Convert Glyphs 2 to Glyphs 3
let glyphs3 = font.upgrade();

Modules§

common
Common types and structures shared between Glyphs 2 and Glyphs 3 formats
glyphs2
Glyphs 2 file format structures
glyphs3
Glyphs 3 file format structures

Enums§

Font
A font loaded from a Glyphs file, either version 2 or 3
Plist
An enum representing a property list.

Traits§

GlyphsFile
Trait for GlyphsFile implemented for both Glyphs2 and Glyphs3.