1 |
/* gnu_java_awt_GdkTextLayout.c |
/* gnu_java_awt_GdkTextLayout.c |
2 |
Copyright (C) 2004 Free Software Foundation, Inc. |
Copyright (C) 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
38 |
|
|
39 |
#include <jni.h> |
#include <jni.h> |
40 |
#include <gtk/gtk.h> |
#include <gtk/gtk.h> |
41 |
|
#include <string.h> |
42 |
|
#include <pango/pango.h> |
43 |
|
#include <pango/pangoft2.h> |
44 |
|
#include <pango/pangofc-font.h> |
45 |
|
#include <freetype/ftglyph.h> |
46 |
|
#include <freetype/ftoutln.h> |
47 |
#include "native_state.h" |
#include "native_state.h" |
48 |
#include "gdkfont.h" |
#include "gdkfont.h" |
49 |
#include "gnu_java_awt_peer_gtk_GdkTextLayout.h" |
#include "gnu_java_awt_peer_gtk_GdkTextLayout.h" |
50 |
|
|
51 |
struct state_table *cp_gtk_native_text_layout_state_table; |
struct state_table *cp_gtk_native_text_layout_state_table; |
52 |
|
|
53 |
|
typedef struct gp |
54 |
|
{ |
55 |
|
JNIEnv *env; |
56 |
|
jobject obj; |
57 |
|
double px; |
58 |
|
double py; |
59 |
|
double sx; |
60 |
|
double sy; |
61 |
|
} generalpath ; |
62 |
|
|
63 |
JNIEXPORT void JNICALL |
JNIEXPORT void JNICALL |
64 |
Java_gnu_java_awt_peer_gtk_GdkTextLayout_initStaticState |
Java_gnu_java_awt_peer_gtk_GdkTextLayout_initStaticState |
65 |
(JNIEnv *env, jclass clazz) |
(JNIEnv *env, jclass clazz) |
206 |
|
|
207 |
gdk_threads_leave (); |
gdk_threads_leave (); |
208 |
} |
} |
209 |
|
|
210 |
|
/* GetOutline code follows ****************************/ |
211 |
|
/********* Freetype callback functions *****************************/ |
212 |
|
|
213 |
|
static int _moveTo( FT_Vector* to, |
214 |
|
void *p) |
215 |
|
{ |
216 |
|
JNIEnv *env; |
217 |
|
jobject obj; |
218 |
|
jclass cls; |
219 |
|
jmethodID method; |
220 |
|
jvalue values[2]; |
221 |
|
generalpath *path = (generalpath *) p; |
222 |
|
|
223 |
|
env = path->env; |
224 |
|
obj = path->obj; |
225 |
|
|
226 |
|
values[0].f = (jfloat)(to->x * path->sx + path->px); |
227 |
|
values[1].f = (jfloat)(to->y * path->sy + path->py); |
228 |
|
|
229 |
|
cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath"); |
230 |
|
method = (*env)->GetMethodID (env, cls, "moveTo", "(FF)V"); |
231 |
|
(*env)->CallVoidMethodA(env, obj, method, values ); |
232 |
|
|
233 |
|
return 0; |
234 |
|
} |
235 |
|
|
236 |
|
static int _lineTo( FT_Vector* to, |
237 |
|
void *p) |
238 |
|
{ |
239 |
|
JNIEnv *env; |
240 |
|
jobject obj; |
241 |
|
jclass cls; |
242 |
|
jmethodID method; |
243 |
|
jvalue values[2]; |
244 |
|
generalpath *path = (generalpath *) p; |
245 |
|
|
246 |
|
env = path->env; |
247 |
|
obj = path->obj; |
248 |
|
values[0].f = (jfloat)(to->x * path->sx + path->px); |
249 |
|
values[1].f = (jfloat)(to->y * path->sy + path->py); |
250 |
|
|
251 |
|
cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath"); |
252 |
|
method = (*env)->GetMethodID (env, cls, "lineTo", "(FF)V"); |
253 |
|
(*env)->CallVoidMethodA(env, obj, method, values ); |
254 |
|
|
255 |
|
return 0; |
256 |
|
} |
257 |
|
|
258 |
|
static int _quadTo( FT_Vector* cp, |
259 |
|
FT_Vector* to, |
260 |
|
void *p) |
261 |
|
{ |
262 |
|
JNIEnv *env; |
263 |
|
jobject obj; |
264 |
|
jclass cls; |
265 |
|
jmethodID method; |
266 |
|
jvalue values[4]; |
267 |
|
generalpath *path = (generalpath *) p; |
268 |
|
|
269 |
|
env = path->env; |
270 |
|
obj = path->obj; |
271 |
|
values[0].f = (jfloat)(cp->x * path->sx + path->px); |
272 |
|
values[1].f = (jfloat)(cp->y * path->sy + path->py); |
273 |
|
values[2].f = (jfloat)(to->x * path->sx + path->px); |
274 |
|
values[3].f = (jfloat)(to->y * path->sy + path->py); |
275 |
|
|
276 |
|
cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath"); |
277 |
|
method = (*env)->GetMethodID (env, cls, "quadTo", "(FFFF)V"); |
278 |
|
(*env)->CallVoidMethodA(env, obj, method, values ); |
279 |
|
|
280 |
|
return 0; |
281 |
|
} |
282 |
|
|
283 |
|
static int _curveTo( FT_Vector* cp1, |
284 |
|
FT_Vector* cp2, |
285 |
|
FT_Vector* to, |
286 |
|
void *p) |
287 |
|
{ |
288 |
|
JNIEnv *env; |
289 |
|
jobject obj; |
290 |
|
jclass cls; |
291 |
|
jmethodID method; |
292 |
|
jvalue values[6]; |
293 |
|
generalpath *path = (generalpath *) p; |
294 |
|
|
295 |
|
env = path->env; |
296 |
|
obj = path->obj; |
297 |
|
values[0].f = (jfloat)(cp1->x * path->sx + path->px); |
298 |
|
values[1].f = (jfloat)(cp1->y * path->sy + path->py); |
299 |
|
values[2].f = (jfloat)(cp2->x * path->sx + path->px); |
300 |
|
values[3].f = (jfloat)(cp2->y * path->sy + path->py); |
301 |
|
values[4].f = (jfloat)(to->x * path->sx + path->px); |
302 |
|
values[5].f = (jfloat)(to->y * path->sy + path->py); |
303 |
|
|
304 |
|
cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath"); |
305 |
|
method = (*env)->GetMethodID (env, cls, "curveTo", "(FFFFFF)V"); |
306 |
|
(*env)->CallVoidMethodA(env, obj, method, values ); |
307 |
|
|
308 |
|
return 0; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
JNIEXPORT jobject JNICALL |
313 |
|
Java_gnu_java_awt_peer_gtk_GdkTextLayout_getOutline |
314 |
|
(JNIEnv *env, jobject obj, jobject transform) |
315 |
|
{ |
316 |
|
struct textlayout *tl; |
317 |
|
generalpath *path; |
318 |
|
jobject gp; |
319 |
|
GSList *current_run; |
320 |
|
PangoLayoutLine *current_line; |
321 |
|
FT_Outline_Funcs ftCallbacks = |
322 |
|
{ |
323 |
|
_moveTo, |
324 |
|
_lineTo, |
325 |
|
_quadTo, |
326 |
|
_curveTo, |
327 |
|
0, |
328 |
|
0 |
329 |
|
}; |
330 |
|
PangoLayoutIter* layoutIterator; |
331 |
|
|
332 |
|
gdk_threads_enter (); |
333 |
|
|
334 |
|
tl = (struct textlayout *)NSA_GET_TEXT_LAYOUT_PTR (env, obj); |
335 |
|
g_assert(tl != NULL); |
336 |
|
g_assert(tl->pango_layout != NULL); |
337 |
|
|
338 |
|
path = g_malloc0 (sizeof (generalpath)); |
339 |
|
g_assert(path != NULL); |
340 |
|
path->env = env; |
341 |
|
|
342 |
|
/* Scaling factors */ |
343 |
|
path->sx = PANGO_SCALE/65536.0; |
344 |
|
path->sy = -PANGO_SCALE/65536.0; |
345 |
|
|
346 |
|
{ /* create a GeneralPath instance */ |
347 |
|
jclass cls; |
348 |
|
jmethodID method; |
349 |
|
|
350 |
|
cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath"); |
351 |
|
method = (*env)->GetMethodID (env, cls, "<init>", "()V"); |
352 |
|
gp = path->obj = (*env)->NewObject (env, cls, method); |
353 |
|
} |
354 |
|
|
355 |
|
layoutIterator = pango_layout_get_iter (tl->pango_layout); |
356 |
|
g_assert (layoutIterator != NULL); |
357 |
|
|
358 |
|
if (pango_layout_iter_get_line (layoutIterator)) |
359 |
|
do |
360 |
|
{ |
361 |
|
PangoRectangle line_logical_rect; |
362 |
|
current_line = pango_layout_iter_get_line (layoutIterator); |
363 |
|
pango_layout_iter_get_line_extents (layoutIterator, |
364 |
|
NULL, |
365 |
|
&line_logical_rect); |
366 |
|
|
367 |
|
path->px = line_logical_rect.x/(double)PANGO_SCALE; |
368 |
|
path->py = line_logical_rect.y/(double)PANGO_SCALE; |
369 |
|
|
370 |
|
current_run = current_line->runs; |
371 |
|
while (current_run) |
372 |
|
{ |
373 |
|
FT_Face ft_face; |
374 |
|
int index; |
375 |
|
PangoGlyphItem *run = current_run->data; |
376 |
|
PangoGlyphString *glyphs = run->glyphs; |
377 |
|
|
378 |
|
PangoAnalysis *analysis = &run->item->analysis; |
379 |
|
g_assert (analysis != NULL); |
380 |
|
g_assert (analysis->font != NULL); |
381 |
|
|
382 |
|
ft_face = pango_fc_font_lock_face ((PangoFcFont *)analysis->font); |
383 |
|
g_assert (ft_face != NULL); |
384 |
|
|
385 |
|
for (index = 0; index < glyphs->num_glyphs; index++) |
386 |
|
{ |
387 |
|
FT_Glyph glyph; |
388 |
|
FT_Error fterror; |
389 |
|
PangoGlyphGeometry pgg = glyphs->glyphs[index].geometry; |
390 |
|
|
391 |
|
fterror = FT_Load_Glyph(ft_face, |
392 |
|
(FT_UInt)(glyphs->glyphs[index].glyph), |
393 |
|
FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP); |
394 |
|
g_assert(fterror == 0); |
395 |
|
|
396 |
|
FT_Get_Glyph (ft_face->glyph, &glyph); |
397 |
|
FT_Outline_Decompose (&(((FT_OutlineGlyph)glyph)->outline), |
398 |
|
&ftCallbacks, path); |
399 |
|
FT_Done_Glyph (glyph); |
400 |
|
|
401 |
|
path->px += pgg.width/(double)PANGO_SCALE; |
402 |
|
} |
403 |
|
|
404 |
|
pango_fc_font_unlock_face ((PangoFcFont *)analysis->font); |
405 |
|
current_run = current_run->next; |
406 |
|
} |
407 |
|
} while (pango_layout_iter_next_line (layoutIterator)); |
408 |
|
|
409 |
|
g_free(path); |
410 |
|
gdk_threads_leave (); |
411 |
|
|
412 |
|
if (transform != NULL) |
413 |
|
{ |
414 |
|
jclass cls; |
415 |
|
jmethodID method; |
416 |
|
|
417 |
|
cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath"); |
418 |
|
method = (*env)->GetMethodID (env, cls, "transform", |
419 |
|
"(Ljava/awt/geom/AffineTransform;)V"); |
420 |
|
(*env)->CallVoidMethod(env, gp, method, transform ); |
421 |
|
} |
422 |
|
|
423 |
|
return gp; |
424 |
|
} |