On this page:
font
display-size
display-text
show-kerning?
name/  c
fontinfo/  c
lib/  c
groups/  c
kerning/  c
features/  c
data/  c
images/  c
color/  c
string->color
color->string
layer-info
glyph
advance
image
layer
foreground
background
guideline
anchor
component
contour
point
1.1 Functions
1.1.1 Reading and writing UFOs
read-ufo
write-ufo
1.1.2 Inspecting glyphs and layers
get-glyph
get-glyphs
remove-glyph
insert-glyph
map-glyphs
for-each-glyphs
filter-glyphs
font-glyphs-list
sort-glyph-list
get-layer
set-layer
map-layers
for-each-layers
decompose-glyph
decompose-font
glyph-bounding-box
font-bounding-box
1.1.3 Sidebearings and spacing properties
get-sidebearings
intersections-at
get-sidebearings-at
set-sidebearings
set-sidebearings-at
adjust-sidebearings
1.1.4 Rounding coordinates
font-round
layer-round
kerning-round
glyph-round
advance-round
image-round
anchor-round
contour-round
component-round
guideline-round
point-round
1.1.5 Contours, components, anchors and guidelines
map-contours
for-each-contours
map-components
for-each-components
map-anchors
for-each-anchors
map-guidelines
for-each-guidelines
map-points
for-each-points
contour->bezier
bezier->contour
component->outlines
contour-open?
reverse-contour
glyph-reverse-directions
glyph-correct-directions
1.1.6 kerning
kern-groups2->3
kerning-group-names
valid-kerning-group-name?
left-kerning-group?
right-kerning-group?
kerning-group?
update-kerning-group-name
lookup-kerning-pair
kerning-value
map-kerning
sorted-kerning-list
1.1.7 More
glyph-signed-area
lowercase-stems
uppercase-stems
correct-directions
print-glyph

1 Font structures and contracts

 (require sfont) package: sfont

struct

(struct font (fontinfo
    groups
    kerning
    features
    glyphs
    layers
    lib
    data
    images))
  fontinfo : fontinfo/c
  groups : groups/c
  kerning : kerning/c
  features : features/c
  glyphs : 
