SVGFonts

Fonts from the SVG-Font format

Version on this page:1.8.0.1
LTS Haskell 22.39:1.8.0.1@rev:1
Stackage Nightly 2024-10-31:1.8.0.1@rev:1
Latest on Hackage:1.8.0.1@rev:1

See all snapshots SVGFonts appears in

BSD-3-Clause licensed by Tillmann Vogt
Maintained by [email protected]
This version can be pinned in stack with:SVGFonts-1.8.0.1@sha256:66a29cebd825bdac6ed1da83bd3a310ba6e8db5d63d6511015d644b9a8ec48b8,3019

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):

  • Kerning (e.g. the two characters in “VA” have a shorter distance than in “VV”)
  • Unicode
  • Ligatures
  • Text boxes with syntax highlighting

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:

  • Introduces a new intermediate type PathInRect
  • Splits out width and height options into dedicated combinators such as fit_width and fit_height
  • There are also several new functions that allow specifying width and height in different ways, such as svgText_fitRect and svgText_fitRect_stretchySpace.

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
  • We use the svgText function with a default options record def, overriding the textFont field to set the font explicitly.
  • We then use the fit_height function to scale the resulting text so it has height h. This corresponds to our previous use of INSIDE_H. This is probably the most common mode, but fit_width and svgText_fitRect also available.
  • Finally, we call set_envelope which converts a PathInRect to a Diagram, by stroking the text path and appropriately setting the envelope.
  • If you actually want an explicit Path instead of a Diagram, you can call drop_rect instead of set_envelope.
  • Note that the old textSVG' function resulted in a centered local origin, whereas all the new API functions result in a local origin and the left end of the text baseline. If you need the text centered, you can of course call centerXY.

Changes

1.8.0.1 (1 Nov 2021)

  • Include a migration guide for updating to version 1.8.

1.8 (15 Oct 2021)

  • Graphics.SVGFonts and Graphics.SVGFonts.Text have been greatly refactored and many of the names have changed. The textSVG function is still provided for a bit of backwards compatibility.
  • New module Graphics.SVGFonts.Wrap for configurable text wrapping.

1.7.0.1-r2 (17 Sep 2021)

  • Bump upper bounds to build with GHC 9.0 and diagrams-core-1.5.

1.7.0.1 (29 Jan 2019)

  • Remove strange kerning values in LinLibertineCut.svg. (#29)
  • Remove non-ASCII character from LinLibertine.svg so users don’t have to worry about encoding/locale settings. (#30)

1.7 (9 May 2018)

  • Fixed a bug (Fontforge seems to use SVG namespace for some time, which it didn’t before)
  • unsafePerformIO removed
  • drop support for GHC 7.6
  • Build with GHC-8.4

1.6.0.3 (25 September 2017)

  • Fix link on Hackage page

1.6.0.2 (21 August 2017)

  • Bug fix: don’t print blank line to stdout when there are no errors. (#23) Thanks to Tim Docker for the fix.

1.6.0.1 (27 October 2016)

  • Allow diagrams-core-1.4
  • Allow diagrams-lib-1.4

1.6 (8 August 2016)

  • Performance improvement: port ReadPath to use attoparsec
  • New loadFont' function, to read font data from an XmlSource
  • Export Kern(..) from Graphics.SVGFonts.ReadFont
  • New Serialize instances for FontData and Kern

1.5.0.1 (6 June 2016)

  • allow base-4.9
  • allow data-default-class-0.1
  • test with GHC 8.0.1

1.5.0.0 (19 April 2015)

  • Split functionality out of ReadFont, into Fonts (built-in fonts) and Text (rendering text to Diagrams).
  • textSVG' and textSVG_ now have the text as a separate argument, distinct from TextOptions.
  • ReadFont does not use unsafePerformIO any more. unsafePerformIO is now only used to load built-in fonts.

1.4.0.3 (2 June 2014)

  • Allow diagrams-lib-1.2.

1.4.0.1 (25 November 2013)

  • Bump diagrams-lib upper bound to < 1.1.

1.4 (10 September 2013)

  • FontData can now be written back to SVG using makeSvgFont inside of the WriteFont module. It is possible to only write back a specfic set of glyphs.
  • The SVG font-face element is now completely supported. This means all possible attributes are read and written back to it. Correct defaults are also set on non optional attributes. Some optional attributes are still required though, due to their use in font rendering.
  • The ReadPath module does not use unsafePerformIO anymore. ReadFont is now the only module with unsafe calls.
  • Minor bug fixes:
    • stemh and stemv are now optional attributes.

1.3.0.2 (14 August 2013)

  • remove old comment causing Haddock build to fail

1.3.0.1 (12 August 2013)

  • fix repo location in .cabal file

1.3: 9 August 2013

  • A bunch of bug fixes, cleanup, and reorganization:
    • Proper data type for Kern and FontData instead of mega-tuples.
    • Fixed several Prelude.read error on font-face attributes.
    • Fixed wrong attribute names.
    • Switched to data-default-class instead of data-default.
  • Require diagrams-lib-0.7.