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.

FontMetricsMapper.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.render.awt;
  8. // FOP
  9. import org.apache.fop.layout.FontInfo;
  10. import org.apache.fop.layout.FontDescriptor;
  11. import org.apache.fop.layout.FontState;
  12. // Java
  13. import java.util.Enumeration;
  14. import java.util.Hashtable;
  15. import java.awt.Graphics2D;
  16. import java.awt.Font;
  17. /**
  18. * This class implements org.apache.fop.layout.FontMetric and
  19. * is added to the hash table in FontInfo. It deferes the
  20. * actual calculation of the metrics to
  21. * AWTFontMetrics. It only keeps the java name and
  22. * style as member varibles
  23. */
  24. public class FontMetricsMapper implements org.apache.fop.layout.FontMetric {
  25. /**
  26. * The first and last non space-character
  27. */
  28. private static final int FIRST_CHAR = 32;
  29. private static final int LAST_CHAR = 255;
  30. /**
  31. * This is a AWTFontMetrics that does the real calculation.
  32. * It is only one class that dynamically determines the font-size.
  33. */
  34. private static AWTFontMetrics metric = null;
  35. /**
  36. * The java name of the font.
  37. * # Make the family name immutable.
  38. */
  39. private final String family;
  40. /**
  41. * The java style of the font.
  42. * # Make the style immutable.
  43. */
  44. private final int style;
  45. /**
  46. * Constructs a new Font-metrics.
  47. * @param family the family name of the font (java value)
  48. * @param style the java type style value of the font
  49. * @param parent an AWT component - this is needed so
  50. * that we can get an instance of
  51. * java.awt.FontMetrics
  52. */
  53. public FontMetricsMapper(String family, int style, Graphics2D graphics) {
  54. this.family = family;
  55. this.style = style;
  56. if (metric == null)
  57. metric = new AWTFontMetrics(graphics);
  58. }
  59. /**
  60. * Determines the font ascent of the Font described by this
  61. * FontMetrics object
  62. * @return ascent in milliponts
  63. */
  64. public int getAscender(int size) {
  65. return metric.getAscender(family, style, size);
  66. }
  67. /**
  68. * The size of a capital letter measured from the font's baseline
  69. */
  70. public int getCapHeight(int size) {
  71. return metric.getCapHeight(family, style, size);
  72. }
  73. /**
  74. * Determines the font descent of the Font described by this
  75. * FontMetrics object
  76. * @return descent in milliponts
  77. */
  78. public int getDescender(int size) {
  79. return metric.getDescender(family, style, size);
  80. }
  81. /**
  82. * Determines the typical font height of this
  83. * FontMetrics object
  84. * @return font height in milliponts
  85. */
  86. public int getXHeight(int size) {
  87. return metric.getXHeight(family, style, size);
  88. }
  89. public int getFirstChar() {
  90. return FIRST_CHAR;
  91. }
  92. public int getLastChar() {
  93. return LAST_CHAR;
  94. }
  95. /**
  96. * return width (in 1/1000ths of point size) of character at
  97. * code point i.
  98. */
  99. public int width(int i, int size) {
  100. return metric.width(i, family, style, size);
  101. }
  102. /**
  103. * return width (in 1/1000ths of point size) of all character
  104. */
  105. public int[] getWidths(int size) {
  106. return metric.getWidths(family, style, size);
  107. }
  108. /**
  109. * Gets a Font instance of the Font that this
  110. * FontMetrics describes in the desired size.
  111. * @return font with the desired characeristics.
  112. */
  113. public Font getFont(int size) {
  114. return metric.getFont(family, style, size);
  115. }
  116. }