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