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.2KB

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