View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.foTools;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.apache.fop.fonts.FontTriplet;
18  import org.eclipse.swt.graphics.Device;
19  import org.eclipse.swt.graphics.Font;
20  import org.eclipse.swt.graphics.FontData;
21  
22  /***
23   * Central storage for SWT fonts.
24   * 
25   * @author Tomas Studva 27.jul 2006
26   */
27  public class SWTFontRepository {
28  
29          /***
30           * Holds data about default SWT font.
31           */
32          private Font defaulFont;
33  
34          /***
35           * Associated device. Can be printer or display device.
36           */
37          private Device device;
38  
39          /***
40           * Holds font datas of all available fonts in system.
41           */
42          private FontData[] fontDatas;
43  
44          private List<Font> fontCache = new ArrayList<Font>();
45  
46          public SWTFontRepository(Device device) {
47                  if (device == null)
48                          throw new IllegalArgumentException();
49                  this.device = device;
50  
51                  // set default font
52                  this.defaulFont = device.getSystemFont();
53  
54                  // get all fonts
55                  FontData[] f1 = this.device.getFontList(null, true);
56                  FontData[] f2 = this.device.getFontList(null, false);
57                  this.fontDatas = new FontData[f1.length + f2.length];
58                  System.arraycopy(f1, 0, this.fontDatas, 0, f1.length);
59                  System.arraycopy(f2, 0, this.fontDatas, f1.length, f2.length);
60          }
61  
62          public SWTFontMetrics getMetrics(FontData fontData) {
63                  return new SWTFontMetrics(fontData, this);
64          }
65  
66          /***
67           * All OS significant resources (fonts, in this case) are returned to
68           * the underlying OS for destruction, caches are cleared. Repository can
69           * be further used, however fonts are going to be recreated when needed.
70           */
71          public void dispose() {
72  
73                  for (int i = 0; i < fontCache.size(); i++) {
74                          Font f = fontCache.get(i);
75                          f.dispose();
76                  }
77                  fontCache.clear();
78          }
79  
80          /***
81           * Main accessor for font objects.
82           * 
83           * @param fontName
84           *                Full (and exact) name of the font.
85           * @param fontHeight
86           *                Font's height(size) (in points = 1/72th 6f an inch)
87           * @return - Font object if desired font is creatable, default in other
88           *         cases.
89           */
90  
91          public Font getFont(String fontName, int fontHeight, int fontStyle) {
92  
93                  if (fontName == null || fontName.equals("")
94                                  || (fontHeight <= 0))
95                          throw new IllegalArgumentException();
96                  // lookup in cache
97                  Font result = lookupInCache(fontName, fontHeight, fontStyle);
98  
99                  if (result == null) {
100                         for (int i = 0; i < fontDatas.length; i++) {
101                                 if (fontDatas[i].getName().equals(fontName)) {
102                                         String name = fontDatas[i].getName();
103                                         result = new Font(this.device, name,
104                                                         fontHeight, fontStyle);
105                                         fontCache.add(result);
106                                         return result;
107                                 }
108                         }
109                 }
110                 if (result != null)
111                         return result;
112                 // TODO Studva FIX
113                 return getDefaultFont();
114                 // throw new IllegalArgumentException("Such font " + fontName +
115                 // " does not exists");
116         }
117 
118         /***
119          * Returns font datas of all available fonts.
120          * 
121          * @return all available font datas
122          */
123         public FontData[] getAvailableFontDatas() {
124                 return this.fontDatas;
125         }
126 
127         protected Font lookupInCache(String fontName, int fontHeight, int fontStyle) {
128                 for (int i = 0; i < fontCache.size(); i++) {
129                         Font f = fontCache.get(i);
130                         FontData fd = f.getFontData()[0];
131                         if (fd.getName().equals(fontName)
132                                         && (fd.getHeight() == fontHeight)
133                                         && (fd.getStyle() == fontStyle)) {
134                                 return f;
135                         }
136                 }
137                 return null;
138         }
139 
140         /***
141          * Default font getter.
142          * 
143          * @return default font
144          */
145         public Font getDefaultFont() {
146                 return this.defaulFont;
147         }
148 
149         /***
150          * Default font data getter.
151          * 
152          * @return default font data
153          */
154         public FontData getDefaultFontData() {
155                 return this.defaulFont.getFontData()[0];
156         }
157 
158         /***
159          * Computes font triplet decsriptor of font data.
160          * 
161          * @param fontData
162          * @return
163          */
164         public static FontTriplet getFontTriplet(FontData fontData) {
165                 return new FontTriplet(fontData.getName(), SWTFontMetrics
166                                 .convertSWTFontStyleToFopFontStyle(fontData
167                                                 .getStyle()),
168                                 getSWTFontWeight(fontData));
169 
170         }
171 
172         /***
173          * Returns weight of SWT font data.
174          * 
175          * @param fontData
176          *                to compute weight from
177          * @return
178          */
179         public static int getSWTFontWeight(FontData fontData) {
180                 // TODO STUDVA font weight
181                 // default
182                 return 400;
183         }
184 
185 }