001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2012-2014 ForgeRock AS 015 */ 016 017package org.forgerock.doc.maven.pre; 018 019import static org.twdata.maven.mojoexecutor.MojoExecutor.element; 020import static org.twdata.maven.mojoexecutor.MojoExecutor.name; 021 022import org.apache.commons.io.FileUtils; 023import org.apache.maven.plugin.MojoExecutionException; 024import org.forgerock.doc.maven.AbstractDocbkxMojo; 025import org.twdata.maven.mojoexecutor.MojoExecutor; 026 027import java.io.File; 028import java.io.IOException; 029import java.net.URL; 030 031/** 032 * Prepare fonts for use with Apache FOP. 033 */ 034public final class Fop { 035 036 /** 037 * The Mojo that holds configuration and related methods. 038 */ 039 private AbstractDocbkxMojo m; 040 041 /** 042 * The Executor to run docbkx-tools. 043 */ 044 private final Executor executor; 045 046 /** 047 * Constructor setting the Mojo that holds the configuration. 048 * 049 * @param mojo The Mojo that holds the configuration. 050 */ 051 public Fop(final AbstractDocbkxMojo mojo) { 052 m = mojo; 053 this.executor = new Executor(); 054 } 055 056 /** 057 * Prepare fonts for use with Apache FOP. 058 * 059 * @throws MojoExecutionException Failed to prepare to use FOP. 060 */ 061 public void execute() throws MojoExecutionException { 062 executor.copyFonts(); 063 executor.generateFontMetrics(); 064 } 065 066 /** 067 * Return a fonts element that includes all the custom fonts. 068 * 069 * @param fontDir Directory containing the custom fonts. 070 * @return Fonts element. 071 */ 072 public static MojoExecutor.Element getFontsElement(final String fontDir) { 073 return element( 074 name("fonts"), 075 element(name("font"), 076 element(name("name"), "DejaVuSans"), 077 element(name("style"), "normal"), 078 element(name("weight"), "normal"), 079 element(name("embedFile"), fontDir + "/DejaVuSans.ttf"), 080 element(name("metricsFile"), fontDir + "/DejaVuSans-metrics.xml")), 081 element(name("font"), 082 element(name("name"), "DejaVuSans"), 083 element(name("style"), "normal"), 084 element(name("weight"), "bold"), 085 element(name("embedFile"), fontDir + "/DejaVuSansCondensed-Bold.ttf"), 086 element(name("metricsFile"), fontDir + "/DejaVuSansCondensed-Bold-metrics.xml")), 087 element(name("font"), 088 element(name("name"), "DejaVuSans"), 089 element(name("style"), "italic"), 090 element(name("weight"), "normal"), 091 element(name("embedFile"), fontDir + "/DejaVuSans-Oblique.ttf"), 092 element(name("metricsFile"), fontDir + "/DejaVuSans-Oblique-metrics.xml")), 093 element(name("font"), 094 element(name("name"), "DejaVuSans"), 095 element(name("style"), "italic"), 096 element(name("weight"), "bold"), 097 element(name("embedFile"), fontDir + "/DejaVuSansCondensed-BoldOblique.ttf"), 098 element(name("metricsFile"), fontDir + "/DejaVuSansCondensed-BoldOblique-metrics.xml")), 099 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}