<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- $Id$ -->
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
<document>
  <header>
    <title>Apache™ FOP: Fonts</title>
    <version>$Revision$</version>
    <authors>
      <person name="Jeremias M&#xE4;rki" email=""/>
      <person name="Tore Engvig" email=""/>
      <person name="Adrian Cumiskey" email=""/>
      <person name="Max Berger" email=""/>
    </authors>
  </header>
  <body>
    <section id="intro">
      <title>Summary</title>
      <p>The following table summarizes the font capabilities of the various Apache� FOP renderers:</p>
      <table>
        <tr>
          <th>Renderer</th>
          <th>Base-14</th>
          <th>AWT/OS</th>
          <th>Custom</th>
          <th>Custom Embedding</th>
        </tr>
        <tr>
          <td>PDF</td>
          <td>yes</td>
          <td>no</td>
          <td>yes</td>
          <td>yes</td>
        </tr>
        <tr>
          <td>PostScript</td>
          <td>yes</td>
          <td>no</td>
          <td>yes</td>
          <td>yes</td>
        </tr>
        <tr>
          <td>PCL</td>
          <td>yes (modified)</td>
          <td>yes (painted as bitmaps)</td>
          <td>yes (painted as bitmaps)</td>
          <td>no</td>
        </tr>
        <tr>
          <td>AFP</td>
          <td>no</td>
          <td>no</td>
          <td>yes</td>
          <td>yes</td>
        </tr>
        <tr>
          <td>Java2D/AWT/Bitmap</td>
          <td>if available from OS</td>
          <td>yes</td>
          <td>yes</td>
          <td>n/a (display only)</td>
        </tr>
        <tr>
          <td>Print</td>
          <td>if available from OS</td>
          <td>yes</td>
          <td>yes</td>
          <td>controlled by OS printer driver</td>
        </tr>
        <tr>
          <td>RTF</td>
          <td>n/a (font metrics not needed)</td>
          <td>n/a</td>
          <td>n/a</td>
          <td>n/a</td>
        </tr>
        <tr>
          <td>TXT</td>
          <td>yes (used for layout but not for output)</td>
          <td>no</td>
          <td>yes (used for layout but not for output)</td>
          <td>no</td>
        </tr>
<!--tr> NOT AVAILABLE
          <td>MIF</td>
          <td>n/a (font metrics not needed)</td>
          <td>n/a</td>
          <td>n/a</td>
          <td>n/a</td>
        </tr-->
