95730b8e58050199a8b7101532802ce51046b9ce
[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_font_memory;
52     private Font                _large_font_memory;
53     // Handy holders for font metrics
54     public FontMetrics          _fm_small;
55     FontMetrics                 _fm_large;
56     FontMetrics                 _fm_large_bold;
57     // hold font measurements
58     int                         _small_max_descent        = 0;
59     int                         _small_max_ascent         = 0;
60     private final int           _min;
61     private final int           _max;
62     private boolean             _decreased_size_by_system = false;
63
64     TreeFontSet( final MainPanel owner ) {
65         _owner = owner;
66         _min = _owner.getConfiguration().getMinBaseFontSize();
67         _max = _owner.getConfiguration().getMinBaseFontSize();
68         setBaseFont( new Font( DEFAULT_FONT, Font.PLAIN, 10 ) );
69     }
70
71     public Font getSmallFont() {
72         return _small_font;
73     }
74
75     void decreaseFontSize( final int min, final boolean decreased_size_by_system ) {
76         if ( decreased_size_by_system && !isDecreasedSizeBySystem() ) {
77             _small_font_memory = _small_font;
78             _large_font_memory = _large_font;
79         }
80         setDecreasedSizeBySystem( decreased_size_by_system );
81         if ( _large_font.getSize() >= min ) {
82             _small_font = _small_font.deriveFont( _small_font.getSize() - FONT_SIZE_CHANGE_STEP );
83             _large_font = _large_font.deriveFont( _large_font.getSize() - FONT_SIZE_CHANGE_STEP );
84             setupFontMetrics();
85         }
86     }
87
88     Font getLargeFontMemory() {
89         return _large_font_memory;
90     }
91
92     Font getBaseFont() {
93         return _base_font;
94     }
95
96     Font getLargeFont() {
97         return _large_font;
98     }
99
100     Font getSmallFontSystem() {
101         return _small_font_system;
102     }
103
104     void increaseFontSize() {
105         _small_font = _small_font.deriveFont( _small_font.getSize() + FONT_SIZE_CHANGE_STEP );
106         _large_font = _large_font.deriveFont( _large_font.getSize() + FONT_SIZE_CHANGE_STEP );
107         setupFontMetrics();
108     }
109
110     boolean isDecreasedSizeBySystem() {
111         return _decreased_size_by_system;
112     }
113
114     void largeFonts() {
115         setDecreasedSizeBySystem( false );
116         _small_font = _small_font.deriveFont( 12f );
117         _large_font = _large_font.deriveFont( 14f );
118         setupFontMetrics();
119     }
120
121     void mediumFonts() {
122         setDecreasedSizeBySystem( false );
123         _small_font = _small_font.deriveFont( 8f );
124         _large_font = _large_font.deriveFont( 10f );
125         setupFontMetrics();
126     }
127
128     void reset() {
129         _large_font_system = _large_font;
130     }
131
132     void setBaseFont( final Font base_font ) {
133         _base_font = base_font;
134         intializeFonts();
135     }
136
137     void smallFonts() {
138         setDecreasedSizeBySystem( false );
139         _small_font = _small_font.deriveFont( SMALL_FONTS_BASE - 1 );
140         _large_font = _large_font.deriveFont( SMALL_FONTS_BASE );
141         setupFontMetrics();
142     }
143
144     void superTinyFonts() {
145         setDecreasedSizeBySystem( false );
146         _small_font = _small_font.deriveFont( 2f );
147         _large_font = _large_font.deriveFont( 3f );
148         setupFontMetrics();
149     }
150
151     void tinyFonts() {
152         setDecreasedSizeBySystem( false );
153         _small_font = _small_font.deriveFont( 5f );
154         _large_font = _large_font.deriveFont( 6f );
155         setupFontMetrics();
156     }
157
158     private Font getLargeFontSystem() {
159         return _large_font_system;
160     }
161
162     private void intializeFonts() {
163         final int small_size = getBaseFont().getSize() - 1;
164         int italic = Font.ITALIC;
165         if ( getBaseFont().getStyle() == Font.BOLD ) {
166             italic = italic + Font.BOLD;
167         }
168         _small_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
169         _large_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
170         _small_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
171         _large_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
172         _small_font_memory = _small_font;
173         _large_font_memory = _large_font;
174         setupFontMetrics();
175     }
176
177     private void setDecreasedSizeBySystem( final boolean decreased_size_by_system ) {
178         _decreased_size_by_system = decreased_size_by_system;
179     }
180
181     private void setupFontMetrics() {
182         _fm_small = _owner.getFontMetrics( _small_font );
183         _fm_large = _owner.getFontMetrics( _large_font );
184         _fm_large_bold = _owner.getFontMetrics( _large_font.deriveFont( Font.BOLD ) );
185         _small_max_descent = _fm_small.getMaxDescent();
186         _small_max_ascent = _fm_small.getMaxAscent() + 1;
187     }
188 }