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.

FontDetails.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.Properties;
  19. import java.util.StringTokenizer;
  20. /**
  21. * Stores width and height details about a font.
  22. */
  23. public class FontDetails
  24. {
  25. private String _fontName;
  26. private int _height;
  27. private final Map<Character, Integer> charWidths = new HashMap<>();
  28. /**
  29. * Construct the font details with the given name and height.
  30. *
  31. * @param fontName The font name.
  32. * @param height The height of the font.
  33. */
  34. public FontDetails( String fontName, int height )
  35. {
  36. _fontName = fontName;
  37. _height = height;
  38. }
  39. public String getFontName()
  40. {
  41. return _fontName;
  42. }
  43. public int getHeight()
  44. {
  45. return _height;
  46. }
  47. public void addChar( char c, int width )
  48. {
  49. charWidths.put(Character.valueOf(c), Integer.valueOf(width));
  50. }
  51. /**
  52. * Retrieves the width of the specified character. If the metrics for
  53. * a particular character are not available it defaults to returning the
  54. * width for the 'W' character.
  55. */
  56. public int getCharWidth( char c )
  57. {
  58. Integer widthInteger = charWidths.get(Character.valueOf(c));
  59. if (widthInteger == null) {
  60. return 'W' == c ? 0 : getCharWidth('W');
  61. }
  62. return widthInteger;
  63. }
  64. public void addChars( char[] characters, int[] widths )
  65. {
  66. for ( int i = 0; i < characters.length; i++ )
  67. {
  68. charWidths.put( Character.valueOf(characters[i]), Integer.valueOf(widths[i]));
  69. }
  70. }
  71. protected static String buildFontHeightProperty(String fontName) {
  72. return "font." + fontName + ".height";
  73. }
  74. protected static String buildFontWidthsProperty(String fontName) {
  75. return "font." + fontName + ".widths";
  76. }
  77. protected static String buildFontCharactersProperty(String fontName) {
  78. return "font." + fontName + ".characters";
  79. }
  80. /**
  81. * Create an instance of <code>FontDetails</code> by loading them from the
  82. * provided property object.
  83. * @param fontName the font name
  84. * @param fontMetricsProps the property object holding the details of this
  85. * particular font.
  86. * @return a new FontDetails instance.
  87. */
  88. public static FontDetails create( String fontName, Properties fontMetricsProps )
  89. {
  90. String heightStr = fontMetricsProps.getProperty( buildFontHeightProperty(fontName) );
  91. String widthsStr = fontMetricsProps.getProperty( buildFontWidthsProperty(fontName) );
  92. String charactersStr = fontMetricsProps.getProperty( buildFontCharactersProperty(fontName) );
  93. // Ensure that this is a font we know about
  94. if(heightStr == null || widthsStr == null || charactersStr == null) {
  95. // We don't know all we need to about this font
  96. // Since we don't know its sizes, we can't work with it
  97. throw new IllegalArgumentException("The supplied FontMetrics doesn't know about the font '" + fontName + "', so we can't use it. Please add it to your font metrics file (see StaticFontMetrics.getFontDetails");
  98. }
  99. int height = Integer.parseInt(heightStr);
  100. FontDetails d = new FontDetails(fontName, height);
  101. String[] charactersStrArray = split(charactersStr, ",", -1);
  102. String[] widthsStrArray = split(widthsStr, ",", -1);
  103. if (charactersStrArray.length != widthsStrArray.length)
  104. throw new RuntimeException("Number of characters does not number of widths for font " + fontName);
  105. for ( int i = 0; i < widthsStrArray.length; i++ )
  106. {
  107. if (charactersStrArray[i].length() != 0)
  108. d.addChar(charactersStrArray[i].charAt(0), Integer.parseInt(widthsStrArray[i]));
  109. }
  110. return d;
  111. }
  112. /**
  113. * Gets the width of all characters in a string.
  114. *
  115. * @param str The string to measure.
  116. * @return The width of the string for a 10 point font.
  117. */
  118. public int getStringWidth(String str)
  119. {
  120. int width = 0;
  121. for (int i = 0; i < str.length(); i++)
  122. {
  123. width += getCharWidth(str.charAt(i));
  124. }
  125. return width;
  126. }
  127. /**
  128. * Split the given string into an array of strings using the given
  129. * delimiter.
  130. */
  131. private static String[] split(String text, String separator, int max)
  132. {
  133. StringTokenizer tok = new StringTokenizer(text, separator);
  134. int listSize = tok.countTokens();
  135. if(max != -1 && listSize > max)
  136. listSize = max;
  137. String[] list = new String[listSize];
  138. for(int i = 0; tok.hasMoreTokens(); i++)
  139. {
  140. if(max != -1 && i == listSize - 1)
  141. {
  142. StringBuilder buf = new StringBuilder((text.length() * (listSize - i)) / listSize);
  143. while(tok.hasMoreTokens())
  144. {
  145. buf.append(tok.nextToken());
  146. if(tok.hasMoreTokens())
  147. buf.append(separator);
  148. }
  149. list[i] = buf.toString().trim();
  150. break;
  151. }
  152. list[i] = tok.nextToken().trim();
  153. }
  154. return list;
  155. }
  156. }