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.HashMap;
15  import java.util.Map;
16  
17  import org.apache.fop.fonts.FontType;
18  import org.eclipse.draw2d.FigureUtilities;
19  import org.eclipse.draw2d.geometry.Dimension;
20  import org.eclipse.swt.SWT;
21  import org.eclipse.swt.graphics.Font;
22  import org.eclipse.swt.graphics.FontData;
23  import org.eclipse.swt.graphics.FontMetrics;
24  
25  /***
26   * Description of SWT font of given name(family name), style and weight for
27   * different sizes.
28   */
29  public class SWTFontMetrics implements org.apache.fop.fonts.FontMetrics {
30  
31          private static final float FF = 750.0f * 1000.0f; // Font Factor
32  
33          private String fontName;
34  
35          private int fontStyle;
36  
37          private SWTFontRepository repository;
38  
39          /***
40           * Cach for created font metrics for concrete sizes.
41           */
42          private Map<Integer, FontMetrics> sizeMetricsMap = new HashMap<Integer, FontMetrics>();
43  
44          /***
45           * Constructor.
46           * 
47           * @param fontData
48           * @param repository
49           */
50          public SWTFontMetrics(FontData fontData, SWTFontRepository repository) {
51                  this.repository = repository;
52                  this.fontName = fontData.getName();
53                  this.fontStyle = fontData.getStyle();
54          }
55  
56          public SWTFontMetrics(String fontName, int fontStyle,
57                          SWTFontRepository repository) {
58                  this.repository = repository;
59                  this.fontName = fontName;
60                  this.fontStyle = fontStyle;
61          }
62  
63          protected int convert(float f) {
64                  return Math.round(f + 0.5f);
65          }
66  
67          protected int convertInput(int size) {
68                  float dpi = 96.0f;
69                  float result = size;
70  
71                  result /= 1000.0f; // --> to points
72                  // result /= 72.0f; // --> to inches
73                  // result *= dpi; // --> to pixels
74  
75                  return Math.round(result);
76          }
77  
78          public int getAscender(int size) {
79                  size = convertInput(size);
80  
81                  return convert((getSWTFontMetrics(size).getHeight()
82                                  - getSWTFontMetrics(size).getDescent()) * FF);
83          }
84  
85          public int getCapHeight(int size) {
86                  return getXHeight(size);
87          }
88  
89          /***
90           * Returns SWT metrics of font of concrete size. Results are cached.
91           * 
92           * @param size
93           * @return
94           */
95          private FontMetrics getSWTFontMetrics(int size) {
96                  // look into cach first
97                  FontMetrics fontMetrics = this.sizeMetricsMap.get(new Integer(
98                                  size));
99                  if (fontMetrics != null)
100                         return fontMetrics;
101 
102                 // not present in cach, create and cach it
103                 fontMetrics = FigureUtilities.getFontMetrics(repository
104                                 .getFont(fontName, size, fontStyle));
105                 this.sizeMetricsMap.put(new Integer(size), fontMetrics);
106                 return fontMetrics;
107 
108         }
109 
110         public int getDescender(int size) {
111                 size = convertInput(size);
112 
113                 return Math.round(-1 * getSWTFontMetrics(size).getDescent()
114                                 * FF);
115         }
116 
117         public String getFontName() {
118                 return fontName;
119         }
120 
121         public FontType getFontType() {
122                 return FontType.TRUETYPE;
123         }
124 
125         public Map getKerningInfo() {
126                 return null;
127         }
128 
129         public int getWidth(int i, int size) {
130                 size = convertInput(size);
131 
132                 StringBuffer buffer = new StringBuffer(100);
133                 for (int count = 0; count < 100; count++) {
134                         buffer.append((char) i);
135                 }
136 
137                 float w = FigureUtilities.getStringExtents(buffer.toString(),
138                                 getFont(size)).width;
139                 // w = computeByAWT(buffer.toString());
140 
141                 // return convert(w/EMPTY_L * 0.85f);
142                 return convert((w / 100.0f) * FF);
143         }
144 
145         public int[] getWidths() {
146                 return null;
147         }
148 
149         public int getXHeight(int size) {
150                 size = convertInput(size);
151 
152                 Dimension d = FigureUtilities.getStringExtents("X",
153                                 getFont(size));
154                 return convert(d.height * FF);
155         }
156 
157         /*
158          * (non-Javadoc)
159          * 
160          * @see org.apache.fop.fonts.FontMetrics#hasKerningInfo()
161          */
162         public boolean hasKerningInfo() {
163                 return false;
164         }
165 
166         /***
167          * Returns SWT font of this metric and given size.
168          * 
169          * @param size
170          *                size(height) of font
171          */
172         public Font getFont(int size) {
173                 return this.repository.getFont(this.fontName, size,
174                                 this.fontStyle);
175         }
176         
177         /***
178          * Converts SWT style to FOP font style.
179          * 
180          * @param SWTStyle
181          *                to convert
182          * @return
183          */
184         public static String convertSWTFontStyleToFopFontStyle(int SWTStyle) {
185                 switch (SWTStyle) {
186                 case SWT.NORMAL:
187                         return "normal";
188                 case SWT.BOLD:
189                         return "bold";
190                 case SWT.ITALIC:
191                         return "italic";
192                 }
193                 // TODO Studva italic bold                
194                 return "normal";
195         }
196 
197 }