1# font-kit 2[![Build Status](https://travis-ci.org/servo/font-kit.svg?branch=master)](https://travis-ci.org/servo/font-kit) 3[![Crates.io](https://img.shields.io/crates/v/font-kit.svg)](https://crates.io/crates/font-kit) 4[![Documentation](https://docs.rs/font-kit/badge.svg)](https://docs.rs/font-kit) 5 6`font-kit` provides a common interface to the various system font libraries and provides 7services such as finding fonts on the system, performing nearest-font matching, and rasterizing 8glyphs. 9 10## Synopsis 11 12```rust 13let font = SystemSource::new() 14 .select_by_postscript_name("ArialMT") 15 .unwrap() 16 .load() 17 .unwrap(); 18 19let glyph_id = font.glyph_for_char('A').unwrap(); 20let mut canvas = Canvas::new(&Size2D::new(32, 32), Format::A8); 21 22font.rasterize_glyph( 23 &mut canvas, 24 glyph_id, 25 32.0, 26 &Point2D::new(0.0, 32.0), 27 HintingOptions::None, 28 RasterizationOptions::GrayscaleAa, 29) 30.unwrap(); 31``` 32 33## Backends 34 35`font-kit` delegates to system libraries to perform tasks. It has two types of backends: a *source* 36and a *loader*. Sources are platform font databases; they allow lookup of installed fonts by name 37or attributes. Loaders are font loading libraries; they allow font files (TTF, OTF, etc.) to be 38loaded from a file on disk or from bytes in memory. Sources and loaders can be freely intermixed at 39runtime; fonts can be looked up via DirectWrite and rendered via FreeType, for example. 40 41Available loaders: 42 43* Core Text (macOS): The system font loader on macOS. Does not do hinting except when bilevel 44 rendering is in use. 45 46* DirectWrite (Windows): The newer system framework for text rendering on Windows. Does vertical 47 hinting but not full hinting. 48 49* FreeType (cross-platform): A full-featured font rendering framework. 50 51Available sources: 52 53* Core Text (macOS): The system font database on macOS. 54 55* DirectWrite (Windows): The newer API to query the system font database on Windows. 56 57* Fontconfig (cross-platform): A technically platform-neutral, but in practice Unix-specific, API 58 to query and match fonts. 59 60* Filesystem (cross-platform): A simple source that reads fonts from a path on disk. This is the 61 default on Android. 62 63* Memory (cross-platform): A source that reads from a fixed set of fonts in memory. 64 65* Multi (cross-platform): A source that allows multiple sources to be queried at once. 66 67On Windows and macOS, the FreeType loader and the Fontconfig source are not built by default. 68To build them, use the `loader-freetype` and `source-fontconfig` Cargo features respectively. If 69you want them to be the default, instead use the `loader-freetype-default` and 70`source-fontconfig-default` Cargo features respectively. Beware that `source-fontconfig-default` is 71rarely what you want on those two platforms! 72 73If you don't need to locate fonts on the system at all—for example, if all your fonts are stored 74with your app—then you can omit the default `source` feature and none of that code will be 75included. 76 77## Features 78 79`font-kit` is capable of doing the following: 80 81* Loading fonts from files or memory. 82 83* Determining whether files on disk or in memory represent fonts. 84 85* Interoperating with native font APIs. 86 87* Querying various metadata about fonts. 88 89* Doing simple glyph-to-character mapping. (For more complex use cases, a shaper is required; 90 proper shaping is beyond the scope of `font-kit`.) 91 92* Reading unhinted or hinted vector outlines from glyphs. 93 94* Calculating glyph and font metrics. 95 96* Looking up glyph advances and origins. 97 98* Rasterizing glyphs using the native rasterizer, optionally using hinting. (Custom rasterizers, 99 such as Pathfinder, can be used in conjunction with the outline API.) 100 101* Looking up all fonts on the system. 102 103* Searching for specific fonts by family or PostScript name. 104 105* Performing font matching according to the [CSS Fonts Module Level 3] specification. 106 107## Dependencies 108 109**Ubuntu Linux** 110 111`sudo apt install pkg-config libfreetype6-dev libfontconfig1-dev` 112 113## License 114 115`font-kit` is licensed under the same terms as Rust itself. 116 117[CSS Fonts Module Level 3]: https://drafts.csswg.org/css-fonts-3/#font-matching-algorithm 118