aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.8.1/libjava/classpath/native/jni/gtk-peer/gnu_java_awt_peer_gtk_FreetypeGlyphVector.c
blob: cb7bf43dcb46de6528fb5318c1333f2850766f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* gnu_java_awt_FreetypeGlyphVector.c
   Copyright (C) 2006  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

#define PANGO_ENABLE_ENGINE
#include <jni.h>
#include <gtk/gtk.h>
#include <string.h>
#include <pango/pango.h>
#include <pango/pangoft2.h>
#include <pango/pangofc-font.h>
#include <freetype/ftglyph.h>
#include <freetype/ftoutln.h>
#include "jcl.h"
#include "gdkfont.h"
#include "gnu_java_awt_peer_gtk_FreetypeGlyphVector.h"
#include "cairographics2d.h"

typedef struct gp
{
  JNIEnv *env;
  jobject obj;
  double px;
  double py;
  double sx;
  double sy;
} generalpath ;

static PangoFcFont *
getFont(JNIEnv *env, jobject obj)
{
  jfieldID fid;
  jobject data;
  jclass cls;
  struct peerfont *pfont;

  cls = (*env)->GetObjectClass (env, obj);
  fid = (*env)->GetFieldID (env, cls, "peer", 
				 "Lgnu/java/awt/peer/gtk/GdkFontPeer;");
  g_assert (fid != 0);

  data = (*env)->GetObjectField (env, obj, fid);
  g_assert (data != NULL);

  pfont = (struct peerfont *) gtkpeer_get_font(env, data);
  g_assert (pfont != NULL);
  g_assert (pfont->font != NULL);

  return (PangoFcFont *)pfont->font;
}

static PangoFontset *
getFontSet(JNIEnv *env, jobject obj)
{
  jfieldID fid;
  jobject data;
  jclass cls;
  struct peerfont *pfont;

  cls = (*env)->GetObjectClass (env, obj);
  fid = (*env)->GetFieldID (env, cls, "peer", 
				 "Lgnu/java/awt/peer/gtk/GdkFontPeer;");
  g_assert (fid != 0);

  data = (*env)->GetObjectField (env, obj, fid);
  g_assert (data != NULL);

  pfont = (struct peerfont *) gtkpeer_get_font (env, data);
  g_assert (pfont != NULL);
  g_assert (pfont->font != NULL);

  return (PangoFontset *)pfont->set;
}

JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getGlyphs
  (JNIEnv *env, jobject obj, jintArray codepoints, jintArray glyphs,
   jlongArray fonts)
{
  PangoFcFont *default_font, *current_font;
  PangoFontset *pfs;
  jint *cpvals;
  jint length;
  int i;

  /* Set up default font and fontset */
  default_font = getFont(env, obj);
  current_font = default_font;
  pfs = getFontSet(env, obj);

  /* Retrieve string information */
  length = (*env)->GetArrayLength (env, codepoints);
  cpvals = (*env)->GetIntArrayElements (env, codepoints, NULL);
  
  jint *glyphArray = (*env)->GetIntArrayElements (env, glyphs, NULL);
  jlong *fontArray = (*env)->GetLongArrayElements (env, fonts, NULL);

  /* A design goal of Pango is to be threadsafe, but it's admitted that it is
   * not actually threadsafe at the moment.  Using gdk locking here to be safe,
   * but I don't know if if actually helps at all... */ 
  gdk_threads_enter();

  for( i = 0; i < length; i++ )
  {
  	/* Ensure the current font has the requested character; if it doesn't,
  	 * try the default font before pulling a new font out of the fontset.
  	 * Once chosen, a font will be used until a character not in the font is
  	 * encountered. */ 
  	if (!pango_fc_font_has_char(current_font, cpvals[i]))
  	  {
  	    if (pango_fc_font_has_char(default_font, cpvals[i]))
  	      {
  	        current_font = default_font;
            g_object_ref(current_font);
  	      }
  	    else
  	      {
  	        current_font = (PangoFcFont*)pango_fontset_get_font(pfs, cpvals[i]);
  	      }
  	  }
  	else
      {
        g_object_ref(current_font);
      }
  	
  	/* Get glyph, and store both glyph and pointer to font */
    glyphArray[i] = (int)pango_fc_font_get_glyph(current_font,
                                                 (gunichar)cpvals[i]);
    fontArray[i] = PTR_TO_JLONG(current_font);
  }
  
  gdk_threads_leave();

  (*env)->ReleaseIntArrayElements (env, glyphs, glyphArray, 0);
  (*env)->ReleaseIntArrayElements (env, codepoints, cpvals, 0);
  (*env)->ReleaseLongArrayElements (env, fonts, fontArray, 0);
}

JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getKerning
  (JNIEnv *env, jobject obj __attribute__((unused)), jint rightGlyph,
   jint leftGlyph, jlong fnt, jfloatArray p)
{
  FT_Face ft_face;
  FT_Vector kern;
  PangoFcFont *font;

  font = JLONG_TO_PTR(PangoFcFont, fnt);
  ft_face = pango_fc_font_lock_face( font );
  g_assert (ft_face != NULL);
  FT_Get_Kerning( ft_face, rightGlyph, leftGlyph, FT_KERNING_DEFAULT, &kern );

  pango_fc_font_unlock_face( font );

  jfloat *pelements = (*env)->GetPrimitiveArrayCritical(env, p, NULL);
  pelements[0] = (jfloat)kern.x/64.0;
  pelements[1] = (jfloat)kern.y/64.0;
  (*env)->ReleasePrimitiveArrayCritical (env, p, pelements, 0);
}

JNIEXPORT jdoubleArray JNICALL 
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getMetricsNative
(JNIEnv *env, jobject obj __attribute__((unused)), jint glyphIndex, jlong fnt)
{
  FT_Face ft_face;
  jdouble *values;
  jdoubleArray retArray = NULL;
  PangoFcFont *font;

  font = JLONG_TO_PTR(PangoFcFont, fnt);
  ft_face = pango_fc_font_lock_face( font );

  g_assert (ft_face != NULL);

  FT_Set_Transform( ft_face, NULL, NULL );

  if( FT_Load_Glyph( ft_face, glyphIndex, FT_LOAD_NO_BITMAP ) != 0 )
    {
      pango_fc_font_unlock_face( font );
      printf("Couldn't load glyph %i\n", glyphIndex);
      return NULL;
    }

  retArray = (*env)->NewDoubleArray (env, 8);
  values = (*env)->GetDoubleArrayElements (env, retArray, NULL);

  values[0] = 0;
  values[1] = (jdouble)ft_face->glyph->advance.x/64.0;
  values[2] = (jdouble)ft_face->glyph->advance.y/64.0;
  values[3] = (jdouble)ft_face->glyph->metrics.horiBearingX/64.0;
  values[4] = -(jdouble)ft_face->glyph->metrics.horiBearingY/64.0;
  values[5] = (jdouble)ft_face->glyph->metrics.width/64.0;
  values[6] = (jdouble)ft_face->glyph->metrics.height/64.0;
  values[7] = 0;

  (*env)->ReleaseDoubleArrayElements (env, retArray, values, 0);
  pango_fc_font_unlock_face( font );

  return retArray;
}

/* GetOutline code follows ****************************/
/********* Freetype callback functions *****************************/

static int _moveTo( const FT_Vector* to,
		    void *p)
{
  JNIEnv *env;
  jobject obj;
  jclass cls;
  jmethodID method;
  jvalue values[2];
  generalpath *path = (generalpath *) p;

  env = path->env;
  obj = path->obj;

  values[0].f = (jfloat)(to->x * path->sx + path->px);
  values[1].f = (jfloat)(to->y * path->sy + path->py);

  cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath");
  method = (*env)->GetMethodID (env, cls, "moveTo", "(FF)V");
  (*env)->CallVoidMethodA(env, obj, method, values );

  return 0;
}

static int _lineTo( const FT_Vector*  to,
		    void *p)
{
  JNIEnv *env;
  jobject obj;
  jclass cls;
  jmethodID method;
  jvalue values[2];
  generalpath *path = (generalpath *) p;

  env = path->env;
  obj = path->obj; 
  values[0].f = (jfloat)(to->x * path->sx + path->px);
  values[1].f = (jfloat)(to->y * path->sy + path->py);

  cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath");
  method = (*env)->GetMethodID (env, cls, "lineTo", "(FF)V");
  (*env)->CallVoidMethodA(env, obj, method, values );

  return 0;
}

static int _quadTo( const FT_Vector*  cp,
		    const FT_Vector*  to,
		    void *p)
{
  JNIEnv *env;
  jobject obj;
  jclass cls;
  jmethodID method;
  jvalue values[4];
  generalpath *path = (generalpath *) p;

  env = path->env;
  obj = path->obj;
  values[0].f = (jfloat)(cp->x * path->sx + path->px);
  values[1].f = (jfloat)(cp->y * path->sy + path->py);
  values[2].f = (jfloat)(to->x * path->sx + path->px);
  values[3].f = (jfloat)(to->y * path->sy + path->py);

  cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath");
  method = (*env)->GetMethodID (env, cls, "quadTo", "(FFFF)V");
  (*env)->CallVoidMethodA(env, obj, method, values );

  return 0;
}

