.TH FONTSRV 4
.SH NAME
fontsrv \- translate font specifications to font paths
.SH SYNOPSIS
.B fontsrv
[
.B -dM
] [
.B -f
.I rulefile
] [
.B -p
.I ftpath
] [
.B -r
.I xdpi
.I ydpi
]
.SH DESCRIPTION
.I Fontsrv
is a translation service for fonts.
Given a font query, a path to an Inferno font is returned.
The command-line options and rules file configure how queries are translated and which fonts are available.
The basic functionality of translating a query is provided by fontsrv through 9P2000:  a write to the
.LR lookup
file sets the query, a subsequent read retrieves the font path.
.SS Options
.TP
.B -d
Enable debug prints.
.TP
.B -M
Serve 9P2000 on file descriptor 0 instead of mounting itself at
.LR /mnt/font .
.TP
.BI -f " rulefile"
Use
.I rulefile
instead of the default
.LR /fonts/rules .
.TP
.BI -p " ftpath"
Use the
.IR ttffs (4)
.LR index
file and fonts at
.IR ftpath
instead of the default
.LR /mnt/ft .
.TP
.BI -r " xdpi ydpi"
Use given dpi instead of default
.LR "96 96" .
.SS Font translation
The font query string can be in two forms:
.IP 1.
.BR programs . literal
.IP 2.
.BR programs . fontnames . styles . sizes
.PP
The literal in the first form is often a path.
This allows most existing programs to use fontsrv with no or few modifications.
The second form is called a font specification, or
.IR fspec .
Examples:
.IP 1.
.BR Acme . /fonts/pelm/unicode.8.font
.IP 2.
.BR man . sans . i . 12
.PP
.IR Programs ,
.IR fontnames ,
.I styles
and
.I sizes
can be comma-separated values (but is often just a single value).
A
.I program
is the name of the requesting program.
A
.I "font name"
is the name of the font.
.I Programs
and
.I "font names"
can be any string, the font matching algorithm determines how well an fspec matches a font.
Strings not matching an available font or an existing program can be useful as anchors in rewrite rules.
A style is one of:
.LR r ,
.LR i ,
.LR b ,
.LR ib .
For regular, italic, bold and italic & bold.
A size is in points (immediately converted to pixels according to dpi), and can be a non-inclusive open-ended range.
Each field can hold the special value
.LR ? .
It is mostly useful as an anchor for rewrite rules and especially useful for the style and size fields (which cannot otherwise contain arbitrary strings).
.PP
The translation is done as follows:
.IP 1.
Parse the query (fspec or literal).  A literal is rewritten to an fspec.
.IP 2.
Apply rewrite rules to the fspec.
.IP 3.
Find and return the font that best matches the fspec.
.PP
Fontsrv makes a list of available fonts by:
.IP 1.
Reading
.LR /mnt/ft/index
(served by
.IR ttffs (4),
i.e. freetype fonts).
.IP 2.
Recursively walking
.LR /fonts
and all
.LR walk
directives from the rules file, looking for files of the form:
.IR fontname . style . size .font
.IP 3.
Using
.LR font
directives from the rules file.
.IP 4.
Testing for presence in the file system for each font in a built-in list of known inferno fonts.
.PP
The literal in the first query form is converted to an fspec by fontsrv, based on a list with known literals (namely known fonts in the inferno-os distribution).
.SS Rules
The rules file is loaded at startup, and when
.LR reload
is written to
the
.LR ctl
file.
It is not an error if the file does not exist.
Empty lines are ignored, as are lines whose first non-blank character is a `#'.
Otherwise, a line contains one directive parsed as a list of quoted strings.
The following directives are recognized:
.TP
.BI dpi " xdpi ydpi"
Set the dpi.
If present, must be the first directive in the file.
If absent, the dpi specified on the command line is used, falling back to the default.
.TP
.BI rewrite " source target"
Add a rewrite rule, rewriting fspec
.I source
to fspec
.IR target .
The semantics of rewrite rules are described later.
.TP
.BI fallback " fontname"
Set the fallback font name, its use is described later.
.TP
.BI font " path fspec"
Add the
.IR font (6)
file at
.I path
to the list of known fonts, and add
.I path
as a literal alias for
.IR fspec .
.TP
.BI walk " fontpath"
Recursively walk
.IR fontpath ,
adding each file of the form
.IB name . style . size .font
as if it was specified as a
.LR font
directive.
.TP
.BI debug " n"
Set the boolean debug printing flag to the numeric value
.IR n .
.SS Rewriting
Rewrite rules are applied to the query fspec in the order they occur in the rules file.
A rewrite rule has a source and target fspec.
A rule matches a query fspec if each of the four fields in the source fspec match.
A field matches if it is empty or one of the values matches one of the values in the query fspec.
If a rule matches, fields in the query fspec are replaced by the corresponding fields in the target fspec, but only if the field in the target fspec is not empty.
.PP
Example rule:
.PP
.EX
	rewrite	acme..r.	'.dejavu sans..11'
.EE
.PP
This rule will match queries for a regular font (of any size and font name), asked by acme.
The programs and styles fields are left intact, the font name and font size are replaced.
Assuming the query fspec has
.LR acme
for the first field, and
.LR r,b
for the third, the result will be:
.LR "acme.dejavu sans.r,b.11" .
.PP
Fontsrv adds the following default rules after those from the rules file:
.PP
.EX
	rewrite .?..	.mono..
	rewrite ..?.	..r.
	rewrite ...?	...12
.EE
The default fallback font name is
.LR mono .
.PP
The easiest way to make rewrite rules for fonts for a program is to start that program and then look at
.LR /mnt/font/cache .
The most recently used translations are returned first, with four fields per line:  query string, parsed query fspec (after literal lookup), rewritten fspec, font best matching rewritten fspec.
The fourth field is the font used.
The second field is the starting point for rewrite rules.
.SS Scoring
The best font for an fspec is found by scoring each available font for the fspec, and returning the font with the highest score.
The following criteria determine the score of the font, from most to least important:
.PP
- Exact font name match
.br
- Exact style match
.br
- Exact size match
.br
- Substring font name match
.br
- Nearby font size
.br
- Font name matches fallback name
.SS Files
The following files are served over 9P2000:
.TP
.LR ctl
Only accepts a write of
.LR reload ,
to reload the rules file.
.TP
.LR lookup
Provides the translation service.
Write a fontspec, then read the result: two lines, the first line the font path (the interesting part), the second line the rewritten fspec.
.TP
.LR list
Allows enumerating available font properties.
By default, a read returns a list of font names (comma-separated, one line in total).
Writing a selection request sets the data to return for reading.
Valid selection requests:
.RS
.TP
.B names
Returns font names (the default).
.TP
.BI styles " name"
Return the styles available for
.IR name .
.TP
.BR sizes " name style"
Returns the styles available for
.I name
and
.IR style .
.RE
.TP
.LR cache
Read-only, returns the lookup most-recently-used lookup cache.
One entry per line, each line with four fields:  original request, original fontspec (i.e. possible literal translated to fontspec), rewritten fontspec, resulting font path.
.SH EXAMPLES
Put the following in your
.LR lib/wmsetup :
.PP
.EX
mount {mntgen} /mnt
ttffs
fontsrv
.EE
.SH FILES
.IB /mnt/font/ ctl
.br
.IB /mnt/font/ lookup
.br
.IB /mnt/font/ list
.br
.IB /mnt/font/ cache
.br
.B /fonts/rules
.br
.B /mnt/ft/index
.br
.B /mnt/ft/*.font
.SH SOURCE
.B /appl/cmd/fontsrv.b
.SH SEE ALSO
.IR showfonts (1),
.IR ttffs (4),
.IR font (6)
.SH BUGS
Fonts cannot be selected based on available glyphs.
.PP
Font sizes are approximations.
Correct font size information is often not available.
.PP
It should be clearer when a size is in pixels and when in points.
That may also be more convenient in rewrite rules.
