1. /*
  2. * @(#)PangoFonts.java 1.7 03/06/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.gtk;
  8. import java.awt.*;
  9. import java.awt.geom.AffineTransform;
  10. import javax.swing.plaf.FontUIResource;
  11. import java.util.StringTokenizer;
  12. import sun.java2d.SunGraphicsEnvironment;
  13. /**
  14. * @author Shannon Hickey
  15. * @author Leif Samuelsson
  16. * @version 1.7 06/23/03
  17. */
  18. class PangoFonts {
  19. // A simple array for now, but this could be a HashMap if
  20. // many more mappings are added
  21. private static final String[][] nameMap = {{"sans", "sansserif"},
  22. {"monospace", "monospaced"}};
  23. /**
  24. * Amount to scale fonts by.
  25. */
  26. private static double fontScale;
  27. static {
  28. GraphicsEnvironment ge =
  29. GraphicsEnvironment.getLocalGraphicsEnvironment();
  30. GraphicsConfiguration gc =
  31. ge.getDefaultScreenDevice().getDefaultConfiguration();
  32. AffineTransform at = gc.getNormalizingTransform();
  33. fontScale = at.getScaleY();
  34. }
  35. private static String mapName(String name) {
  36. for (int i = 0; i < nameMap.length; i++) {
  37. if (name.equals(nameMap[i][0])) {
  38. return nameMap[i][1];
  39. }
  40. }
  41. return null;
  42. }
  43. /**
  44. * Parses a String containing a pango font description and returns
  45. * a Font object.
  46. *
  47. * @param pangoName a String describing a pango font
  48. * e.g. "Sans Italic 10"
  49. * @return a Font object as a FontUIResource
  50. * or null if no suitable font could be created.
  51. */
  52. static Font lookupFont(String pangoName) {
  53. String family = "";
  54. int style = Font.PLAIN;
  55. int size = 10;
  56. StringTokenizer tok = new StringTokenizer(pangoName);
  57. while (tok.hasMoreTokens()) {
  58. String word = tok.nextToken();
  59. if (word.equalsIgnoreCase("italic")) {
  60. style |= Font.ITALIC;
  61. } else if (word.equalsIgnoreCase("bold")) {
  62. style |= Font.BOLD;
  63. } else if (GTKScanner.CHARS_DIGITS.indexOf(word.charAt(0)) != -1) {
  64. try {
  65. size = Integer.parseInt(word);
  66. } catch (NumberFormatException ex) {
  67. }
  68. } else {
  69. if (family.length() > 0) {
  70. family += " ";
  71. }
  72. family += word;
  73. }
  74. }
  75. // Scale the font
  76. size = (int)(size * fontScale);
  77. // Retrieve the DPI setting, if available, and scale the font
  78. // accordingly.
  79. int dpi = 96;
  80. Object value =
  81. Toolkit.getDefaultToolkit().getDesktopProperty("gnome.Xft/DPI");
  82. if (value instanceof Integer) {
  83. dpi = ((Integer)value).intValue() / 1024;
  84. }
  85. size = (int)((double)dpi * (double)size / 96.0);
  86. if (size < 1) {
  87. size = 1;
  88. }
  89. String mappedName = mapName(family.toLowerCase());
  90. if (mappedName != null) {
  91. family = mappedName;
  92. }
  93. Font font = new FontUIResource(family, style, size);
  94. if (!SunGraphicsEnvironment.isLogicalFont(font) &&
  95. !SunGraphicsEnvironment.fontSupportsDefaultEncoding(font)) {
  96. // Font does not contain enough glyphs for this locale, fallback
  97. // to SansSerif.
  98. // PENDING: should create a composite Font here.
  99. font = new FontUIResource("sansserif", style, size);
  100. }
  101. return font;
  102. }
  103. }