22d3950eaef17969ec1b79a089bfc3c36f230fa5
[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.Component;
32 import java.awt.Font;
33 import java.awt.FontMetrics;
34
35 /*
36  * Maintains the fonts for drawing a tree.
37  */
38 public final class TreeFontSet {
39
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 Component     _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     // 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
66     TreeFontSet( final Component owner ) {
67         _owner = owner;
68         setBaseFont( new Font( DEFAULT_FONT, Font.PLAIN, 10 ) );
69     }
70
71     void decreaseFontSize() {
72         if ( _large_font.getSize() > 0 ) {
73             _small_font = _small_font.deriveFont( _small_font.getSize() - FONT_SIZE_CHANGE_STEP );
74             _large_font = _large_font.deriveFont( _large_font.getSize() - FONT_SIZE_CHANGE_STEP );
75             _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
76             _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() - FONT_SIZE_CHANGE_STEP );
77             setupFontMetrics();
78         }
79     }
80     
81     void reset() {
82         _large_font_system = _large_font;
83         
84     }
85
86     Font getBaseFont() {
87         return _base_font;
88     }
89     
90   
91     Font getLargeFont() {
92         return getLargeFontSystem();
93     }
94     
95     private Font getLargeFontSystem() {
96         return _large_font_system;
97     }
98
99     Font getLargeItalicFont() {
100         return _large_italic_font;
101     }
102     
103     Font getLargeItalicFontSystem() {
104         return _large_italic_font_system;
105     }
106
107     public Font getSmallFont() {
108         return _small_font;
109     }
110     
111     public Font getSmallFontSystem() {
112         return _small_font_system;
113     }
114
115     Font getSmallItalicFont() {
116         return _small_italic_font;
117     }
118     
119     Font getSmallItalicFontSystem() {
120         return _small_italic_font_system;
121     }
122
123     void increaseFontSize() {
124         _small_font = _small_font.deriveFont( _small_font.getSize() + FONT_SIZE_CHANGE_STEP );
125         _large_font = _large_font.deriveFont( _large_font.getSize() + FONT_SIZE_CHANGE_STEP );
126         _small_italic_font = _small_italic_font.deriveFont( _small_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
127         _large_italic_font = _large_italic_font.deriveFont( _large_italic_font.getSize() + FONT_SIZE_CHANGE_STEP );
128         setupFontMetrics();
129     }
130
131     private void intializeFonts() {
132         final int small_size = getBaseFont().getSize() - 1;
133         int italic = Font.ITALIC;
134         if ( getBaseFont().getStyle() == Font.BOLD ) {
135             italic = italic + Font.BOLD;
136         }
137         _small_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
138         _large_font = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
139         _small_italic_font = new Font( getBaseFont().getFontName(), italic, small_size );
140         _large_italic_font = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
141         _small_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), small_size );
142         _large_font_system = new Font( getBaseFont().getFontName(), getBaseFont().getStyle(), getBaseFont().getSize() );
143         _small_italic_font_system = new Font( getBaseFont().getFontName(), italic, small_size );
144         _large_italic_font_system = new Font( getBaseFont().getFontName(), italic, getBaseFont().getSize() );
145         
146         
147         
148         
149         
150         setupFontMetrics();
151     }
152
153     void largeFonts() {
154         _small_font = _small_font.deriveFont( 12f );
155         _large_font = _large_font.deriveFont( 14f );
156         _small_italic_font = _small_italic_font.deriveFont( 12f );
157         _large_italic_font = _large_italic_font.deriveFont( 14f );
158         setupFontMetrics();
159     }
160
161     void mediumFonts() {
162         _small_font = _small_font.deriveFont( 8f );
163         _large_font = _large_font.deriveFont( 10f );
164         _small_italic_font = _small_italic_font.deriveFont( 8f );
165         _large_italic_font = _large_italic_font.deriveFont( 10f );
166         setupFontMetrics();
167     }
168
169     void setBaseFont( final Font base_font ) {
170         _base_font = base_font;
171         intializeFonts();
172     }
173
174     private void setupFontMetrics() {
175         _fm_small = _owner.getFontMetrics( _small_font );
176         _fm_large = _owner.getFontMetrics( _large_font );
177         _fm_small_italic = _owner.getFontMetrics( _small_italic_font );
178         _fm_small_italic_bold = _owner.getFontMetrics( _small_italic_font.deriveFont( Font.BOLD ) );
179         _fm_large_italic = _owner.getFontMetrics( _large_italic_font );
180         _fm_large_italic_bold = _owner.getFontMetrics( _large_italic_font.deriveFont( Font.BOLD ) );
181         _small_max_descent = _fm_small.getMaxDescent();
182         _small_max_ascent = _fm_small.getMaxAscent() + 1;
183     }
184
185     void smallFonts() {
186         _small_font = _small_font.deriveFont( 7f );
187         _large_font = _large_font.deriveFont( 8f );
188         _small_italic_font = _small_italic_font.deriveFont( 7f );
189         _large_italic_font = _large_italic_font.deriveFont( 8f );
190         setupFontMetrics();
191     }
192
193     void superTinyFonts() {
194         _small_font = _small_font.deriveFont( 2f );
195         _large_font = _large_font.deriveFont( 3f );
196         _small_italic_font = _small_italic_font.deriveFont( 2f );
197         _large_italic_font = _large_italic_font.deriveFont( 3f );
198         setupFontMetrics();
199     }
200
201     void tinyFonts() {
202         _small_font = _small_font.deriveFont( 5f );
203         _large_font = _large_font.deriveFont( 6f );
204         _small_italic_font = _small_italic_font.deriveFont( 5f );
205         _large_italic_font = _large_italic_font.deriveFont( 6f );
206         setupFontMetrics();
207     }
208 }