wiki Wiki | contuct me Contuct me |

Menu

V.4. 日本語フォントを使えるようにする

Apache FOPはデフォルトの状態では日本語フォントが含まれていないため、日本語を含む XSL-FO からPDF文書を作成した場合に、文字化けが発生します。 ここでは、日本語フォントを使えるようにするための設定を行います。

Apache FOPでデフォルトで用意してあるフォントは14種類です。 Helvetica (normal, bold, italic, bold italic), Times (normal, bold, italic, bold italic), Courier (normal, bold, italic, bold italic), Symbol and ZapfDingbats.

設定は以下の手順で行います。

  1. フォント・メトリクス・ファイルの作成

    日本語TrueTypeフォントからフォント情報を取り出して、XMLフォーマットの フォント・メトリクス・ファイルを作成します。

  2. userconfig.xml の修正

    XSL-FO で使用するフォント名とフォント・メトリクス・ファイル との関連付けを userconfig.xml に記述します。

詳細は FOP:Fonts を参照してください。

V.4.1. フォント・メトリクス・ファイルの作成

Apache FOPで使用できるフォントは Type1フォント TrueTypeフォント の2種類です。最初にWindowsXpで標準で用意されている「MSゴシック」と「MS明朝体」 のフォント・メトリックス・ファイルを作成します。

WindowsXpのインストール済みのフォントは C:/Windows/Fonts フォルダに 格納されています。「MSゴシック体」フォントは msgothic.ttc という名前で保管されていて、フォント名は「MSゴシック &MSPゴシック&MS UIGothic」、 「MS Gothic &MS PGothic & MS UIGothic」の2つのフォント名が表示されています。 「MS明朝体」も同様で、フォントのファイル名は msmincho.ttc でフォント名は「MS明朝 &MSP明朝」、「MS Mincho &MS PMincho」の2つのフォント名が あります。

1つのフォントファイルに対して2つのフォント名が定義されているようですが、いったいどちらの 名前でフォント・メトリックス・ファイルを作成すればいいのでしょう?

V.4.1.1. フォントファイルに含まれるフォント名の調査

調査するにはApache FOPに含まれる org.apache.fop.fonts.apps.TTFReader クラスを実行して調べます。Antで実行して調査しますので、 プロジェクト直下にビルド設定ファイルとして build-fop.xml ファイルを作成します。

例 V.1. TTFReaderを使ってフォント名を調査する

							
build-fop.xml
<project name="fop.docbook" basedir="." default="all">
  <!--
  =====================================================================
  ==
  == Apache FOP フォント定義 ビルドファイル
  ==
  == 目的:日本語フォントはデフォルトで登録されていないので、
  ==      フォント定義ファイルの作成などの処理を行う。
  ==
  == copyright (c) exacteye.com allright reserved.
  ==
  =====================================================================
  -->
  <property environment="env"/> 1
  <property name="font.path" value="${env.SystemRoot}/fonts"/> 2
  <property name="lib.dir" value="${basedir}/lib"/>
  <property name="fop.home" value="${basedir}/fop-0.20.5"/> 3
  <property name="fop.conf.dir" value="${fop.home}/conf"/>
  
  <path id="fop.class.path">
    <pathelement location="${fop.home}/build/fop.jar"/>
    <pathelement location="${fop.home}/lib/xercesImpl-2.2.1.jar"/>
    <pathelement location="${lib.dir}/xalan.jar"/> 4
    <!--
    このjar ファイルのバージョンが古かったのでFileNotExceptionが
    発生していた。
    <pathelement location="${fop.home}/lib/xalan-2.4.1.jar"/>
    -->
  </path>

  <target name="all">
    <antcall target="getMinchoFontName"/>
    <antcall target="getGothicFontName"/>
  </target>  
  <!--
  =====================================================================
  ==
  == わざとエラーを出しTTCファイルに含まれるフォント名を判別する
  == 調査用
  ==
  =====================================================================
  -->
  <!-- MS明朝体 --> 5
  <target name="getMinchoFontName">
    <echo message="-------------------------------------------------"/>
    <echo message="msmicho.ttcに含まれるフォント名の確認"/>
    <echo message="-------------------------------------------------"/>
    <java 
      classname="org.apache.fop.fonts.apps.TTFReader"
      args="${font.path}/msmincho.ttc test.xml" 
      >
      <classpath refid="fop.class.path"/>      
    </java>    
  </target>

  <!-- MSゴシック体 -->
  <target name="getGothicFontName">
    <echo message="-------------------------------------------------"/>
    <echo message="msgothic.ttcに含まれるフォント名の確認"/>
    <echo message="-------------------------------------------------"/>
    <java 
      classname="org.apache.fop.fonts.apps.TTFReader"
      args="${font.path}/msgothic.ttc test.xml" 
      >
      <classpath refid="fop.class.path"/>      
    </java>    
  </target>


