PageRenderTime 137ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/usermodel/FontDetails.java

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