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.

FontState.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.layout;
  41. import org.apache.fop.apps.FOPException;
  42. import org.apache.fop.fo.properties.FontVariant;
  43. public class FontState {
  44. protected FontInfo fontInfo;
  45. private String fontName;
  46. private int fontSize;
  47. private String fontFamily;
  48. private String fontStyle;
  49. private String fontWeight;
  50. private FontMetric metric;
  51. private int fontVariant;
  52. public FontState(FontInfo fontInfo, String fontFamily, String fontStyle, String fontWeight, int fontSize, int fontVariant) throws FOPException {
  53. this.fontInfo = fontInfo;
  54. this.fontFamily = fontFamily;
  55. this.fontStyle = fontStyle;
  56. this.fontWeight = fontWeight;
  57. this.fontSize = fontSize;
  58. this.fontName = fontInfo.fontLookup(fontFamily,fontStyle,fontWeight);
  59. this.metric = fontInfo.getMetricsFor(fontName);
  60. this.fontVariant = fontVariant;
  61. }
  62. public int getAscender() {
  63. return metric.getAscender(fontSize) / 1000;
  64. }
  65. public int getCapHeight() {
  66. return metric.getCapHeight(fontSize) / 1000;
  67. }
  68. public int getDescender() {
  69. return metric.getDescender(fontSize) / 1000;
  70. }
  71. public String getFontName() {
  72. return this.fontName;
  73. }
  74. public int getFontSize() {
  75. return this.fontSize;
  76. }
  77. public String getFontWeight() {
  78. return this.fontWeight;
  79. }
  80. public String getFontFamily() {
  81. return this.fontFamily;
  82. }
  83. public String getFontStyle() {
  84. return this.fontStyle;
  85. }
  86. public int getFontVariant() {
  87. return this.fontVariant;
  88. }
  89. public FontInfo getFontInfo() {
  90. return this.fontInfo;
  91. }
  92. public int getXHeight() {
  93. return metric.getXHeight(fontSize) / 1000;
  94. }
  95. public java.util.Hashtable getKerning() {
  96. java.util.Hashtable ret=new java.util.Hashtable();
  97. try {
  98. FontMetric fm=fontInfo.getMetricsFor(fontFamily, fontStyle,
  99. fontWeight);
  100. if (fm instanceof org.apache.fop.layout.FontDescriptor) {
  101. org.apache.fop.layout.FontDescriptor fdes=
  102. (org.apache.fop.layout.FontDescriptor)fm;
  103. ret=fdes.getKerningInfo();
  104. }
  105. } catch (Exception e) {}
  106. return ret;
  107. }
  108. public int width(int charnum) {
  109. // returns width of given character number in millipoints
  110. return (metric.width(charnum, fontSize) / 1000);
  111. }
  112. /**
  113. * Map a java character (unicode) to a font character
  114. * Default uses CodePointMapping
  115. */
  116. public char mapChar(char c) {
  117. try {
  118. FontMetric fm=fontInfo.getMetricsFor(fontFamily, fontStyle,
  119. fontWeight);
  120. if (fm instanceof org.apache.fop.render.pdf.Font) {
  121. org.apache.fop.render.pdf.Font f=
  122. (org.apache.fop.render.pdf.Font)fm;
  123. return f.mapChar(c);
  124. }
  125. } catch (Exception e) {}
  126. // Use default CodePointMapping
  127. if (c > 127) {
  128. char d = org.apache.fop.render.pdf.CodePointMapping.map[c];
  129. if (d != 0) {
  130. c = d;
  131. } else {
  132. c = '#';
  133. }
  134. }
  135. return c;
  136. }
  137. }