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.

FontInfo.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.layout;
  8. import java.util.HashMap;
  9. import java.util.Enumeration;
  10. import org.apache.fop.apps.FOPException;
  11. public class FontInfo {
  12. HashMap usedFonts;
  13. HashMap triplets; // look up a font-triplet to find a font-name
  14. HashMap fonts; // look up a font-name to get a font (that implements FontMetric at least)
  15. public FontInfo() {
  16. this.triplets = new HashMap();
  17. this.fonts = new HashMap();
  18. this.usedFonts = new HashMap();
  19. }
  20. public void addFontProperties(String name, String family, String style,
  21. String weight) {
  22. /*
  23. * add the given family, style and weight as a lookup for the font
  24. * with the given name
  25. */
  26. String key = createFontKey(family, style, weight);
  27. this.triplets.put(key, name);
  28. }
  29. public void addMetrics(String name, FontMetric metrics) {
  30. // add the given metrics as a font with the given name
  31. this.fonts.put(name, metrics);
  32. }
  33. public String fontLookup(String family, String style,
  34. String weight) throws FOPException {
  35. return fontLookup(createFontKey(family, style, weight));
  36. }
  37. public String fontLookup(String key) throws FOPException {
  38. String f = (String)this.triplets.get(key);
  39. if (f == null) {
  40. int i = key.indexOf(',');
  41. String s = "any" + key.substring(i);
  42. f = (String)this.triplets.get(s);
  43. if (f == null) {
  44. f = (String)this.triplets.get("any,normal,normal");
  45. if (f == null) {
  46. throw new FOPException("no default font defined by OutputConverter");
  47. }
  48. //log.error("defaulted font to any,normal,normal");
  49. }
  50. //log.error("unknown font " + key
  51. // + " so defaulted font to any");
  52. }
  53. usedFonts.put(f, fonts.get(f));
  54. return f;
  55. }
  56. public boolean hasFont(String family, String style, String weight) {
  57. String key = createFontKey(family, style, weight);
  58. return this.triplets.get(key) != null;
  59. }
  60. /**
  61. * Creates a key from the given strings
  62. */
  63. public static String createFontKey(String family, String style,
  64. String weight) {
  65. int i;
  66. try {
  67. i = Integer.parseInt(weight);
  68. } catch (NumberFormatException e) {
  69. i = 0;
  70. }
  71. if (i > 600)
  72. weight = "bold";
  73. else if (i > 0)
  74. weight = "normal";
  75. return family + "," + style + "," + weight;
  76. }
  77. public HashMap getFonts() {
  78. return this.fonts;
  79. }
  80. public HashMap getUsedFonts() {
  81. return this.usedFonts;
  82. }
  83. public FontMetric getMetricsFor(String fontName) throws FOPException {
  84. usedFonts.put(fontName, fonts.get(fontName));
  85. return (FontMetric)fonts.get(fontName);
  86. }
  87. }