<!--tr> NOT AVAILABLE
          <td>SVG</td>
          <td>if available from OS</td>
          <td>yes</td>
          <td>no</td>
          <td>no</td>
        </tr-->
        <tr>
          <td>XML</td>
          <td>yes</td>
          <td>no</td>
          <td>yes</td>
          <td>n/a</td>
        </tr>
      </table>
    </section>
    <section>
      <title>Base-14 Fonts</title>
      <p>
        The Adobe PostScript and PDF Specification specify a set of 14 fonts that must be
        available to every PostScript interpreter and PDF reader:
        Helvetica (normal, bold, italic, bold italic),
        Times (normal, bold, italic, bold italic),
        Courier (normal, bold, italic, bold italic),
        Symbol and ZapfDingbats.
      </p>
      <p>
        The following font family names are hard-coded into FOP for the Base-14 font set:
      </p>
      <table>
        <tr>
          <th>Base-14 font</th>
          <th>font families</th>
        </tr>
        <tr>
          <td>Helvetica</td>
          <td>Helvetica, sans-serif, SansSerif</td>
        </tr>
        <tr>
          <td>Times</td>
          <td>Times, Times Roman, Times-Roman, serif, any</td>
        </tr>
        <tr>
          <td>Courier</td>
          <td>Courier, monospace, Monospaced</td>
        </tr>
        <tr>
          <td>Symbol</td>
          <td>Symbol</td>
        </tr>
        <tr>
          <td>ZapfDingbats</td>
          <td>ZapfDingbats</td>
        </tr>
      </table>
      <p>
        Please note that recent versions of Adobe Acrobat Reader replace
        "Helvetica" with "Arial" and "Times" with "Times New Roman" internally.
        GhostScript replaces "Helvetica" with "Nimbus Sans L" and "Times" with 
        "Nimbus Roman No9 L". Other document viewers may do similar font
        substitutions. If you need to make sure that there are no such
        substitutions, you need to specify an explicit font and embed it in
        the target document.
      </p>
    </section>
    <section id="missing-fonts">
      <title>Missing Fonts</title>
      <p>
        When FOP does not have a specific font at its disposal (because it's 
        not installed in the operating system or set up in FOP's configuration),
        the font is replaced with "any". "any" is internally mapped to the
        Base-14 font "Times" (see above).
      </p>
    </section>
    <section id="missing-glyphs">
      <title>Missing Glyphs</title>
      <p>
        Every font contains a particular set of
        <a href="http://en.wikipedia.org/wiki/Glyph">glyphs</a>. If no glyph can be found for
        a given character, FOP will issue a warning and use the glpyh for "#" (if available)
        instead. Before it does that, it consults a (currently hard-coded) registry of
        glyph substitution groups (see Glyphs.java in Apache XML Graphics Commons).
        This registry can supply alternative glyphs in some cases (like using space when a no-break
        space is requested). But there's no guarantee that the result will be as expected (for
        example, in the case of hyphens and similar glyphs). A better way is to use a font that
        has all the necessary glyphs. This glyph substitution is only a last resort.
      </p>
    </section>
    <section id="awt">
      <title>Java2D/AWT/Operating System Fonts</title>
      <p>
        The Java2D family of renderers (Java2D, AWT, Print, TIFF, PNG), use the
        Java AWT subsystem for font metric information. Through operating system
        registration, the AWT subsystem knows what fonts are available on the system,
        and the font metrics for each one.
      </p>
      <p>
        When working with one of these output formats and you're missing a font, just
        install it in your operating system and they should be available for these
        renderers. Please note that this is not true for other output formats such as
        PDF or PostScript.
      </p>
    </section>
    <section id="custom">
      <title>Custom Fonts</title>
      <p>
        Support for custom fonts is highly output format dependent (see above table).
        This section shows how to add Type 1 and TrueType fonts to the PDF, PostScript and
        Java2D-based renderers. Other renderers (like AFP) support other font formats. Details
        in this case can be found on the page about <a href="output.html">output formats</a>.
      </p>
      <p>
        In earlier FOP versions, it was always necessary to create an XML font metrics file
        if you wanted to add a custom font. This unconvenient step has been removed and in
        addition to that, FOP supports auto-registration of fonts, i.e. FOP can find fonts
        installed in your operating system or can scan user-specified directories for fonts.
        Font registration via XML font metrics file is still supported and may still be necessary
        for some very special cases as fallback variant while we stabilize font auto-detection.
      </p>
      <p>
        Basic information about fonts can be found at:
      </p>
      <ul>
        <li>
          <a href="http://partners.adobe.com/asn/developer/type/ftypes.html">Adobe font types</a>
        </li>
        <li>
          <a href="http://partners.adobe.com/asn/developer/technotes/fonts.html">Adobe Font Technote</a>
        </li>
      </ul>
    </section>
    <section id="basics">
      <title>Basic font configuration</title>
      <p>
        If you want FOP to use custom fonts, you need to tell it where to find them. This
        is done in the configuration file and once per renderer (because each output format
        is a little different). In the basic form, you can either tell FOP to find your
        operating system fonts or you can specify directories that it will search for
        support fonts. These fonts will then automatically be registered.
      </p>
      <source><![CDATA[
<renderers>
   <renderer mime="application/pdf">
      <fonts>
         <!-- register all the fonts found in a directory -->
         <directory>C:\MyFonts1</directory>
  
         <!-- register all the fonts found in a directory and all of its sub directories (use with care) -->
         <directory recursive="true">C:\MyFonts2</directory>

         <!-- automatically detect operating system installed fonts -->
         <auto-detect/>  
      </fonts>
   </renderer>
</renderers>]]></source>
      <note>
        Review the documentation for <a href="configuration.html">FOP Configuration</a>
        for instructions on making the FOP configuration available to FOP when it runs.
        Otherwise, FOP has no way of finding your custom font information. It is currently
        not possible to easily configure fonts from Java code.
      </note>
    </section>
    <section id="advanced">
      <title>Advanced font configuration</title>
      <p>
        The instructions found above should be sufficient for most users. Below are some
        additional instructions in case the basic font configuration doesn't lead to
        the desired results.
      </p>
      <section id="type1-metrics">
        <title>Type 1 Font Metrics</title>
        <p>FOP includes PFMReader, which reads the PFM file that normally comes with a Type 1 font, and generates an appropriate font metrics file for it.
          To use it, run the class org.apache.fop.fonts.apps.PFMReader:</p>
        <p>Windows:</p>
        <source>java -cp build\fop.jar;lib\avalon-framework.jar;lib\commons-logging.jar;lib\commons-io.jar
          org.apache.fop.fonts.apps.PFMReader [options] pfm-file xml-file</source>
        <p>Unix:</p>
        <source>java -cp build/fop.jar:lib/avalon-framework.jar:lib/commons-logging.jar:lib/commons-io.jar
          org.apache.fop.fonts.apps.PFMReader [options] pfm-file xml-file</source>
        <p>PFMReader [options]:</p>
        <ul>
          <li><strong>-fn &lt;fontname&gt;</strong> By default, FOP uses the fontname from the
            .pfm file when embedding the font. Use the "-fn" option to override this name with one you have
            chosen. This may be useful in some cases to ensure that applications using the output document
            (Acrobat Reader for example) use the embedded font instead of a local font with the same
            name.</li>
        </ul>
        <note>The classpath in the above example has been simplified for readability.
          You will have to adjust the classpath to the names of the actual JAR files in the lib directory.
          xml-apis.jar, xercesImpl.jar, xalan.jar and serializer.jar are not necessary for JDK version 1.4 or later.</note>
        <note>The tool will construct some values (FontBBox, StemV and ItalicAngle) based on assumptions and calculations which are only an approximation to the real values.
          FontBBox and Italic Angle can be found in the human-readable part of the PFB file or in the AFM file.
          The PFMReader tool does not yet interpret PFB or AFM files, so if you want to be correct, you may have to adjust the values in the XML file manually.
          The constructed values however appear to have no visible influence.</note>
      </section>
      <section id="truetype-metrics">
        <title>TrueType Font Metrics</title>
        <p>FOP includes TTFReader, which reads the TTF file and generates an appropriate font metrics file for it.
          Use it in a similar manner to PFMReader.
          For example, to create such a metrics file in Windows from the TrueType font at c:\myfonts\cmr10.ttf:</p>
        <source>java -cp build\fop.jar;lib\avalon-framework.jar;lib\commons-logging.jar;lib\commons-io.jar
          org.apache.fop.fonts.apps.TTFReader [options]
          C:\myfonts\cmr10.ttf ttfcm.xml</source>
        <p>TTFReader [options]:</p>
        <ul>
          <li><strong>-d &lt;DEBUG | INFO &gt;</strong> Sets the debug level (default is
            INFO).</li>
          <li><strong>-fn &lt;fontname&gt;</strong> Same as for PFMReader.</li>
          <li><strong>-ttcname &lt;fontname&gt;</strong> If you're reading data from a
            TrueType Collection (.ttc file) you must specify which font from the collection you will read
            metrics from.
            If you read from a .ttc file without this option, the fontnames will be listed for you.</li>
          <li><strong>-enc ansi</strong> Creates a WinAnsi-encoded font metrics file.
            Without this option, a CID-keyed font metrics file is created.
            The table below summarizes the differences between these two encoding options as currently
            used within FOP.
            Please note that this information only applies to TrueType fonts and TrueType collections:</li>
        </ul>
        <table id="ttf-encoding">
          <tr>
            <th>Issue</th>
            <th>WinAnsi</th>
            <th>CID-keyed</th>
          </tr>
          <tr>
            <td>Usable Character Set</td>
            <td>Limited to WinAnsi character set, which is roughly equivalent to iso-8889-1.</td>
            <td>Limited only by the characters in the font itself.</td>
          </tr>
          <tr>
            <td>Embedding the Font</td>
            <td>Optional.</td>
            <td>Mandatory. Not embedding the font produces invalid PDF documents.</td>
          </tr>
        </table>
        <warning>
          You may experience failures with certain TrueType fonts, especially if they don't contain 
          the so-called Unicode "cmap" table. TTFReader can currently not deal with font like this.
        </warning>
      </section>
      <section id="truetype-collections-metrics">
        <title>TrueType Collections</title>
        <p>TrueType collections (.ttc files) contain more than one font.
          To create metrics files for these fonts, you must specify which font in the collection should be generated, by using the "-ttcname" option with the TTFReader.</p>
        <p>To get a list of the fonts in a collection, just start the TTFReader as if it were a normal TrueType file (without the -ttcname option).
          It will display all of the font names and exit with an Exception.</p>
        <p>Here is an example of generating a metrics file for a .ttc file:</p>
        <source>java -cp build\fop.jar;lib\avalon-framework.jar;lib\commons-logging.jar;lib\commons-io.jar
          org.apache.fop.fonts.apps.TTFReader -ttcname "MS Mincho"
          msmincho.ttc msminch.xml</source>
        <p>
          Alternatively, the individual sub-fonts of a TrueType Collections can be selected
          using the "sub-font" attribute on the "font" element. That means that generating
          an XML font metrics file for TrueType collections is not necessary anymore. Example:
        </p>
        <source><![CDATA[<font embed-url="gulim.ttc" sub-font="GulimChe">
  <font-triplet name="GulimChe" style="normal" weight="normal"/>
</font>]]></source>
      </section>
      <section id="register">
        <title>Register Fonts with FOP</title>
        <p>You must tell FOP how to find and use the font metrics files by registering them in the <a href="configuration.html">FOP Configuration</a>. Add entries for your custom fonts, regardless of font type, to the configuration file in a manner similar to the following:</p>
        <source><![CDATA[
<renderers>
   <renderer mime="application/pdf">
      <fonts>
         <!-- register a particular font -->
         <font metrics-url="file:///C:/myfonts/FTL_____.xml" kerning="yes"
           embed-url="file:///C:/myfonts/FTL_____.pfb"
           encoding-mode="single-byte">
            <font-triplet name="FrutigerLight" style="normal" weight="normal"/>
         </font>
  
         <!-- register all the fonts found in a directory -->
         <directory>C:\MyFonts1</directory>
  
         <!-- register all the fonts found in a directory and all of its sub directories (use with care) -->
         <directory recursive="true">C:\MyFonts2</directory>

         <!-- automatically detect operating system installed fonts -->
         <auto-detect/>  
      </fonts>
   </renderer>
</renderers>]]></source>
        <ul>
          <li>
            URLs are used to access the font metric and font files.
            Relative URLs are resolved relative to the font-base property (or base) if available.
            See <a href="configuration.html">FOP: Configuration</a> for more information.
          </li>
          <li>The "metrics-url" attribute is generally not necessary except if you run into problems with certain fonts.</li>
          <li>Either an "embed-url" or a "metrics-url" must be specified for font tag configurations.</li>
          <li>The font "kerning" attribute is optional. Default is "true".</li>
          <li>If embedding is off (i.e. embed-url is not set), the output will position the text correctly (from the metrics file), but it will not be displayed or printed correctly unless the viewer has the applicable font available to their local system.</li>
          <li>When setting the "embed-url" attribute for Type 1 fonts, be sure to specify the PFB (actual font data), not PFM (font metrics) file that you used to generate the XML font metrics file.</li>
          <li>The attribute "encoding-mode" is optional an may have the following values:
            <ul>
              <li>auto: default font encoding mode ("cid" for Truetype, "single-byte" for Type 1)</li>
              <li>single-byte: use single-byte encodings in the target format (if applicable)</li>
              <li>cid: encode as CID-keyed font (currently only supported for PDF output with TrueType fonts)</li>
            </ul>
          </li>
          <li>The fonts "directory" tag can be used to register fonts contained within a single or list of directory paths.  The "recursive" attribute can be specified to recursively add fonts from all sub directories.</li>
          <li>The fonts "auto-detect" tag can be used to automatically register fonts that are found to be installed on the native operating system.</li>
          <li>Fonts registered with "font" tag configurations override fonts found by means of "directory" tag definitions.</li>
          <li>Fonts found as a result of a "directory" tag configuration override fonts found as a result of the "auto-detect" tag being specified.</li>
          <li>
            If relative URLs are specified, they are evaluated relative to the value of the 
            "font-base" setting. If there is no "font-base" setting, the fonts are evaluated 
            relative to the base directory.
          </li>
        </ul>
