diagrams / SVGFonts

Fonts from the SVG-Font format
http://hackage.haskell.org/package/SVGFonts
Other
20 stars 12 forks source link

SVGFonts

Native font support for the Diagrams library. The SVG-Font format is easy to parse and was therefore chosen for a font library completely written in Haskell.

You can convert your own font to SVG with FontForge or use one of the SVG fonts included with the library.

Features

Complete implementation of the features that fontforge produces (but not the complete SVG format):

XML speed issues can be solved by trimming the svg file to only those characters that are used (or maybe binary xml one day)

Version 1.0 of this library supports texturing which would only make sense in a Diagrams Backend that does rasterization in Haskell.

Example

import Diagrams.Prelude
import Diagrams.Backend.SVG.CmdLine
import qualified Graphics.SVGFonts as F

main = do
  font <- F.loadFont "/path/to/font.svg"
  let
    diagram :: Diagram B
    diagram =
      (F.drop_rect$ F.fit_height 22$ F.svgText def{F.textFont = font} "Hello World!")
      # stroke # fc blue # lc blue # bg lightgrey # fillRule EvenOdd # showOrigin
  mainWith diagram

Usage

Convert your favourite font (i.e. .ttf) into a .svg file with fontforge (the menu item under "Save All"). If a font converted on your own doesn't work, try the repair options, and if this still doesn't work edit the file by hand or report an issue. Remember that a lot of fonts are not allowed to be distributed freely.

Porting to version 1.8

Version 1.8 of the library greatly improved the API but introduced a number of breaking changes.

Previously, functions provided by the library took a large TextOpts options record and directly produced a diagrams Path. For example, it was common to see code like this:

text' font h s = (strokeP $ textSVG' (TextOpts font INSIDE_H KERN False h h) s)
               # lw none # fc black

Compared to this, version 1.8:

The only things remaining in TextOpts are options for the font, spacing mode, and underline.

Here is an idiomatic way to translate the above example code into the new API:

text' font h s
  = s
  # svgText def { textFont = font }
  # fit_height h
  # set_envelope
  # lw none # fc black