メニュー > Java > Apache FOP > フォントの埋め込み

フォントの埋め込み - 日本語対応

(2006/09/11)

XMLに日本語が含まれる場合

 XMLに日本語が含まれる場合はどうなるのでしょうか。
 ここではXMLに
FOP_HOME\examples\embedding\xml\xml\projectteam.xmlを
 XSLに
FOP_HOME\examples\embedding\xml\xslt\projectteam2fo.xsl
を用います。
 ファイルに修正を加えるため、この2つのファイルを適当な場所にコピーしてください。

 ものは試しです。早速XMLファイル(projectteam.xml)に日本語を含めてみましょう。
<?xml version="1.0" encoding="UTF-8"?>
<projectteam>
  <projectname>キラーアプリケーション</projectname>
  <member>
    <name>三浦 太郎</name>
    <function>lead</function>
    <email>tarou@killerapp.fun</email>
  </member>
  <member>
    <name>Paul Coder</name>
    <function>dev</function>
    <email>paul.coder@killerapp.fun</email>
  </member>
  <member>
    <name>Max Hacker</name>
    <function>dev</function>
    <email>max.hacker@killerapp.fun</email>
  </member>
  <member>
    <name>Donna Book</name>
    <function>doc</function>
    <email>donna.book@killerapp.fun</email>
  </member>
  <member>
    <name>Henry Tester</name>
    <function>qa</function>
    <email>henry.tester@killerapp.fun</email>
  </member>
</projectteam>
赤字の部分が日本語に変更された箇所です。このファイルを用いてPDFを生成してみましょう。


 日本語の部分が#に文字化けしてしまいました。
 原因はPDFの文章のプロパティを見れば明白です。


 PDFに日本語フォントが含まれていません。文字化けするのも当然です。
 PDF作成時に日本語フォントを指定する必要がありそうです。

XMLフォントメトリクスファイルの生成

 FOPでの日本語フォント使用方法を解説します。
 フォントメトリクスファイルはFOPに含まれるTTFReaderを使って作成します。クラスパスの設定など面倒なので、fop.batをコピーして修正して使います。
@ECHO OFF

rem %~dp0 is the expanded pathname of the current script under NT
set LOCAL_FOP_HOME=
if "%OS%"=="Windows_NT" set LOCAL_FOP_HOME=%~dp0

set LIBDIR=%LOCAL_FOP_HOME%lib
set LOCALCLASSPATH=%LOCAL_FOP_HOME%build\fop.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xml-apis.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xercesImpl-2.2.1.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xalan-2.4.1.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\batik.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\avalon-framework-cvs-20020806.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jimi-1.0.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_core.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_codec.jar
java -cp "%LOCALCLASSPATH%" org.apache.fop.fonts.apps.TTFReader %1 %2 %3 %4 %5 %6 %7 %8
 この内容をfont.batファイルに保存します。

 早速使ってみましょう。msgothic.ttcとmsmincho.ttcの2つのフォントファイルからからフォントメトリクスファイルを生成します。

 ttcファイルには複数のフォントが含まれます。まずmsgothic.ttcとmsmincho.ttcに含まれるフォント名を列挙してみます。
msgothic.ttcの場合
D:\fop-0.20.5>font.bat C:\WINDOWS\Fonts\MSGOTHIC.TTC msgothic.xml
TTF Reader v1.1.1

Reading C:\WINDOWS\Fonts\MSGOTHIC.TTC...

This is a TrueType collection file with 3 fonts
Containing the following fonts:
MS Gothic
MS PGothic
MS UI Gothic
java.lang.IllegalArgumentException: For TrueType collection you must specify which font to select (-ttcname)
        at org.apache.fop.fonts.TTFFile.readFont(TTFFile.java:416)
        at org.apache.fop.fonts.apps.TTFReader.loadTTF(TTFReader.java:222)
        at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:184)
 msmincho.ttcの場合
D:\fop-0.20.5>font.bat C:\WINDOWS\Fonts\MSMINCHO.TTC msmincho.xml
TTF Reader v1.1.1

Reading C:\WINDOWS\Fonts\MSMINCHO.TTC...

