View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2012-2014 ForgeRock AS
15   */
16  
17  package org.forgerock.doc.maven.pre;
18  
19  import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
20  import static org.twdata.maven.mojoexecutor.MojoExecutor.name;
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.forgerock.doc.maven.AbstractDocbkxMojo;
25  import org.twdata.maven.mojoexecutor.MojoExecutor;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.net.URL;
30  
31  /**
32   * Prepare fonts for use with Apache FOP.
33   */
34  public final class Fop {
35  
36      /**
37       * The Mojo that holds configuration and related methods.
38       */
39      private AbstractDocbkxMojo m;
40  
41      /**
42       * The Executor to run docbkx-tools.
43       */
44      private final Executor executor;
45  
46      /**
47       * Constructor setting the Mojo that holds the configuration.
48       *
49       * @param mojo The Mojo that holds the configuration.
50       */
51      public Fop(final AbstractDocbkxMojo mojo) {
52          m = mojo;
53          this.executor = new Executor();
54      }
55  
56      /**
57       * Prepare fonts for use with Apache FOP.
58       *
59       * @throws MojoExecutionException Failed to prepare to use FOP.
60       */
61      public void execute() throws MojoExecutionException {
62          executor.copyFonts();
63          executor.generateFontMetrics();
64      }
65  
66      /**
67       * Return a fonts element that includes all the custom fonts.
68       *
69       * @param fontDir Directory containing the custom fonts.
70       * @return Fonts element.
71       */
72      public static MojoExecutor.Element getFontsElement(final String fontDir) {
73          return element(
74                  name("fonts"),
75                  element(name("font"),
76                          element(name("name"), "DejaVuSans"),
77                          element(name("style"), "normal"),
78                          element(name("weight"), "normal"),
79                          element(name("embedFile"), fontDir + "/DejaVuSans.ttf"),
80                          element(name("metricsFile"), fontDir + "/DejaVuSans-metrics.xml")),
81                  element(name("font"),
82                          element(name("name"), "DejaVuSans"),
83                          element(name("style"), "normal"),
84                          element(name("weight"), "bold"),
85                          element(name("embedFile"), fontDir + "/DejaVuSansCondensed-Bold.ttf"),
86                          element(name("metricsFile"), fontDir + "/DejaVuSansCondensed-Bold-metrics.xml")),
87                  element(name("font"),
88                          element(name("name"), "DejaVuSans"),
89                          element(name("style"), "italic"),
90                          element(name("weight"), "normal"),
91                          element(name("embedFile"), fontDir + "/DejaVuSans-Oblique.ttf"),
92                          element(name("metricsFile"), fontDir + "/DejaVuSans-Oblique-metrics.xml")),
93                  element(name("font"),
94                          element(name("name"), "DejaVuSans"),
95                          element(name("style"), "italic"),
96                          element(name("weight"), "bold"),
97                          element(name("embedFile"), fontDir + "/DejaVuSansCondensed-BoldOblique.ttf"),
98                          element(name("metricsFile"), fontDir + "/DejaVuSansCondensed-BoldOblique-metrics.xml")),
99                  element(name("font"),
100                         element(name("name"), "DejaVuSansMono"),
101                         element(name("style"), "normal"),
102                         element(name("weight"), "normal"),
103                         element(name("embedFile"), fontDir + "/DejaVuSansMono.ttf"),
104                         element(name("metricsFile"), fontDir + "/DejaVuSansMono-metrics.xml")),
105                 element(name("font"),
106                         element(name("name"), "DejaVuSansMono"),
107                         element(name("style"), "normal"),
108                         element(name("weight"), "bold"),
109                         element(name("embedFile"), fontDir + "/DejaVuSansMono-Bold.ttf"),
110                         element(name("metricsFile"), fontDir + "/DejaVuSansMono-Bold-metrics.xml")),
111                 element(name("font"),
112                         element(name("name"), "DejaVuSansMono"),
113                         element(name("style"), "italic"),
114                         element(name("weight"), "normal"),
115                         element(name("embedFile"), fontDir + "/DejaVuSansMono-Oblique.ttf"),
116                         element(name("metricsFile"), fontDir + "/DejaVuSansMono-Oblique-metrics.xml")),
117                 element(name("font"),
118                         element(name("name"), "DejaVuSansMono"),
119                         element(name("style"), "italic"),
120                         element(name("weight"), "bold"),
121                         element(name("embedFile"), fontDir + "/DejaVuSansMono-BoldOblique.ttf"),
122                         element(name("metricsFile"), fontDir + "/DejaVuSansMono-BoldOblique-metrics.xml")),
123                 element(name("font"),
124                         element(name("name"), "DejaVuSerif"),
125                         element(name("style"), "normal"),
126                         element(name("weight"), "normal"),
127                         element(name("embedFile"), fontDir + "/DejaVuSerif.ttf"),
128                         element(name("metricsFile"), fontDir + "/DejaVuSerif-metrics.xml")),
129                 element(name("font"),
130                         element(name("name"), "DejaVuSerif"),
131                         element(name("style"), "normal"),
132                         element(name("weight"), "bold"),
133                         element(name("embedFile"), fontDir + "/DejaVuSerifCondensed-Bold.ttf"),
134                         element(name("metricsFile"), fontDir + "/DejaVuSerifCondensed-Bold-metrics.xml")),
135                 element(name("font"),
136                         element(name("name"), "DejaVuSerif"),
137                         element(name("style"), "italic"),
138                         element(name("weight"), "normal"),
139                         element(name("embedFile"), fontDir + "/DejaVuSerif-Italic.ttf"),
140                         element(name("metricsFile"), fontDir + "/DejaVuSerif-Italic-metrics.xml")),
141                 element(name("font"),
142                         element(name("name"), "DejaVuSerif"),
143                         element(name("style"), "italic"),
144                         element(name("weight"), "bold"),
145                         element(name("embedFile"), fontDir + "/DejaVuSerifCondensed-BoldItalic.ttf"),
146                         element(name("metricsFile"), fontDir + "/DejaVuSerifCondensed-BoldItalic-metrics.xml")));
147     }
148 
149     /**
150      * Enclose methods to run plugins.
151      */
152     class Executor extends MojoExecutor {
153 
154         /**
155          * Copy fonts for use when generating FO output.
156          *
157          * @throws MojoExecutionException Failed to copy fonts.
158          */
159         public void copyFonts() throws MojoExecutionException {
160             String[] fonts = {"/fonts/DejaVuSans-Oblique.ttf",
161                 "/fonts/DejaVuSans.ttf",
162                 "/fonts/DejaVuSansCondensed-Bold.ttf",
163                 "/fonts/DejaVuSansCondensed-BoldOblique.ttf",
164                 "/fonts/DejaVuSansMono-Bold.ttf",
165                 "/fonts/DejaVuSansMono-BoldOblique.ttf",
166                 "/fonts/DejaVuSansMono-Oblique.ttf",
167                 "/fonts/DejaVuSansMono.ttf",
168                 "/fonts/DejaVuSerif-Italic.ttf",
169                 "/fonts/DejaVuSerif.ttf",
170                 "/fonts/DejaVuSerifCondensed-Bold.ttf",
171                 "/fonts/DejaVuSerifCondensed-BoldItalic.ttf"};
172 
173             for (String font : fonts) {
174                 final URL source = getClass().getResource(font);
175                 final File destination = new File(
176                         m.getBuildDirectory(), font.replaceAll("/", File.separator));
177                 try {
178                     FileUtils.copyURLToFile(source, destination);
179                 } catch (IOException e) {
180                     throw new MojoExecutionException(
181                             "Failed to copy file: " + font + ". " + e.getMessage());
182                 }
183             }
184         }
185 
186         /**
187          * Generate font metrics files.
188          *
189          * @throws MojoExecutionException Failed to generate font metrics.
190          */
191         public void generateFontMetrics() throws MojoExecutionException {
192 
193             final String fontsDir = m.path(m.getFontsDirectory());
194 
195             executeMojo(
196                     plugin(
197                             groupId("com.agilejava.docbkx"),
198                             artifactId("docbkx-fop-support"),
199                             version(m.getDocbkxVersion())),
200                     goal("generate"),
201                     configuration(
202                             element(name("ansi"), m.useAnsi()),
203                             element(name("sourceDirectory"), fontsDir),
204                             element(name("targetDirectory"), fontsDir)),
205                     executionEnvironment(m.getProject(), m.getSession(), m.getPluginManager()));
206         }
207     }
208 }