How to add new fonts?


I would like to know how do I add new
fonts to root?

Can I use unicode fonts, such as arialuni.ttf?
It seem that copy a ttf file to /usr/share/root/fonts/
won’t work.

I found that there are some arial*.ttf in
/usr/share/root/fonts/, but fonts list in
TLatex->SetTextAttributes do not shown
any arial fonts, is that normal?



The TTF fonts ROOT is using are in $ROOTSYS/fonts . Any *.ttf file put in that directory can be accessed by :

Int_t TTF::SetTextFont(const char *fontname)

which access the font by name. In the $ROOTSYS/fonts directory there is already several fonts files which are not used by ROOT. For instance: BlackChancery.ttf

Now, in TAttText the fonts are accessed by a number between 1 and 14 which correspond to the following table:

  1 : timesi.ttf                                       
  2 : timesbd.ttf
  3 : timesi.ttf
  4 : arial.ttf
  5 : ariali.ttf
  6 : arialbd.ttf
  7 : arialbi.ttf
  8 : cour.ttf
  9 : couri.ttf
 10 : courbd.ttf
 11 : courbi.ttf
 12 : symbol.ttf
 13 : times.ttf
 14 : wingding.ttf

So the other fonts cannot be accessed because they are not in this table. May be adding the possibility to set font by name in TAttText could solve this issue. I will check.


I got some problem.

Int_t TTF::SetTextFont(const char *fontname)

what is the fontname?
arialuni.ttf or “Arial Unicode MS”?

I put that line in my file before a TGraph->Draw,
an error shown:
Error: unrecognized language construct …

Thanks again.

The font name is the file name. But as I said it will not works. Changes are needed.

With Qt-layer on you can take advantage of the Qt API … bstitution to provide your own font to replace the standard ROOT font name. However, it is done globally. I.e. such substitution affects all your TCanvas / TPad instances and it does NOT change the PostScript output.

I read through this discussion, and was sad to see the limitation of the 14-entry font table. But I discovered an easy hack is to simply copy the desired font over the equivalent Arial font in $ROOTSYS/fonts. For examples, I copied comic.ttf into arial.ttf, arialb.ttf, etc. Now all text appears as Comic Sans on the plots within ROOT.

My main concern, though, is that I’d like to be able to change the font used in the PDF output, and the same approach doesn’t work there. I’ve had some luck, though, with simply editing the PDF as a text file and replacing the references to Helvetica with, for example, the corresponding MinionPro fonts. The only immediately apparent problem there is that the character spacing is still based on the previous font.

That the point. With the 14 fonts now available we ensure the correspondence between screen, PDF and PS. PDF and PS have there own way to deal with fonts. Changing the fonts the way you did has no effect on PDF nor on PS.

I would like to draw the attention of the ROOT team to the fact that there may be licensing issues with the current set of fonts. For instance, times.ttf indicates :

[quote]This software is a valuable asset of Monotype. Unless you have entered into a specific license agreement granting you additional rights, your use of this software is limited to your workstation for your own publishing use. You may not copy or distribute this software.[/quote]So unless there is an agreement between the ROOT team and Monotype, there must thus be a licensing issue for ROOT to be distributed with the times font. And anyway there must be an issue for ROOT users to publish documents using this font.

I suggest switching to free fonts.

I now understand better the choice of these 14 fonts, which happen to be the so-called “base 14 fonts”, that is fonts that do not need to be embedded into PDF documents because they were chosen by Adobe in this purpose. I’m not sure about the license issue then, it’s not clear.

Anyway, in case someone would be interested in changing the font in a PS, EPS or PDF file, here is a quick and dirty trick :

  • PS file : Edit the file with a text editor and replace all the occurrences of the unwanted font (the default being Helvetica-Bold for ROOT figures) by the name of the wanted one (for instance LiberationSerif-Regular), then save the file.
  • EPS file : Same process, but for some reason you have to remove the four following lines at the beginning of the file :

/reEncode {exch findfont dup length dict begin {1 index /FID eq {pop pop} {def} ifelse } forall /Encoding exch def currentdict end dup /FontName get exch definefont pop } def [/Times-Bold /Times-Italic /Times-BoldItalic /Helvetica /Helvetica-Oblique /UnPilgi /UnPilgiOblique /Courier /Courier-Oblique /Courier-Bold /Courier-BoldOblique /Times-Roman /AvantGarde-Book /AvantGarde-BookOblique /AvantGarde-Demi /AvantGarde-DemiOblique /Bookman-Demi /Bookman-DemiItalic /Bookman-Light /Bookman-LightItalic /Helvetica-Narrow /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique /Helvetica-Narrow-Oblique /NewCenturySchlbk-Roman /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic /NewCenturySchlbk-Italic /Palatino-Bold /Palatino-BoldItalic /Palatino-Italic /Palatino-Roman ] {ISOLatin1Encoding reEncode } forall

  • PDF file : Same process as for PS files.

Notes :

  • The name of the font to use is the name of the corresponding ttf file in /usr/share/fonts, that is without the ttf extension but with the same letter case and dashes.
  • This won’t work for every fonts. For instance, I cannot manage to get it working with Computer Modern (cmr10).
  • This is a trial and error workaround, I’m not sure I understand what it actually does. I mean, this is not what I would call “font embedding” since it is just a call to some existing font on the system, and not even ps fonts, so I have not tried yet but it will probably not display correctly on another computer (hopefully it will fall back to default fonts though).

Edit : Ok, now I got the embedding part. So if you want for instance to use a Computer Modern font (LaTeX’s default), just compile a tex file into a ps one, edit it with a text editor, copy the lines between “%%BeginFont: CMR10” (for Computer Modern, aka CMR) and “%%EndFont” and paste them between the “%%BeginSetup” and “%%EndSetup” lines of the PS or EPS file you want to use it. Then change the font inside your file to be CMR10 as explained above. Beware that for small figures it may dramatically increase the file size. Beware also that all the needed characters should be defined in the file from which you take the font, otherwise they will appear as blank spaces.

Edit2 : Just noticed that this solution had already been given by jklukas, and also have the spacing issue. The workaround would be either to manually adjust spaces beforehand, which is very painful, or to manually adjust them inside the PS/EPS file, which is still painful though a bit less IMO.