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.

FontLoader.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.fop.fonts.truetype.TTFFontLoader;
  29. import org.apache.fop.fonts.type1.Type1FontLoader;
  30. /**
  31. * Base class for font loaders.
  32. */
  33. public abstract class FontLoader {
  34. /** logging instance */
  35. protected static final Log log = LogFactory.getLog(FontLoader.class);
  36. /** URI representing the font file */
  37. protected String fontFileURI = null;
  38. /** the FontResolver to use for font URI resolution */
  39. protected FontResolver resolver = null;
  40. /** the loaded font */
  41. protected CustomFont returnFont = null;
  42. /** true if the font has been loaded */
  43. protected boolean loaded = false;
  44. /** true if the font will be embedded, false if it will be referenced only. */
  45. protected boolean embedded = true;
  46. /** true if kerning information shall be loaded if available. */
  47. protected boolean useKerning = true;
  48. /**
  49. * Default constructor.
  50. * @param fontFileURI the URI to the PFB file of a Type 1 font
  51. * @param embedded indicates whether the font is embedded or referenced
  52. * @param useKerning indicates whether kerning information shall be loaded if available
  53. * @param resolver the font resolver used to resolve URIs
  54. */
  55. public FontLoader(String fontFileURI, boolean embedded, boolean useKerning,
  56. FontResolver resolver) {
  57. this.fontFileURI = fontFileURI;
  58. this.embedded = embedded;
  59. this.useKerning = useKerning;
  60. this.resolver = resolver;
  61. }
  62. private static boolean isType1(String fontURI) {
  63. return fontURI.toLowerCase().endsWith(".pfb");
  64. }
  65. /**
  66. * Loads a custom font from a File. In the case of Type 1 fonts, the PFB file must be specified.
  67. * @param fontFile the File representation of the font
  68. * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
  69. * @param embedded indicates whether the font is embedded or referenced
  70. * @param embeddingMode the embedding mode
  71. * @param encodingMode the requested encoding mode
  72. * @param resolver the font resolver to use when resolving URIs
  73. * @return the newly loaded font
  74. * @throws IOException In case of an I/O error
  75. */
  76. public static CustomFont loadFont(File fontFile, String subFontName,
  77. boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode,
  78. FontResolver resolver) throws IOException {
  79. return loadFont(fontFile.toURI().toURL(), subFontName,
  80. embedded, embeddingMode, encodingMode, resolver);
  81. }
  82. /**
  83. * Loads a custom font from an URL. In the case of Type 1 fonts, the PFB file must be specified.
  84. * @param fontUrl the URL representation of the font
  85. * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
  86. * @param embedded indicates whether the font is embedded or referenced
  87. * @param embeddingMode the embedding mode of the font
  88. * @param encodingMode the requested encoding mode
  89. * @param resolver the font resolver to use when resolving URIs
  90. * @return the newly loaded font
  91. * @throws IOException In case of an I/O error
  92. */
  93. public static CustomFont loadFont(URL fontUrl, String subFontName,
  94. boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode,
  95. FontResolver resolver) throws IOException {
  96. return loadFont(fontUrl.toExternalForm(), subFontName,
  97. embedded, embeddingMode, encodingMode, true,
  98. resolver);
  99. }
  100. /**
  101. * Loads a custom font from a URI. In the case of Type 1 fonts, the PFB file must be specified.
  102. * @param fontFileURI the URI to the font
  103. * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
  104. * @param embedded indicates whether the font is embedded or referenced
  105. * @param embeddingMode the embedding mode of the font
  106. * @param encodingMode the requested encoding mode
  107. * @param useKerning indicates whether kerning information should be loaded if available
  108. * @param resolver the font resolver to use when resolving URIs
  109. * @return the newly loaded font
  110. * @throws IOException In case of an I/O error
  111. */
  112. public static CustomFont loadFont(String fontFileURI, String subFontName,
  113. boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode,
  114. boolean useKerning, FontResolver resolver) throws IOException {
  115. fontFileURI = fontFileURI.trim();
  116. boolean type1 = isType1(fontFileURI);
  117. FontLoader loader;
  118. if (type1) {
  119. if (encodingMode == EncodingMode.CID) {
  120. throw new IllegalArgumentException(
  121. "CID encoding mode not supported for Type 1 fonts");
  122. }
  123. if (embeddingMode == EmbeddingMode.SUBSET) {
  124. throw new IllegalArgumentException(
  125. "Subset embedding for Type 1 fonts is not supported");
  126. }
  127. loader = new Type1FontLoader(fontFileURI, embedded, useKerning, resolver);
  128. } else {
  129. loader = new TTFFontLoader(fontFileURI, subFontName,
  130. embedded, embeddingMode, encodingMode, useKerning, resolver);
  131. }
  132. return loader.getFont();
  133. }
  134. /**
  135. * Opens a font URI and returns an input stream.
  136. * @param resolver the FontResolver to use for font URI resolution
  137. * @param uri the URI representing the font
  138. * @return the InputStream to read the font from.
  139. * @throws IOException In case of an I/O error
  140. * @throws MalformedURLException If an invalid URL is built
  141. */
  142. public static InputStream openFontUri(FontResolver resolver, String uri)
  143. throws IOException, MalformedURLException {
  144. InputStream in = null;
  145. if (resolver != null) {
  146. Source source = resolver.resolve(uri);
  147. if (source == null) {
  148. String err = "Cannot load font: failed to create Source for font file "
  149. + uri;
  150. throw new IOException(err);
  151. }
  152. if (source instanceof StreamSource) {
  153. in = ((StreamSource) source).getInputStream();
  154. }
  155. if (in == null && source.getSystemId() != null) {
  156. in = new java.net.URL(source.getSystemId()).openStream();
  157. }
  158. if (in == null) {
  159. String err = "Cannot load font: failed to create InputStream from"
  160. + " Source for font file " + uri;
  161. throw new IOException(err);
  162. }
  163. } else {
  164. in = new URL(uri).openStream();
  165. }
  166. return in;
  167. }
  168. /**
  169. * Reads/parses the font data.
  170. * @throws IOException In case of an I/O error
  171. */
  172. protected abstract void read() throws IOException;
  173. /**
  174. * Returns the custom font that was read using this instance of FontLoader.
  175. * @return the newly loaded font
  176. * @throws IOException if an I/O error occurs
  177. */
  178. public CustomFont getFont() throws IOException {
  179. if (!loaded) {
  180. read();
  181. }
  182. return this.returnFont;
  183. }
  184. }