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 final static float SMALL_FONTS_BASE = 8;
40 private final static String DEFAULT_FONT = "Verdana";
41 private final static float FONT_SIZE_CHANGE_STEP = 1.0f;
42 static final int BOLD_AND_ITALIC = Font.BOLD + Font.ITALIC;
43 // the owner (needed to get font metrics)
44 private final MainPanel _owner;
46 private Font _small_font;
47 private Font _large_font;
48 private Font _small_italic_font;
49 private Font _large_italic_font;
50 private Font _base_font;
51 private Font _small_font_system;
52 private Font _large_font_system;
53 private Font _small_italic_font_system;
54 private Font _large_italic_font_system;
55 // Handy holders for font metrics
56 public FontMetrics _fm_small;
57 FontMetrics _fm_large;
58 FontMetrics _fm_small_italic;
59 FontMetrics _fm_small_italic_bold;
60 FontMetrics _fm_large_italic;
61 FontMetrics _fm_large_italic_bold;
62 // hold font measurements
63 int _small_max_descent = 0;
64 int _small_max_ascent = 0;
65 private final int _min;
66 private final int _max;
67 private boolean _decreased_size_by_system = false;
69 TreeFontSet( final MainPanel owner ) {
71 _min = _owner.getConfiguration().getMinBaseFontSize();
72 _max = _owner.getConfiguration().getMinBaseFontSize();
73 setBaseFont( new Font( DEFAULT_FONT, Font.PLAIN, 10 ) );
76 public Font getSmallFont() {
80 public Font getSmallFontSystem() {
81 return _small_font_system;
85 void decreaseFontSize( final int min, final boolean decreased_size_by_system ) {
86 setDecreasedSizeBySystem( decreased_size_by_system );
87 if ( _large_font.getSize() >= min ) {
88 _small_font = _small_font.deriveFont( _small_font.getSize() - FONT_SIZE_CHANGE_STEP );
89 _large_font = _large_font.deriveFont( _large_font.getSize() - FONT_SIZE_CHANGE_STEP );
90 _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
91 _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
105 Font getLargeFont() {
109 Font getLargeItalicFont() {
110 return _large_italic_font;
113 Font getLargeItalicFontSystem() {
114 return _large_italic_font_system;
117 Font getSmallItalicFont() {
118 return _small_italic_font;
121 Font getSmallItalicFontSystem() {
122 return _small_italic_font_system;
125 void increaseFontSize() {
126 _small_font = _small_font.deriveFont( _small_font.getSize() + FONT_SIZE_CHANGE_STEP );
127 _large_font = _large_font.deriveFont( _large_font.getSize() + FONT_SIZE_CHANGE_STEP );
128 _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
129 _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
134 setDecreasedSizeBySystem( false );
135 _small_font = _small_font.deriveFont( 12f );
136 _large_font = _large_font.deriveFont( 14f );
137 _small_italic_font = _small_italic_font.deriveFont( 12f );
138 _large_italic_font = _large_italic_font.deriveFont( 14f );
143 setDecreasedSizeBySystem( false );
144 _small_font = _small_font.deriveFont( 8f );
145 _large_font = _large_font.deriveFont( 10f );
146 _small_italic_font = _small_italic_font.deriveFont( 8f );
147 _large_italic_font = _large_italic_font.deriveFont( 10f );
152 _large_font_system = _large_font;
155 void setBaseFont( final Font base_font ) {
156 _base_font = base_font;
161 setDecreasedSizeBySystem( false );
162 _small_font = _small_font.deriveFont( SMALL_FONTS_BASE - 1 );
163 _large_font = _large_font.deriveFont( SMALL_FONTS_BASE );
164 _small_italic_font = _small_italic_font.deriveFont( SMALL_FONTS_BASE - 1 );
165 _large_italic_font = _large_italic_font.deriveFont( SMALL_FONTS_BASE );
169 void superTinyFonts() {
170 setDecreasedSizeBySystem( false );
171 _small_font = _small_font.deriveFont( 2f );
172 _large_font = _large_font.deriveFont( 3f );
173 _small_italic_font = _small_italic_font.deriveFont( 2f );
174 _large_italic_font = _large_italic_font.deriveFont( 3f );
179 setDecreasedSizeBySystem( false );
180 _small_font = _small_font.deriveFont( 5f );
181 _large_font = _large_font.deriveFont( 6f );
182 _small_italic_font = _small_italic_font.deriveFont( 5f );
183 _large_italic_font = _large_italic_font.deriveFont( 6f );
187 private Font getLargeFontSystem() {
188 return _large_font_system;
191 private void intializeFonts() {
192 final int small_size = getBaseFont().getSize() - 1;
193 int italic = Font.ITALIC;
194 if ( getBaseFont().getStyle() == Font.BOLD ) {
195 italic = italic + Font.BOLD;
197 _small_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
198 _large_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
199 _small_italic_font = new Font( getBaseFont().getFontName(), italic, small_size );
200 _large_italic_font = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
201 _small_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
202 _large_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
203 _small_italic_font_system = new Font( getBaseFont().getFontName(), italic, small_size );
204 _large_italic_font_system = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
208 private void setupFontMetrics() {
209 _fm_small = _owner.getFontMetrics( _small_font );
210 _fm_large = _owner.getFontMetrics( _large_font );
211 _fm_small_italic = _owner.getFontMetrics( _small_italic_font );
212 _fm_small_italic_bold = _owner.getFontMetrics( _small_italic_font.deriveFont( Font.BOLD ) );
213 _fm_large_italic = _owner.getFontMetrics( _large_italic_font );
214 _fm_large_italic_bold = _owner.getFontMetrics( _large_italic_font.deriveFont( Font.BOLD ) );
215 _small_max_descent = _fm_small.getMaxDescent();
216 _small_max_ascent = _fm_small.getMaxAscent() + 1;
219 boolean isDecreasedSizeBySystem() {
220 return _decreased_size_by_system;
223 private void setDecreasedSizeBySystem( final boolean decreased_size_by_system ) {
224 _decreased_size_by_system = decreased_size_by_system;