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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.InternalResourceResolver;
  24. import org.apache.fop.fonts.truetype.OFFontLoader;
  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 resource resolver to use for font URI resolution */
  35. protected final InternalResourceResolver resourceResolver;
  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 resourceResolver the font resolver used to resolve URIs
  54. */
  55. public FontLoader(URI fontFileURI, boolean embedded, boolean useKerning,
  56. boolean useAdvanced, InternalResourceResolver resourceResolver) {
  57. this.fontFileURI = fontFileURI;
  58. this.embedded = embedded;
  59. this.useKerning = useKerning;
  60. this.useAdvanced = useAdvanced;
  61. this.resourceResolver = resourceResolver;
  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 embeddingMode the embedding mode of the font
  72. * @param encodingMode the requested encoding mode
  73. * @param useKerning indicates whether kerning information should be loaded if available
  74. * @param useAdvanced indicates whether advanced typographic information shall be loaded if
  75. * available
  76. * @param resourceResolver the font resolver to use when resolving URIs
  77. * @return the newly loaded font
  78. * @throws IOException In case of an I/O error
  79. */
  80. public static CustomFont loadFont(URI fontFileURI, String subFontName,
  81. boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode,
  82. boolean useKerning, boolean useAdvanced, InternalResourceResolver resourceResolver) throws IOException {
  83. boolean type1 = isType1(fontFileURI);
  84. FontLoader loader;
  85. if (type1) {
  86. if (encodingMode == EncodingMode.CID) {
  87. throw new IllegalArgumentException(
  88. "CID encoding mode not supported for Type 1 fonts");
  89. }
  90. if (embeddingMode == EmbeddingMode.SUBSET) {
  91. throw new IllegalArgumentException(
  92. "Subset embedding for Type 1 fonts is not supported");
  93. }
  94. loader = new Type1FontLoader(fontFileURI, embedded, useKerning, resourceResolver);
  95. } else {
  96. loader = new OFFontLoader(fontFileURI, subFontName, embedded, embeddingMode,
  97. encodingMode, useKerning, useAdvanced, resourceResolver);
  98. }
  99. return loader.getFont();
  100. }
  101. /**
  102. * Reads/parses the font data.
  103. * @throws IOException In case of an I/O error
  104. */
  105. protected abstract void read() throws IOException;
  106. /**
  107. * Returns the custom font that was read using this instance of FontLoader.
  108. * @return the newly loaded font
  109. * @throws IOException if an I/O error occurs
  110. */
  111. public CustomFont getFont() throws IOException {
  112. if (!loaded) {
  113. read();
  114. }
  115. return this.returnFont;
  116. }
  117. }