cleanup
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TreeFontSet.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
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
11 //
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.
16 //
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.
21 //
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
25 //
26 // Contact: phylosoft @ gmail . com
27 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28
29 package org.forester.archaeopteryx;
30
31 import java.awt.Font;
32 import java.awt.FontMetrics;
33
34 /*
35  * Maintains the fonts for drawing a tree.
36  */
37 public final class TreeFontSet {
38
39     final static float          SMALL_FONTS_BASE          = 8;
40     private final static String DEFAULT_FONT              = "Verdana";
41     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;
45     // The fonts
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     private Font                _small_font_memory;
56     private Font                _large_font_memory;
57     private Font                _small_italic_font_memory;
58     private Font                _large_italic_font_memory;
59     // Handy holders for font metrics
60     public FontMetrics          _fm_small;
61     FontMetrics                 _fm_large;
62     FontMetrics                 _fm_small_italic;
63     FontMetrics                 _fm_small_italic_bold;
64     FontMetrics                 _fm_large_italic;
65     FontMetrics                 _fm_large_italic_bold;
66     // hold font measurements
67     int                         _small_max_descent        = 0;
68     int                         _small_max_ascent         = 0;
69     private final int           _min;
70     private final int           _max;
71     private boolean             _decreased_size_by_system = false;
72
73     TreeFontSet( final MainPanel owner ) {
74         _owner = owner;
75         _min = _owner.getConfiguration().getMinBaseFontSize();
76         _max = _owner.getConfiguration().getMinBaseFontSize();
77         setBaseFont( new Font( DEFAULT_FONT, Font.PLAIN, 10 ) );
78     }
79
80     public Font getSmallFont() {
81         return _small_font;
82     }
83
84     void decreaseFontSize( final int min, final boolean decreased_size_by_system ) {
85         if ( decreased_size_by_system && !isDecreasedSizeBySystem() ) {
86             _small_font_memory = _small_font;
87             _large_font_memory = _large_font;
88             _small_italic_font_memory = _small_italic_font;
89             _large_italic_font_memory = _large_italic_font;
90         }
91         setDecreasedSizeBySystem( decreased_size_by_system );
92         if ( _large_font.getSize() >= min ) {
93             _small_font = _small_font.deriveFont( _small_font.getSize() - FONT_SIZE_CHANGE_STEP );
94             _large_font = _large_font.deriveFont( _large_font.getSize() - FONT_SIZE_CHANGE_STEP );
95             _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
96             _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
97             setupFontMetrics();
98         }
99     }
100
101     Font getLargeFontMemory() {
102         return _large_font_memory;
103     }
104
105     Font getBaseFont() {
106         return _base_font;
107     }
108
109     Font getLargeFont() {
110         return _large_font;
111     }
112
113     Font getLargeItalicFont() {
114         return _large_italic_font;
115     }
116
117     Font getLargeItalicFontSystem() {
118         return _large_italic_font_system;
119     }
120
121     Font getSmallFontSystem() {
122         return _small_font_system;
123     }
124
125     Font getSmallItalicFont() {
126         return _small_italic_font;
127     }
128
129     Font getSmallItalicFontSystem() {
130         return _small_italic_font_system;
131     }
132
133     void increaseFontSize() {
134         _small_font = _small_font.deriveFont( _small_font.getSize() + FONT_SIZE_CHANGE_STEP );
135         _large_font = _large_font.deriveFont( _large_font.getSize() + FONT_SIZE_CHANGE_STEP );
136         _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
137         _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
138         setupFontMetrics();
139     }
140
141     boolean isDecreasedSizeBySystem() {
142         return _decreased_size_by_system;
143     }
144
145     void largeFonts() {
146         setDecreasedSizeBySystem( false );
147         _small_font = _small_font.deriveFont( 12f );
148         _large_font = _large_font.deriveFont( 14f );
149         _small_italic_font = _small_italic_font.deriveFont( 12f );
150         _large_italic_font = _large_italic_font.deriveFont( 14f );
151         setupFontMetrics();
152     }
153
154     void mediumFonts() {
155         setDecreasedSizeBySystem( false );
156         _small_font = _small_font.deriveFont( 8f );
157         _large_font = _large_font.deriveFont( 10f );
158         _small_italic_font = _small_italic_font.deriveFont( 8f );
159         _large_italic_font = _large_italic_font.deriveFont( 10f );
160         setupFontMetrics();
161     }
162
163     void reset() {
164         _large_font_system = _large_font;
165     }
166
167     void setBaseFont( final Font base_font ) {
168         _base_font = base_font;
169         intializeFonts();
170     }
171
172     void smallFonts() {
173         setDecreasedSizeBySystem( false );
174         _small_font = _small_font.deriveFont( SMALL_FONTS_BASE - 1 );
175         _large_font = _large_font.deriveFont( SMALL_FONTS_BASE );
176         _small_italic_font = _small_italic_font.deriveFont( SMALL_FONTS_BASE - 1 );
177         _large_italic_font = _large_italic_font.deriveFont( SMALL_FONTS_BASE );
178         setupFontMetrics();
179     }
180
181     void superTinyFonts() {
182         setDecreasedSizeBySystem( false );
183         _small_font = _small_font.deriveFont( 2f );
184         _large_font = _large_font.deriveFont( 3f );
185         _small_italic_font = _small_italic_font.deriveFont( 2f );
186         _large_italic_font = _large_italic_font.deriveFont( 3f );
187         setupFontMetrics();
188     }
189
190     void tinyFonts() {
191         setDecreasedSizeBySystem( false );
192         _small_font = _small_font.deriveFont( 5f );
193         _large_font = _large_font.deriveFont( 6f );
194         _small_italic_font = _small_italic_font.deriveFont( 5f );
195         _large_italic_font = _large_italic_font.deriveFont( 6f );
196         setupFontMetrics();
197     }
198
199     private Font getLargeFontSystem() {
200         return _large_font_system;
201     }
202
203     private void intializeFonts() {
204         final int small_size = getBaseFont().getSize() - 1;
205         int italic = Font.ITALIC;
206         if ( getBaseFont().getStyle() == Font.BOLD ) {
207             italic = italic + Font.BOLD;
208         }
209         _small_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
210         _large_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
211         _small_italic_font = new Font( getBaseFont().getFontName(), italic, small_size );
212         _large_italic_font = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
213         _small_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
214         _large_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
215         _small_italic_font_system = new Font( getBaseFont().getFontName(), italic, small_size );
216         _large_italic_font_system = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
217         _small_font_memory = _small_font;
218         _large_font_memory = _large_font;
219         _small_italic_font_memory = _small_italic_font;
220         _large_italic_font_memory = _large_italic_font;
221         setupFontMetrics();
222     }
223
224     private void setDecreasedSizeBySystem( final boolean decreased_size_by_system ) {
225         _decreased_size_by_system = decreased_size_by_system;
226     }
227
228     private void setupFontMetrics() {
229         _fm_small = _owner.getFontMetrics( _small_font );
230         _fm_large = _owner.getFontMetrics( _large_font );
231         _fm_small_italic = _owner.getFontMetrics( _small_italic_font );
232         _fm_small_italic_bold = _owner.getFontMetrics( _small_italic_font.deriveFont( Font.BOLD ) );
233         _fm_large_italic = _owner.getFontMetrics( _large_italic_font );
234         _fm_large_italic_bold = _owner.getFontMetrics( _large_italic_font.deriveFont( Font.BOLD ) );
235         _small_max_descent = _fm_small.getMaxDescent();
236         _small_max_ascent = _fm_small.getMaxAscent() + 1;
237     }
238 }