This is a TrueType collection file with 2 fonts
Containing the following fonts:
MS Mincho
MS PMincho
java.lang.IllegalArgumentException: For TrueType collection you must specify which font to select (-ttcname)
        at org.apache.fop.fonts.TTFFile.readFont(TTFFile.java:416)
        at org.apache.fop.fonts.apps.TTFReader.loadTTF(TTFReader.java:222)
        at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:184)
 例外が発生しますが気にしなくても大丈夫です。
 これによりmsgothic.ttcにはMS Gothic、MS PGothic、MS UI Gothicがmsmincho.ttcにはMS Mincho、 MS PMinchoが含まれていることがわかります。

  フォント名がわかったところでフォントメトリクスファイルを作成します。フォントメトリクスファイルは以下のコマンドで生成されます。
font.bat -ttcname "フォント名" フォントファイル フォントメトリクスファイル
 実際にやってみましょう。msgothic.ttcからMS Gothicフォントのフォントメトリクスファイルを作成します。
D:\fop-0.20.5>font.bat -ttcname "MS Gothic" C:\WINDOWS\Fonts\MSGOTHIC.TTC msgothic.xml
TTF Reader v1.1.1

Reading C:\WINDOWS\Fonts\MSGOTHIC.TTC...

This is a TrueType collection file with 3 fonts
Containing the following fonts:
* MS Gothic
MS PGothic
MS UI Gothic
Number of glyphs in font: 20458
Postscript format 3
Creating xml font file...

Creating CID encoded metrics
Writing xml font file msgothic.xml...

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\D:\fop-0.20.5\msgothic.xml (ファイル名、ディレクトリ名、またはボリューム ラベルの構文が間違っています。)
        at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:263)
        at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
        at org.apache.fop.fonts.apps.TTFReader.writeFontXML(TTFReader.java:243)
        at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:196)
Caused by: java.io.FileNotFoundException: file:\D:\fop-0.20.5\msgothic.xml (ファイル名、ディレクトリ名、またはボリューム ラベルの構文が間違っています。)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:253)
... 3 more
---------
java.io.FileNotFoundException: file:\D:\fop-0.20.5\msgothic.xml (ファイル名、ディレクトリ名、またはボリューム ラベルの構文が間違っています。)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:253)
        at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
        at org.apache.fop.fonts.apps.TTFReader.writeFontXML(TTFReader.java:243)
        at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:196)
This font contains no embedding license restrictions
 なんとエラーが発生してしまいました。これはFOP0.20.5に含まれるXalanに問題があるようです。そこで最新のXalanを使用することにします。
 以下のサイトからXalanの最新版(2.7.0)を入手します。

http://xalan.apache.org/

 解凍したファイルから serializer.jar
xalan.jar
xercesImpl.jar
xml-apis.jar
をFOP_HOME\libに入れます。
 バッチファイルを以下のように修正します。
font.bat(修正前)
@ECHO OFF

rem %~dp0 is the expanded pathname of the current script under NT
set LOCAL_FOP_HOME=
if "%OS%"=="Windows_NT" set LOCAL_FOP_HOME=%~dp0

set LIBDIR=%LOCAL_FOP_HOME%lib
set LOCALCLASSPATH=%LOCAL_FOP_HOME%build\fop.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xml-apis.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xercesImpl-2.2.1.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xalan-2.4.1.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\batik.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\avalon-framework-cvs-20020806.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jimi-1.0.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_core.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_codec.jar
java -cp "%LOCALCLASSPATH%" org.apache.fop.fonts.apps.TTFReader %1 %2 %3 %4 %5 %6 %7 %8
↓font.bat(修正後)
@ECHO OFF

rem %~dp0 is the expanded pathname of the current script under NT
set LOCAL_FOP_HOME=
if "%OS%"=="Windows_NT" set LOCAL_FOP_HOME=%~dp0

set LIBDIR=%LOCAL_FOP_HOME%lib
set LOCALCLASSPATH=%LOCAL_FOP_HOME%build\fop.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xml-apis.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\serializer.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xercesImpl.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xalan.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\batik.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\avalon-framework-cvs-20020806.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jimi-1.0.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_core.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_codec.jar
java -cp "%LOCALCLASSPATH%" org.apache.fop.fonts.apps.TTFReader %1 %2 %3 %4 %5 %6 %7 %8
 忘れないうちにfop.batにも修正を入れておきます。

 再度フォントメトリクスファイルを作成してみます。
