Read TrueType and OpenType fonts in Scala
TrueType and OpenType fonts have become a de facto standard for describing
how character data, or text, should be visually rendered. As binary data,
font files are not as straightforward to read as many other files. This is
particularly true of TrueType
and OpenType
fonts, which can include a
variety of font-related data, not all of which is needed for all applications:
most of the data in a font file is exists to describe the shapes of each
glyph, which is not easy to work with. So usually, reading font files is left
to graphics software, web browsers or the operating system.
But there remains useful data (or metadata) in a font file which many applications can take advantage of. In particular this includes font metrics data, essentially the widths of glyphs, and information about the font's weight or slant. Phoenicia provides read-only access to this data.
- reads TrueType and OpenType font files
- can read from any Turbulence source
- access font metrics and calculate widths of individual glyphs or strings of text
All terms and types are defined in the phoenicia
package:
import phoenicia.*
A TrueType font, which usually has the .ttf
extension, can be loaded from any
value Turbulence source that can
be read as Bytes
, and includes files on disk (using
Galilei), classpath resources (using
Hellenism) or URLs (using
Telekinesis).
A new Ttf
object can be created using the Ttf
factory, like so:
val font = Ttf(url"https://example.com/typography.ttf")
This will capture the binary data of the font, but will not interpret it; that will be performed lazily on different parts of the, only when it is needed.
Currently, Phoenicia does not provide any features that are unique to
OpenType fonts; every feature is supported by both TrueType and OpenType fonts.
So it is sufficient to use the Ttf
factory to load OTF files too. At a later
time, it may be necessary to provide an Otf
factory as well.
The only useful operation currently implemented on Ttf
instances is the
advanceWidth
method, which takes a Char
value and returns its width as a
number of font design units. This width is specifically its advance width,
the amount of horizontal distance traveled to accommodate the glyph before the
next glyph; typically a lower value for a narrow character like l
than for a
wide character like w
.
A related method, width, takes a Text
value, and returns the sum of the
advance widths of its characters, when rendered at normal spacing. The result
is specified in ems
, with the type Quantity[Ems[1]]
.
Phoenicia is classified as embryotic. For reference, Soundness projects are categorized into one of the following five stability levels:
- embryonic: for experimental or demonstrative purposes only, without any guarantees of longevity
- fledgling: of proven utility, seeking contributions, but liable to significant redesigns
- maturescent: major design decisions broady settled, seeking probatory adoption and refinement
- dependable: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version
1.0.0
or later - adamantine: proven, reliable and production-ready, with no further breaking changes ever anticipated
Projects at any stability level, even embryonic projects, can still be used, as long as caution is taken to avoid a mismatch between the project's stability level and the required stability and maintainability of your own project.
Phoenicia is designed to be small. Its entire source code currently consists of 240 lines of code.
Phoenicia will ultimately be built by Fury, when it is published. In the meantime, two possibilities are offered, however they are acknowledged to be fragile, inadequately tested, and unsuitable for anything more than experimentation. They are provided only for the necessity of providing some answer to the question, "how can I try Phoenicia?".
-
Copy the sources into your own project
Read the
fury
file in the repository root to understand Phoenicia's build structure, dependencies and source location; the file format should be short and quite intuitive. Copy the sources into a source directory in your own project, then repeat (recursively) for each of the dependencies.The sources are compiled against the latest nightly release of Scala 3. There should be no problem to compile the project together with all of its dependencies in a single compilation.
-
Build with Wrath
Wrath is a bootstrapping script for building Phoenicia and other projects in the absence of a fully-featured build tool. It is designed to read the
fury
file in the project directory, and produce a collection of JAR files which can be added to a classpath, by compiling the project and all of its dependencies, including the Scala compiler itself.Download the latest version of
wrath
, make it executable, and add it to your path, for example by copying it to/usr/local/bin/
.Clone this repository inside an empty directory, so that the build can safely make clones of repositories it depends on as peers of
phoenicia
. Runwrath -F
in the repository root. This will download and compile the latest version of Scala, as well as all of Phoenicia's dependencies.If the build was successful, the compiled JAR files can be found in the
.wrath/dist
directory.
Contributors to Phoenicia are welcome and encouraged. New contributors may like to look for issues marked beginner.
We suggest that all contributors read the Contributing Guide to make the process of contributing to Phoenicia easier.
Please do not contact project maintainers privately with questions unless there is a good reason to keep them private. While it can be tempting to repsond to such questions, private answers cannot be shared with a wider audience, and it can result in duplication of effort.
Phoenicia was designed and developed by Jon Pretty, and commercial support and training on all aspects of Scala 3 is available from Propensive OÜ.
The Phoenicians were responsible for creating the first recorded alphabet. Their land, mostly modern-day Lebanon, was Phoenicia.
In general, Soundness project names are always chosen with some rationale, however it is usually frivolous. Each name is chosen for more for its uniqueness and intrigue than its concision or catchiness, and there is no bias towards names with positive or "nice" meanings—since many of the libraries perform some quite unpleasant tasks.
Names should be English words, though many are obscure or archaic, and it should be noted how willingly English adopts foreign words. Names are generally of Greek or Latin origin, and have often arrived in English via a romance language.
The logo shows a close-up view of a character in a serif font.
Phoenicia is copyright © 2025 Jon Pretty & Propensive OÜ, and is made available under the Apache 2.0 License.