Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/native/libfontmanager/DrawGlyphList.c
41152 views
1
/*
2
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include "jlong.h"
27
#include "math.h"
28
#include "string.h"
29
#include "stdlib.h"
30
#include "sunfontids.h"
31
#include "fontscalerdefs.h"
32
#include "glyphblitting.h"
33
#include "GraphicsPrimitiveMgr.h"
34
#include "sun_java2d_loops_DrawGlyphList.h"
35
#include "sun_java2d_loops_DrawGlyphListAA.h"
36
37
38
/*
39
* Need to account for the rare case when (eg) repainting damaged
40
* areas results in the drawing location being negative, in which
41
* case (int) rounding always goes towards zero. We need to always
42
* round down instead, so that we paint at the correct position.
43
* We only call "floor" when value is < 0 (ie rarely).
44
* Storing the result of (eg) (x+ginfo->topLeftX) benchmarks is more
45
* expensive than repeating the calculation as we do here.
46
* "floor" shows up as a significant cost in app-level microbenchmarks.
47
* This macro avoids calling it on positive values, instead using an
48
* (int) cast.
49
*/
50
#define FLOOR_ASSIGN(l, r)\
51
if ((r)<0) (l) = ((int)floor(r)); else (l) = ((int)(r))
52
53
GlyphBlitVector* setupBlitVector(JNIEnv *env, jobject glyphlist,
54
jint fromGlyph, jint toGlyph) {
55
56
int g;
57
size_t bytesNeeded;
58
jlong *imagePtrs;
59
jfloat* positions = NULL;
60
GlyphInfo *ginfo;
61
GlyphBlitVector *gbv;
62
63
jfloat x = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListX);
64
jfloat y = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListY);
65
jint len = toGlyph - fromGlyph;
66
jlongArray glyphImages = (jlongArray)
67
(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphImages);
68
jfloatArray glyphPositions =
69
(*env)->GetBooleanField(env, glyphlist, sunFontIDs.glyphListUsePos)
70
? (jfloatArray)
71
(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphListPos)
72
: NULL;
73
74
bytesNeeded = sizeof(GlyphBlitVector)+sizeof(ImageRef)*len;
75
gbv = (GlyphBlitVector*)malloc(bytesNeeded);
76
if (gbv == NULL) {
77
return NULL;
78
}
79
gbv->numGlyphs = len;
80
gbv->glyphs = (ImageRef*)((unsigned char*)gbv+sizeof(GlyphBlitVector));
81
82
imagePtrs = (*env)->GetPrimitiveArrayCritical(env, glyphImages, NULL);
83
if (imagePtrs == NULL) {
84
free(gbv);
85
return (GlyphBlitVector*)NULL;
86
}
87
88
if (glyphPositions) {
89
int n = fromGlyph * 2 - 1;
90
91
positions =
92
(*env)->GetPrimitiveArrayCritical(env, glyphPositions, NULL);
93
if (positions == NULL) {
94
(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,
95
imagePtrs, JNI_ABORT);
96
free(gbv);
97
return (GlyphBlitVector*)NULL;
98
}
99
100
for (g=0; g<len; g++) {
101
jfloat px = x + positions[++n];
102
jfloat py = y + positions[++n];
103
104
ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);
105
gbv->glyphs[g].glyphInfo = ginfo;
106
gbv->glyphs[g].pixels = ginfo->image;
107
gbv->glyphs[g].width = ginfo->width;
108
gbv->glyphs[g].rowBytes = ginfo->rowBytes;
109
gbv->glyphs[g].height = ginfo->height;
110
FLOOR_ASSIGN(gbv->glyphs[g].x, px + ginfo->topLeftX);
111
FLOOR_ASSIGN(gbv->glyphs[g].y, py + ginfo->topLeftY);
112
}
113
(*env)->ReleasePrimitiveArrayCritical(env,glyphPositions,
114
positions, JNI_ABORT);
115
} else {
116
for (g=0; g<len; g++) {
117
ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);
118
gbv->glyphs[g].glyphInfo = ginfo;
119
gbv->glyphs[g].pixels = ginfo->image;
120
gbv->glyphs[g].width = ginfo->width;
121
gbv->glyphs[g].rowBytes = ginfo->rowBytes;
122
gbv->glyphs[g].height = ginfo->height;
123
FLOOR_ASSIGN(gbv->glyphs[g].x, x + ginfo->topLeftX);
124
FLOOR_ASSIGN(gbv->glyphs[g].y, y + ginfo->topLeftY);
125
126
/* copy image data into this array at x/y locations */
127
x += ginfo->advanceX;
128
y += ginfo->advanceY;
129
}
130
}
131
132
(*env)->ReleasePrimitiveArrayCritical(env, glyphImages, imagePtrs,
133
JNI_ABORT);
134
135
if (!glyphPositions) {
136
(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListX, x);
137
(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListY, y);
138
}
139
140
return gbv;
141
}
142
143
jint RefineBounds(GlyphBlitVector *gbv, SurfaceDataBounds *bounds) {
144
int index;
145
jint dx1, dy1, dx2, dy2;
146
ImageRef glyphImage;
147
int num = gbv->numGlyphs;
148
SurfaceDataBounds glyphs;
149
150
glyphs.x1 = glyphs.y1 = 0x7fffffff;
151
glyphs.x2 = glyphs.y2 = 0x80000000;
152
for (index = 0; index < num; index++) {
153
glyphImage = gbv->glyphs[index];
154
dx1 = (jint) glyphImage.x;
155
dy1 = (jint) glyphImage.y;
156
dx2 = dx1 + glyphImage.width;
157
dy2 = dy1 + glyphImage.height;
158
if (glyphs.x1 > dx1) glyphs.x1 = dx1;
159
if (glyphs.y1 > dy1) glyphs.y1 = dy1;
160
if (glyphs.x2 < dx2) glyphs.x2 = dx2;
161
if (glyphs.y2 < dy2) glyphs.y2 = dy2;
162
}
163
164
SurfaceData_IntersectBounds(bounds, &glyphs);
165
return (bounds->x1 < bounds->x2 && bounds->y1 < bounds->y2);
166
}
167
168
169
170
171
/* since the AA and non-AA loop functions share a common method
172
* signature, can call both through this common function since
173
* there's no difference except for the inner loop.
174
* This could be a macro but there's enough of those already.
175
*/
176
static void drawGlyphList(JNIEnv *env, jobject self,
177
jobject sg2d, jobject sData,
178
GlyphBlitVector *gbv, jint pixel, jint color,
179
NativePrimitive *pPrim, DrawGlyphListFunc *func) {
180
181
SurfaceDataOps *sdOps;
182
SurfaceDataRasInfo rasInfo;
183
CompositeInfo compInfo;
184
int clipLeft, clipRight, clipTop, clipBottom;
185
int ret;
186
187
sdOps = SurfaceData_GetOps(env, sData);
188
if (sdOps == 0) {
189
return;
190
}
191
192
if (pPrim->pCompType->getCompInfo != NULL) {
193
GrPrim_Sg2dGetCompInfo(env, sg2d, pPrim, &compInfo);
194
}
195
196
GrPrim_Sg2dGetClip(env, sg2d, &rasInfo.bounds);
197
if (rasInfo.bounds.y2 <= rasInfo.bounds.y1 ||
198
rasInfo.bounds.x2 <= rasInfo.bounds.x1)
199
{
200
return;
201
}
202
203
ret = sdOps->Lock(env, sdOps, &rasInfo, pPrim->dstflags);
204
if (ret != SD_SUCCESS) {
205
if (ret == SD_SLOWLOCK) {
206
if (!RefineBounds(gbv, &rasInfo.bounds)) {
207
SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
208
return;
209
}
210
} else {
211
return;
212
}
213
}
214
215
sdOps->GetRasInfo(env, sdOps, &rasInfo);
216
if (!rasInfo.rasBase) {
217
SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
218
return;
219
}
220
clipLeft = rasInfo.bounds.x1;
221
clipRight = rasInfo.bounds.x2;
222
clipTop = rasInfo.bounds.y1;
223
clipBottom = rasInfo.bounds.y2;
224
if (clipRight > clipLeft && clipBottom > clipTop) {
225
226
(*func)(&rasInfo,
227
gbv->glyphs, gbv->numGlyphs,
228
pixel, color,
229
clipLeft, clipTop,
230
clipRight, clipBottom,
231
pPrim, &compInfo);
232
SurfaceData_InvokeRelease(env, sdOps, &rasInfo);
233
234
}
235
SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
236
}
237
238
static unsigned char* getLCDGammaLUT(int gamma);
239
static unsigned char* getInvLCDGammaLUT(int gamma);
240
241
static void drawGlyphListLCD(JNIEnv *env, jobject self,
242
jobject sg2d, jobject sData,
243
GlyphBlitVector *gbv, jint pixel, jint color,
244
jboolean rgbOrder, int contrast,
245
NativePrimitive *pPrim,
246
DrawGlyphListLCDFunc *func) {
247
248
SurfaceDataOps *sdOps;
249
SurfaceDataRasInfo rasInfo;
250
CompositeInfo compInfo;
251
int clipLeft, clipRight, clipTop, clipBottom;
252
int ret;
253
254
sdOps = SurfaceData_GetOps(env, sData);
255
if (sdOps == 0) {
256
return;
257
}
258
259
if (pPrim->pCompType->getCompInfo != NULL) {
260
GrPrim_Sg2dGetCompInfo(env, sg2d, pPrim, &compInfo);
261
}
262
263
GrPrim_Sg2dGetClip(env, sg2d, &rasInfo.bounds);
264
if (rasInfo.bounds.y2 <= rasInfo.bounds.y1 ||
265
rasInfo.bounds.x2 <= rasInfo.bounds.x1)
266
{
267
return;
268
}
269
270
ret = sdOps->Lock(env, sdOps, &rasInfo, pPrim->dstflags);
271
if (ret != SD_SUCCESS) {
272
if (ret == SD_SLOWLOCK) {
273
if (!RefineBounds(gbv, &rasInfo.bounds)) {
274
SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
275
return;
276
}
277
} else {
278
return;
279
}
280
}
281
282
sdOps->GetRasInfo(env, sdOps, &rasInfo);
283
if (!rasInfo.rasBase) {
284
SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
285
return;
286
}
287
clipLeft = rasInfo.bounds.x1;
288
clipRight = rasInfo.bounds.x2;
289
clipTop = rasInfo.bounds.y1;
290
clipBottom = rasInfo.bounds.y2;
291
292
if (clipRight > clipLeft && clipBottom > clipTop) {
293
294
(*func)(&rasInfo,
295
gbv->glyphs, gbv->numGlyphs,
296
pixel, color,
297
clipLeft, clipTop,
298
clipRight, clipBottom, (jint)rgbOrder,
299
getLCDGammaLUT(contrast), getInvLCDGammaLUT(contrast),
300
pPrim, &compInfo);
301
SurfaceData_InvokeRelease(env, sdOps, &rasInfo);
302
303
}
304
SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);
305
}
306
307
/*
308
* Class: sun_java2d_loops_DrawGlyphList
309
* Method: DrawGlyphList
310
* Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;Lsun/java2d/font/GlyphList;II)V
311
*/
312
JNIEXPORT void JNICALL
313
Java_sun_java2d_loops_DrawGlyphList_DrawGlyphList
314
(JNIEnv *env, jobject self,
315
jobject sg2d, jobject sData, jobject glyphlist,
316
jint fromGlyph, jint toGlyph) {
317
318
jint pixel, color;
319
GlyphBlitVector* gbv;
320
NativePrimitive *pPrim;
321
322
if ((pPrim = GetNativePrim(env, self)) == NULL) {
323
return;
324
}
325
326
if ((gbv = setupBlitVector(env, glyphlist, fromGlyph, toGlyph)) == NULL) {
327
return;
328
}
329
330
pixel = GrPrim_Sg2dGetPixel(env, sg2d);
331
color = GrPrim_Sg2dGetEaRGB(env, sg2d);
332
drawGlyphList(env, self, sg2d, sData, gbv, pixel, color,
333
pPrim, pPrim->funcs.drawglyphlist);
334
free(gbv);
335
336
}
337
338
/*
339
* Class: sun_java2d_loops_DrawGlyphListAA
340
* Method: DrawGlyphListAA
341
* Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;Lsun/java2d/font/GlyphList;II)V
342
*/
343
JNIEXPORT void JNICALL
344
Java_sun_java2d_loops_DrawGlyphListAA_DrawGlyphListAA
345
(JNIEnv *env, jobject self,
346
jobject sg2d, jobject sData, jobject glyphlist,
347
jint fromGlyph, jint toGlyph) {
348
349
jint pixel, color;
350
GlyphBlitVector* gbv;
351
NativePrimitive *pPrim;
352
353
if ((pPrim = GetNativePrim(env, self)) == NULL) {
354
return;
355
}
356
357
if ((gbv = setupBlitVector(env, glyphlist, fromGlyph, toGlyph)) == NULL) {
358
return;
359
}
360
pixel = GrPrim_Sg2dGetPixel(env, sg2d);
361
color = GrPrim_Sg2dGetEaRGB(env, sg2d);
362
drawGlyphList(env, self, sg2d, sData, gbv, pixel, color,
363
pPrim, pPrim->funcs.drawglyphlistaa);
364
free(gbv);
365
}
366
367
/*
368
* Class: sun_java2d_loops_DrawGlyphListLCD
369
* Method: DrawGlyphListLCD
370
* Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;Lsun/java2d/font/GlyphList;II)V
371
*/
372
JNIEXPORT void JNICALL
373
Java_sun_java2d_loops_DrawGlyphListLCD_DrawGlyphListLCD
374
(JNIEnv *env, jobject self,
375
jobject sg2d, jobject sData, jobject glyphlist,
376
jint fromGlyph, jint toGlyph) {
377
378
jint pixel, color, contrast;
379
jboolean rgbOrder;
380
GlyphBlitVector* gbv;
381
NativePrimitive *pPrim;
382
383
if ((pPrim = GetNativePrim(env, self)) == NULL) {
384
return;
385
}
386
387
if ((gbv = setupLCDBlitVector(env, glyphlist, fromGlyph, toGlyph))
388
== NULL) {
389
return;
390
}
391
pixel = GrPrim_Sg2dGetPixel(env, sg2d);
392
color = GrPrim_Sg2dGetEaRGB(env, sg2d);
393
contrast = GrPrim_Sg2dGetLCDTextContrast(env, sg2d);
394
rgbOrder = (*env)->GetBooleanField(env,glyphlist, sunFontIDs.lcdRGBOrder);
395
drawGlyphListLCD(env, self, sg2d, sData, gbv, pixel, color,
396
rgbOrder, contrast,
397
pPrim, pPrim->funcs.drawglyphlistlcd);
398
free(gbv);
399
}
400
401
/*
402
* LCD text utilises a filter which spreads energy to adjacent subpixels.
403
* So we add 3 bytes (one whole pixel) of padding at the start of every row
404
* to hold energy from the very leftmost sub-pixel.
405
* This is to the left of the intended glyph image position so LCD text also
406
* adjusts the top-left X position of the padded image one pixel to the left
407
* so a glyph image is drawn in the same place it would be if the padding
408
* were not present.
409
*
410
* So in the glyph cache for LCD text the first two bytes of every row are
411
* zero.
412
* We make use of this to be able to adjust the rendering position of the
413
* text when the client specifies a fractional metrics sub-pixel positioning
414
* rendering hint.
415
*
416
* So the first 6 bytes in a cache row looks like :
417
* 00 00 Ex G0 G1 G2
418
*
419
* where
420
* 00 are the always zero bytes
421
* Ex is extra energy spread from the glyph into the left padding pixel.
422
* Gn are the RGB component bytes of the first pixel of the glyph image
423
* For an RGB display G0 is the red component, etc.
424
*
425
* If a glyph is drawn at X=12 then the G0 G1 G2 pixel is placed at that
426
* position : ie G0 is drawn in the first sub-pixel at X=12
427
*
428
* Draw at X=12,0
429
* PIXEL POS 11 11 11 12 12 12 13 13 13
430
* SUBPX POS 0 1 2 0 1 2 0 1 2
431
* 00 00 Ex G0 G1 G2
432
*
433
* If a sub-pixel rounded glyph position is calculated as being X=12.33 -
434
* ie 12 and one-third pixels, we want the result to look like this :
435
* Draw at X=12,1
436
* PIXEL POS 11 11 11 12 12 12 13 13 13
437
* SUBPX POS 0 1 2 0 1 2 0 1 2
438
* 00 00 Ex G0 G1 G2
439
*
440
* ie the G0 byte is moved one sub-pixel to the right.
441
* To do this we need to make two adjustments :
442
* - set X=X+1
443
* - set start of scan row to start+2, ie index past the two zero bytes
444
* ie we don't need the 00 00 bytes at all any more. Rendering start X
445
* can skip over those.
446
*
447
* Lets look at the final case :
448
* If a sub-pixel rounded glyph position is calculated as being X=12.67 -
449
* ie 12 and two-third pixels, we want the result to look like this :
450
* Draw at X=12,2
451
* PIXEL POS 11 11 11 12 12 12 13 13 13
452
* SUBPX POS 0 1 2 0 1 2 0 1 2
453
* 00 00 Ex G0 G1 G2
454
*
455
* ie the G0 byte is moved two sub-pixels to the right, so that the image
456
* starts at 12.67
457
* To do this we need to make these two adjustments :
458
* - set X=X+1
459
* - set start of scan row to start+1, ie index past the first zero byte
460
* In this case the second of the 00 bytes is used as a no-op on the first
461
* red sub-pixel position.
462
*
463
* The final adjustment needed to make all this work is note that if
464
* we moved the start of row one or two bytes in we will go one or two bytes
465
* past the end of the row. So the glyph cache needs to have 2 bytes of
466
* zero padding at the end of each row. This is the extra memory cost to
467
* accommodate this algorithm.
468
*
469
* The resulting text is perhaps fractionally better in overall perception
470
* than rounding to the whole pixel grid, as a few issues arise.
471
*
472
* * the improvement in inter-glyph spacing as well as being limited
473
* to 1/3 pixel resolution, is also limited because the glyphs were hinted
474
* so they fit to the whole pixel grid. It may be worthwhile to pursue
475
* disabling x-axis gridfitting.
476
*
477
* * an LCD display may have gaps between the pixels that are greater
478
* than the subpixels. Thus for thin stemmed fonts, if the shift causes
479
* the "heart" of a stem to span whole pixels it may appear more diffuse -
480
* less sharp. Eliminating hinting would probably not make this worse - in
481
* effect we have already doing that here. But it would improve the spacing.
482
*
483
* * perhaps contradicting the above point in some ways, more diffuse glyphs
484
* are better at reducing colour fringing, but what appears to be more
485
* colour fringing in this FM case is more likely attributable to a greater
486
* likelihood for glyphs to abutt. In integer metrics or even whole pixel
487
* rendered fractional metrics, there's typically more space between the
488
* glyphs. Perhaps disabling X-axis grid-fitting will help with that.
489
*/
490
GlyphBlitVector* setupLCDBlitVector(JNIEnv *env, jobject glyphlist,
491
jint fromGlyph, jint toGlyph) {
492
493
int g;
494
size_t bytesNeeded;
495
jlong *imagePtrs;
496
jfloat* positions = NULL;
497
GlyphInfo *ginfo;
498
GlyphBlitVector *gbv;
499
500
jfloat x = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListX);
501
jfloat y = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListY);
502
jint len = toGlyph - fromGlyph;
503
jlongArray glyphImages = (jlongArray)
504
(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphImages);
505
jfloatArray glyphPositions =
506
(*env)->GetBooleanField(env, glyphlist, sunFontIDs.glyphListUsePos)
507
? (jfloatArray)
508
(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphListPos)
509
: NULL;
510
jboolean subPixPos =
511
(*env)->GetBooleanField(env,glyphlist, sunFontIDs.lcdSubPixPos);
512
513
bytesNeeded = sizeof(GlyphBlitVector)+sizeof(ImageRef)*len;
514
gbv = (GlyphBlitVector*)malloc(bytesNeeded);
515
if (gbv == NULL) {
516
return NULL;
517
}
518
gbv->numGlyphs = len;
519
gbv->glyphs = (ImageRef*)((unsigned char*)gbv+sizeof(GlyphBlitVector));
520
521
imagePtrs = (*env)->GetPrimitiveArrayCritical(env, glyphImages, NULL);
522
if (imagePtrs == NULL) {
523
free(gbv);
524
return (GlyphBlitVector*)NULL;
525
}
526
527
/* The position of the start of the text is adjusted up so
528
* that we can round it to an integral pixel position for a
529
* bitmap glyph or non-subpixel positioning, and round it to an
530
* integral subpixel position for that case, hence 0.5/3 = 0.166667
531
* Presently subPixPos means FM, and FM disables embedded bitmaps
532
* Therefore if subPixPos is true we should never get embedded bitmaps
533
* and the glyphlist will be homogenous. This test and the position
534
* adjustments will need to be per glyph once this case becomes
535
* heterogenous.
536
* Also set subPixPos=false if detect a B&W bitmap as we only
537
* need to test that on a per glyph basis once the list becomes
538
* heterogenous
539
*/
540
if (subPixPos && len > 0) {
541
ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[fromGlyph]);
542
if (ginfo == NULL) {
543
(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,
544
imagePtrs, JNI_ABORT);
545
free(gbv);
546
return (GlyphBlitVector*)NULL;
547
}
548
/* rowBytes==width tests if its a B&W or LCD glyph */
549
if (ginfo->width == ginfo->rowBytes) {
550
subPixPos = JNI_FALSE;
551
}
552
}
553
554
if (glyphPositions) {
555
int n = fromGlyph * 2 - 1;
556
557
positions =
558
(*env)->GetPrimitiveArrayCritical(env, glyphPositions, NULL);
559
if (positions == NULL) {
560
(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,
561
imagePtrs, JNI_ABORT);
562
free(gbv);
563
return (GlyphBlitVector*)NULL;
564
}
565
566
for (g=0; g<len; g++) {
567
jfloat px, py;
568
569
ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);
570
if (ginfo == NULL) {
571
(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,
572
imagePtrs, JNI_ABORT);
573
free(gbv);
574
return (GlyphBlitVector*)NULL;
575
}
576
gbv->glyphs[g].glyphInfo = ginfo;
577
gbv->glyphs[g].pixels = ginfo->image;
578
gbv->glyphs[g].width = ginfo->width;
579
gbv->glyphs[g].rowBytes = ginfo->rowBytes;
580
gbv->glyphs[g].height = ginfo->height;
581
582
px = x + positions[++n];
583
py = y + positions[++n];
584
585
/*
586
* Subpixel positioning may be requested for LCD text.
587
*
588
* Subpixel positioning can take place only in the direction in
589
* which the subpixels increase the resolution.
590
* So this is useful for the typical case of vertical stripes
591
* increasing the resolution in the direction of the glyph
592
* advances - ie typical horizontally laid out text.
593
* If the subpixel stripes are horizontal, subpixel positioning
594
* can take place only in the vertical direction, which isn't
595
* as useful - you would have to be drawing rotated text on
596
* a display which actually had that organisation. A pretty
597
* unlikely combination.
598
* So this is supported only for vertical stripes which
599
* increase the horizontal resolution.
600
* If in this case the client also rotates the text then there
601
* will still be some benefit for small rotations. For 90 degree
602
* rotation there's no horizontal advance and less benefit
603
* from the subpixel rendering too.
604
* The test for width==rowBytes detects the case where the glyph
605
* is a B&W image obtained from an embedded bitmap. In that
606
* case we cannot apply sub-pixel positioning so ignore it.
607
* This is handled on a per glyph basis.
608
*/
609
if (subPixPos) {
610
int frac;
611
float pos;
612
613
px += 0.1666667f - 0.5f;
614
py += 0.1666667f - 0.5f;
615
616
pos = px + ginfo->topLeftX;
617
FLOOR_ASSIGN(gbv->glyphs[g].x, pos);
618
/* Calculate the fractional pixel position - ie the subpixel
619
* position within the RGB/BGR triple. We are rounding to
620
* the nearest, even though we just do (int) since at the
621
* start of the loop the position was already adjusted by
622
* 0.5 (sub)pixels to get rounding.
623
* Thus the "fractional" position will be 0, 1 or 2.
624
* eg 0->0.32 is 0, 0.33->0.66 is 1, > 0.66->0.99 is 2.
625
* We can use an (int) cast here since the floor operation
626
* above guarantees us that the value is positive.
627
*/
628
frac = (int)((pos - gbv->glyphs[g].x)*3);
629
if (frac == 0) {
630
/* frac rounded down to zero, so this is equivalent
631
* to no sub-pixel positioning.
632
*/
633
gbv->glyphs[g].rowBytesOffset = 0;
634
} else {
635
/* In this case we need to adjust both the position at
636
* which the glyph will be positioned by one pixel to the
637
* left and adjust the position in the glyph image row
638
* from which to extract the data
639
* Every glyph image row has 2 bytes padding
640
* on the right to account for this.
641
*/
642
gbv->glyphs[g].rowBytesOffset = 3-frac;
643
gbv->glyphs[g].x += 1;
644
}
645
} else {
646
FLOOR_ASSIGN(gbv->glyphs[g].x, px + ginfo->topLeftX);
647
gbv->glyphs[g].rowBytesOffset = 0;
648
}
649
FLOOR_ASSIGN(gbv->glyphs[g].y, py + ginfo->topLeftY);
650
}
651
(*env)->ReleasePrimitiveArrayCritical(env,glyphPositions,
652
positions, JNI_ABORT);
653
} else {
654
for (g=0; g<len; g++) {
655
jfloat px = x;
656
jfloat py = y;
657
ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);
658
if (ginfo == NULL) {
659
(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,
660
imagePtrs, JNI_ABORT);
661
free(gbv);
662
return (GlyphBlitVector*)NULL;
663
}
664
gbv->glyphs[g].glyphInfo = ginfo;
665
gbv->glyphs[g].pixels = ginfo->image;
666
gbv->glyphs[g].width = ginfo->width;
667
gbv->glyphs[g].rowBytes = ginfo->rowBytes;
668
gbv->glyphs[g].height = ginfo->height;
669
670
if (subPixPos) {
671
int frac;
672
float pos;
673
674
px += 0.1666667f - 0.5f;
675
py += 0.1666667f - 0.5f;
676
677
pos = px + ginfo->topLeftX;
678
FLOOR_ASSIGN(gbv->glyphs[g].x, pos);
679
frac = (int)((pos - gbv->glyphs[g].x)*3);
680
if (frac == 0) {
681
gbv->glyphs[g].rowBytesOffset = 0;
682
} else {
683
gbv->glyphs[g].rowBytesOffset = 3-frac;
684
gbv->glyphs[g].x += 1;
685
}
686
} else {
687
FLOOR_ASSIGN(gbv->glyphs[g].x, px + ginfo->topLeftX);
688
gbv->glyphs[g].rowBytesOffset = 0;
689
}
690
FLOOR_ASSIGN(gbv->glyphs[g].y, py + ginfo->topLeftY);
691
692
/* copy image data into this array at x/y locations */
693
x += ginfo->advanceX;
694
y += ginfo->advanceY;
695
}
696
}
697
698
(*env)->ReleasePrimitiveArrayCritical(env, glyphImages, imagePtrs,
699
JNI_ABORT);
700
if (!glyphPositions) {
701
(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListX, x);
702
(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListY, y);
703
}
704
705
return gbv;
706
}
707
708
/* LCD text needs to go through a gamma (contrast) adjustment.
709
* Gamma is constrained to the range 1.0->2.2 with a quantization of
710
* 0.01 (more than good enough). Representing as an integer with that
711
* precision yields a range 100->250 thus we need to store up to 151 LUTs
712
* and inverse LUTs.
713
* We allocate the actual LUTs on an as needed basis. Typically zero or
714
* one is what will be needed.
715
* Colour component values are in the range 0.0->1.0 represented as an integer
716
* in the range 0->255 (ie in a byte). It is assumed that even if we have 5
717
* bit colour components these are presented mapped on to 8 bit components.
718
* lcdGammaLUT references LUTs which convert linear colour components
719
* to a gamma adjusted space, and
720
* lcdInvGammaLUT references LUTs which convert gamma adjusted colour
721
* components to a linear space.
722
*/
723
#define MIN_GAMMA 100
724
#define MAX_GAMMA 250
725
#define LCDLUTCOUNT (MAX_GAMMA-MIN_GAMMA+1)
726
UInt8 *lcdGammaLUT[LCDLUTCOUNT];
727
UInt8 *lcdInvGammaLUT[LCDLUTCOUNT];
728
729
void initLUT(int gamma) {
730
int i,index;
731
double ig,g;
732
733
index = gamma-MIN_GAMMA;
734
735
lcdGammaLUT[index] = (UInt8*)malloc(256);
736
lcdInvGammaLUT[index] = (UInt8*)malloc(256);
737
if (gamma==100) {
738
for (i=0;i<256;i++) {
739
lcdGammaLUT[index][i] = (UInt8)i;
740
lcdInvGammaLUT[index][i] = (UInt8)i;
741
}
742
return;
743
}
744
745
ig = ((double)gamma)/100.0;
746
g = 1.0/ig;
747
lcdGammaLUT[index][0] = (UInt8)0;
748
lcdInvGammaLUT[index][0] = (UInt8)0;
749
lcdGammaLUT[index][255] = (UInt8)255;
750
lcdInvGammaLUT[index][255] = (UInt8)255;
751
for (i=1;i<255;i++) {
752
double val = ((double)i)/255.0;
753
double gval = pow(val, g);
754
double igval = pow(val, ig);
755
lcdGammaLUT[index][i] = (UInt8)(255*gval);
756
lcdInvGammaLUT[index][i] = (UInt8)(255*igval);
757
}
758
}
759
760
static unsigned char* getLCDGammaLUT(int gamma) {
761
int index;
762
763
if (gamma<MIN_GAMMA) {
764
gamma = MIN_GAMMA;
765
} else if (gamma>MAX_GAMMA) {
766
gamma = MAX_GAMMA;
767
}
768
index = gamma-MIN_GAMMA;
769
if (!lcdGammaLUT[index]) {
770
initLUT(gamma);
771
}
772
return (unsigned char*)lcdGammaLUT[index];
773
}
774
775
static unsigned char* getInvLCDGammaLUT(int gamma) {
776
int index;
777
778
if (gamma<MIN_GAMMA) {
779
gamma = MIN_GAMMA;
780
} else if (gamma>MAX_GAMMA) {
781
gamma = MAX_GAMMA;
782
}
783
index = gamma-MIN_GAMMA;
784
if (!lcdInvGammaLUT[index]) {
785
initLUT(gamma);
786
}
787
return (unsigned char*)lcdInvGammaLUT[index];
788
}
789
790
#if 0
791
void printDefaultTables(int gamma) {
792
int i;
793
UInt8 *g, *ig;
794
lcdGammaLUT[gamma-MIN_GAMMA] = NULL;
795
lcdInvGammaLUT[gamma-MIN_GAMMA] = NULL;
796
g = getLCDGammaLUT(gamma);
797
ig = getInvLCDGammaLUT(gamma);
798
printf("UInt8 defaultGammaLUT[256] = {\n");
799
for (i=0;i<256;i++) {
800
if (i % 8 == 0) {
801
printf(" /* %3d */ ", i);
802
}
803
printf("%4d, ",(int)(g[i]&0xff));
804
if ((i+1) % 8 == 0) {
805
printf("\n");
806
}
807
}
808
printf("};\n");
809
810
printf("UInt8 defaultInvGammaLUT[256] = {\n");
811
for (i=0;i<256;i++) {
812
if (i % 8 == 0) {
813
printf(" /* %3d */ ", i);
814
}
815
printf("%4d, ",(int)(ig[i]&0xff));
816
if ((i+1) % 8 == 0) {
817
printf("\n");
818
}
819
}
820
printf("};\n");
821
}
822
#endif
823
824
/* These tables are generated for a Gamma adjustment of 1.4 */
825
UInt8 defaultGammaLUT[256] = {
826
/* 0 */ 0, 4, 7, 10, 13, 15, 17, 19,
827
/* 8 */ 21, 23, 25, 27, 28, 30, 32, 33,
828
/* 16 */ 35, 36, 38, 39, 41, 42, 44, 45,
829
/* 24 */ 47, 48, 49, 51, 52, 53, 55, 56,
830
/* 32 */ 57, 59, 60, 61, 62, 64, 65, 66,
831
/* 40 */ 67, 69, 70, 71, 72, 73, 75, 76,
832
/* 48 */ 77, 78, 79, 80, 81, 83, 84, 85,
833
/* 56 */ 86, 87, 88, 89, 90, 91, 92, 93,
834
/* 64 */ 94, 96, 97, 98, 99, 100, 101, 102,
835
/* 72 */ 103, 104, 105, 106, 107, 108, 109, 110,
836
/* 80 */ 111, 112, 113, 114, 115, 116, 117, 118,
837
/* 88 */ 119, 120, 121, 122, 123, 124, 125, 125,
838
/* 96 */ 126, 127, 128, 129, 130, 131, 132, 133,
839
/* 104 */ 134, 135, 136, 137, 138, 138, 139, 140,
840
/* 112 */ 141, 142, 143, 144, 145, 146, 147, 147,
841
/* 120 */ 148, 149, 150, 151, 152, 153, 154, 154,
842
/* 128 */ 155, 156, 157, 158, 159, 160, 161, 161,
843
/* 136 */ 162, 163, 164, 165, 166, 167, 167, 168,
844
/* 144 */ 169, 170, 171, 172, 172, 173, 174, 175,
845
/* 152 */ 176, 177, 177, 178, 179, 180, 181, 181,
846
/* 160 */ 182, 183, 184, 185, 186, 186, 187, 188,
847
/* 168 */ 189, 190, 190, 191, 192, 193, 194, 194,
848
/* 176 */ 195, 196, 197, 198, 198, 199, 200, 201,
849
/* 184 */ 201, 202, 203, 204, 205, 205, 206, 207,
850
/* 192 */ 208, 208, 209, 210, 211, 212, 212, 213,
851
/* 200 */ 214, 215, 215, 216, 217, 218, 218, 219,
852
/* 208 */ 220, 221, 221, 222, 223, 224, 224, 225,
853
/* 216 */ 226, 227, 227, 228, 229, 230, 230, 231,
854
/* 224 */ 232, 233, 233, 234, 235, 236, 236, 237,
855
/* 232 */ 238, 239, 239, 240, 241, 242, 242, 243,
856
/* 240 */ 244, 244, 245, 246, 247, 247, 248, 249,
857
/* 248 */ 249, 250, 251, 252, 252, 253, 254, 255,
858
};
859
860
UInt8 defaultInvGammaLUT[256] = {
861
/* 0 */ 0, 0, 0, 0, 0, 1, 1, 1,
862
/* 8 */ 2, 2, 2, 3, 3, 3, 4, 4,
863
/* 16 */ 5, 5, 6, 6, 7, 7, 8, 8,
864
/* 24 */ 9, 9, 10, 10, 11, 12, 12, 13,
865
/* 32 */ 13, 14, 15, 15, 16, 17, 17, 18,
866
/* 40 */ 19, 19, 20, 21, 21, 22, 23, 23,
867
/* 48 */ 24, 25, 26, 26, 27, 28, 29, 29,
868
/* 56 */ 30, 31, 32, 32, 33, 34, 35, 36,
869
/* 64 */ 36, 37, 38, 39, 40, 40, 41, 42,
870
/* 72 */ 43, 44, 45, 45, 46, 47, 48, 49,
871
/* 80 */ 50, 51, 52, 52, 53, 54, 55, 56,
872
/* 88 */ 57, 58, 59, 60, 61, 62, 63, 64,
873
/* 96 */ 64, 65, 66, 67, 68, 69, 70, 71,
874
/* 104 */ 72, 73, 74, 75, 76, 77, 78, 79,
875
/* 112 */ 80, 81, 82, 83, 84, 85, 86, 87,
876
/* 120 */ 88, 89, 90, 91, 92, 93, 95, 96,
877
/* 128 */ 97, 98, 99, 100, 101, 102, 103, 104,
878
/* 136 */ 105, 106, 107, 109, 110, 111, 112, 113,
879
/* 144 */ 114, 115, 116, 117, 119, 120, 121, 122,
880
/* 152 */ 123, 124, 125, 127, 128, 129, 130, 131,
881
/* 160 */ 132, 133, 135, 136, 137, 138, 139, 140,
882
/* 168 */ 142, 143, 144, 145, 146, 148, 149, 150,
883
/* 176 */ 151, 152, 154, 155, 156, 157, 159, 160,
884
/* 184 */ 161, 162, 163, 165, 166, 167, 168, 170,
885
/* 192 */ 171, 172, 173, 175, 176, 177, 178, 180,
886
/* 200 */ 181, 182, 184, 185, 186, 187, 189, 190,
887
/* 208 */ 191, 193, 194, 195, 196, 198, 199, 200,
888
/* 216 */ 202, 203, 204, 206, 207, 208, 210, 211,
889
/* 224 */ 212, 214, 215, 216, 218, 219, 220, 222,
890
/* 232 */ 223, 224, 226, 227, 228, 230, 231, 232,
891
/* 240 */ 234, 235, 236, 238, 239, 241, 242, 243,
892
/* 248 */ 245, 246, 248, 249, 250, 252, 253, 255,
893
};
894
895
896
/* Since our default is 140, here we can populate that from pre-calculated
897
* data, it needs only 512 bytes - plus a few more of overhead - and saves
898
* about that many intrinsic function calls plus other FP calculations.
899
*/
900
void initLCDGammaTables() {
901
memset(lcdGammaLUT, 0, LCDLUTCOUNT * sizeof(UInt8*));
902
memset(lcdInvGammaLUT, 0, LCDLUTCOUNT * sizeof(UInt8*));
903
/* printDefaultTables(140); */
904
lcdGammaLUT[40] = defaultGammaLUT;
905
lcdInvGammaLUT[40] = defaultInvGammaLUT;
906
}
907
908