PageRenderTime 68ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/src/viewer/PanelSizeProperties.java

https://bitbucket.org/teamwildtreechase/hatparsing
Java | 302 lines | 233 code | 61 blank | 8 comment | 11 complexity | ff1591ee5d4150aa4e36b81108ed1566 MD5 | raw file
  1. /*
  2. * Copyright (c) <2013> <Gideon Maillette de Buy Wenniger>. All Rights Reserved.
  3. */
  4. package viewer;
  5. import java.awt.Font;
  6. import java.awt.FontFormatException;
  7. import java.awt.FontMetrics;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import javax.swing.JPanel;
  11. public class PanelSizeProperties
  12. {
  13. private int fontSize = 15;
  14. private Font font, smallFont, ultraSmallFont;
  15. private FontMetrics metrics;
  16. private int topMargin = 60;
  17. private int bottomMargin = 60;
  18. private int leftMargin = 20;
  19. private int rightMargin = 30;
  20. private int wordSpace = 10;
  21. private int textTopMargin = 4;
  22. private int levelSizeFactor = 5;
  23. private int levelSize = 2;
  24. private final String fontName = "Arial Unicode MS"; //"GNU Unifont" "unifont";// "Gentium";
  25. private final String ARIAL_UNICODE_FONT_FILE_NAME = "ARIALUNI.TTF"; // Not open source, but otherwise the best font
  26. private final String CODE2000_FONT_FILE_NAME = "CODE2000.TTF"; // Not open source, but otherwise the best font
  27. //private final String fontFileName = "CODE2000.TTF";
  28. private final String DEFAULT_FONT_FILE_NAME = "DejaVuSans.ttf"; // We settle for this as the best "free" TTF font, although limited in scope
  29. //private final String fontFileName = "LucidaSansRegular.ttf";
  30. private final Font basicUnicodeFont = createBasicUnicodeFontFromStream();
  31. private static final boolean UseSourceFolderToGetUnicodeFont = true;
  32. public int fontSize()
  33. {
  34. return fontSize;
  35. }
  36. public int smallFontSize()
  37. {
  38. int result = fontSize - 4;
  39. if(result > 1)
  40. {
  41. return result;
  42. }
  43. return 1;
  44. }
  45. public int mediumFontSize()
  46. {
  47. int result = fontSize - 3;
  48. if(result > 1)
  49. {
  50. return result;
  51. }
  52. return 1;
  53. }
  54. public int ultraSmallFontSize()
  55. {
  56. int result = fontSize - 5;
  57. if(result > 1)
  58. {
  59. return result;
  60. }
  61. return 1;
  62. }
  63. public int fontDescendent()
  64. {
  65. return metrics.getDescent();
  66. }
  67. public int fontAscent()
  68. {
  69. return metrics.getAscent();
  70. }
  71. public Font font()
  72. {
  73. return font;
  74. }
  75. public Font smallFont()
  76. {
  77. return smallFont;
  78. }
  79. public Font ultraSmallFont()
  80. {
  81. return ultraSmallFont;
  82. }
  83. public int topMargin()
  84. {
  85. return topMargin;
  86. }
  87. public int bottomMargin()
  88. {
  89. return bottomMargin;
  90. }
  91. public int leftMargin()
  92. {
  93. return leftMargin;
  94. }
  95. public int rightMargin()
  96. {
  97. return rightMargin;
  98. }
  99. public int textTopMargin()
  100. {
  101. return textTopMargin;
  102. }
  103. public void setLevelSize(int levelSize)
  104. {
  105. this.levelSize = levelSize;
  106. }
  107. public void setLevelSizeFactor(int levelSizeFactor)
  108. {
  109. this.levelSizeFactor = levelSizeFactor;
  110. }
  111. public int fontHeight()
  112. {
  113. return metrics.getHeight();
  114. }
  115. public int doubleFontHeight()
  116. {
  117. return 2 * fontHeight();
  118. }
  119. public int tripleFontHeight()
  120. {
  121. return 3 * fontHeight();
  122. }
  123. public int levelSize()
  124. {
  125. return levelSize;
  126. }
  127. public int wordSpace()
  128. {
  129. return wordSpace;
  130. }
  131. public void setWordSpace(int wordSpace)
  132. {
  133. this.wordSpace = wordSpace;
  134. }
  135. public void setTopMargin(int topMargin)
  136. {
  137. this.topMargin = topMargin;
  138. }
  139. public int increaseFontSize() {
  140. return ++fontSize;
  141. }
  142. public int decreaseFontSize() {
  143. if (fontSize==1) return 1;
  144. return --fontSize;
  145. }
  146. public int stringWidth(String word)
  147. {
  148. return this.metrics.stringWidth(word);
  149. }
  150. public static int computeStringRenderLength(JPanel panel, String word, Font font)
  151. {
  152. FontMetrics metrics = panel.getFontMetrics(font);
  153. return metrics.stringWidth(word);
  154. }
  155. public int getMaxNumLabelsTree()
  156. {
  157. return this.levelSizeFactor - 2;
  158. }
  159. private InputStream getArialUnicodeFontIfAvailable()
  160. {
  161. InputStream fontStream = PanelSizeProperties.class.getResourceAsStream("/" + ARIAL_UNICODE_FONT_FILE_NAME);
  162. return fontStream;
  163. }
  164. private InputStream getCode2000FontIfAvailable()
  165. {
  166. InputStream fontStream = PanelSizeProperties.class.getResourceAsStream("/" + CODE2000_FONT_FILE_NAME);
  167. return fontStream;
  168. }
  169. private InputStream getDefaultFont()
  170. {
  171. InputStream fontStream = PanelSizeProperties.class.getResourceAsStream("/" + DEFAULT_FONT_FILE_NAME);
  172. return fontStream;
  173. }
  174. private InputStream getBestAvailableFont()
  175. {
  176. // First try to get the ARIAL Unicode font if it is available
  177. InputStream fontStream = getArialUnicodeFontIfAvailable();
  178. if(fontStream == null)
  179. {
  180. // Otherwise use Code2000 Font if available
  181. fontStream = getCode2000FontIfAvailable();
  182. }
  183. if(fontStream == null)
  184. {
  185. // Otherwise use the default font
  186. fontStream = getDefaultFont();
  187. }
  188. if(fontStream == null)
  189. {
  190. throw new RuntimeException("Font \"" + DEFAULT_FONT_FILE_NAME + "\" not found... make sure it is in the right place");
  191. }
  192. return fontStream;
  193. }
  194. private Font createBasicUnicodeFontFromStream()
  195. {
  196. InputStream fontStream = getBestAvailableFont();
  197. try
  198. {
  199. Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
  200. return font;
  201. }
  202. catch (FontFormatException e)
  203. {
  204. e.printStackTrace();
  205. throw new RuntimeException(e);
  206. } catch (IOException e)
  207. {
  208. e.printStackTrace();
  209. throw new RuntimeException(e);
  210. }
  211. }
  212. private Font createUnicodeFont(int style, int fontSize)
  213. {
  214. return basicUnicodeFont.deriveFont(style, (float)fontSize);
  215. }
  216. private void createFontsFromStream()
  217. {
  218. font = createUnicodeFont(Font.PLAIN, fontSize);
  219. smallFont = createUnicodeFont(Font.PLAIN, smallFontSize());
  220. ultraSmallFont = createUnicodeFont(Font.PLAIN, ultraSmallFontSize());
  221. }
  222. private void createFontsFromJaveJreFonts()
  223. {
  224. font = new Font(fontName, Font.PLAIN, fontSize);
  225. smallFont = new Font(fontName, Font.PLAIN, smallFontSize());
  226. ultraSmallFont = new Font(fontName, Font.PLAIN, ultraSmallFontSize());
  227. }
  228. public void loadFont(JPanel panel)
  229. {
  230. if(UseSourceFolderToGetUnicodeFont)
  231. {
  232. createFontsFromStream();
  233. }
  234. else
  235. {
  236. createFontsFromJaveJreFonts();
  237. }
  238. metrics = panel.getFontMetrics(font);
  239. levelSize = fontHeight() * levelSizeFactor;
  240. }
  241. }