D:\fop-0.20.5>font.bat -ttcname "MS Gothic" C:\WINDOWS\Fonts\MSGOTHIC.TTC msgothic.xml
TTF Reader v1.1.1

Reading C:\WINDOWS\Fonts\MSGOTHIC.TTC...

This is a TrueType collection file with 3 fonts
Containing the following fonts:
* MS Gothic
MS PGothic
MS UI Gothic
Number of glyphs in font: 20458
Postscript format 3
Creating xml font file...

Creating CID encoded metrics
Writing xml font file msgothic.xml...

This font contains no embedding license restrictions
 今度はエラーを吐きません。実行フォルダにmsgothic.xmlファイルが生成されていれば成功です。

ユーザ定義ファイルの編集

 ユーザ定義ファイル(userconfig.xml)に日本語フォントに関する情報を追記します。userconfig.xmlはFOPの展開フォルダ\confにあります。
  <fonts>

    <font metrics-file="msgothic.xml" embed-file="C:\WINDOWS\Fonts\MSGOTHIC.TTC" kerning="yes">
        <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>
  <fonts>
</configuration>

XSLファイルの修正

 日本語フォントを使うにはXSLファイルで明示的に日本語フォントを指定する必要があります。FOP_HOME\examples\embedding\xml\xslt\projectteam2fo.xslに以下のような修正を加えます。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
  <!-- ========================= -->
  <!-- root element: projectteam -->
  <!-- ========================= -->
  <xsl:template match="projectteam">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simpleA4">
        <fo:flow flow-name="xsl-region-body">
          <fo:block font-family="Gothic" font-size="16pt" font-weight="bold" space-after="5mm">Project: <xsl:value-of select="projectname"/>
          </fo:block>
          <fo:block font-family="Gothic" font-size="10pt">
            <fo:table table-layout="fixed">
              <fo:table-column column-width="4cm"/>
              <fo:table-column column-width="4cm"/>
              <fo:table-column column-width="5cm"/>
              <fo:table-body>

実行

 コマンドラインからの実行は以下の通りになります。
D:\fop-0.20.5>fop.bat -c conf\userconfig.xml -xsl examples\embedding\xml\xslt\projectteam2fo.xsl -xml examples\embedding\xml\xml\projectteam.xml -pdf nihongo.pdf
[INFO] Using org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser as SAX2 Parser
[INFO] Using org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser as SAX2 Parser
[INFO] FOP 0.20.5
[INFO] Using org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser as SAX2 Parser
[INFO] building formatting object tree
[INFO] setting up fonts
[INFO] [1]
[INFO] Using org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser as SAX2 Parser
[INFO] Parsing of document complete, stopping renderer

 Javaプログラムの実装は以下の通りになります。
File xmlFile = new File("projectteam.xml"); // XMLファイル
File xslFile = new File("projectteam2fo.xsl"); // XSLファイル
File pdfFile = new File("projectteam.pdf"); // PDFの出力ファイル

File configFile = new File("conf\\userconfig.xml");
new Options(configFile);

InputStream xmlIn = new BufferedInputStream(new FileInputStream(xmlFile));
InputStream xslIn = new BufferedInputStream(new FileInputStream(xslFile));
OutputStream pdfOut = new BufferedOutputStream(new FileOutputStream(pdfFile));

TraxInputHandler inputHandler = new TraxInputHandler(new InputSource(xmlIn),
                                                     new InputSource(xslIn));

Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(pdfOut);

inputHandler.run(driver);

xmlIn.close();
xslIn.close();
pdfOut.close();
 上記のコードには例外処理が書かれておりませんので、実用には使えません。
 userconfig.xmlの読み込みはOptionsのコンストラクタを呼び出すだけで完了します。Optionsのインスタンスは不要です。




Copyright (C)2005-2009 Miura Computer Limited. ALL RIGHTS RESERVED.