Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FontInfoFinder.java 7.5KB

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.autodetect;
  19. import java.io.IOException;
  20. import java.net.URL;
  21. import java.net.URLConnection;
  22. import java.util.Collection;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Set;
  26. import java.util.regex.Pattern;
  27. import org.apache.commons.io.IOUtils;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.fop.fonts.CachedFontInfo;
  31. import org.apache.fop.fonts.CustomFont;
  32. import org.apache.fop.fonts.EmbedFontInfo;
  33. import org.apache.fop.fonts.Font;
  34. import org.apache.fop.fonts.FontCache;
  35. import org.apache.fop.fonts.FontLoader;
  36. import org.apache.fop.fonts.FontResolver;
  37. import org.apache.fop.fonts.FontTriplet;
  38. import org.apache.fop.fonts.FontUtil;
  39. /**
  40. * Attempts to determine correct FontInfo
  41. */
  42. public class FontInfoFinder {
  43. /** logging instance */
  44. private Log log = LogFactory.getLog(FontInfoFinder.class);
  45. /**
  46. * Attempts to determine FontTriplets from a given CustomFont.
  47. * It seems to be fairly accurate but will probably require some tweaking over time
  48. *
  49. * @param customFont CustomFont
  50. * @param triplet Collection that will take the generated triplets
  51. */
  52. private void generateTripletsFromFont(CustomFont customFont, Collection triplets) {
  53. if (log.isTraceEnabled()) {
  54. log.trace("Font: " + customFont.getFullName()
  55. + ", family: " + customFont.getFamilyNames()
  56. + ", PS: " + customFont.getFontName()
  57. + ", EmbedName: " + customFont.getEmbedFontName());
  58. }
  59. // default style and weight triplet vales (fallback)
  60. String strippedName = stripQuotes(customFont.getStrippedFontName());
  61. String subName = customFont.getFontSubName();
  62. String searchName = strippedName.toLowerCase();
  63. if (subName != null) {
  64. searchName += subName.toLowerCase();
  65. }
  66. String style = guessStyle(customFont, searchName);
  67. int weight = FontUtil.guessWeight(searchName);
  68. //Full Name usually includes style/weight info so don't use these traits
  69. //If we still want to use these traits, we have to make FontInfo.fontLookup() smarter
  70. String fullName = stripQuotes(customFont.getFullName());
  71. triplets.add(new FontTriplet(fullName, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL));
  72. if (!fullName.equals(strippedName)) {
  73. triplets.add(new FontTriplet(strippedName, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL));
  74. }
  75. Set familyNames = customFont.getFamilyNames();
  76. Iterator iter = familyNames.iterator();
  77. while (iter.hasNext()) {
  78. String familyName = stripQuotes((String)iter.next());
  79. if (!fullName.equals(familyName)) {
  80. triplets.add(new FontTriplet(familyName, style, weight));
  81. }
  82. }
  83. }
  84. private final Pattern quotePattern = Pattern.compile("'");
  85. private String stripQuotes(String name) {
  86. return quotePattern.matcher(name).replaceAll("");
  87. }
  88. private String guessStyle(CustomFont customFont, String fontName) {
  89. // style
  90. String style = Font.STYLE_NORMAL;
  91. if (customFont.getItalicAngle() > 0) {
  92. style = Font.STYLE_ITALIC;
  93. } else {
  94. style = FontUtil.guessStyle(fontName);
  95. }
  96. return style;
  97. }
  98. /**
  99. * Attempts to determine FontInfo from a given custom font
  100. * @param fontUrl the font URL
  101. * @param customFont the custom font
  102. * @param fontCache font cache (may be null)
  103. * @return
  104. */
  105. private EmbedFontInfo fontInfoFromCustomFont(
  106. URL fontUrl, CustomFont customFont, FontCache fontCache) {
  107. List fontTripletList = new java.util.ArrayList();
  108. generateTripletsFromFont(customFont, fontTripletList);
  109. String embedUrl;
  110. embedUrl = fontUrl.toExternalForm();
  111. EmbedFontInfo fontInfo = new EmbedFontInfo(null, customFont.isKerningEnabled(),
  112. fontTripletList, embedUrl);
  113. if (fontCache != null) {
  114. fontCache.addFont(fontInfo);
  115. }
  116. return fontInfo;
  117. }
  118. /**
  119. * Attempts to determine EmbedFontInfo from a given font file.
  120. *
  121. * @param fontUrl font URL. Assumed to be local.
  122. * @param resolver font resolver used to resolve font
  123. * @param fontCache font cache (may be null)
  124. * @return newly created embed font info
  125. */
  126. public EmbedFontInfo find(URL fontUrl, FontResolver resolver, FontCache fontCache) {
  127. String embedUrl = null;
  128. embedUrl = fontUrl.toExternalForm();
  129. long fileLastModified = -1;
  130. if (fontCache != null) {
  131. try {
  132. URLConnection conn = fontUrl.openConnection();
  133. try {
  134. fileLastModified = conn.getLastModified();
  135. } finally {
  136. //An InputStream is created even if it's not accessed, but we need to close it.
  137. IOUtils.closeQuietly(conn.getInputStream());
  138. }
  139. } catch (IOException e) {
  140. // Should never happen, because URL must be local
  141. log.debug("IOError: " + e.getMessage());
  142. fileLastModified = 0;
  143. }
  144. // firstly try and fetch it from cache before loading/parsing the font file
  145. if (fontCache.containsFont(embedUrl)) {
  146. CachedFontInfo fontInfo = fontCache.getFont(embedUrl);
  147. if (fontInfo.lastModified() == fileLastModified) {
  148. return fontInfo;
  149. } else {
  150. // out of date cache item
  151. fontCache.removeFont(embedUrl);
  152. }
  153. // is this a previously failed parsed font?
  154. } else if (fontCache.isFailedFont(embedUrl, fileLastModified)) {
  155. if (log.isDebugEnabled()) {
  156. log.debug("Skipping font file that failed to load previously: " + embedUrl);
  157. }
  158. return null;
  159. }
  160. }
  161. // try to determine triplet information from font file
  162. CustomFont customFont = null;
  163. try {
  164. customFont = FontLoader.loadFont(fontUrl, resolver);
  165. } catch (Exception e) {
  166. //TODO Too verbose (it's an error but we don't care if some fonts can't be loaded)
  167. if (log.isErrorEnabled()) {
  168. log.error("Unable to load font file: " + embedUrl + ". Reason: " + e.getMessage());
  169. }
  170. if (fontCache != null) {
  171. fontCache.registerFailedFont(embedUrl, fileLastModified);
  172. }
  173. return null;
  174. }
  175. return fontInfoFromCustomFont(fontUrl, customFont, fontCache);
  176. }
  177. }