inprogress
[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                _base_font;
49     private Font                _small_font_system;
50     private Font                _large_font_system;
51     private Font                _small_italic_font_system;
52     private Font                _large_italic_font_system;
53     private Font                _small_font_memory;
54     private Font                _large_font_memory;
55     private Font                _small_italic_font_memory;
56     private Font                _large_italic_font_memory;
57     // Handy holders for font metrics
58     public FontMetrics          _fm_small;
59     FontMetrics                 _fm_large;
60     FontMetrics                 _fm_small_italic;
61     FontMetrics                 _fm_small_italic_bold;
62     FontMetrics                 _fm_large_italic;
63     FontMetrics                 _fm_large_italic_bold;
64     // hold font measurements
65     int                         _small_max_descent        = 0;
66     int                         _small_max_ascent         = 0;
67     private final int           _min;
68     private final int           _max;
69     private boolean             _decreased_size_by_system = false;
70
71     TreeFontSet( final MainPanel owner ) {
72         _owner = owner;
73         _min = _owner.getConfiguration().getMinBaseFontSize();
74         _max = _owner.getConfiguration().getMinBaseFontSize();
75         setBaseFont( new Font( DEFAULT_FONT, Font.PLAIN, 10 ) );
76     }
77
78     public Font getSmallFont() {
79         return _small_font;
80     }
81
82     void decreaseFontSize( final int min, final boolean decreased_size_by_system ) {
83         if ( decreased_size_by_system && !isDecreasedSizeBySystem() ) {
84             _small_font_memory = _small_font;
85             _large_font_memory = _large_font;
86             _small_italic_font_memory = _small_italic_font;
87             _large_italic_font_memory = _large_italic_font;
88         }
89         setDecreasedSizeBySystem( decreased_size_by_system );
90         if ( _large_font.getSize() >= min ) {
91             _small_font = _small_font.deriveFont( _small_font.getSize() - FONT_SIZE_CHANGE_STEP );
92             _large_font = _large_font.deriveFont( _large_font.getSize() - FONT_SIZE_CHANGE_STEP );
93             _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
94             _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
95             setupFontMetrics();
96         }
97     }
98
99     Font getLargeFontMemory() {
100         return _large_font_memory;
101     }
102
103     Font getBaseFont() {
104         return _base_font;
105     }
106
107     Font getLargeFont() {
108         return _large_font;
109     }
110
111     Font getLargeItalicFont() {
112         return _large_italic_font;
113     }
114
115     Font getLargeItalicFontSystem() {
116         return _large_italic_font_system;
117     }
118
119     Font getSmallFontSystem() {
120         return _small_font_system;
121     }
122
123     Font getSmallItalicFont() {
124         return _small_italic_font;
125     }
126
127     Font getSmallItalicFontSystem() {
128         return _small_italic_font_system;
129     }
130
131     void increaseFontSize() {
132         _small_font = _small_font.deriveFont( _small_font.getSize() + FONT_SIZE_CHANGE_STEP );
133         _large_font = _large_font.deriveFont( _large_font.getSize() + FONT_SIZE_CHANGE_STEP );
134         _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
135         _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
136         setupFontMetrics();
137     }
138
139     boolean isDecreasedSizeBySystem() {
140         return _decreased_size_by_system;
141     }
142
143     void largeFonts() {
144         setDecreasedSizeBySystem( false );
145         _small_font = _small_font.deriveFont( 12f );
146         _large_font = _large_font.deriveFont( 14f );
147         _small_italic_font = _small_italic_font.deriveFont( 12f );
148         _large_italic_font = _large_italic_font.deriveFont( 14f );
149         setupFontMetrics();
150     }
151
152     void mediumFonts() {
153         setDecreasedSizeBySystem( false );
154         _small_font = _small_font.deriveFont( 8f );
155         _large_font = _large_font.deriveFont( 10f );
156         _small_italic_font = _small_italic_font.deriveFont( 8f );
157         _large_italic_font = _large_italic_font.deriveFont( 10f );
158         setupFontMetrics();
159     }
160
161     void reset() {
162         _large_font_system = _large_font;
163     }
164
165     void setBaseFont( final Font base_font ) {
166         _base_font = base_font;
167         intializeFonts();
168     }
169
170     void smallFonts() {
171         setDecreasedSizeBySystem( false );
172         _small_font = _small_font.deriveFont( SMALL_FONTS_BASE - 1 );
173         _large_font = _large_font.deriveFont( SMALL_FONTS_BASE );
174         _small_italic_font = _small_italic_font.deriveFont( SMALL_FONTS_BASE - 1 );
175         _large_italic_font = _large_italic_font.deriveFont( SMALL_FONTS_BASE );
176         setupFontMetrics();
177     }
178
179     void superTinyFonts() {
180         setDecreasedSizeBySystem( false );
181         _small_font = _small_font.deriveFont( 2f );
182         _large_font = _large_font.deriveFont( 3f );
183         _small_italic_font = _small_italic_font.deriveFont( 2f );
184         _large_italic_font = _large_italic_font.deriveFont( 3f );
185         setupFontMetrics();
186     }
187
188     void tinyFonts() {
189         setDecreasedSizeBySystem( false );
190         _small_font = _small_font.deriveFont( 5f );
191         _large_font = _large_font.deriveFont( 6f );
192         _small_italic_font = _small_italic_font.deriveFont( 5f );
193         _large_italic_font = _large_italic_font.deriveFont( 6f );
194         setupFontMetrics();
195     }
196
197     private Font getLargeFontSystem() {
198         return _large_font_system;
199     }
200
201     private void intializeFonts() {
202         final int small_size = getBaseFont().getSize() - 1;
203         int italic = Font.ITALIC;
204         if ( getBaseFont().getStyle() == Font.BOLD ) {
205             italic = italic + Font.BOLD;
206         }
207         _small_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
208         _large_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
209         _small_italic_font = new Font( getBaseFont().getFontName(), italic, small_size );
210         _large_italic_font = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
211         _small_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
212         _large_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
213         _small_italic_font_system = new Font( getBaseFont().getFontName(), italic, small_size );
214         _large_italic_font_system = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
215         _small_font_memory = _small_font;
216         _large_font_memory = _large_font;
217         _small_italic_font_memory = _small_italic_font;
218         _large_italic_font_memory = _large_italic_font;
219         setupFontMetrics();
220     }
221
222     private void setDecreasedSizeBySystem( final boolean decreased_size_by_system ) {
223         _decreased_size_by_system = decreased_size_by_system;
224     }
225
226     private void setupFontMetrics() {
227         _fm_small = _owner.getFontMetrics( _small_font );
228         _fm_large = _owner.getFontMetrics( _large_font );
229         _fm_small_italic = _owner.getFontMetrics( _small_italic_font );
230         _fm_small_italic_bold = _owner.getFontMetrics( _small_italic_font.deriveFont( Font.BOLD ) );
231         _fm_large_italic = _owner.getFontMetrics( _large_italic_font );
232         _fm_large_italic_bold = _owner.getFontMetrics( _large_italic_font.deriveFont( Font.BOLD ) );
233         _small_max_descent = _fm_small.getMaxDescent();
234         _small_max_ascent = _fm_small.getMaxAscent() + 1;
235     }
236 }