You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultFontConfig.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.apps.FOPException;
  25. import org.apache.fop.configuration.Configuration;
  26. import org.apache.fop.configuration.ConfigurationException;
  27. import org.apache.fop.events.EventProducer;
  28. import org.apache.fop.util.LogUtil;
  29. /**
  30. * The font configuration data for the more generic fonts such as TTF and Type1, that are used by
  31. * most the renderers.
  32. */
  33. public final class DefaultFontConfig implements FontConfig {
  34. private static final Log log = LogFactory.getLog(DefaultFontConfig.class);
  35. private final List<Directory> directories = new ArrayList<Directory>();
  36. private final List<Font> fonts = new ArrayList<Font>();
  37. private final List<String> referencedFontFamilies = new ArrayList<String>();
  38. private final boolean autoDetectFonts;
  39. private DefaultFontConfig(boolean autoDetectFonts) {
  40. this.autoDetectFonts = autoDetectFonts;
  41. }
  42. /**
  43. * Parses the morge generic font information.
  44. */
  45. public static final class DefaultFontConfigParser implements FontConfig.FontConfigParser {
  46. /**
  47. * Parses the font configuration and return the configuration object.
  48. *
  49. * @param cfg the configuration data
  50. * @param strict whether or not to enforce strict validation
  51. * @return the font configuration object
  52. * @throws FOPException if an error occurs when creating the configuration object
  53. */
  54. public DefaultFontConfig parse(Configuration cfg, boolean strict) throws FOPException {
  55. return new ParserHelper(cfg, strict).instance;
  56. }
  57. /** {@inheritDoc} */
  58. public DefaultFontConfig parse(Configuration cfg, boolean strict,
  59. FontEventAdapter eventAdapter) throws FOPException {
  60. return new ParserHelper(cfg, strict, eventAdapter).instance;
  61. }
  62. /** {@inheritDoc} */
  63. public FontConfig parse(Configuration cfg, FontManager fontManager, boolean strict,
  64. EventProducer eventProducer) throws FOPException {
  65. return parse(cfg, strict);
  66. }
  67. }
  68. private static final class ParserHelper {
  69. private boolean strict;
  70. private Configuration config;
  71. private Configuration fontInfoCfg;
  72. private FontEventAdapter eventAdapter;
  73. private DefaultFontConfig instance;
  74. private ParserHelper(Configuration cfg, boolean strict) throws FOPException {
  75. this(cfg, strict, null);
  76. }
  77. private ParserHelper(Configuration cfg, boolean strict, FontEventAdapter eventAdapter)
  78. throws FOPException {
  79. this.eventAdapter = eventAdapter;
  80. if (cfg == null || cfg.getChild("fonts", false) == null) {
  81. instance = null;
  82. } else {
  83. this.strict = strict;
  84. this.config = cfg;
  85. this.fontInfoCfg = cfg.getChild("fonts", false);
  86. instance = new DefaultFontConfig(fontInfoCfg.getChild("auto-detect", false) != null);
  87. parse();
  88. }
  89. }
  90. private void parse() throws FOPException {
  91. parseFonts();
  92. parseReferencedFonts();
  93. parseDirectories();
  94. }
  95. private void parseFonts() throws FOPException {
  96. for (Configuration fontCfg : fontInfoCfg.getChildren("font")) {
  97. String embed = fontCfg.getAttribute("embed-url", null);
  98. if (embed == null) {
  99. LogUtil.handleError(log, "Font configuration without embed-url attribute",
  100. strict);
  101. continue;
  102. }
  103. Font font = new Font(fontCfg.getAttribute("metrics-url", null), embed,
  104. fontCfg.getAttribute("embed-url-afm", null),
  105. fontCfg.getAttribute("embed-url-pfm", null),
  106. fontCfg.getAttribute("sub-font", null),
  107. fontCfg.getAttributeAsBoolean("kerning", true),
  108. fontCfg.getAttributeAsBoolean("advanced", true),
  109. fontCfg.getAttribute("encoding-mode", EncodingMode.AUTO.getName()),
  110. fontCfg.getAttribute("embedding-mode", EncodingMode.AUTO.getName()),
  111. fontCfg.getAttributeAsBoolean("simulate-style", false),
  112. fontCfg.getAttributeAsBoolean("embed-as-type1", false));
  113. instance.fonts.add(font);
  114. boolean hasTriplets = false;
  115. for (Configuration tripletCfg : fontCfg.getChildren("font-triplet")) {
  116. FontTriplet fontTriplet = getFontTriplet(tripletCfg, strict);
  117. font.tripletList.add(fontTriplet);
  118. hasTriplets = true;
  119. }
  120. // no font triplet info
  121. if (!hasTriplets) {
  122. LogUtil.handleError(log, "font without font-triplet", strict);
  123. }
  124. try {
  125. if (eventAdapter != null && font.getSimulateStyle()
  126. && !config.getAttribute("mime").equals("application/pdf")) {
  127. eventAdapter.fontFeatureNotSuppprted(this, "simulate-style", "PDF");
  128. }
  129. if (eventAdapter != null && font.getEmbedAsType1()
  130. && !config.getAttribute("mime").equals("application/postscript")) {
  131. throw new FOPException("The embed-as-type1 attribute is only supported in postscript");
  132. }
  133. } catch (ConfigurationException ex) {
  134. LogUtil.handleException(log, ex, true);
  135. }
  136. }
  137. }
  138. private void parseReferencedFonts() throws FOPException {
  139. Configuration referencedFontsCfg = fontInfoCfg.getChild("referenced-fonts", false);
  140. if (referencedFontsCfg != null) {
  141. for (Configuration match : referencedFontsCfg.getChildren("match")) {
  142. try {
  143. instance.referencedFontFamilies.add(match.getAttribute("font-family"));
  144. } catch (ConfigurationException ce) {
  145. LogUtil.handleException(log, ce, strict);
  146. continue;
  147. }
  148. }
  149. }
  150. }
  151. private void parseDirectories() throws FOPException {
  152. for (Configuration directoriesCfg : fontInfoCfg.getChildren("directory")) {
  153. boolean recursive = directoriesCfg.getAttributeAsBoolean("recursive", false);
  154. String directory;
  155. try {
  156. directory = directoriesCfg.getValue();
  157. } catch (ConfigurationException e) {
  158. LogUtil.handleException(log, e, strict);
  159. continue;
  160. }
  161. if (directory == null) {
  162. LogUtil.handleException(log,
  163. new FOPException("directory defined without value"), strict);
  164. continue;
  165. }
  166. instance.directories.add(new Directory(directory, recursive));
  167. }
  168. }
  169. /**
  170. * Creates a new FontTriplet given a triple Configuration
  171. *
  172. * @param tripletCfg a triplet configuration
  173. * @return a font triplet font key
  174. * @throws FOPException thrown if a FOP exception occurs
  175. */
  176. private FontTriplet getFontTriplet(Configuration tripletCfg, boolean strict)
  177. throws FOPException {
  178. try {
  179. String name = tripletCfg.getAttribute("name");
  180. if (name == null) {
  181. LogUtil.handleError(log, "font-triplet without name", strict);
  182. return null;
  183. }
  184. String weightStr = tripletCfg.getAttribute("weight");
  185. if (weightStr == null) {
  186. LogUtil.handleError(log, "font-triplet without weight", strict);
  187. return null;
  188. }
  189. int weight = FontUtil.parseCSS2FontWeight(FontUtil.stripWhiteSpace(weightStr));
  190. String style = tripletCfg.getAttribute("style");
  191. if (style == null) {
  192. LogUtil.handleError(log, "font-triplet without style", strict);
  193. return null;
  194. } else {
  195. style = FontUtil.stripWhiteSpace(style);
  196. }
  197. return FontInfo.createFontKey(name, style, weight);
  198. } catch (ConfigurationException e) {
  199. LogUtil.handleException(log, e, strict);
  200. }
  201. return null;
  202. }
  203. }
  204. /**
  205. * Returns the list of fonts that were parsed.
  206. * @return a list of fonts
  207. */
  208. public List<Font> getFonts() {
  209. return Collections.unmodifiableList(fonts);
  210. }
  211. /**
  212. * Returns a list of directories that were parsed.
  213. * @return a list of directories
  214. */
  215. public List<Directory> getDirectories() {
  216. return Collections.unmodifiableList(directories);
  217. }
  218. /**
  219. * Returns a list of referenced font families.
  220. * @return the referenced font families
  221. */
  222. public List<String> getReferencedFontFamily() {
  223. return Collections.unmodifiableList(referencedFontFamilies);
  224. }
  225. /**
  226. * Whether or not to enable auto-detecting of fonts in the system.
  227. * @return true to enable auto-detect
  228. */
  229. public boolean isAutoDetectFonts() {
  230. return autoDetectFonts;
  231. }
  232. /**
  233. * The directory to find fonts within.
  234. */
  235. public static final class Directory {
  236. private final String directory;
  237. private final boolean recursive;
  238. private Directory(String directory, boolean recurse) {
  239. this.directory = directory;
  240. this.recursive = recurse;
  241. }
  242. /**
  243. * Returns a String representing the directory to find fonts within.
  244. * @return the directory
  245. */
  246. public String getDirectory() {
  247. return directory;
  248. }
  249. /**
  250. * Returns whether or not to recurse through the directory when finding fonts.
  251. * @return true to recurse through the directory and sub-directories
  252. */
  253. public boolean isRecursive() {
  254. return recursive;
  255. }
  256. }
  257. /**
  258. * Represents a font object within the FOP conf.
  259. */
  260. public static final class Font {
  261. private final String metrics;
  262. private final String embedUri;
  263. private String afm;
  264. private String pfm;
  265. private final String subFont;
  266. private final boolean kerning;
  267. private final boolean advanced;
  268. private final String encodingMode;
  269. private final String embeddingMode;
  270. public String getEncodingMode() {
  271. return encodingMode;
  272. }
  273. private final boolean embedAsType1;
  274. private final boolean simulateStyle;
  275. private final List<FontTriplet> tripletList = new ArrayList<FontTriplet>();
  276. public List<FontTriplet> getTripletList() {
  277. return Collections.unmodifiableList(tripletList);
  278. }
  279. private Font(String metrics, String embed, String afm, String pfm, String subFont, boolean kerning,
  280. boolean advanced, String encodingMode, String embeddingMode, boolean simulateStyle,
  281. boolean embedAsType1) {
  282. this.metrics = metrics;
  283. this.embedUri = embed;
  284. this.afm = afm;
  285. this.pfm = pfm;
  286. this.subFont = subFont;
  287. this.kerning = kerning;
  288. this.advanced = advanced;
  289. this.encodingMode = encodingMode;
  290. this.embeddingMode = embeddingMode;
  291. this.simulateStyle = simulateStyle;
  292. this.embedAsType1 = embedAsType1;
  293. }
  294. /**
  295. * Whether or not to allow kerning of glyphs.
  296. * @return true to allow glyph kerning
  297. */
  298. public boolean isKerning() {
  299. return kerning;
  300. }
  301. public boolean isAdvanced() {
  302. return advanced;
  303. }
  304. /**
  305. * Gets the String representing the metrics file.
  306. * @return the metrics file
  307. */
  308. public String getMetrics() {
  309. return metrics;
  310. }
  311. /**
  312. * Gets the URI of the font to embed.
  313. * @return the font URI
  314. */
  315. public String getEmbedURI() {
  316. return embedUri;
  317. }
  318. /**
  319. * Gets the sub font within, for example, a TTC.
  320. * @return the sub font name
  321. */
  322. public String getSubFont() {
  323. return subFont;
  324. }
  325. public String getEmbeddingMode() {
  326. return embeddingMode;
  327. }
  328. public String getAfm() {
  329. return afm;
  330. }
  331. public String getPfm() {
  332. return pfm;
  333. }
  334. public boolean getSimulateStyle() {
  335. return simulateStyle;
  336. }
  337. public boolean getEmbedAsType1() {
  338. return embedAsType1;
  339. }
  340. }
  341. }