Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FontLoader.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.IOException;
  20. import java.net.URI;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.apps.io.URIResolverWrapper;
  24. import org.apache.fop.fonts.truetype.TTFFontLoader;
  25. import org.apache.fop.fonts.type1.Type1FontLoader;
  26. /**
  27. * Base class for font loaders.
  28. */
  29. public abstract class FontLoader {
  30. /** logging instance */
  31. protected static final Log log = LogFactory.getLog(FontLoader.class);
  32. /** URI representing the font file */
  33. protected final URI fontFileURI;
  34. /** the FontResolver to use for font URI resolution */
  35. protected final URIResolverWrapper resolver;
  36. /** the loaded font */
  37. protected CustomFont returnFont;
  38. /** true if the font has been loaded */
  39. protected boolean loaded;
  40. /** true if the font will be embedded, false if it will be referenced only. */
  41. protected boolean embedded;
  42. /** true if kerning information false be loaded if available. */
  43. protected boolean useKerning;
  44. /** true if advanced typographic information shall be loaded if available. */
  45. protected boolean useAdvanced;
  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 useKerning indicates whether kerning information shall be loaded if available
  51. * @param useAdvanced indicates whether advanced typographic information shall be loaded if
  52. * available
  53. * @param resolver the font resolver used to resolve URIs
  54. */
  55. public FontLoader(URI fontFileURI, boolean embedded, boolean useKerning,
  56. boolean useAdvanced, URIResolverWrapper resolver) {
  57. this.fontFileURI = fontFileURI;
  58. this.embedded = embedded;
  59. this.useKerning = useKerning;
  60. this.useAdvanced = useAdvanced;
  61. this.resolver = resolver;
  62. }
  63. private static boolean isType1(URI fontURI) {
  64. return fontURI.toASCIIString().toLowerCase().endsWith(".pfb");
  65. }
  66. /**
  67. * Loads a custom font from a URI. In the case of Type 1 fonts, the PFB file must be specified.
  68. * @param fontFileURI the URI to the font
  69. * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
  70. * @param embedded indicates whether the font is embedded or referenced
  71. * @param encodingMode the requested encoding mode
  72. * @param useKerning indicates whether kerning information should be loaded if available
  73. * @param useAdvanced indicates whether advanced typographic information shall be loaded if
  74. * available
  75. * @param resolver the font resolver to use when resolving URIs
  76. * @return the newly loaded font
  77. * @throws IOException In case of an I/O error
  78. */
  79. public static CustomFont loadFont(URI fontFileURI, String subFontName,
  80. boolean embedded, EncodingMode encodingMode, boolean useKerning,
  81. boolean useAdvanced, URIResolverWrapper resolver) throws IOException {
  82. boolean type1 = isType1(fontFileURI);
  83. FontLoader loader;
  84. if (type1) {
  85. if (encodingMode == EncodingMode.CID) {
  86. throw new IllegalArgumentException(
  87. "CID encoding mode not supported for Type 1 fonts");
  88. }
  89. loader = new Type1FontLoader(fontFileURI, embedded, useKerning, resolver);
  90. } else {
  91. loader = new TTFFontLoader(fontFileURI, subFontName,
  92. embedded, encodingMode, useKerning, useAdvanced, resolver);
  93. }
  94. return loader.getFont();
  95. }
  96. /**
  97. * Reads/parses the font data.
  98. * @throws IOException In case of an I/O error
  99. */
  100. protected abstract void read() throws IOException;
  101. /**
  102. * Returns the custom font that was read using this instance of FontLoader.
  103. * @return the newly loaded font
  104. * @throws IOException if an I/O error occurs
  105. */
  106. public CustomFont getFont() throws IOException {
  107. if (!loaded) {
  108. read();
  109. }
  110. return this.returnFont;
  111. }
  112. }