Source code: org/libsdl/font/ttf/TTFFont.java
1
2
3 package org.libsdl.font.ttf;
4
5 import org.libsdl.*;
6 import org.libsdl.video.SDLSurface;
7 import org.libsdl.video.SDLColor;
8
9 /**
10 * The <code>TTFFont</code> class represents a loaded TrueType
11 * font. This class requires the SDL_ttf library to be loaded.
12 * If the SDL_ttf library is <b>not</b> loaded, an exception of
13 * type <code>SDLLibraryNotAvailableException</code> is thrown when
14 * attempting to instantiate the object.
15 * <p>
16 * ------ EXAMPLE ------
17 * <p><blockquote><pre>
18 * String fontPath = ...
19 * int fontPtSize = ...
20 * SDL sdl = SDL.getInstance();
21 * sdl.init(SDL.SDL_INIT_VIDEO);
22 * try {
23 * TTFFont font = new TTFFont(fontPath, fontPtSize);
24 * font.getHeight(); // etc...
25 * } catch (SDLLibraryNotAvailableException libe) {
26 * ...
27 * }
28 * </pre></blockquote><p>
29 *
30 * @author Eric Wittmann
31 * @version $revision$
32 */
33 public class TTFFont extends SDLStruct
34 {
35 public static int TTF_STYLE_NORMAL = 0x00;
36 public static int TTF_STYLE_BOLD = 0x01;
37 public static int TTF_STYLE_ITALIC = 0x02;
38 public static int TTF_STYLE_UNDERLINE = 0x04;
39
40 private static native int openFont(String filename, int ptsize);
41
42 /**
43 * Create a new <code>TTFFont</code> object from a TrueType font
44 * file and a given point size.
45 * @param filename String - full path to the font file
46 * @param ptsize Integer - size of the font
47 */
48 public TTFFont(String filename, int ptsize) throws SDLException,
49 SDLLibraryNotAvailableException
50 {
51 super(openFont(filename, ptsize));
52 }
53
54 /**
55 * Get the style of the font.
56 * <p>
57 * <b>Corresponds</b>:<blockquote><code>TTF_GetFontStyle()</code></blockquote>
58 * <p>
59 * @return Integer - bitwise OR of font styles (see static integers above)
60 */
61 public native int getStyle();
62
63 /**
64 * Set the style of the font.
65 * <p>
66 * <b>Corresponds</b>:<blockquote><code>TTF_SetFontStyle()</code></blockquote>
67 * <p>
68 * @param style Integer - bitwise OR of font styles (see static integers above)
69 */
70 public native void setStyle(int style);
71
72 /**
73 * Get font's height.
74 * <p>
75 * <b>Corresponds</b>:<blockquote><code>TTF_FontHeight()</code></blockquote>
76 * <p>
77 * @return Integer - the height of the font
78 */
79 public native int getHeight();
80
81 /**
82 * Get font's ascent.
83 * <p>
84 * <b>Corresponds</b>:<blockquote><code>TTF_FontAscent()</code></blockquote>
85 * <p>
86 * @return Integer - the ascent of the font
87 */
88 public native int getAscent();
89
90 /**
91 * Get font's descent.
92 * <p>
93 * <b>Corresponds</b>:<blockquote><code>TTF_FontDescent()</code></blockquote>
94 * <p>
95 * @return Integer - the descent of the font
96 */
97 public native int getDescent();
98
99 /**
100 * Get font's line skip.
101 * <p>
102 * <b>Corresponds</b>:<blockquote><code>TTF_FontLineSkip()</code></blockquote>
103 * <p>
104 * @return Integer - the line skip of the font
105 */
106 public native int getLineSkip();
107
108 // public native TTFGlyphMetrics getGlyphMetrics(char ch);
109 // public native TTFTextMetrics getTextSize(String str);
110
111 /**
112 * Get font's height.
113 * <p>
114 * <b>Corresponds</b>:<blockquote><code>TTF_FontHeight()</code></blockquote>
115 * <p>
116 * @return Integer - the height of the font
117 */
118 public native SDLSurface renderTextBlended(String text, SDLColor color);
119
120 /**
121 * Closes the font when the object is garbage collected. I'm thinking about
122 * making <code>close</code> a user-called method rather than having it
123 * handled by the Finalizer thread.
124 * <p>
125 * <b>Corresponds</b>:<blockquote><code>TTF_CloseFont()</code></blockquote>
126 */
127 public void finalize() {
128 close();
129 }
130
131 private native void close();
132 }