(or/c (listof glyph?)
      (hash/c name/c glyph? #:immutable #t))
  layers : 
(or/c (listof layer-info?)
      (listof (cons/c name/c layer-info?)))
  lib : lib/c
  data : data/c
  images : images/c
A structure type for fonts. Font structures are pict-convertible? and are printed at the REPL. The way they are printed depends on the parameters display-size, display-text and show-kerning?.

Glyphs are stored internally in an hash table so they can be easily accessed by name, however the guard of font accept a list of glyphs and transform it in hash table. Font layers informations are stored in an association list (to preserve the order) and, like glyphs, the guard accepts a list of layer-info and transform it in an hash table.

Fonts, like all the structures defined below (with the exception of advance) implement the gen:geometric interface.

parameter

(display-size)  natural-number/c

(display-size size)  void?
  size : natural-number/c
The size of the font.

parameter

(display-text)  (listof (listof symbol?))

(display-text text)  void?
  text : (listof (listof symbol?))
The text to be printed. Every line is represented as a list of symbols (the name of glyphs). If a glyph is not in the font it is not shown.

parameter

(show-kerning?)  boolean?

(show-kerning? show)  void?
  show : boolean?
Determine wheter kerning is applied to the sample text.

A name is a symbol.

Fontinfo is an immutable hash table whone keys are name/c.

Libs are immutable hash table whose keys are name/c.

Groups are represented in an immutable hash table whose keys and values are name/c and list of name/c.

Kerning table is represented in an immutable hash table; keys are name/c and represent the first item in the kerning pair. Values are immutable hash tables with name/c as key and a real number as value. For example:
(hash 'v
      (hash 'o -20
            'e -18))
specifies a kerning table where the value the kerning between v and o is -20 and the kerning between v and e is -18. The hash table don’t have a clear order, but when the font is written with write-ufo the kerning pairs are sorted with sorted-kerning-list.

Features are stored in a string.

value

data/c : flat-contract?

Data and images (defined in UFO3 specifications) are normally represented by paths to files.

A color is a list of four numbers between 0 and 1. The first three value are th rgb values, the fourth one is the alpha value (transparency).

procedure

(string->color s)  color/c

  s : string?
Produces a color from a string in the format "r,g,b,a".

procedure

(color->string c)  string?

  c : color/c
Produces a string in the format "r,g,b,a" from a color.

struct

(struct layer-info (name color lib))

  name : name/c
  color : (or/c color/c #f)
  lib : lib/c
Layer infos are used to store informations about layers at font level. They can associate a layer’s name to a color and arbitrary informations stored in a lib (see also the UFO specifications.)

If a layer is added to a glyph, the respective layer-info has to be added to the font layers.

struct

(struct glyph (name advance unicodes note image layers lib))

  name : name/c
  advance : advance?
  unicodes : (listof natural-number/c)
  note : (or/c string? #f)
  image : (or/c image? #f)
  layers : (or/c (listof layer?) (hash/c name/c layer? #:immutable #t))
  lib : lib/c
A structure type for glyphs. Layers are stored in an hash tables (but, like glyphs in fonts can be passed as lists to the constructor).

Transformations can be applied to glyphs, however only scale will affect the advance width and height of the glyph.

struct

(struct advance (width height))

  width : real?
  height : real?
The advance width and height of a glyph. Advance dictates where the next glyph should be placed.

struct

(struct image (filename matrix color))

  filename : string?
  matrix : trans-mat?
  color : (or/c color/c #f)
An image is defined by the filename, a transformation matrix and a color (if the color is false, then no color is specified).

struct

(struct layer (name guidelines anchors contours components))

  name : name/c
  guidelines : (listof guideline?)
  anchors : (listof anchor?)
  contours : (listof contour?)
  components : (listof component?)
A layer is used to store informations about contours, anchors, components and guidelines.

Transformations applied to a layer are applied to its content.

value

foreground : 'public.default

value

background : 'public.background

Names of foreground and background layers.

struct

(struct guideline (pos angle name color identifier))

  pos : vec?
  angle : real?
  name : (or/c string? #f)
  color : (or/c color/c #f)
  identifier : (or/c symbol? #f)

struct

(struct anchor (pos name color identifier))

  pos : vec?
  name : string?
  color : (or/c color/c #f)
  identifier : (or/c symbol? #f)

struct

(struct component (base matrix identifier))

  base : name/c
  matrix : trans-mat?
  identifier : (or/c symbol? #f)

struct

(struct contour (identifier points))

  identifier : (or/c symbol? #f)
  points : (listof point?)

struct

(struct point (pos type smooth name identifier))

  pos : vec?
  type : (one-of/c 'move 'line 'offcurve 'curve 'qcurve)
  smooth : boolean?
  name : (or/c string? #f)
  identifier : (or/c symbol? #f)

1.1 Functions

1.1.1 Reading and writing UFOs

procedure

(read-ufo file)  font?

  file : path-string?
Read the UFO file and produce a font.

procedure

(write-ufo f    
  file    
  [#:overwrite overwrite    
  #:format format])  void?
  f : font?
  file : path-string?
  overwrite : boolean? = #t
  format : (list/c 2 3) = 2
Write the font to file, if #:overwrite is #f and the file already exists, an error is raised. The optional keyword argument #:format is used to save in a specific UFO format (default is 2).

1.1.2 Inspecting glyphs and layers

procedure

(get-glyph f g)  (or/c glyph? #f)

  f : font?
  g : name/c
Produces the glyph named g from the font f. If f hasn’t a glyph with that name, returns false.

procedure

(get-glyphs f gs)  (listof glyph?)

  f : font?
  gs : (listof name/c)
Maps every name in gs with the corresponding glyph in f (if the glyph exists).

procedure

(remove-glyph f g)  font?

  f : font?
  g : name/c
Functionally remove (produces a new font) the glyph named g from f.

procedure

(insert-glyph f g)  font?

  f : font?
  g : glyph?
Functionally insert (produces a new font) the glyph g in f.

procedure

(map-glyphs proc f [#:sorted sorted])  (listof any/c)

  proc : (-> glyph? any/c)
  f : font?
  sorted : boolean? = #f

procedure

(for-each-glyphs proc f [#:sorted sorted])  void?

  proc : (-> glyph? any/c)
  f : font?
  sorted : boolean? = #f

procedure

(filter-glyphs proc f)  (listof glyph?)

  proc : (-> glyph? any/c)
  f : font?
Like map for-each and filter, but the procedures are applied to the glyphs in the given font. If sorted is #t in map-glyphs and for-each-glyphs the glyphs will be sorted by name before the operation.

procedure

(font-glyphs-list f)  (listof glyph?)

  f : font?
Produces a list of the glyphs in f.

procedure

(sort-glyph-list gl [#:key key #:pred pred])  (listof glyph?)

  gl : (listof glyph?)
  key : (-> glyph? any/c)
   = (lambda (g) (symbol->string (glyph-name g)))
  pred : (-> any/c any/c boolean?) = string<?
Produce a list of sorted glyphs. The way glyphs are sorted is controlled by key and pred.

procedure

(get-layer g [l])  layer?

  g : glyph?
  l : name/c = foreground
Produce the layer named l in the given glyph, the default layer is foreground.

procedure

(set-layer g new-layer)  glyph?

  g : glyph?
  new-layer : layer?
Functionally sets a layer in a glyph.

procedure

(map-layers proc g [#:sorted sorted])  (listof any/c)

  proc : (-> layer? any/c)
  g : glyph?
  sorted : boolean? = #f

procedure

(for-each-layers proc g [#:sorted sorted])  void?

  proc : (-> layer? any/c)
  g : glyph?
  sorted : boolean? = #f
Like map and for-each, but the procedures are applied to the layers in the given glyph. If sorted is #t the layers will be sorted by name before the operation.

procedure

(decompose-glyph f g)  glyph?

  f : font?
  g : glyph?
Produce a glyph from the given glyph decomposing components to outline. The ’context’ from which the components (other glyphs) are retrieved is a font.

procedure

(decompose-font f)  font?

  f : font?
Produce a font from the given font decomposing all glyphs.

procedure

(glyph-bounding-box g f)  bounding-box/c

  g : glyph?
  f : font?
(glyph-bounding-box g)  bounding-box/c
  g : glyph?
Produces the bounding box for the glyph, if a font is provided the bounding box will take into account components, otherwise the bounding box will be the bounding box of contours only.

procedure

(font-bounding-box f [components])  bounding-box/c

  f : font?
  components : boolean? = #t
Produces the bounding box of the font, if components is #t components are taken into account.

1.1.3 Sidebearings and spacing properties

procedure

(get-sidebearings g f)  (or/c (cons/c real? real?) #f)

  g : glyph?
  f : font?
(get-sidebearings g)  (or/c (cons/c real? real?) #f)
  g : glyph?
Produces the sidebearings of the glyph in a pair (or #f if they can’t be found), if a font is provided the sidebearings will take into account components. The car of the pair represents the left sidebearing, while the cdr represents the right one.

procedure

(intersections-at g f h)  (listof vec?)

  g : glyph?
  f : font?
  h : real?
(intersections-at g h)  (listof vec?)
  g : glyph?
  h : real?
Produces a list of intersections between the outlines (if a font is provided it will take into account components) and the horizontal line passing through (0, h).

procedure

(get-sidebearings-at g f h)  (or/c (cons/c real? real?) #f)

  g : glyph?
  f : font?
  h : real?
(get-sidebearings-at g h)  (or/c (cons/c real? real?) #f)
  g : glyph?
  h : real?
Like get-sidebearings but the sidebearings are calculated from the leftmost and rightmost intersections produced by intersection-at.

procedure

(set-sidebearings g f left right)  glyph?

  g : glyph?
  f : font?
  left : real?
  right : real?
(set-sidebearings g left right)  glyph?
  g : glyph?
  left : real?
  right : real?
Produces a glyph with the given sidebearings (ie. if get-sidebearings is applied to the new glyph the result is (cons left right)).

procedure

(set-sidebearings-at g f left right h)  glyph?

  g : glyph?
  f : font?
  left : real?
  right : real?
  h : real?
(set-sidebearings-at g left right h)  glyph?
  g : glyph?
  left : real?
  right : real?
  h : real?
Produces a glyph with the given sidebearings at the given height (ie. if get-sidebearings-at is applied to the new glyph the result is (cons left right)).

procedure

(adjust-sidebearings g f left right)  glyph?

  g : glyph?
  f : font?
  left : real?
  right : real?
(adjust-sidebearings g left right)  glyph?
  g : glyph?
  left : real?
  right : real?
Produces a glyph adding left and right to the sidebearings.

1.1.4 Rounding coordinates

procedure

(font-round f)  font?

  f : font?

procedure

(layer-round l)  layer?

  l : layer?

procedure

(kerning-round k)  kerning/c

  k : kerning/c

procedure

(glyph-round g)  glyph?

  g : glyph?

procedure

(advance-round a)  advance?

  a : advance?

procedure

(image-round i)  image?

  i : image?

procedure

(anchor-round a)  anchor?

  a : anchor?

procedure

(contour-round c)  contour?

  c : contour?

procedure

(component-round c)  component?

  c : component?

procedure

(guideline-round g)  guideline?

  g : guideline?

procedure

(point-round p)  point?

  p : point?
Round coordinates. This may be necessary for font editors that do not accept non-integer numbers for coordinates.

1.1.5 Contours, components, anchors and guidelines

procedure

(map-contours proc o)  (listof any/c)

  proc : (-> contour? any/c)
  o : (or/c layer? glyph?)

procedure

(for-each-contours proc o)  void?

  proc : (-> contour? any)
  o : (or/c layer? glyph?)
Like map and for-each, but apply the procedure to every contour in a layer or, if used with a glyph to the foreground layer of the glyph.

procedure

(map-components proc o)  (listof any/c)

  proc : (-> component? any/c)
  o : (or/c layer? glyph?)

procedure

(for-each-components proc o)  void?

  proc : (-> component? any)
  o : (or/c layer? glyph?)
Like map and for-each, but apply the procedure to every component in a layer or, if used with a glyph to the foreground layer of the glyph.

procedure

(map-anchors proc o)  (listof any/c)

  proc : (-> anchor? any/c)
  o : (or/c layer? glyph?)

procedure

(for-each-anchors proc o)  void?

  proc : (-> anchor? any)
  o : (or/c layer? glyph?)
Like map and for-each, but apply the procedure to every anchor in a layer or, if used with a glyph to the foreground layer of the glyph.

procedure

(map-guidelines proc o)  (listof any/c)

  proc : (-> guideline? any/c)
  o : (or/c layer? glyph?)

procedure

(for-each-guidelines proc o)  void?

  proc : (-> guideline? any)
  o : (or/c layer? glyph?)
Like map and for-each, but apply the procedure to every guideline in a layer or, if used with a glyph to the foreground layer of the glyph.

procedure

(map-points proc o)  (listof any/c)

  proc : (-> point? any/c)
  o : (or/c layer? glyph?)

procedure

(for-each-points proc o)  void?

  proc : (-> point? any)
  o : (or/c layer? glyph?)
Like map and for-each, but apply the procedure to every point in a contour.

procedure

(contour->bezier c)  bezier/c

  c : contour?
Produces a cubic bezier/c from a contour. Line segments are transformed in cubic bezier segments where the first control point is equal to the start point and the second control point is equal to the end point.

procedure

(bezier->contour b)  contour?

  b : bezier/c
Produces a contour from a bezier/c.

procedure

(component->outlines c g)  (listof contour?)

  c : component?
  g : glyph?
Apply the transformation matrix of the component to the base glyph. Produces a list of contours.

procedure

(contour-open? c)  boolean?

  c : contour?
True if the contour is open (ie. if the contour starts with a move point.

procedure

(reverse-contour c)  contour?

  c : contour?
Produces a contour with points in the reverse order.

procedure

(glyph-reverse-directions g)  glyph?

  g : glyph?
Produces a glyph with contours reversed.

procedure

(glyph-correct-directions g)  glyph?

  g : glyph?
Produces a glyph following the postscript convention.

1.1.6 kerning

procedure

(kern-groups2->3 f)  font?

  f : font?
Produces a new font with kerning groups names following the UFO3 convention.

procedure

(kerning-group-names f)

  (cons/c (listof name/c) (listof name/c))
  f : font?
Produces the kerning groups names in a pair of lists. The car is for left groups, the cdr for right groups.

procedure

(valid-kerning-group-name? g side)  boolean?

  g : name/c
  side : (or/c 'left 'right)
True if the name is a valid kerning group name for the given side. UFO3 requires left groups to start with public.kern1. and right groups with public.kern2. .

procedure

(left-kerning-group? g)  boolean?

  g : name/c
True if the name is a valid left kerning group name. UFO3 requires left groups to start with public.kern1. .

procedure

(right-kerning-group? g)  boolean?

  g : name/c
True if the name is a valid right kerning group name. UFO3 requires right groups to start with public.kern2. .

procedure

(kerning-group? g)  boolean?

  g : name/c
True if the name is a kerning group name.

procedure

(update-kerning-group-name g side)  name/c

  g : name/c
  side : (or/c 'left 'right)
Produce a new name with the appropriate prefix.

procedure

(lookup-kerning-pair f left right)  
real? boolean?
  f : font?
  left : name/c
  right : name/c
Find the kerning for the pair left and right in the font. The second value returned is a boolean that signals if the kerning pair is defined or not. If the kerning pair is not defined the kerning for that pair is always zero. The kerning pair lookup doesn’t check possible conflicts, it always returns the first value found (first glyphs pairs, then group glyph pairs, finally group group pairs).

procedure

(kerning-value f left right)  real?

  f : font?
  left : name/c
  right : name/c
Find the kerning for the pair left and right in the font (this is the first value prouced by lookup-kerning-pair). If the kerning pair is not defined the kerning for that pair is always zero.

procedure

(map-kerning proc k)  kerning/c

  proc : (-> real? real?)
  k : kerning/c
Apply the procedure to every kerning pair, produce a new kerning table with the updated kerning values.

procedure

(sorted-kerning-list k)

  (listof (cons/c name/c (listof (cons/c name/c real?))))
  k : kerning/c
Produces an association list that repressents the kerning table sorted according sorted according the kerning levels hierarchy defined by the UFO specifications: the first level is for group combinations, then group-glyph (or glyph-group) combinations, finally glyph combinations; this means that group names precede glyph names.

1.1.7 More

procedure

(glyph-signed-area g f sides)  real?

  g : glyph?
  f : font?
  sides : natural-number/c
(glyph-signed-area g sides)  real?
  g : glyph?
  sides : natural-number/c
Produces the area of the glyph (if font s provided components will be considered too). If the directions of a contour is counterclockwise the ’signed’ area will be positive, therefore if the sum of ’signed’ areas for all contours in the glyph is negative, contours direction is almost certainly wrong for postscript fonts.

procedure

(lowercase-stems f)  real?

  f : font?
Produces the width of lowercase stems from the glyph n, if the font hasn’t a glyph named n an error is raised.

procedure

(uppercase-stems f)  real?

  f : font?
Produces the width of uppercase stems from the glyph H, if the font hasn’t a glyph named H an error is raised.

procedure

(correct-directions f)  font?

  f : font?
Produces a new font for which every glyph has a positive signed area.

procedure

(print-glyph f gn)  pict?

  f : font?
  gn : name/c
Print the font’s glyph named gn at the REPL. Actually, by evaluating an expression that produce a glyph, the glyph is showed at the REPL, with print-glyph, however, the right vertical metrics of the fonts can be used.