</project>						
				
1

システムの環境変数を env という変数名で定義します。

2

フォントのインストールフォルダを定義します。

3

Apache FOPのインストールフォルダを定義します。

4

クラスパスの定義ですが、FOPの /lib フォルダにある xalan-2.4.1.jar を使用するとエラーが発生しますので 最新のxalan.jarをクラスパスに入れておきます。

5

このターゲットを実行すると msmincho.ttc に含まれる フォント名を表示します。通常は -ttcname [フォント名] というオプションを入れてフォント・メトリックス・ファイルを作成します。


getGothicFontNameターゲットを実行すると、 msgothic.ttc に含まれているフォント名が分かります。 MS Gothic,MS PGothic,MS UI Gothicがこの場合含まれているようです。

					
Buildfile: D:\project\docbook\build-fop.xml
getGothicFontName:
     [echo] -------------------------------------------------
     [echo] msgothic.ttcに含まれるフォント名の確認
     [echo] -------------------------------------------------
     [java] The args attribute is deprecated. Please use nested arg elements.
     [java] TTF Reader v1.1.1
     [java] Reading C:\WINDOWS/fonts/msgothic.ttc...
     [java] This is a TrueType collection file with 3 fonts
     [java] Containing the following fonts:
     [java] MS Gothic
     [java] MS PGothic
     [java] MS UI Gothic
     [java] java.lang.IllegalArgumentException: For TrueType collection you must 
specify which font to select (-ttcname)
     [java] at org.apache.fop.fonts.TTFFile.readFont(TTFFile.java:416)
     [java] at org.apache.fop.fonts.apps.TTFReader.loadTTF(TTFReader.java:222)
     [java] at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:184)
     ....
BUILD SUCCESSFUL
Total time: 2 seconds       
        
				

同様に getMinchoFontName ターゲットも実行してMS明朝体のフォント名 を確認します。MS Mincho, MS PMincho が含まれていました。

V.4.1.2. Antビルドファイルの修正

フォント名が分かったので先ほど作成した build-fop.xml ファイルを修正してフォント・メトリックス・ファイルを作成できるように修正します。

例 V.2. フォント・メトリックス・ファイルの作成

							
build-fop2.xml
  <!-- MS ゴシック体 -->
  <target name="confMSGothic">
    <java
      classname="org.apache.fop.fonts.apps.TTFReader">
      <arg line="-ttcname &quot;MS Gothic&quot; 1
${font.path}/msgothic.ttc ${fop.conf.dir}/msgothic.xml"/>
      <classpath refid="fop.class.path"/>
    </java>
  </target>
						
1

-ttcname [フォント名] [フォントファイル名] [フォント・メトリックス・ファイルの保存先] で指定してください。


[注意]注意

フォント名に半角空白文字が含まれている場合は前後に&quot; を入れてください。

Antビルドを実行すると、以下のような出力が得られるはずです。 以下は confMSGothic ターゲットを実行した結果です。

FOP_HOME/conf/ フォルダに mspgothic.xml というファイルが出来ていると思います。 こちら で確認できます。

					
Buildfile: D:\project\docbook\build-fop.xml
confMSGothic:
     [java] TTF Reader v1.1.1
     [java] Reading C:\WINDOWS/fonts/msgothic.ttc...
     [java] This is a TrueType collection file with 3 fonts
     [java] Containing the following fonts:
     [java] * MS Gothic
     [java] MS PGothic
     [java] MS UI Gothic
     [java] Number of glyphs in font: 20458
     [java] Postscript format 3
     [java] Creating xml font file...
     [java] Creating CID encoded metrics
     [java] Writing xml font file D:\project\docbook/fop-0.20.5/conf/msgothic.xml...
     [java] This font contains no embedding license restrictions
BUILD SUCCESSFUL
Total time: 3 seconds
      		
				

実行して出来たフォント・メトリックス・ファイルの一覧です。

V.4.2. userconfig.xmlの修正

フォント・メトリックス・ファイルは作成できたので、今度は XSL-FO に記述するフォント名と 実際のフォント・メトリックス・ファイルの関係を定義する必要があります。 Apache FOPでは FOP_HOME/conf/userconfig.xml に記述します。

[注意]注意

記述する際の注意

  • フォント名は自由につけてかまわない。

  • 一つのフォント・メトリックス・ファイルに対して複数のフォント名を指定できる。

  • フォント名に「MS明朝体」などの日本語名を指定する場合には、UTF-8の 文字コードで保存します。