<!--note>Cocoon users will need to setup the config, see FOPSerializer for more information.</note-->
      </section>
      <section id="autodetect">
        <title>Auto-Detect and auto-embed feature</title>
        <p>When the "auto-detect" flag is set in the configuration, FOP will automatically search for fonts in the default paths for your operating system.</p>
        <p>FOP will also auto-detect fonts which are available in the classpath, if they are described as "application/x-font" in the MANIFEST.MF file. For example, if your .jar file contains font/myfont.ttf:</p>
        <source>Manifest-Version: 1.0
          
          Name: font/myfont.ttf
          Content-Type: application/x-font</source>
        <p>This feature allows you to create JAR files containing fonts. The JAR files can be added to fop by providem them in the classpath, e.g. copying them into the lib/ directory.</p>
        <section id="font-cache">
          <title>The font cache</title>
          <p>
            Apache FOP maintains a cache file that is used to speed up auto-detection. This file
            is usually found in the ".fop" directory under the user's home directory. It's called
            "fop-fonts.cache". When the user's home directory is not writable, the font cache file
            is put in the directory for temporary files.
          </p>
          <p>
            If there was a problem loading a particular font, it is flagged in the cache file so
            it is not loaded anymore. So, if a font is actually around but is still not found
            by Apache FOP, it's worth a try to delete the font cache file which forces Apache FOP
            to reparse all fonts.
          </p>
        </section>
      </section>
      <section id="embedding">
        <title>Embedding</title>
        <p>
          By default, all fonts are embedded if an output format supports font embedding. In some
          cases, however, it is preferred that some fonts are only referenced. When working
          with referenced fonts it is important to be in control of the target environment where
          the produced document is consumed, i.e. the necessary fonts have to be installed there.
        </p>
        <p>
          There are two different ways how you can specify that a font should be referenced:
        </p>
        <ol>
          <li>
            When using the old-style "font" element to configure a single font, font referencing
            is controlled by the embed-url attribute. If you don't specify the embed-url attribute
            the font will not be embedded, but will only be referenced.
          </li>
          <li>
            For automatically configured fonts there's a different mechanism to specify which
            fonts should be referenced rather than embedded. This is done in the "referenced-fonts"
            element in the configuration. Here's an example:
          </li>
        </ol>
        <source><![CDATA[
<fop version="1.0">
   <fonts>
      <referenced-fonts>
        <match font-family="Helvetica"/>
        <match font-family="DejaVu.*"/>
      </referenced-fonts>
   </fonts>
</fop>]]></source>
        <p>
          At the moment, you can only match fonts against their font-family. It is possible to use
          regular expressions as is shown in the second example above ("DejaVu.*"). The syntax for
          the regular expressions used here are the one used by the
          <a href="http://java.sun.com/j2se/1.4/docs/api/java/util/regex/package-summary.html"><code>java.util.regex</code> package</a>.
          So, in the above snippet "Helvetica" and all variants of the "DejaVu" font family are
          referenced. If you want to reference all fonts, just specify <code>font-family=".*"</code>.
        </p>
        <p>
          The <code>referenced-fonts</code> element can be placed either inside the general
          <code>fonts</code> element (right under the root) or in the <code>fonts</code> element
          under the renderer configuration. In the first case, matches apply to all renderers.
          In the second case, matches only apply to the renderer where the element was specified.
          Both cases can be used at the same time.
        </p>
        <p>
          Various notes related to embedded fonts:
        </p>
        <ul>
          <li>The PostScript renderer does not yet support TrueType fonts, but can embed Type 1 fonts.</li>
          <li>The font is simply embedded into the PDF file, it is not converted.</li>
          <li>When FOP embeds a font, it adds a prefix to the fontname to ensure that the name will not match the fontname of an installed font.
            This is helpful with older versions of Acrobat Reader that preferred installed fonts over embedded fonts.</li>
          <li>When embedding PostScript fonts, the entire font is always embedded.</li>
          <li>When embedding TrueType fonts (ttf) or TrueType Collections (ttc), a subset of the
          original font, containing only the glyphs used, is embedded in the output document.
          That's the default, but if you specify encoding-mode="single-byte" (see above), the
          complete font is embedded.</li>
        </ul>
      </section>
      <section id="substitution">
        <title>Substitution</title>
        <p>When a &lt;substitutions/&gt; section is defined in the configuration, FOP will re-map any font-family references found in your FO input to a given substitution font.</p>
        <ul>
          <li>If a &lt;substitution/&gt; is declared, it is mandatory that both a &lt;from/&gt; and &lt;to/&gt; child element is declared with a font-family attribute.</li>
          <li>Both font-weight and font-style are optional attributes, if they are provided then a value of 'normal' is assumed.</li>
        </ul>
        <p>For example you could make all FO font-family references to 'Arial' with weights between 700 and 900 reference the normal 'Arial Black' font.</p>
        <source><![CDATA[
<fop version="1.0">
   <fonts>
      <substitutions>
         <substitution>
            <from font-family="Arial" font-weight="700..900"/>
            <to font-family="Arial Black"/>
         </substitution>
         <substitution>
            <from font-family="FrutigerLight"/>
            <to font-family="Times" font-weight="bold" font-style="italic"/>
         </substitution>
      </substitutions>
   </fonts>
</fop>]]></source>
      </section>
