Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.InputStream;
  20. import java.net.URI;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.Set;
  24. import java.util.regex.Pattern;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.fop.apps.io.InternalResourceResolver;
  29. import org.apache.fop.fonts.CustomFont;
  30. import org.apache.fop.fonts.EmbedFontInfo;
  31. import org.apache.fop.fonts.EmbeddingMode;
  32. import org.apache.fop.fonts.EncodingMode;
  33. import org.apache.fop.fonts.Font;
  34. import org.apache.fop.fonts.FontCache;
  35. import org.apache.fop.fonts.FontEventListener;
  36. import org.apache.fop.fonts.FontLoader;
  37. import org.apache.fop.fonts.FontTriplet;
  38. import org.apache.fop.fonts.FontUris;
  39. import org.apache.fop.fonts.FontUtil;
  40. import org.apache.fop.fonts.MultiByteFont;
  41. import org.apache.fop.fonts.truetype.FontFileReader;
  42. import org.apache.fop.fonts.truetype.OFFontLoader;
  43. import org.apache.fop.fonts.truetype.TTFFile;
  44. /**
  45. * Attempts to determine correct FontInfo
  46. */
  47. public class FontInfoFinder {
  48. /** logging instance */
  49. private final Log log = LogFactory.getLog(FontInfoFinder.class);
  50. private FontEventListener eventListener;
  51. /**
  52. * Sets the font event listener that can be used to receive events about particular events
  53. * in this class.
  54. * @param listener the font event listener
  55. */
  56. public void setEventListener(FontEventListener listener) {
  57. this.eventListener = listener;
  58. }
  59. /**
  60. * Attempts to determine FontTriplets from a given CustomFont.
  61. * It seems to be fairly accurate but will probably require some tweaking over time
  62. *
  63. * @param customFont CustomFont
  64. * @param triplets Collection that will take the generated triplets
  65. */
  66. private void generateTripletsFromFont(CustomFont customFont, Collection<FontTriplet> triplets) {
  67. if (log.isTraceEnabled()) {
  68. log.trace("Font: " + customFont.getFullName()
  69. + ", family: " + customFont.getFamilyNames()
  70. + ", PS: " + customFont.getFontName()
  71. + ", EmbedName: " + customFont.getEmbedFontName());
  72. }
  73. // default style and weight triplet vales (fallback)
  74. String strippedName = stripQuotes(customFont.getStrippedFontName());
  75. //String subName = customFont.getFontSubName();
  76. String fullName = stripQuotes(customFont.getFullName());
  77. String searchName = fullName.toLowerCase();
  78. String style = guessStyle(customFont, searchName);
  79. int weight; //= customFont.getWeight();
  80. int guessedWeight = FontUtil.guessWeight(searchName);
  81. //We always take the guessed weight for now since it yield much better results.
  82. //OpenType's OS/2 usWeightClass value proves to be unreliable.
  83. weight = guessedWeight;
  84. //Full Name usually includes style/weight info so don't use these traits
  85. //If we still want to use these traits, we have to make FontInfo.fontLookup() smarter
  86. triplets.add(new FontTriplet(fullName, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL));
  87. if (!fullName.equals(strippedName)) {
  88. triplets.add(new FontTriplet(strippedName, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL));
  89. }
  90. Set<String> familyNames = customFont.getFamilyNames();
  91. for (String familyName : familyNames) {
  92. familyName = stripQuotes(familyName);
  93. if (!fullName.equals(familyName)) {
  94. /* Heuristic:
  95. * The more similar the family name to the full font name,
  96. * the higher the priority of its triplet.
  97. * (Lower values indicate higher priorities.) */
  98. int priority = fullName.startsWith(familyName)
  99. ? fullName.length() - familyName.length()
  100. : fullName.length();
  101. triplets.add(new FontTriplet(familyName, style, weight, priority));
  102. }
  103. }
  104. }
  105. private final Pattern quotePattern = Pattern.compile("'");
  106. private String stripQuotes(String name) {
  107. return quotePattern.matcher(name).replaceAll("");
  108. }
  109. private String guessStyle(CustomFont customFont, String fontName) {
  110. // style
  111. String style = Font.STYLE_NORMAL;
  112. if (customFont.getItalicAngle() > 0) {
  113. style = Font.STYLE_ITALIC;
  114. } else {
  115. style = FontUtil.guessStyle(fontName);
  116. }
  117. return style;
  118. }
  119. /**
  120. * Attempts to determine FontInfo from a given custom font
  121. * @param fontUri the font URI
  122. * @param customFont the custom font
  123. * @param fontCache font cache (may be null)
  124. * @return FontInfo from the given custom font
  125. */
  126. private EmbedFontInfo getFontInfoFromCustomFont(URI fontUri, CustomFont customFont,
  127. FontCache fontCache, InternalResourceResolver resourceResolver) {
  128. FontUris fontUris = new FontUris(fontUri, null);
  129. List<FontTriplet> fontTripletList = new java.util.ArrayList<FontTriplet>();
  130. generateTripletsFromFont(customFont, fontTripletList);
  131. String subFontName = null;
  132. if (customFont instanceof MultiByteFont) {
  133. subFontName = ((MultiByteFont) customFont).getTTCName();
  134. }
  135. EmbedFontInfo fontInfo = new EmbedFontInfo(fontUris, customFont.isKerningEnabled(),
  136. customFont.isAdvancedEnabled(), fontTripletList, subFontName);
  137. fontInfo.setPostScriptName(customFont.getFontName());
  138. if (fontCache != null) {
  139. fontCache.addFont(fontInfo, resourceResolver);
  140. }
  141. return fontInfo;
  142. }
  143. /**
  144. * Attempts to determine EmbedFontInfo from a given font file.
  145. *
  146. * @param fontURI the URI of the font resource
  147. * @param resourceResolver font resolver used to resolve font
  148. * @param fontCache font cache (may be null)
  149. * @return an array of newly created embed font info. Generally, this array
  150. * will have only one entry, unless the fontUrl is a TrueType Collection
  151. */
  152. public EmbedFontInfo[] find(URI fontURI, InternalResourceResolver resourceResolver, FontCache fontCache) {
  153. URI embedUri = resourceResolver.resolveFromBase(fontURI);
  154. String embedStr = embedUri.toASCIIString();
  155. boolean useKerning = true;
  156. boolean useAdvanced = true;
  157. long fileLastModified = -1;
  158. if (fontCache != null) {
  159. fileLastModified = FontCache.getLastModified(fontURI);
  160. // firstly try and fetch it from cache before loading/parsing the font file
  161. if (fontCache.containsFont(embedStr)) {
  162. EmbedFontInfo[] fontInfos = fontCache.getFontInfos(embedStr, fileLastModified);
  163. if (fontInfos != null) {
  164. return fontInfos;
  165. }
  166. // is this a previously failed parsed font?
  167. } else if (fontCache.isFailedFont(embedStr, fileLastModified)) {
  168. if (log.isDebugEnabled()) {
  169. log.debug("Skipping font file that failed to load previously: " + embedUri);
  170. }
  171. return null;
  172. }
  173. }
  174. // try to determine triplet information from font file
  175. CustomFont customFont = null;
  176. if (fontURI.toASCIIString().toLowerCase().endsWith(".ttc")) {
  177. // Get a list of the TTC Font names
  178. List<String> ttcNames = null;
  179. InputStream in = null;
  180. try {
  181. in = resourceResolver.getResource(fontURI);
  182. TTFFile ttf = new TTFFile(false, false);
  183. FontFileReader reader = new FontFileReader(in);
  184. ttcNames = ttf.getTTCnames(reader);
  185. } catch (Exception e) {
  186. if (this.eventListener != null) {
  187. this.eventListener.fontLoadingErrorAtAutoDetection(this,
  188. fontURI.toASCIIString(), e);
  189. }
  190. return null;
  191. } finally {
  192. IOUtils.closeQuietly(in);
  193. }
  194. List<EmbedFontInfo> embedFontInfoList = new java.util.ArrayList<EmbedFontInfo>();
  195. // For each font name ...
  196. for (String fontName : ttcNames) {
  197. if (log.isDebugEnabled()) {
  198. log.debug("Loading " + fontName);
  199. }
  200. try {
  201. OFFontLoader ttfLoader = new OFFontLoader(fontURI, fontName, true,
  202. EmbeddingMode.AUTO, EncodingMode.AUTO, useKerning, useAdvanced,
  203. resourceResolver, false, false);
  204. customFont = ttfLoader.getFont();
  205. if (this.eventListener != null) {
  206. customFont.setEventListener(this.eventListener);
  207. }
  208. } catch (Exception e) {
  209. if (fontCache != null) {
  210. fontCache.registerFailedFont(embedUri.toASCIIString(), fileLastModified);
  211. }
  212. if (this.eventListener != null) {
  213. this.eventListener.fontLoadingErrorAtAutoDetection(this,
  214. embedUri.toASCIIString(), e);
  215. }
  216. continue;
  217. }
  218. EmbedFontInfo fi = getFontInfoFromCustomFont(fontURI, customFont, fontCache,
  219. resourceResolver);
  220. if (fi != null) {
  221. embedFontInfoList.add(fi);
  222. }
  223. }
  224. return embedFontInfoList.toArray(
  225. new EmbedFontInfo[embedFontInfoList.size()]);
  226. } else {
  227. // The normal case
  228. try {
  229. FontUris fontUris = new FontUris(fontURI, null);
  230. customFont = FontLoader.loadFont(fontUris, null, true, EmbeddingMode.AUTO, EncodingMode.AUTO,
  231. useKerning, useAdvanced, resourceResolver, false, false);
  232. if (this.eventListener != null) {
  233. customFont.setEventListener(this.eventListener);
  234. }
  235. } catch (Exception e) {
  236. if (fontCache != null) {
  237. fontCache.registerFailedFont(embedUri.toASCIIString(), fileLastModified);
  238. }
  239. if (this.eventListener != null) {
  240. this.eventListener.fontLoadingErrorAtAutoDetection(this,
  241. embedUri.toASCIIString(), e);
  242. }
  243. return null;
  244. }
  245. EmbedFontInfo fi = getFontInfoFromCustomFont(fontURI, customFont, fontCache, resourceResolver);
  246. if (fi != null) {
  247. return new EmbedFontInfo[] {fi};
  248. } else {
  249. return null;
  250. }
  251. }
  252. }
  253. }