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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(FontUris fontUris) {
  64. return fontUris.getEmbed().toASCIIString().toLowerCase().endsWith(".pfb") || fontUris.getAfm() != null
  65. || fontUris.getPfm() != null;
  66. }
  67. /**
  68. * Loads a custom font from a URI. In the case of Type 1 fonts, the PFB file must be specified.
  69. * @param fontUris the URI to the font
  70. * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise)
  71. * @param embedded indicates whether the font is embedded or referenced
  72. * @param embeddingMode the embedding mode of the font
  73. * @param encodingMode the requested encoding mode
  74. * @param useKerning indicates whether kerning information should be loaded if available
  75. * @param useAdvanced indicates whether advanced typographic information shall be loaded if
  76. * available
  77. * @param resourceResolver the font resolver to use when resolving URIs
  78. * @return the newly loaded font
  79. * @throws IOException In case of an I/O error
  80. */
  81. public static CustomFont loadFont(FontUris fontUris, String subFontName,
  82. boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode,
  83. boolean useKerning, boolean useAdvanced, InternalResourceResolver resourceResolver,
  84. boolean simulateStyle, boolean embedAsType1) throws IOException {
  85. boolean type1 = isType1(fontUris);
  86. FontLoader loader;
  87. if (type1) {
  88. if (encodingMode == EncodingMode.CID) {
  89. throw new IllegalArgumentException(
  90. "CID encoding mode not supported for Type 1 fonts");
  91. }
  92. loader = new Type1FontLoader(fontUris, embedded, embeddingMode, useKerning,
  93. resourceResolver);
  94. } else {
  95. loader = new OFFontLoader(fontUris.getEmbed(), subFontName, embedded, embeddingMode,
  96. encodingMode, useKerning, useAdvanced, resourceResolver, simulateStyle, embedAsType1);
  97. }
  98. return loader.getFont();
  99. }
  100. /**
  101. * Reads/parses the font data.
  102. * @throws IOException In case of an I/O error
  103. */
  104. protected abstract void read() throws IOException;
  105. /**
  106. * Returns the custom font that was read using this instance of FontLoader.
  107. * @return the newly loaded font
  108. * @throws IOException if an I/O error occurs
  109. */
  110. public CustomFont getFont() throws IOException {
  111. if (!loaded) {
  112. read();
  113. }
  114. return this.returnFont;
  115. }
  116. }