3

I'm using Diagrams library to generate some text with a custom font. Looking at the documentation for Text it is not clear how can one specify particular .ttf font file? I tried using different .ttf files like in the example below, but no one worked.

discountText :: Maybe Int -> Diagram B
discountText discount =
  case discount of
    Nothing -> mempty
    Just value ->
      text (show value ++ " %") # font "Roboto-Light.ttf" # fontSize 90

Another alternative seems to be SVGFonts, but I would like to achieve this without another library. So how do you guys set custom fonts in Diagrams?

UPD: I find the answer given below very explanatory. To solve the problem I actually ended up using SVGFonts which adds extra step of converting .ttf into .svg, but does the job.

Roman
  • 2,079
  • 4
  • 35
  • 53

1 Answers1

4

Font handling in Diagrams is highly dependent on the backend. diagrams-cairo should be able to use any TTF font on your system. On Linux, it will look up names using FontConfig. It should use the OS-provided font lookup mechanism on other OSes, but I can't recall ever testing this myself.

What backend are you planning to use? I'm partial to diagrams-cairo for text handling, but it's the most difficult backend to install, especially on not-Linux. It's likely other backends have improved since the last time I tested their text handling, also.

bergey
  • 3,041
  • 11
  • 17
  • I'm using Rasterific, since I'm working with different image formats. – Roman Jun 30 '18 at 22:32
  • I'm not certain, but looking at `diagram-rasterific` code for [text rendering](https://github.com/diagrams/diagrams-rasterific/blob/master/src/Diagrams/Backend/Rasterific.hs#L369) and [font selection](https://github.com/diagrams/diagrams-rasterific/blob/master/src/Diagrams/Backend/Rasterific/Text.hs#L69), I think you may need to recompile `diagrams-rasterfic` with your font, or otherwise modify it. – bergey Jul 01 '18 at 11:30