以下のように記述します。

				
userconfig.xml
				
    <!-- 
    ===========================================================================
    ==
    == 日本語フォントの定義の追加
    ==
    ===========================================================================
     -->
    <!-- MS明朝体 -->
    <font
      metrics-file="D:\project\docbook\fop-0.20.5\conf\msmincho.xml"
      kerning="yes" embed-file="C:\WINDOWS\Fonts\msmincho.ttc">
      <font-triplet name="Mincho" style="normal" weight="normal" />
      <font-triplet name="Mincho" style="normal" weight="bold" />
      <font-triplet name="Mincho" style="italic" weight="normal" />
      <font-triplet name="Mincho" style="italic" weight="bold" />
      <font-triplet name="MS-Mincho" style="normal"
        weight="normal" />
      <font-triplet name="MS-Mincho" style="normal" weight="bold" />
      <font-triplet name="MS-Mincho" style="italic"
        weight="normal" />
      <font-triplet name="MS-Mincho" style="italic" weight="bold" />
      <font-triplet name="MSMincho" style="normal"
        weight="normal" />
      <font-triplet name="MSMincho" style="normal" weight="bold" />
      <font-triplet name="MSMincho" style="italic"
        weight="normal" />
      <font-triplet name="MSMincho" style="italic" weight="bold" />
      <font-triplet name="MS明朝" style="normal" weight="normal" />
      <font-triplet name="MS明朝" style="normal" weight="bold" />
      <font-triplet name="MS明朝" style="italic" weight="normal" />
      <font-triplet name="MS明朝" style="italic" weight="bold" />
    </font>
    
    <!-- MSP明朝体 -->
    <font
      metrics-file="D:\project\docbook\fop-0.20.5\conf\mspmincho.xml"
      kerning="yes" embed-file="C:\WINDOWS\Fonts\msmincho.ttc">
      <font-triplet name="PMincho" style="normal" weight="normal" />
      <font-triplet name="PMincho" style="normal" weight="bold" />
      <font-triplet name="PMincho" style="italic" weight="normal" />
      <font-triplet name="PMincho" style="italic" weight="bold" />
      <font-triplet name="MS-PMincho" style="normal"
        weight="normal" />
      <font-triplet name="MS-PMincho" style="normal"
        weight="bold" />
      <font-triplet name="MS-PMincho" style="italic"
        weight="normal" />
      <font-triplet name="MS-PMincho" style="italic"
        weight="bold" />
      <font-triplet name="MSPMincho" style="normal"
        weight="normal" />
      <font-triplet name="MSPMincho" style="normal" weight="bold" />
      <font-triplet name="MSPMincho" style="italic"
        weight="normal" />
      <font-triplet name="MSPMincho" style="italic" weight="bold" />
      <font-triplet name="MSP明朝" style="normal" weight="normal" />
      <font-triplet name="MSP明朝" style="normal" weight="bold" />
      <font-triplet name="MSP明朝" style="italic" weight="normal" />
      <font-triplet name="MSP明朝" style="italic" weight="bold" />
    </font>
    
    <!-- MSゴシック体 -->
    <font
      metrics-file="D:\project\docbook\fop-0.20.5\conf\msgothic.xml"
      kerning="yes" embed-file="C:\WINDOWS\Fonts\msgothic.ttc">
      <font-triplet name="Gothic" style="normal" weight="normal" />
      <font-triplet name="Gothic" style="normal" weight="bold" />
      <font-triplet name="Gothic" style="italic" weight="normal" />
      <font-triplet name="Gothic" style="italic" weight="bold" />
      <font-triplet name="MS-Gothic" style="normal"
        weight="normal" />
      <font-triplet name="MS-Gothic" style="normal" weight="bold" />
      <font-triplet name="MS-Gothic" style="italic"
        weight="normal" />
      <font-triplet name="MS-Gothic" style="italic" weight="bold" />
      <font-triplet name="MSGothic" style="normal"
        weight="normal" />
      <font-triplet name="MSGothic" style="normal" weight="bold" />
      <font-triplet name="MSGothic" style="italic"
        weight="normal" />
      <font-triplet name="MSGothic" style="italic" weight="bold" />
      <font-triplet name="MSゴシック" style="normal" weight="normal" />
      <font-triplet name="MSゴシック" style="normal" weight="bold" />
      <font-triplet name="MSゴシック" style="italic" weight="normal" />
      <font-triplet name="MSゴシック" style="italic" weight="bold" />
    </font>
    
    <!-- MSPゴシック体 -->
    <font
      metrics-file="D:\project\docbook\fop-0.20.5\conf\mspgothic.xml"
      kerning="yes" embed-file="C:\WINDOWS\Fonts\msgothic.ttc">
      <font-triplet name="PGothic" style="normal" weight="normal" />
      <font-triplet name="PGothic" style="normal" weight="bold" />
      <font-triplet name="PGothic" style="italic" weight="normal" />
      <font-triplet name="PGothic" style="italic" weight="bold" />
      <font-triplet name="MS-PGothic" style="normal"
        weight="normal" />
      <font-triplet name="MS-PGothic" style="normal"
        weight="bold" />
      <font-triplet name="MS-PGothic" style="italic"
        weight="normal" />
      <font-triplet name="MS-PGothic" style="italic"
        weight="bold" />
      <font-triplet name="MSPGothic" style="normal"
        weight="normal" />
      <font-triplet name="MSPGothic" style="normal" weight="bold" />
      <font-triplet name="MSPGothic" style="italic"
        weight="normal" />
      <font-triplet name="MSPGothic" style="italic" weight="bold" />
      <font-triplet name="MSPゴシック" style="normal" weight="normal" />
      <font-triplet name="MSPゴシック" style="normal" weight="bold" />
      <font-triplet name="MSPゴシック" style="italic" weight="normal" />
      <font-triplet name="MSPゴシック" style="italic" weight="bold" />
    </font>
    
    <!-- MSUIゴシック体 -->
    <font
      metrics-file="D:\project\docbook\fop-0.20.5\conf\msuigothic.xml"
      kerning="yes" embed-file="C:\WINDOWS\Fonts\msgothic.ttc">
      <font-triplet name="UIGothic" style="normal"
        weight="normal" />
      <font-triplet name="UIGothic" style="normal" weight="bold" />
      <font-triplet name="UIGothic" style="italic"
        weight="normal" />
      <font-triplet name="UIGothic" style="italic" weight="bold" />
      <font-triplet name="UI Gothic" style="normal"
        weight="normal" />
      <font-triplet name="UI Gothic" style="normal" weight="bold" />
      <font-triplet name="UI Gothic" style="italic"
        weight="normal" />
      <font-triplet name="UI Gothic" style="italic" weight="bold" />
      <font-triplet name="MS-UIGothic" style="normal"
        weight="normal" />
      <font-triplet name="MS-UIGothic" style="normal"
        weight="bold" />
      <font-triplet name="MS-UIGothic" style="italic"
        weight="normal" />
      <font-triplet name="MS-UIGothic" style="italic"
        weight="bold" />
      <font-triplet name="MSUIGothic" style="normal"
        weight="normal" />
      <font-triplet name="MSUIGothic" style="normal"
        weight="bold" />
      <font-triplet name="MSUIGothic" style="italic"
        weight="normal" />
      <font-triplet name="MSUIGothic" style="italic"
        weight="bold" />
      <font-triplet name="MSUIゴシック" style="normal"
        weight="normal" />
      <font-triplet name="MSUIゴシック" style="normal" weight="bold" />
      <font-triplet name="MSUIゴシック" style="italic"
        weight="normal" />
      <font-triplet name="MSUIゴシック" style="italic" weight="bold" />
    </font>
    		
			

