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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 encodingMode the requested encoding mode
  71. * @param resolver the font resolver to use when resolving URIs
  72. * @return the newly loaded font
  73. * @throws IOException In case of an I/O error
  74. */
  75. public static CustomFont loadFont(File fontFile, String subFontName,
  76. boolean embedded, EncodingMode encodingMode, FontResolver resolver) throws IOException {
  77. return loadFont(fontFile.toURI().toURL(), subFontName,
  78. embedded, encodingMode, resolver);
  79. }
  80. /**
  81. * Loads a custom font from an URL. In the case of Type 1 fonts, the PFB file must be specified.
  82. * @param fontUrl the URL representation of the font
  83. * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
  84. * @param embedded indicates whether the font is embedded or referenced
  85. * @param encodingMode the requested encoding mode
  86. * @param resolver the font resolver to use when resolving URIs
  87. * @return the newly loaded font
  88. * @throws IOException In case of an I/O error
  89. */
  90. public static CustomFont loadFont(URL fontUrl, String subFontName,
  91. boolean embedded, EncodingMode encodingMode,
  92. FontResolver resolver) throws IOException {
  93. return loadFont(fontUrl.toExternalForm(), subFontName,
  94. embedded, encodingMode, true,
  95. resolver);
  96. }
  97. /**
  98. * Loads a custom font from a URI. In the case of Type 1 fonts, the PFB file must be specified.
  99. * @param fontFileURI the URI to the font
  100. * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
  101. * @param embedded indicates whether the font is embedded or referenced
  102. * @param encodingMode the requested encoding mode
  103. * @param useKerning indicates whether kerning information should be loaded if available
  104. * @param resolver the font resolver to use when resolving URIs
  105. * @return the newly loaded font
  106. * @throws IOException In case of an I/O error
  107. */
  108. public static CustomFont loadFont(String fontFileURI, String subFontName,
  109. boolean embedded, EncodingMode encodingMode, boolean useKerning,
  110. FontResolver resolver) throws IOException {
  111. fontFileURI = fontFileURI.trim();
  112. boolean type1 = isType1(fontFileURI);
  113. FontLoader loader;
  114. if (type1) {
  115. if (encodingMode == EncodingMode.CID) {
  116. throw new IllegalArgumentException(
  117. "CID encoding mode not supported for Type 1 fonts");
  118. }
  119. loader = new Type1FontLoader(fontFileURI, embedded, useKerning, resolver);
  120. } else {
  121. loader = new TTFFontLoader(fontFileURI, subFontName,
  122. embedded, encodingMode, useKerning, resolver);
  123. }
  124. return loader.getFont();
  125. }
  126. /**
  127. * Opens a font URI and returns an input stream.
  128. * @param resolver the FontResolver to use for font URI resolution
  129. * @param uri the URI representing the font
  130. * @return the InputStream to read the font from.
  131. * @throws IOException In case of an I/O error
  132. * @throws MalformedURLException If an invalid URL is built
  133. */
  134. public static InputStream openFontUri(FontResolver resolver, String uri)
  135. throws IOException, MalformedURLException {
  136. InputStream in = null;
  137. if (resolver != null) {
  138. Source source = resolver.resolve(uri);
  139. if (source == null) {
  140. String err = "Cannot load font: failed to create Source for font file "
  141. + uri;
  142. throw new IOException(err);
  143. }
  144. if (source instanceof StreamSource) {
  145. in = ((StreamSource) source).getInputStream();
  146. }
  147. if (in == null && source.getSystemId() != null) {
  148. in = new java.net.URL(source.getSystemId()).openStream();
  149. }
  150. if (in == null) {
  151. String err = "Cannot load font: failed to create InputStream from"
  152. + " Source for font file " + uri;
  153. throw new IOException(err);
  154. }
  155. } else {
  156. in = new URL(uri).openStream();
  157. }
  158. return in;
  159. }
  160. /**
  161. * Reads/parses the font data.
  162. * @throws IOException In case of an I/O error
  163. */
  164. protected abstract void read() throws IOException;
  165. /**
  166. * Returns the custom font that was read using this instance of FontLoader.
  167. * @return the newly loaded font
  168. * @throws IOException if an I/O error occurs
  169. */
  170. public CustomFont getFont() throws IOException {
  171. if (!loaded) {
  172. read();
  173. }
  174. return this.returnFont;
  175. }
  176. }