2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // Copyright (C) 2003-2007 Ethalinda K.S. Cannon
10 // All rights reserved
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 // Contact: phylosoft @ gmail . com
27 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
29 package org.forester.archaeopteryx;
32 import java.awt.FontMetrics;
35 * Maintains the fonts for drawing a tree.
37 public final class TreeFontSet {
39 static final int BOLD_AND_ITALIC = Font.BOLD + Font.ITALIC;
40 final static float FONT_SIZE_CHANGE_STEP = 1.0f;
41 final static float SMALL_FONTS_BASE = 8;
42 private final static String DEFAULT_FONT = "Verdana";
43 private Font _base_font;
44 private boolean _decreased_size_by_system = false;
45 private FontMetrics _fm_large;
46 // Handy holders for font metrics
47 private FontMetrics _fm_small;
48 private Font _large_font;
49 private Font _large_font_memory;
50 private Font _large_font_system;
51 private final int _max;
52 private final int _min;
53 // the owner (needed to get font metrics)
54 private final MainPanel _owner;
56 private Font _small_font;
57 private Font _small_font_memory;
58 private Font _small_font_system;
59 private int _small_max_ascent = 0;
60 // hold font measurements
61 private int _small_max_descent = 0;
63 TreeFontSet( final MainPanel owner ) {
65 _min = _owner.getConfiguration().getMinBaseFontSize();
66 _max = _owner.getConfiguration().getMinBaseFontSize();
67 setBaseFont( new Font( DEFAULT_FONT, Font.PLAIN, 10 ) );
70 public FontMetrics getFontMetricsLarge() {
74 public FontMetrics getFontMetricsSmall() {
78 public Font getSmallFont() {
82 public int getSmallMaxAscent() {
83 return _small_max_ascent;
86 public int getSmallMaxDescent() {
87 return _small_max_descent;
90 void decreaseFontSize( final int min, final boolean decreased_size_by_system ) {
91 if ( decreased_size_by_system && !isDecreasedSizeBySystem() ) {
92 _small_font_memory = _small_font;
93 _large_font_memory = _large_font;
95 setDecreasedSizeBySystem( decreased_size_by_system );
96 if ( _large_font.getSize() >= min ) {
97 _small_font = _small_font.deriveFont( _small_font.getSize() - FONT_SIZE_CHANGE_STEP );
98 _large_font = _large_font.deriveFont( _large_font.getSize() - FONT_SIZE_CHANGE_STEP );
107 Font getLargeFont() {
111 Font getLargeFontMemory() {
112 return _large_font_memory;
115 Font getSmallFontSystem() {
116 return _small_font_system;
119 void increaseFontSize() {
120 _small_font = _small_font.deriveFont( _small_font.getSize() + FONT_SIZE_CHANGE_STEP );
121 _large_font = _large_font.deriveFont( _large_font.getSize() + FONT_SIZE_CHANGE_STEP );
125 boolean isDecreasedSizeBySystem() {
126 return _decreased_size_by_system;
130 setDecreasedSizeBySystem( false );
131 _small_font = _small_font.deriveFont( 12f );
132 _large_font = _large_font.deriveFont( 14f );
137 setDecreasedSizeBySystem( false );
138 _small_font = _small_font.deriveFont( 8f );
139 _large_font = _large_font.deriveFont( 10f );
144 _large_font_system = _large_font;
147 void setBaseFont( final Font base_font ) {
148 _base_font = base_font;
153 setDecreasedSizeBySystem( false );
154 _small_font = _small_font.deriveFont( SMALL_FONTS_BASE - 1 );
155 _large_font = _large_font.deriveFont( SMALL_FONTS_BASE );
159 void superTinyFonts() {
160 setDecreasedSizeBySystem( false );
161 _small_font = _small_font.deriveFont( 2f );
162 _large_font = _large_font.deriveFont( 3f );
167 setDecreasedSizeBySystem( false );
168 _small_font = _small_font.deriveFont( 5f );
169 _large_font = _large_font.deriveFont( 6f );
173 private Font getLargeFontSystem() {
174 return _large_font_system;
177 private void intializeFonts() {
178 final int small_size = getBaseFont().getSize() - 1;
179 int italic = Font.ITALIC;
180 if ( getBaseFont().getStyle() == Font.BOLD ) {
181 italic = italic + Font.BOLD;
183 _small_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
184 _large_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
185 _small_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
186 _large_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
187 _small_font_memory = _small_font;
188 _large_font_memory = _large_font;
192 private void setDecreasedSizeBySystem( final boolean decreased_size_by_system ) {
193 _decreased_size_by_system = decreased_size_by_system;
196 private void setupFontMetrics() {
197 _fm_small = _owner.getFontMetrics( _small_font );
198 _fm_large = _owner.getFontMetrics( _large_font );
199 _small_max_descent = _fm_small.getMaxDescent();
200 _small_max_ascent = _fm_small.getMaxAscent() + 1;