1. /*
  2. * @(#)PangoFonts.java 1.11 04/03/31
  3. *
  4. * Copyright 2004 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.font.FontManager;
  13. /**
  14. * @author Shannon Hickey
  15. * @author Leif Samuelsson
  16. * @version 1.11 03/31/04
  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. * Calculate a default scale factor for fonts in this L&F to match
  25. * the reported resolution of the screen.
  26. * Java 2D specified a default user-space scale of 72dpi.
  27. * This is unlikely to correspond to that of the real screen.
  28. * The Xserver reports a value which may be used to adjust for this.
  29. * and Java 2D exposes it via a normalizing transform.
  30. * However many Xservers report a hard-coded 90dpi whilst others report a
  31. * calculated value based on possibly incorrect data.
  32. * That is something that must be solved at the X11 level
  33. * Note that in an X11 multi-screen environment, the default screen
  34. * is the one used by the JRE so it is safe to use it here.
  35. */
  36. private static double fontScale;
  37. static {
  38. GraphicsEnvironment ge =
  39. GraphicsEnvironment.getLocalGraphicsEnvironment();
  40. GraphicsConfiguration gc =
  41. ge.getDefaultScreenDevice().getDefaultConfiguration();
  42. AffineTransform at = gc.getNormalizingTransform();
  43. fontScale = at.getScaleY();
  44. }
  45. private static String mapName(String name) {
  46. for (int i = 0; i < nameMap.length; i++) {
  47. if (name.equals(nameMap[i][0])) {
  48. return nameMap[i][1];
  49. }
  50. }
  51. return null;
  52. }
  53. /**
  54. * Parses a String containing a pango font description and returns
  55. * a Font object.
  56. *
  57. * @param pangoName a String describing a pango font
  58. * e.g. "Sans Italic 10"
  59. * @return a Font object as a FontUIResource
  60. * or null if no suitable font could be created.
  61. */
  62. static Font lookupFont(String pangoName) {
  63. String family = "";
  64. int style = Font.PLAIN;
  65. int size = 10;
  66. StringTokenizer tok = new StringTokenizer(pangoName);
  67. while (tok.hasMoreTokens()) {
  68. String word = tok.nextToken();
  69. if (word.equalsIgnoreCase("italic")) {
  70. style |= Font.ITALIC;
  71. } else if (word.equalsIgnoreCase("bold")) {
  72. style |= Font.BOLD;
  73. } else if (GTKScanner.CHARS_DIGITS.indexOf(word.charAt(0)) != -1) {
  74. try {
  75. size = Integer.parseInt(word);
  76. } catch (NumberFormatException ex) {
  77. }
  78. } else {
  79. if (family.length() > 0) {
  80. family += " ";
  81. }
  82. family += word;
  83. }
  84. }
  85. /*
  86. * Java 2D font point sizes are in a user-space scale of 72dpi.
  87. * GTK allows a user to configure a "dpi" property used to scale
  88. * the fonts used to match a user's preference.
  89. * To match the font size of GTK apps we need to obtain this DPI and
  90. * adjust as follows:
  91. * Some versions of GTK use XSETTINGS if available to dynamically
  92. * monitor user-initiated changes in the DPI to be used by GTK
  93. * apps. This value is also made available as the Xft.dpi X resource.
  94. * This is presumably a function of the font preferences API and/or
  95. * the manner in which it requests the toolkit to update the default
  96. * for the desktop. This dual approach is probably necessary since
  97. * other versions of GTK - or perhaps some apps - determine the size
  98. * to use only at start-up from that X resource.
  99. * If that resource is not set then GTK scales for the DPI resolution
  100. * reported by the Xserver using the formula
  101. * DisplayHeight(dpy, screen) / DisplayHeightMM(dpy, screen) * 25.4
  102. * (25.4mm == 1 inch).
  103. * JDK tracks the Xft.dpi XSETTINGS property directly so it can
  104. * dynamically change font size by tracking just that value.
  105. * If that resource is not available use the same fall back formula
  106. * as GTK (see calculation for fontScale).
  107. *
  108. * GTK's default setting for Xft.dpi is 96 dpi (and it seems -1
  109. * apparently also can mean that "default"). However this default
  110. * isn't used if there's no property set. The real default in the
  111. * absence of a resource is the Xserver reported dpi.
  112. * Finally this DPI is used to calculate the nearest Java 2D font
  113. * 72 dpi font size.
  114. * There are cases in which JDK behaviour may not exactly mimic
  115. * GTK native app behaviour :
  116. * 1) When a GTK app is not able to dynamically track the changes
  117. * (does not use XSETTINGS), JDK will resize but other apps will
  118. * not. This is OK as JDK is exhibiting preferred behaviour and
  119. * this is probably how all later GTK apps will behave
  120. * 2) When a GTK app does not use XSETTINGS and for some reason
  121. * the XRDB property is not present. JDK will pick up XSETTINGS
  122. * and the GTK app will use the Xserver default. Since its
  123. * impossible for JDK to know that some other GTK app is not
  124. * using XSETTINGS its impossible to account for this and in any
  125. * case for it to be a problem the values would have to be different.
  126. * It also seems unlikely to arise except when a user explicitly
  127. * deletes the X resource database entry.
  128. * 3) Because of rounding errors sizes may differ very slightly
  129. * between JDK and GTK. To fix that would at the very least require
  130. * Swing to specify floating pt font sizes.
  131. * Eg "10 pts" for GTK at 96 dpi to get the same size at Java 2D's
  132. * 72 dpi you'd need to specify exactly 13.33.
  133. * There also some other issues to be aware of for the future:
  134. * GTK specifies the Xft.dpi value as server-wide which when used
  135. * on systems with 2 distinct X screens with different physical DPI
  136. * the font sizes will inevitably appear different. It would have
  137. * been a more user-friendly design to further adjust that one
  138. * setting depending on the screen resolution to achieve perceived
  139. * equivalent sizes. If such a change were ever to be made in GTK
  140. * we would need to update for that.
  141. */
  142. double dsize = size;
  143. int dpi = 96;
  144. Object value =
  145. Toolkit.getDefaultToolkit().getDesktopProperty("gnome.Xft/DPI");
  146. if (value instanceof Integer) {
  147. dpi = ((Integer)value).intValue() / 1024;
  148. if (dpi == -1) {
  149. dpi = 96;
  150. }
  151. if (dpi < 50) { /* 50 dpi is the minimum value gnome allows */
  152. dpi = 50;
  153. }
  154. /* The Java rasteriser assumes pts are in a user space of
  155. * 72 dpi, so we need to adjust for that.
  156. */
  157. dsize = ((double)(dpi * size)/ 72.0);
  158. } else {
  159. /* If there's no property, GTK scales for the resolution
  160. * reported by the Xserver using the formula listed above.
  161. * fontScale already accounts for the 72 dpi Java 2D space.
  162. */
  163. dsize = size * fontScale;
  164. }
  165. /* Round size to nearest integer pt size */
  166. size = (int)(dsize + 0.5);
  167. if (size < 1) {
  168. size = 1;
  169. }
  170. String mappedName = mapName(family.toLowerCase());
  171. if (mappedName != null) {
  172. family = mappedName;
  173. }
  174. Font font = new FontUIResource(family, style, size);
  175. if (!FontManager.fontSupportsDefaultEncoding(font)) {
  176. font = FontManager.getCompositeFontUIResource(font);
  177. }
  178. return font;
  179. }
  180. }