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
|
/* 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. */
#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 "native_state.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 *)NSA_GET_FONT_PTR (env, data);
g_assert (pfont != NULL);
g_assert (pfont->font != NULL);
return (PangoFcFont *)pfont->font;
}
JNIEXPORT jint JNICALL
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getGlyph
(JNIEnv *env, jobject obj, jint codepoint)
{
FT_Face ft_face;
jint glyph_index;
PangoFcFont *font;
font = getFont(env, obj);
ft_face = pango_fc_font_lock_face( font );
g_assert (ft_face != NULL);
glyph_index = FT_Get_Char_Index( ft_face, codepoint );
pango_fc_font_unlock_face (font);
return glyph_index;
}
JNIEXPORT jobject JNICALL
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getKerning
(JNIEnv *env, jobject obj, jint rightGlyph, jint leftGlyph)
{
FT_Face ft_face;
FT_Vector kern;
jclass cls;
jmethodID method;
jvalue values[2];
PangoFcFont *font;
font = getFont(env, obj);
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 );
values[0].d = (jdouble)kern.x/64.0;
values[1].d = (jdouble)kern.y/64.0;
cls = (*env)->FindClass (env, "java/awt/geom/Point2D$Double");
method = (*env)->GetMethodID (env, cls, "<init>", "(DD)V");
return (*env)->NewObjectA(env, cls, method, values);
}
JNIEXPORT jdoubleArray JNICALL
Java_gnu_java_awt_peer_gtk_FreetypeGlyphVector_getMetricsNative
(JNIEnv *env, jobject obj, jint glyphIndex )
{
FT_Face ft_face;
jdouble *values;
jdoubleArray retArray = NULL;
PangoFcFont *font;
font = getFont(env, obj);
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_DEFAULT ) != 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, jint glyphIndex)
{
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 = getFont(env, obj);
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 );
FT_Outline_Decompose (&(((FT_OutlineGlyph)glyph)->outline),
&ftCallbacks, path);
FT_Done_Glyph( glyph );
pango_fc_font_unlock_face( font );
g_free(path);
return gp;
}
|