Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FontHelper |
|
| 1.0;1 |
1 | /* | |
2 | * DynamicJasper: A library for creating reports dynamically by specifying | |
3 | * columns, groups, styles, etc. at runtime. It also saves a lot of development | |
4 | * time in many cases! (http://sourceforge.net/projects/dynamicjasper) | |
5 | * | |
6 | * Copyright (C) 2008 FDV Solutions (http://www.fdvsolutions.com) | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * | |
11 | * License as published by the Free Software Foundation; either | |
12 | * | |
13 | * version 2.1 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This library is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * | |
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 | * | |
25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
26 | * | |
27 | * | |
28 | */ | |
29 | ||
30 | package ar.com.fdvs.dj.core; | |
31 | ||
32 | import java.awt.FontMetrics; | |
33 | import java.awt.Graphics; | |
34 | import java.awt.image.BufferedImage; | |
35 | ||
36 | import ar.com.fdvs.dj.domain.constants.Font; | |
37 | ||
38 | 0 | public class FontHelper { |
39 | ||
40 | 1 | private static Graphics graphics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics(); |
41 | ||
42 | /** | |
43 | * Calculates the minium height needed for the specified font and size. | |
44 | * You must also take in count if there is any padding, etc in your textfield. | |
45 | * NOTE: Under development. only works with SansSerif | |
46 | * @param font | |
47 | * @return | |
48 | */ | |
49 | public static int getHeightFor(Font font) { | |
50 | 2 | FontMetrics fm = getFontMetric(font); |
51 | 2 | return fm.getHeight(); |
52 | ||
53 | } | |
54 | ||
55 | public static int getWidthFor(Font font, String text) { | |
56 | 0 | FontMetrics fm = getFontMetric(font); |
57 | 0 | return fm.stringWidth(text); |
58 | } | |
59 | ||
60 | /** | |
61 | * @param font | |
62 | * @return | |
63 | */ | |
64 | private static FontMetrics getFontMetric(Font font) { | |
65 | 2 | java.awt.Font awtFont = java.awt.Font.decode(font.getStandardFontname()); |
66 | ||
67 | 2 | FontMetrics fm = graphics.getFontMetrics(awtFont); |
68 | 2 | return fm; |
69 | } | |
70 | ||
71 | ||
72 | public static void main(String[] args) { | |
73 | 0 | System.out.println( |
74 | getWidthFor(Font.ARIAL_MEDIUM, "Ale Gomez") | |
75 | ); | |
76 | 0 | } |
77 | } |