V.4.3. 動作確認を行う。

実際に日本語フォントでPDF文書が作成できるか確認します。 hello.jp.fo というファイルを作成します。

				
hello.jp.fo
				
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <fo:layout-master-set>
    <fo:simple-page-master master-name="simple">
      <fo:region-body margin-top="3cm"/>
      <fo:region-before extent="3cm"/>
      <fo:region-after extent="1.5cm"/>
    </fo:simple-page-master>
  </fo:layout-master-set>

  <fo:page-sequence master-reference="simple">
    <fo:flow flow-name="xsl-region-body">
      <fo:block font-size="18pt" text-align="center" font-family="Mincho">
        ハロー (明朝体)
      </fo:block>
      <fo:block font-size="18pt" text-align="center" font-family="PMincho">
        ハロー (P明朝体)
      </fo:block>
      <fo:block font-size="18pt" text-align="center" font-family="Gothic">
        ハロー (MSゴシック)
      </fo:block>
      <fo:block font-size="18pt" text-align="center" font-family="PGothic">
        ハロー (MSPゴシック)
      </fo:block>
      <fo:block font-size="18pt" text-align="center" font-family="UI Gothic">
        ハロー (MSUIゴシック)
      </fo:block>
    </fo:flow>
  </fo:page-sequence>

</fo:root>
				
			

作成したら FOP_HOME/examples/fo/basic に保存します。

FOP コマンドで実際に実行してみます。

				
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

>cd /D D:\project\docbook\fop-0.20.5

>fop examples\fo\basic\hello.jp.fo hello.pdf -c conf\userconfig.xml
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] FOP 0.20.5
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] building formatting object tree
[INFO] setting up fonts
[INFO] [1]
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] Parsing of document complete, stopping renderer

D:\project\docbook\fop-0.20.5>
			
			
[注意]注意

実行時にオプションで -c conf\userconfig.xml を指定してください。

hello.pdf のように日本語がきちんと表示されているのを確認します。