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.

FontSetup.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.render.java2d;
  19. // FOP
  20. import java.awt.Graphics2D;
  21. import java.awt.GraphicsEnvironment;
  22. import java.util.List;
  23. import java.util.Set;
  24. import javax.xml.transform.Source;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.fop.fonts.CustomFont;
  28. import org.apache.fop.fonts.EmbedFontInfo;
  29. import org.apache.fop.fonts.Font;
  30. import org.apache.fop.fonts.FontInfo;
  31. import org.apache.fop.fonts.FontLoader;
  32. import org.apache.fop.fonts.FontResolver;
  33. import org.apache.fop.fonts.FontTriplet;
  34. import org.apache.fop.fonts.FontUtil;
  35. import org.apache.fop.fonts.LazyFont;
  36. /**
  37. * Sets up the Java2D/AWT fonts. It is similar to
  38. * org.apache.fop.render.fonts.FontSetup.
  39. * Assigns the font (with metrics) to internal names like "F1" and
  40. * assigns family-style-weight triplets to the fonts.
  41. */
  42. public class FontSetup {
  43. /** logging instance */
  44. protected static Log log = LogFactory.getLog(FontSetup.class);
  45. private static final int LAST_PREDEFINED_FONT_NUMBER = 14;
  46. private static final Set HARDCODED_FONT_NAMES;
  47. static {
  48. HARDCODED_FONT_NAMES = new java.util.HashSet();
  49. HARDCODED_FONT_NAMES.add("any");
  50. HARDCODED_FONT_NAMES.add("sans-serif");
  51. HARDCODED_FONT_NAMES.add("serif");
  52. HARDCODED_FONT_NAMES.add("monospace");
  53. HARDCODED_FONT_NAMES.add("Helvetica");
  54. HARDCODED_FONT_NAMES.add("Times");
  55. HARDCODED_FONT_NAMES.add("Courier");
  56. HARDCODED_FONT_NAMES.add("Symbol");
  57. HARDCODED_FONT_NAMES.add("ZapfDingbats");
  58. HARDCODED_FONT_NAMES.add("Times Roman");
  59. HARDCODED_FONT_NAMES.add("Times-Roman");
  60. HARDCODED_FONT_NAMES.add("Computer-Modern-Typewriter");
  61. }
  62. /**
  63. * Sets up the font info object.
  64. *
  65. * Adds metrics for basic fonts and useful family-style-weight
  66. * triplets for lookup.
  67. *
  68. * @param fontInfo the font info object to set up
  69. * @param configuredFontList of fop config fonts
  70. * @param resolver for resolving the font file URI
  71. * @param graphics needed for access to font metrics
  72. */
  73. public static void setup(FontInfo fontInfo, List configuredFontList,
  74. FontResolver resolver, Graphics2D graphics) {
  75. FontMetricsMapper metric;
  76. int normal, bold, bolditalic, italic;
  77. /*
  78. * available java fonts are:
  79. * Serif - bold, normal, italic, bold-italic
  80. * SansSerif - bold, normal, italic, bold-italic
  81. * MonoSpaced - bold, normal, italic, bold-italic
  82. */
  83. normal = java.awt.Font.PLAIN;
  84. bold = java.awt.Font.BOLD;
  85. italic = java.awt.Font.ITALIC;
  86. bolditalic = java.awt.Font.BOLD + java.awt.Font.ITALIC;
  87. metric = new SystemFontMetricsMapper("SansSerif", normal, graphics);
  88. // --> goes to F1
  89. fontInfo.addMetrics("F1", metric);
  90. metric = new SystemFontMetricsMapper("SansSerif", italic, graphics);
  91. // --> goes to F2
  92. fontInfo.addMetrics("F2", metric);
  93. metric = new SystemFontMetricsMapper("SansSerif", bold, graphics);
  94. // --> goes to F3
  95. fontInfo.addMetrics("F3", metric);
  96. metric = new SystemFontMetricsMapper("SansSerif", bolditalic, graphics);
  97. // --> goes to F4
  98. fontInfo.addMetrics("F4", metric);
  99. metric = new SystemFontMetricsMapper("Serif", normal, graphics);
  100. // --> goes to F5
  101. fontInfo.addMetrics("F5", metric);
  102. metric = new SystemFontMetricsMapper("Serif", italic, graphics);
  103. // --> goes to F6
  104. fontInfo.addMetrics("F6", metric);
  105. metric = new SystemFontMetricsMapper("Serif", bold, graphics);
  106. // --> goes to F7
  107. fontInfo.addMetrics("F7", metric);
  108. metric = new SystemFontMetricsMapper("Serif", bolditalic, graphics);
  109. // --> goes to F8
  110. fontInfo.addMetrics("F8", metric);
  111. metric = new SystemFontMetricsMapper("MonoSpaced", normal, graphics);
  112. // --> goes to F9
  113. fontInfo.addMetrics("F9", metric);
  114. metric = new SystemFontMetricsMapper("MonoSpaced", italic, graphics);
  115. // --> goes to F10
  116. fontInfo.addMetrics("F10", metric);
  117. metric = new SystemFontMetricsMapper("MonoSpaced", bold, graphics);
  118. // --> goes to F11
  119. fontInfo.addMetrics("F11", metric);
  120. metric = new SystemFontMetricsMapper("MonoSpaced", bolditalic, graphics);
  121. // --> goes to F12
  122. fontInfo.addMetrics("F12", metric);
  123. metric = new SystemFontMetricsMapper("Serif", normal, graphics);
  124. //"Symbol" doesn't seem to work here, but "Serif" does the job just fine. *shrug*
  125. // --> goes to F13 and F14
  126. fontInfo.addMetrics("F13", metric);
  127. fontInfo.addMetrics("F14", metric);
  128. // Custom type 1 fonts step 1/2
  129. // fontInfo.addMetrics("F15", new OMEP());
  130. // fontInfo.addMetrics("F16", new GaramondLightCondensed());
  131. // fontInfo.addMetrics("F17", new BauerBodoniBoldItalic());
  132. /* any is treated as serif */
  133. fontInfo.addFontProperties("F5", "any", "normal", Font.WEIGHT_NORMAL);
  134. fontInfo.addFontProperties("F6", "any", "italic", Font.WEIGHT_NORMAL);
  135. fontInfo.addFontProperties("F6", "any", "oblique", Font.WEIGHT_NORMAL);
  136. fontInfo.addFontProperties("F7", "any", "normal", Font.WEIGHT_BOLD);
  137. fontInfo.addFontProperties("F8", "any", "italic", Font.WEIGHT_BOLD);
  138. fontInfo.addFontProperties("F8", "any", "oblique", Font.WEIGHT_BOLD);
  139. fontInfo.addFontProperties("F1", "sans-serif", "normal", Font.WEIGHT_NORMAL);
  140. fontInfo.addFontProperties("F2", "sans-serif", "oblique", Font.WEIGHT_NORMAL);
  141. fontInfo.addFontProperties("F2", "sans-serif", "italic", Font.WEIGHT_NORMAL);
  142. fontInfo.addFontProperties("F3", "sans-serif", "normal", Font.WEIGHT_BOLD);
  143. fontInfo.addFontProperties("F4", "sans-serif", "oblique", Font.WEIGHT_BOLD);
  144. fontInfo.addFontProperties("F4", "sans-serif", "italic", Font.WEIGHT_BOLD);
  145. fontInfo.addFontProperties("F5", "serif", "normal", Font.WEIGHT_NORMAL);
  146. fontInfo.addFontProperties("F6", "serif", "oblique", Font.WEIGHT_NORMAL);
  147. fontInfo.addFontProperties("F6", "serif", "italic", Font.WEIGHT_NORMAL);
  148. fontInfo.addFontProperties("F7", "serif", "normal", Font.WEIGHT_BOLD);
  149. fontInfo.addFontProperties("F8", "serif", "oblique", Font.WEIGHT_BOLD);
  150. fontInfo.addFontProperties("F8", "serif", "italic", Font.WEIGHT_BOLD);
  151. fontInfo.addFontProperties("F9", "monospace", "normal", Font.WEIGHT_NORMAL);
  152. fontInfo.addFontProperties("F10", "monospace", "oblique", Font.WEIGHT_NORMAL);
  153. fontInfo.addFontProperties("F10", "monospace", "italic", Font.WEIGHT_NORMAL);
  154. fontInfo.addFontProperties("F11", "monospace", "normal", Font.WEIGHT_BOLD);
  155. fontInfo.addFontProperties("F12", "monospace", "oblique", Font.WEIGHT_BOLD);
  156. fontInfo.addFontProperties("F12", "monospace", "italic", Font.WEIGHT_BOLD);
  157. fontInfo.addFontProperties("F1", "Helvetica", "normal", Font.WEIGHT_NORMAL);
  158. fontInfo.addFontProperties("F2", "Helvetica", "oblique", Font.WEIGHT_NORMAL);
  159. fontInfo.addFontProperties("F2", "Helvetica", "italic", Font.WEIGHT_NORMAL);
  160. fontInfo.addFontProperties("F3", "Helvetica", "normal", Font.WEIGHT_BOLD);
  161. fontInfo.addFontProperties("F4", "Helvetica", "oblique", Font.WEIGHT_BOLD);
  162. fontInfo.addFontProperties("F4", "Helvetica", "italic", Font.WEIGHT_BOLD);
  163. fontInfo.addFontProperties("F5", "Times", "normal", Font.WEIGHT_NORMAL);
  164. fontInfo.addFontProperties("F6", "Times", "oblique", Font.WEIGHT_NORMAL);
  165. fontInfo.addFontProperties("F6", "Times", "italic", Font.WEIGHT_NORMAL);
  166. fontInfo.addFontProperties("F7", "Times", "normal", Font.WEIGHT_BOLD);
  167. fontInfo.addFontProperties("F8", "Times", "oblique", Font.WEIGHT_BOLD);
  168. fontInfo.addFontProperties("F8", "Times", "italic", Font.WEIGHT_BOLD);
  169. fontInfo.addFontProperties("F9", "Courier", "normal", Font.WEIGHT_NORMAL);
  170. fontInfo.addFontProperties("F10", "Courier", "oblique", Font.WEIGHT_NORMAL);
  171. fontInfo.addFontProperties("F10", "Courier", "italic", Font.WEIGHT_NORMAL);
  172. fontInfo.addFontProperties("F11", "Courier", "normal", Font.WEIGHT_BOLD);
  173. fontInfo.addFontProperties("F12", "Courier", "oblique", Font.WEIGHT_BOLD);
  174. fontInfo.addFontProperties("F12", "Courier", "italic", Font.WEIGHT_BOLD);
  175. fontInfo.addFontProperties("F13", "Symbol", "normal", Font.WEIGHT_NORMAL);
  176. fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", Font.WEIGHT_NORMAL);
  177. // Custom type 1 fonts step 2/2
  178. // fontInfo.addFontProperties("F15", "OMEP", "normal", FontInfo.NORMAL);
  179. // fontInfo.addFontProperties("F16", "Garamond-LightCondensed", "normal", FontInfo.NORMAL);
  180. // fontInfo.addFontProperties("F17", "BauerBodoni", "italic", FontInfo.BOLD);
  181. /* for compatibility with PassiveTex */
  182. fontInfo.addFontProperties("F5", "Times-Roman", "normal", Font.WEIGHT_NORMAL);
  183. fontInfo.addFontProperties("F6", "Times-Roman", "oblique", Font.WEIGHT_NORMAL);
  184. fontInfo.addFontProperties("F6", "Times-Roman", "italic", Font.WEIGHT_NORMAL);
  185. fontInfo.addFontProperties("F7", "Times-Roman", "normal", Font.WEIGHT_BOLD);
  186. fontInfo.addFontProperties("F8", "Times-Roman", "oblique", Font.WEIGHT_BOLD);
  187. fontInfo.addFontProperties("F8", "Times-Roman", "italic", Font.WEIGHT_BOLD);
  188. fontInfo.addFontProperties("F5", "Times Roman", "normal", Font.WEIGHT_NORMAL);
  189. fontInfo.addFontProperties("F6", "Times Roman", "oblique", Font.WEIGHT_NORMAL);
  190. fontInfo.addFontProperties("F6", "Times Roman", "italic", Font.WEIGHT_NORMAL);
  191. fontInfo.addFontProperties("F7", "Times Roman", "normal", Font.WEIGHT_BOLD);
  192. fontInfo.addFontProperties("F8", "Times Roman", "oblique", Font.WEIGHT_BOLD);
  193. fontInfo.addFontProperties("F8", "Times Roman", "italic", Font.WEIGHT_BOLD);
  194. fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter",
  195. "normal", Font.WEIGHT_NORMAL);
  196. int lastNum = configureInstalledAWTFonts(fontInfo, graphics, LAST_PREDEFINED_FONT_NUMBER + 1);
  197. addConfiguredFonts(fontInfo, configuredFontList, resolver, lastNum++);
  198. }
  199. private static int configureInstalledAWTFonts(FontInfo fontInfo, Graphics2D graphics,
  200. int startNumber) {
  201. int num = startNumber;
  202. GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  203. java.awt.Font[] fonts = env.getAllFonts();
  204. for (int i = 0; i < fonts.length; i++) {
  205. java.awt.Font f = fonts[i];
  206. if (HARDCODED_FONT_NAMES.contains(f.getName())) {
  207. continue; //skip
  208. }
  209. if (log.isTraceEnabled()) {
  210. log.trace("AWT Font: " + f.getFontName()
  211. + ", family: " + f.getFamily()
  212. + ", PS: " + f.getPSName()
  213. + ", Name: " + f.getName()
  214. + ", Angle: " + f.getItalicAngle()
  215. + ", Style: " + f.getStyle());
  216. }
  217. String searchName = FontUtil.stripWhiteSpace(f.getName()).toLowerCase();
  218. String guessedStyle = FontUtil.guessStyle(searchName);
  219. int guessedWeight = FontUtil.guessWeight(searchName);
  220. num++;
  221. String fontKey = "F" + num;
  222. int style = convertToAWTFontStyle(guessedStyle, guessedWeight);
  223. addFontMetricsMapper(fontInfo, f.getName(), fontKey, graphics, style);
  224. //Register appropriate font triplets matching the font. Two different strategies:
  225. //Example: "Arial Bold", normal, normal
  226. addFontTriplet(fontInfo, f.getName(),
  227. Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, fontKey);
  228. if (!f.getName().equals(f.getFamily())) {
  229. //Example: "Arial", bold, normal
  230. addFontTriplet(fontInfo, f.getFamily(),
  231. guessedStyle, guessedWeight, fontKey);
  232. }
  233. }
  234. return num;
  235. }
  236. /**
  237. * Add fonts from configuration file starting with internal name F<num>.
  238. *
  239. * @param fontInfo the font info object to set up
  240. * @param fontList a list of EmbedFontInfo objects
  241. * @param num starting index for internal font numbering
  242. * @param resolver the font resolver
  243. */
  244. private static void addConfiguredFonts(FontInfo fontInfo, List fontList, FontResolver resolver, int num) {
  245. if (fontList == null || fontList.size() < 1) {
  246. log.debug("No user configured fonts found.");
  247. return;
  248. }
  249. if (resolver == null) {
  250. // Ensure that we have minimal font resolution capabilities
  251. resolver = org.apache.fop.fonts.FontSetup
  252. .createMinimalFontResolver();
  253. }
  254. String internalName = null;
  255. for (int i = 0; i < fontList.size(); i++) {
  256. EmbedFontInfo configFontInfo = (EmbedFontInfo) fontList.get(i);
  257. String fontFile = configFontInfo.getEmbedFile();
  258. internalName = "F" + num;
  259. num++;
  260. try {
  261. FontMetricsMapper font = null;
  262. String metricsUrl = configFontInfo.getMetricsFile();
  263. // If the user specified an XML-based metrics file, we'll use it
  264. // Otherwise, calculate metrics directly from the font file.
  265. if (metricsUrl != null) {
  266. LazyFont fontMetrics = new LazyFont(configFontInfo, resolver);
  267. Source fontSource = resolver.resolve(configFontInfo.getEmbedFile());
  268. font = new CustomFontMetricsMapper(fontMetrics, fontSource);
  269. } else {
  270. CustomFont fontMetrics = FontLoader.loadFont(fontFile, resolver);
  271. font = new CustomFontMetricsMapper(fontMetrics);
  272. }
  273. fontInfo.addMetrics(internalName, font);
  274. List triplets = configFontInfo.getFontTriplets();
  275. for (int c = 0; c < triplets.size(); c++) {
  276. FontTriplet triplet = (FontTriplet) triplets.get(c);
  277. if (log.isDebugEnabled()) {
  278. log.debug("Registering: " + triplet + " under " + internalName);
  279. }
  280. fontInfo.addFontProperties(internalName, triplet);
  281. }
  282. } catch (Exception e) {
  283. log.warn("Unable to load custom font from file '" + fontFile + "'", e);
  284. }
  285. }
  286. }
  287. private static void addFontTriplet(FontInfo fontInfo, String fontName, String fontStyle,
  288. int fontWeight, String fontKey) {
  289. FontTriplet triplet = FontInfo.createFontKey(fontName, fontStyle, fontWeight);
  290. fontInfo.addFontProperties(fontKey, triplet);
  291. }
  292. private static void addFontMetricsMapper(FontInfo fontInfo, String family, String fontKey,
  293. Graphics2D graphics, int style) {
  294. FontMetricsMapper metric = new SystemFontMetricsMapper(family, style, graphics);
  295. fontInfo.addMetrics(fontKey, metric);
  296. }
  297. private static int convertToAWTFontStyle(String fontStyle, int fontWeight) {
  298. int style = java.awt.Font.PLAIN;
  299. if (fontWeight >= Font.WEIGHT_BOLD) {
  300. style |= java.awt.Font.BOLD;
  301. }
  302. if (!"normal".equals(fontStyle)) {
  303. style |= java.awt.Font.ITALIC;
  304. }
  305. return style;
  306. }
  307. }