static int _curveTo( const FT_Vector*  cp1,
		     const FT_Vector*  cp2,
		     const FT_Vector*  to,
		     void *p)
{
  JNIEnv *env;
  jobject obj;
  jclass cls;
  jmethodID method;
  jvalue values[6];
  generalpath *path = (generalpath *) p;

  env = path->env;
  obj = path->obj;
  values[0].f = (jfloat)(cp1->x * path->sx + path->px);
  values[1].f = (jfloat)(cp1->y * path->sy + path->py);
  values[2].f = (jfloat)(cp2->x * path->sx + path->px);
  values[3].f = (jfloat)(cp2->y * path->sy + path->py);
  values[4].f = (jfloat)(to->x * path->sx + path->px);
  values[5].f = (jfloat)(to->y * path->sy + path->py);

  cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath");
  method = (*env)->GetMethodID (env, cls, "curveTo", "(FFFFFF)V");
  (*env)->CallVoidMethodA(env, obj, method, values );

  return 0;
}


JNIEXPORT jobject JNICALL 
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getGlyphOutlineNative
 (JNIEnv *env, jobject obj __attribute__((unused)), jint glyphIndex, jlong fnt)
{
  generalpath *path;
  jobject gp;
  FT_Outline_Funcs ftCallbacks =
    {
      (FT_Outline_MoveToFunc) _moveTo,
      (FT_Outline_LineToFunc) _lineTo,
      (FT_Outline_ConicToFunc) _quadTo,
      (FT_Outline_CubicToFunc) _curveTo,
      0,
      0
    };
  PangoFcFont *font;
  FT_Face ft_face;
  FT_Glyph glyph;

  font = JLONG_TO_PTR(PangoFcFont, fnt);
  ft_face = pango_fc_font_lock_face( font );

  g_assert (ft_face != NULL);

  path = g_malloc0 (sizeof (generalpath));
  g_assert(path != NULL);
  path->env = env;

  path->px = path->py = 0.0;
  path->sx = 1.0/64.0;
  path->sy = -1.0/64.0;

  {  /* create a GeneralPath instance */
    jclass cls;
    jmethodID method;
    
    cls = (*env)->FindClass (env, "java/awt/geom/GeneralPath");
    method = (*env)->GetMethodID (env, cls, "<init>", "()V");
    gp = path->obj = (*env)->NewObject (env, cls, method);
  }
	      
  if(FT_Load_Glyph(ft_face,
		   (FT_UInt)(glyphIndex),
		   FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP) != 0)
    {
      pango_fc_font_unlock_face( font );
      g_free(path); 
      return NULL;
    }

  FT_Get_Glyph( ft_face->glyph, &glyph );
  if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
    {
      FT_Outline_Decompose (&(((FT_OutlineGlyph)glyph)->outline),
			    &ftCallbacks, path);
    }
  else
    {
      char format[5];

      format[0] = (glyph->format & 0xFF000000) >> 24;
      format[1] = (glyph->format & 0x00FF0000) >> 16;
      format[2] = (glyph->format & 0x0000FF00) >> 8;
      format[3] = (glyph->format & 0x000000FF);
      format[4] = '\0';
      printf("WARNING: Unable to create outline for font %s %s of format %s\n",
	     ft_face->family_name, ft_face->style_name, format);
    }
  FT_Done_Glyph( glyph );
  
  pango_fc_font_unlock_face( font );

  g_free(path); 

  return gp; 
}

JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_dispose
 (JNIEnv *env, jobject obj __attribute__((unused)), jlongArray fontset)
{
  PangoFcFont *font;
  jlong *fontArray; 
  int i, length;

  length = (*env)->GetArrayLength (env, fontset);
  fontArray = (*env)->GetLongArrayElements (env, fontset, NULL);
  
  gdk_threads_enter();
  
  for( i = 0; i < length; i++ )
  {
    font = JLONG_TO_PTR(PangoFcFont, fontArray[i]);
    g_object_unref(font);
  }
  
  gdk_threads_leave();

  (*env)->ReleaseLongArrayElements (env, fontset, fontArray, 0);
}

JNIEXPORT jlong JNICALL 
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getNativeFontPointer
 (JNIEnv *env, jobject obj, jint n)
{
  int i;
  PangoFcFont *font = getFont(env, obj);
  
  for (i = 0; i < n; i++)
    g_object_ref(font);
  
  return PTR_TO_JLONG(font);
}