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.

InstalledFontCollection.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. import java.awt.Graphics2D;
  20. import java.awt.GraphicsEnvironment;
  21. import java.util.Set;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.fonts.Font;
  25. import org.apache.fop.fonts.FontCollection;
  26. import org.apache.fop.fonts.FontInfo;
  27. import org.apache.fop.fonts.FontTriplet;
  28. import org.apache.fop.fonts.FontUtil;
  29. /**
  30. * A custom AWT font collection
  31. */
  32. public class InstalledFontCollection implements FontCollection {
  33. private static Log log = LogFactory.getLog(InstalledFontCollection.class);
  34. private static final Set<String> HARDCODED_FONT_NAMES;
  35. static {
  36. HARDCODED_FONT_NAMES = new java.util.HashSet<String>();
  37. HARDCODED_FONT_NAMES.add("any");
  38. HARDCODED_FONT_NAMES.add("sans-serif");
  39. HARDCODED_FONT_NAMES.add("serif");
  40. HARDCODED_FONT_NAMES.add("monospace");
  41. HARDCODED_FONT_NAMES.add("Helvetica");
  42. HARDCODED_FONT_NAMES.add("Times");
  43. HARDCODED_FONT_NAMES.add("Courier");
  44. HARDCODED_FONT_NAMES.add("Symbol");
  45. HARDCODED_FONT_NAMES.add("ZapfDingbats");
  46. HARDCODED_FONT_NAMES.add("Times Roman");
  47. HARDCODED_FONT_NAMES.add("Times-Roman");
  48. HARDCODED_FONT_NAMES.add("Computer-Modern-Typewriter");
  49. }
  50. /** Required by new instances of FontMetricsMapper */
  51. private final Java2DFontMetrics java2DFontMetrics;
  52. /**
  53. * Main constructor
  54. *
  55. * @param java2DFontMetrics required by new instances of FontMetricsMapper
  56. */
  57. public InstalledFontCollection(Java2DFontMetrics java2DFontMetrics) {
  58. this.java2DFontMetrics = java2DFontMetrics;
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public int setup(int start, FontInfo fontInfo) {
  64. int num = start;
  65. GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  66. java.awt.Font[] fonts = env.getAllFonts();
  67. for (int i = 0; i < fonts.length; i++) {
  68. java.awt.Font f = fonts[i];
  69. if (HARDCODED_FONT_NAMES.contains(f.getName())) {
  70. continue; //skip
  71. }
  72. if (log.isTraceEnabled()) {
  73. log.trace("AWT Font: " + f.getFontName()
  74. + ", family: " + f.getFamily()
  75. + ", PS: " + f.getPSName()
  76. + ", Name: " + f.getName()
  77. + ", Angle: " + f.getItalicAngle()
  78. + ", Style: " + f.getStyle());
  79. }
  80. String searchName = FontUtil.stripWhiteSpace(f.getName()).toLowerCase();
  81. String guessedStyle = FontUtil.guessStyle(searchName);
  82. int guessedWeight = FontUtil.guessWeight(searchName);
  83. num++;
  84. String fontKey = "F" + num;
  85. int style = convertToAWTFontStyle(guessedStyle, guessedWeight);
  86. addFontMetricsMapper(fontInfo, f.getName(), fontKey, java2DFontMetrics, style);
  87. //Register appropriate font triplets matching the font. Two different strategies:
  88. //Example: "Arial Bold", normal, normal
  89. addFontTriplet(fontInfo, f.getName(),
  90. Font.STYLE_NORMAL, Font.WEIGHT_NORMAL, fontKey);
  91. if (!f.getName().equals(f.getFamily())) {
  92. //Example: "Arial", bold, normal
  93. addFontTriplet(fontInfo, f.getFamily(),
  94. guessedStyle, guessedWeight, fontKey);
  95. }
  96. }
  97. return num;
  98. }
  99. private static void addFontTriplet(FontInfo fontInfo, String fontName, String fontStyle,
  100. int fontWeight, String fontKey) {
  101. FontTriplet triplet = FontInfo.createFontKey(fontName, fontStyle, fontWeight);
  102. fontInfo.addFontProperties(fontKey, triplet);
  103. }
  104. private static void addFontMetricsMapper(FontInfo fontInfo, String family, String fontKey,
  105. Java2DFontMetrics java2DFontMetrics, int style) {
  106. FontMetricsMapper metric = new SystemFontMetricsMapper(family, style, java2DFontMetrics);
  107. fontInfo.addMetrics(fontKey, metric);
  108. }
  109. private static int convertToAWTFontStyle(String fontStyle, int fontWeight) {
  110. int style = java.awt.Font.PLAIN;
  111. if (fontWeight >= Font.WEIGHT_BOLD) {
  112. style |= java.awt.Font.BOLD;
  113. }
  114. if (!"normal".equals(fontStyle)) {
  115. style |= java.awt.Font.ITALIC;
  116. }
  117. return style;
  118. }
  119. }