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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.apache.fop.fonts.FontInfo;
  23. import org.apache.fop.fonts.Font;
  24. import org.apache.fop.fonts.FontTriplet;
  25. // Java
  26. import java.awt.Graphics2D;
  27. import java.awt.GraphicsEnvironment;
  28. import java.util.Set;
  29. /**
  30. * Sets up the Java2D/AWT fonts. It is similar to
  31. * org.apache.fop.render.fonts.FontSetup.
  32. * Assigns the font (with metrics) to internal names like "F1" and
  33. * assigns family-style-weight triplets to the fonts.
  34. */
  35. public class FontSetup {
  36. /** logging instance */
  37. protected static Log log = LogFactory.getLog(FontSetup.class);
  38. private static final int LAST_PREDEFINED_FONT_NUMBER = 14;
  39. private static final Set HARDCODED_FONT_NAMES;
  40. static {
  41. HARDCODED_FONT_NAMES = new java.util.HashSet();
  42. HARDCODED_FONT_NAMES.add("any");
  43. HARDCODED_FONT_NAMES.add("sans-serif");
  44. HARDCODED_FONT_NAMES.add("serif");
  45. HARDCODED_FONT_NAMES.add("monospace");
  46. HARDCODED_FONT_NAMES.add("Helvetica");
  47. HARDCODED_FONT_NAMES.add("Times");
  48. HARDCODED_FONT_NAMES.add("Courier");
  49. HARDCODED_FONT_NAMES.add("Symbol");
  50. HARDCODED_FONT_NAMES.add("ZapfDingbats");
  51. HARDCODED_FONT_NAMES.add("Times Roman");
  52. HARDCODED_FONT_NAMES.add("Times-Roman");
  53. HARDCODED_FONT_NAMES.add("Computer-Modern-Typewriter");
  54. }
  55. /**
  56. * Sets up the font info object.
  57. *
  58. * Adds metrics for basic fonts and useful family-style-weight
  59. * triplets for lookup.
  60. *
  61. * @param fontInfo the font info object to set up
  62. * @param graphics needed for acces to font metrics
  63. */
  64. public static void setup(FontInfo fontInfo, Graphics2D graphics) {
  65. FontMetricsMapper metric;
  66. int normal, bold, bolditalic, italic;
  67. /*
  68. * available java fonts are:
  69. * Serif - bold, normal, italic, bold-italic
  70. * SansSerif - bold, normal, italic, bold-italic
  71. * MonoSpaced - bold, normal, italic, bold-italic
  72. */
  73. normal = java.awt.Font.PLAIN;
  74. bold = java.awt.Font.BOLD;
  75. italic = java.awt.Font.ITALIC;
  76. bolditalic = java.awt.Font.BOLD + java.awt.Font.ITALIC;
  77. metric = new FontMetricsMapper("SansSerif", normal, graphics);
  78. // --> goes to F1
  79. fontInfo.addMetrics("F1", metric);
  80. metric = new FontMetricsMapper("SansSerif", italic, graphics);
  81. // --> goes to F2
  82. fontInfo.addMetrics("F2", metric);
  83. metric = new FontMetricsMapper("SansSerif", bold, graphics);
  84. // --> goes to F3
  85. fontInfo.addMetrics("F3", metric);
  86. metric = new FontMetricsMapper("SansSerif", bolditalic, graphics);
  87. // --> goes to F4
  88. fontInfo.addMetrics("F4", metric);
  89. metric = new FontMetricsMapper("Serif", normal, graphics);
  90. // --> goes to F5
  91. fontInfo.addMetrics("F5", metric);
  92. metric = new FontMetricsMapper("Serif", italic, graphics);
  93. // --> goes to F6
  94. fontInfo.addMetrics("F6", metric);
  95. metric = new FontMetricsMapper("Serif", bold, graphics);
  96. // --> goes to F7
  97. fontInfo.addMetrics("F7", metric);
  98. metric = new FontMetricsMapper("Serif", bolditalic, graphics);
  99. // --> goes to F8
  100. fontInfo.addMetrics("F8", metric);
  101. metric = new FontMetricsMapper("MonoSpaced", normal, graphics);
  102. // --> goes to F9
  103. fontInfo.addMetrics("F9", metric);
  104. metric = new FontMetricsMapper("MonoSpaced", italic, graphics);
  105. // --> goes to F10
  106. fontInfo.addMetrics("F10", metric);
  107. metric = new FontMetricsMapper("MonoSpaced", bold, graphics);
  108. // --> goes to F11
  109. fontInfo.addMetrics("F11", metric);
  110. metric = new FontMetricsMapper("MonoSpaced", bolditalic, graphics);
  111. // --> goes to F12
  112. fontInfo.addMetrics("F12", metric);
  113. metric = new FontMetricsMapper("Serif", normal, graphics);
  114. //"Symbol" doesn't seem to work here, but "Serif" does the job just fine. *shrug*
  115. // --> goes to F13 and F14
  116. fontInfo.addMetrics("F13", metric);
  117. fontInfo.addMetrics("F14", metric);
  118. // Custom type 1 fonts step 1/2
  119. // fontInfo.addMetrics("F15", new OMEP());
  120. // fontInfo.addMetrics("F16", new GaramondLightCondensed());
  121. // fontInfo.addMetrics("F17", new BauerBodoniBoldItalic());
  122. /* any is treated as serif */
  123. fontInfo.addFontProperties("F5", "any", "normal", Font.WEIGHT_NORMAL);
  124. fontInfo.addFontProperties("F6", "any", "italic", Font.WEIGHT_NORMAL);
  125. fontInfo.addFontProperties("F6", "any", "oblique", Font.WEIGHT_NORMAL);
  126. fontInfo.addFontProperties("F7", "any", "normal", Font.WEIGHT_BOLD);
  127. fontInfo.addFontProperties("F8", "any", "italic", Font.WEIGHT_BOLD);
  128. fontInfo.addFontProperties("F8", "any", "oblique", Font.WEIGHT_BOLD);
  129. fontInfo.addFontProperties("F1", "sans-serif", "normal", Font.WEIGHT_NORMAL);
  130. fontInfo.addFontProperties("F2", "sans-serif", "oblique", Font.WEIGHT_NORMAL);
  131. fontInfo.addFontProperties("F2", "sans-serif", "italic", Font.WEIGHT_NORMAL);
  132. fontInfo.addFontProperties("F3", "sans-serif", "normal", Font.WEIGHT_BOLD);
  133. fontInfo.addFontProperties("F4", "sans-serif", "oblique", Font.WEIGHT_BOLD);
  134. fontInfo.addFontProperties("F4", "sans-serif", "italic", Font.WEIGHT_BOLD);
  135. fontInfo.addFontProperties("F5", "serif", "normal", Font.WEIGHT_NORMAL);
  136. fontInfo.addFontProperties("F6", "serif", "oblique", Font.WEIGHT_NORMAL);
  137. fontInfo.addFontProperties("F6", "serif", "italic", Font.WEIGHT_NORMAL);
  138. fontInfo.addFontProperties("F7", "serif", "normal", Font.WEIGHT_BOLD);
  139. fontInfo.addFontProperties("F8", "serif", "oblique", Font.WEIGHT_BOLD);
  140. fontInfo.addFontProperties("F8", "serif", "italic", Font.WEIGHT_BOLD);
  141. fontInfo.addFontProperties("F9", "monospace", "normal", Font.WEIGHT_NORMAL);
  142. fontInfo.addFontProperties("F10", "monospace", "oblique", Font.WEIGHT_NORMAL);
  143. fontInfo.addFontProperties("F10", "monospace", "italic", Font.WEIGHT_NORMAL);
  144. fontInfo.addFontProperties("F11", "monospace", "normal", Font.WEIGHT_BOLD);
  145. fontInfo.addFontProperties("F12", "monospace", "oblique", Font.WEIGHT_BOLD);
  146. fontInfo.addFontProperties("F12", "monospace", "italic", Font.WEIGHT_BOLD);
  147. fontInfo.addFontProperties("F1", "Helvetica", "normal", Font.WEIGHT_NORMAL);
  148. fontInfo.addFontProperties("F2", "Helvetica", "oblique", Font.WEIGHT_NORMAL);
  149. fontInfo.addFontProperties("F2", "Helvetica", "italic", Font.WEIGHT_NORMAL);
  150. fontInfo.addFontProperties("F3", "Helvetica", "normal", Font.WEIGHT_BOLD);
  151. fontInfo.addFontProperties("F4", "Helvetica", "oblique", Font.WEIGHT_BOLD);
  152. fontInfo.addFontProperties("F4", "Helvetica", "italic", Font.WEIGHT_BOLD);
  153. fontInfo.addFontProperties("F5", "Times", "normal", Font.WEIGHT_NORMAL);
  154. fontInfo.addFontProperties("F6", "Times", "oblique", Font.WEIGHT_NORMAL);
  155. fontInfo.addFontProperties("F6", "Times", "italic", Font.WEIGHT_NORMAL);
  156. fontInfo.addFontProperties("F7", "Times", "normal", Font.WEIGHT_BOLD);
  157. fontInfo.addFontProperties("F8", "Times", "oblique", Font.WEIGHT_BOLD);
  158. fontInfo.addFontProperties("F8", "Times", "italic", Font.WEIGHT_BOLD);
  159. fontInfo.addFontProperties("F9", "Courier", "normal", Font.WEIGHT_NORMAL);
  160. fontInfo.addFontProperties("F10", "Courier", "oblique", Font.WEIGHT_NORMAL);
  161. fontInfo.addFontProperties("F10", "Courier", "italic", Font.WEIGHT_NORMAL);
  162. fontInfo.addFontProperties("F11", "Courier", "normal", Font.WEIGHT_BOLD);
  163. fontInfo.addFontProperties("F12", "Courier", "oblique", Font.WEIGHT_BOLD);
  164. fontInfo.addFontProperties("F12", "Courier", "italic", Font.WEIGHT_BOLD);
  165. fontInfo.addFontProperties("F13", "Symbol", "normal", Font.WEIGHT_NORMAL);
  166. fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", Font.WEIGHT_NORMAL);
  167. // Custom type 1 fonts step 2/2
  168. // fontInfo.addFontProperties("F15", "OMEP", "normal", FontInfo.NORMAL);
  169. // fontInfo.addFontProperties("F16", "Garamond-LightCondensed", "normal", FontInfo.NORMAL);
  170. // fontInfo.addFontProperties("F17", "BauerBodoni", "italic", FontInfo.BOLD);
  171. /* for compatibility with PassiveTex */
  172. fontInfo.addFontProperties("F5", "Times-Roman", "normal", Font.WEIGHT_NORMAL);
  173. fontInfo.addFontProperties("F6", "Times-Roman", "oblique", Font.WEIGHT_NORMAL);
  174. fontInfo.addFontProperties("F6", "Times-Roman", "italic", Font.WEIGHT_NORMAL);
  175. fontInfo.addFontProperties("F7", "Times-Roman", "normal", Font.WEIGHT_BOLD);
  176. fontInfo.addFontProperties("F8", "Times-Roman", "oblique", Font.WEIGHT_BOLD);
  177. fontInfo.addFontProperties("F8", "Times-Roman", "italic", Font.WEIGHT_BOLD);
  178. fontInfo.addFontProperties("F5", "Times Roman", "normal", Font.WEIGHT_NORMAL);
  179. fontInfo.addFontProperties("F6", "Times Roman", "oblique", Font.WEIGHT_NORMAL);
  180. fontInfo.addFontProperties("F6", "Times Roman", "italic", Font.WEIGHT_NORMAL);
  181. fontInfo.addFontProperties("F7", "Times Roman", "normal", Font.WEIGHT_BOLD);
  182. fontInfo.addFontProperties("F8", "Times Roman", "oblique", Font.WEIGHT_BOLD);
  183. fontInfo.addFontProperties("F8", "Times Roman", "italic", Font.WEIGHT_BOLD);
  184. fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter",
  185. "normal", Font.WEIGHT_NORMAL);
  186. configureInstalledAWTFonts(fontInfo, graphics, LAST_PREDEFINED_FONT_NUMBER + 1);
  187. }
  188. private static void configureInstalledAWTFonts(FontInfo fontInfo, Graphics2D graphics,
  189. int startNumber) {
  190. int num = startNumber;
  191. GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  192. String[] allFontFamilies = env.getAvailableFontFamilyNames();
  193. for (int i = 0; i < allFontFamilies.length; i++) {
  194. String family = allFontFamilies[i];
  195. if (HARDCODED_FONT_NAMES.contains(family)) {
  196. continue; //skip
  197. }
  198. if (log.isDebugEnabled()) {
  199. log.debug("Registering: " + family);
  200. }
  201. //Java does not give info about what variants of a font is actually supported, so
  202. //we simply register all the basic variants. If we use GraphicsEnvironment.getAllFonts()
  203. //we don't get reliable info whether a font is italic or bold or both.
  204. int fontStyle;
  205. fontStyle = java.awt.Font.PLAIN;
  206. registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
  207. num++;
  208. fontStyle = java.awt.Font.ITALIC;
  209. registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
  210. num++;
  211. fontStyle = java.awt.Font.BOLD;
  212. registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
  213. num++;
  214. fontStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC;
  215. registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
  216. num++;
  217. }
  218. }
  219. private static void registerFontTriplet(FontInfo fontInfo, String family, int fontStyle,
  220. String fontKey, Graphics2D graphics) {
  221. FontMetricsMapper metric = new FontMetricsMapper(family, fontStyle, graphics);
  222. fontInfo.addMetrics(fontKey, metric);
  223. int weight = Font.WEIGHT_NORMAL;
  224. if ((fontStyle & java.awt.Font.BOLD) != 0) {
  225. weight = Font.WEIGHT_BOLD;
  226. }
  227. String style = "normal";
  228. if ((fontStyle & java.awt.Font.ITALIC) != 0) {
  229. style = "italic";
  230. }
  231. FontTriplet triplet = FontInfo.createFontKey(family, style, weight);
  232. fontInfo.addFontProperties(fontKey, triplet);
  233. }
  234. }