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

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