1 /* gnu_java_awt_GdkFont.c
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 #define PANGO_ENABLE_ENGINE
39 #include <pango/pango.h>
40 #include <pango/pangoft2.h>
41 #include <pango/pangofc-font.h>
46 #include FT_TRUETYPE_TABLES_H
49 #include "gnu_java_awt_peer_gtk_GdkFontPeer.h"
51 enum java_awt_font_style {
52 java_awt_font_PLAIN = 0,
53 java_awt_font_BOLD = 1,
54 java_awt_font_ITALIC = 2
57 enum java_awt_font_baseline {
58 java_awt_font_ROMAN_BASELINE = 0,
59 java_awt_font_CENTER_BASELINE = 1,
60 java_awt_font_HANGING_BASELINE = 2
63 static PangoFT2FontMap *ft2_map = NULL;
65 JNIEXPORT void JNICALL
66 Java_gnu_java_awt_peer_gtk_GdkFontPeer_initStaticState
67 (JNIEnv *env, jclass clazz __attribute__((unused)))
69 gtkpeer_init_font_IDs(env);
70 ft2_map = PANGO_FT2_FONT_MAP(pango_ft2_font_map_new());
73 JNIEXPORT void JNICALL
74 Java_gnu_java_awt_peer_gtk_GdkFontPeer_initState
75 (JNIEnv *env, jobject self)
77 struct peerfont *pfont = NULL;
81 g_assert (self != NULL);
82 pfont = (struct peerfont *) g_malloc0 (sizeof (struct peerfont));
83 g_assert (pfont != NULL);
84 gtkpeer_set_font (env, self, pfont);
90 JNIEXPORT void JNICALL
91 Java_gnu_java_awt_peer_gtk_GdkFontPeer_dispose
92 (JNIEnv *env, jobject self)
94 struct peerfont *pfont = NULL;
98 pfont = (struct peerfont *) gtkpeer_get_font (env, self);
99 g_assert (pfont != NULL);
100 if (pfont->layout != NULL)
101 g_object_unref (pfont->layout);
102 if (pfont->font != NULL)
103 g_object_unref (pfont->font);
104 if (pfont->set != NULL)
105 g_object_unref (pfont->set);
106 if (pfont->ctx != NULL)
107 g_object_unref (pfont->ctx);
108 if (pfont->desc != NULL)
109 pango_font_description_free (pfont->desc);
112 gdk_threads_leave ();
116 JNIEXPORT void JNICALL
117 Java_gnu_java_awt_peer_gtk_GdkFontPeer_releasePeerGraphicsResource
118 (JNIEnv *env, jobject java_font)
120 struct peerfont *pfont = NULL;
124 pfont = (struct peerfont *) gtkpeer_get_font (env, java_font);
125 g_assert (pfont != NULL);
126 if (pfont->graphics_resource != NULL)
128 cairo_font_face_destroy ((cairo_font_face_t *) pfont->graphics_resource);
129 pfont->graphics_resource = NULL;
136 JNIEXPORT void JNICALL
137 Java_gnu_java_awt_peer_gtk_GdkFontPeer_getFontMetrics
138 (JNIEnv *env, jobject java_font, jdoubleArray java_metrics)
141 struct peerfont *pfont = NULL;
142 jdouble *native_metrics = NULL;
151 pfont = (struct peerfont *) gtkpeer_get_font (env, java_font);
152 g_assert (pfont != NULL);
153 face = pango_fc_font_lock_face ((PangoFcFont *)pfont->font);
156 = (*env)->GetDoubleArrayElements (env, java_metrics, NULL);
158 g_assert (native_metrics != NULL);
160 x_ppem = face->size->metrics.x_ppem;
161 y_ppem = face->size->metrics.y_ppem;
162 units_per_em = face->units_per_EM;
163 factorx = units_per_em / x_ppem;
164 factory = units_per_em / y_ppem;
165 native_metrics[FONT_METRICS_ASCENT] = face->ascender / factory;
166 native_metrics[FONT_METRICS_MAX_ASCENT] = face->bbox.yMax / factory;
167 native_metrics[FONT_METRICS_DESCENT] = - face->descender / factory;
168 native_metrics[FONT_METRICS_MAX_DESCENT] = - face->bbox.yMin / factory;
169 native_metrics[FONT_METRICS_MAX_ADVANCE] = face->max_advance_width / factorx;
170 native_metrics[FONT_METRICS_HEIGHT] = face->height / factory;
171 native_metrics[FONT_METRICS_UNDERLINE_OFFSET] =
172 face->underline_position / factory;
173 native_metrics[FONT_METRICS_UNDERLINE_THICKNESS] =
174 face->underline_thickness / factory;
176 pango_fc_font_unlock_face((PangoFcFont *)pfont->font);
178 (*env)->ReleaseDoubleArrayElements (env,
185 JNIEXPORT void JNICALL
186 Java_gnu_java_awt_peer_gtk_GdkFontPeer_getTextMetrics
187 (JNIEnv *env, jobject java_font, jstring str, jdoubleArray java_metrics)
189 struct peerfont *pfont = NULL;
190 const char *cstr = NULL;
191 jdouble *native_metrics = NULL;
200 pfont = (struct peerfont *) gtkpeer_get_font(env, java_font);
201 g_assert (pfont != NULL);
203 cstr = (*env)->GetStringUTFChars (env, str, NULL);
204 g_assert(cstr != NULL);
206 pango_layout_set_text (pfont->layout, cstr, -1);
207 pango_layout_get_extents (pfont->layout, NULL, &log);
209 line_count = pango_layout_get_line_count (pfont->layout);
210 for (i = 0; i < line_count; i++)
212 pango_layout_line_get_extents (pango_layout_get_line (pfont->layout, i),
217 (*env)->ReleaseStringUTFChars (env, str, cstr);
218 pango_layout_set_text (pfont->layout, "", -1);
220 native_metrics = (*env)->GetDoubleArrayElements (env, java_metrics, NULL);
221 g_assert (native_metrics != NULL);
223 native_metrics[TEXT_METRICS_X_BEARING]
224 = PANGO_PIXELS( ((double)log.x) );
226 native_metrics[TEXT_METRICS_Y_BEARING]
227 = PANGO_PIXELS( ((double)log.y) );
229 native_metrics[TEXT_METRICS_HEIGHT]
230 = PANGO_PIXELS( ((double)log.height) );
232 native_metrics[TEXT_METRICS_WIDTH]
233 = PANGO_PIXELS( ((double)width) );
235 native_metrics[TEXT_METRICS_X_ADVANCE]
236 = PANGO_PIXELS( ((double) (log.x + log.width)) );
238 native_metrics[TEXT_METRICS_Y_ADVANCE]
239 = PANGO_PIXELS( ((double) (log.y + log.height)) );
241 (*env)->ReleaseDoubleArrayElements (env, java_metrics, native_metrics, 0);
247 JNIEXPORT void JNICALL
248 Java_gnu_java_awt_peer_gtk_GdkFontPeer_setFont
249 (JNIEnv *env, jobject self, jstring family_name_str, jint style_int, jint size)
251 struct peerfont *pfont = NULL;
252 char const *family_name = NULL;
253 enum java_awt_font_style style;
255 gdk_threads_enter ();
257 style = (enum java_awt_font_style) style_int;
259 g_assert (self != NULL);
260 pfont = (struct peerfont *) gtkpeer_get_font(env, self);
261 g_assert (pfont != NULL);
263 /* Clear old font information */
264 if (pfont->ctx != NULL)
265 g_object_unref (pfont->ctx);
266 if (pfont->font != NULL)
267 g_object_unref (pfont->font);
268 if (pfont->set != NULL)
269 g_object_unref (pfont->set);
270 if (pfont->desc != NULL)
271 pango_font_description_free (pfont->desc);
273 /* Set new description information */
274 pfont->desc = pango_font_description_new ();
275 g_assert (pfont->desc != NULL);
277 family_name = (*env)->GetStringUTFChars(env, family_name_str, 0);
278 g_assert (family_name != NULL);
279 pango_font_description_set_family (pfont->desc, family_name);
280 (*env)->ReleaseStringUTFChars(env, family_name_str, family_name);
282 if (style & java_awt_font_BOLD)
283 pango_font_description_set_weight (pfont->desc, PANGO_WEIGHT_BOLD);
285 if (style & java_awt_font_ITALIC)
286 pango_font_description_set_style (pfont->desc, PANGO_STYLE_ITALIC);
288 pango_font_description_set_size (pfont->desc, size * PANGO_SCALE);
290 /* Create new context */
291 pfont->ctx = pango_ft2_font_map_create_context (ft2_map);
292 g_assert (pfont->ctx != NULL);
294 pango_context_set_font_description (pfont->ctx, pfont->desc);
295 pango_context_set_language (pfont->ctx, gtk_get_default_language());
297 /* Create new fontset and default font */
298 pfont->set = pango_context_load_fontset(pfont->ctx, pfont->desc,
299 gtk_get_default_language());
300 pfont->font = pango_context_load_font (pfont->ctx, pfont->desc);
301 g_assert (pfont->font != NULL);
303 if (pfont->layout == NULL)
304 pfont->layout = pango_layout_new (pfont->ctx);
305 g_assert (pfont->layout != NULL);
307 gdk_threads_leave ();
311 JNIEXPORT jbyteArray JNICALL
312 Java_gnu_java_awt_peer_gtk_GdkFontPeer_getTrueTypeTable
313 (JNIEnv *env, jobject self, jbyte n, jbyte a, jbyte m, jbyte e)
315 struct peerfont *pfont = NULL;
321 jbyteArray result_array;
324 pfont = (struct peerfont *) gtkpeer_get_font(env, self);
328 gdk_threads_enter ();
329 face = pango_fc_font_lock_face ((PangoFcFont *)pfont->font);
330 tag = FT_MAKE_TAG( n, a, m, e );
332 /* Get the length of the table requested */
333 error = FT_Load_Sfnt_Table( face, tag, 0, NULL, &length );
336 pango_fc_font_unlock_face ((PangoFcFont *)pfont->font);
337 gdk_threads_leave ();
341 buffer = (FT_Byte *)g_malloc0( length );
342 if ( buffer == NULL )
344 pango_fc_font_unlock_face ((PangoFcFont *)pfont->font);
345 gdk_threads_leave ();
348 /* get the table data */
349 error = FT_Load_Sfnt_Table( face, tag, 0, buffer, &length );
352 pango_fc_font_unlock_face ((PangoFcFont *)pfont->font);
354 gdk_threads_leave ();
358 /* copy to a jbytearray */
359 result_array = (*env)->NewByteArray (env, length);
361 rbuf = (*env)->GetByteArrayElements (env, result_array, NULL);
362 memcpy(rbuf, buffer, length);
363 (*env)->ReleaseByteArrayElements (env, result_array, rbuf, 0);
366 pango_fc_font_unlock_face ((PangoFcFont *)pfont->font);
367 gdk_threads_leave ();