選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

InstalledFontCollection.java 5.1KB

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