<!-- The following section should no longer be required
      <section id="embedding-base14">
        <title>Explicitly embedding the base 14 fonts</title>
        <p>
          There are cases where you might want to force the embedding of one or more of the base 14 fonts that
          can normally be considered available on the target platform (viewer, printer). One of these cases is
          <a href="pdfa.html">PDF/A</a> which mandates the embedding of even the base 14 fonts. Embedding a font such as Helvetica or
          Courier is straight-forward. The "Symbol" and "ZapfDingbats" fonts, however, currently present a 
          problem because FOP cannot correctly determine the encoding of these two single-byte fonts through
          the PFM file. FOP now correctly interprets the "encoding" value in the XML font metrics file, but the
          PFMReader application writes "UnknownEncoding" to the generated XML file. In order to embed "Symbol"
          and "ZapfDingbats" you have to manually change the XML font metrics file and specify "SymbolEncoding"
          or "ZapfdingbatsEncoding" encoding respectively as the value for the "encoding" element.
        </p>
        <p>Example:</p>
        <source><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<font-metrics type="TYPE1">
  <font-name>Symbol</font-name>
  <embed/>
  <encoding>SymbolEncoding</encoding>
  <cap-height>673</cap-height>
  <x-height>766</x-height>
  [..]]]></source>
      </section-->
    </section>
    <section id="selection">
      <title>Font Selection Strategies</title>
      <p>
          There are two font selection strategies: character-by-character or auto. The default is auto.</p>
      <p>Auto selected the first font from the list which is able to display the most characters in a given word. This means (assume font A has characters for abclmn, font B for lnmxyz, fontlist is A,B):</p>
      <ul>
        <li>aaa lll xxx would be displayed in fonts A A B</li>
        <li>aaaxx would be displayed in font A</li>
        <li>aaaxxx would be displayed in font A</li>
        <li>aaaxxxx would be displayed in font B</li>
      </ul>
      <p>Character-by-Character is NOT yet supported!</p>
    </section>
    <section id="font-list">
      <title>Font List Command-Line Tool</title>
      <p>
        FOP contains a small command-line tool that lets you generate a list of all configured
        fonts. Its class name is: <code>org.apache.fop.tools.fontlist.FontListMain</code>.
        Run it with the "-?" parameter to get help for the various options.
      </p>
    </section>
  </body>
</document>