Path: blob/master/src/java.desktop/share/native/libfontmanager/DrawGlyphList.c
41152 views
/*1* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jlong.h"26#include "math.h"27#include "string.h"28#include "stdlib.h"29#include "sunfontids.h"30#include "fontscalerdefs.h"31#include "glyphblitting.h"32#include "GraphicsPrimitiveMgr.h"33#include "sun_java2d_loops_DrawGlyphList.h"34#include "sun_java2d_loops_DrawGlyphListAA.h"353637/*38* Need to account for the rare case when (eg) repainting damaged39* areas results in the drawing location being negative, in which40* case (int) rounding always goes towards zero. We need to always41* round down instead, so that we paint at the correct position.42* We only call "floor" when value is < 0 (ie rarely).43* Storing the result of (eg) (x+ginfo->topLeftX) benchmarks is more44* expensive than repeating the calculation as we do here.45* "floor" shows up as a significant cost in app-level microbenchmarks.46* This macro avoids calling it on positive values, instead using an47* (int) cast.48*/49#define FLOOR_ASSIGN(l, r)\50if ((r)<0) (l) = ((int)floor(r)); else (l) = ((int)(r))5152GlyphBlitVector* setupBlitVector(JNIEnv *env, jobject glyphlist,53jint fromGlyph, jint toGlyph) {5455int g;56size_t bytesNeeded;57jlong *imagePtrs;58jfloat* positions = NULL;59GlyphInfo *ginfo;60GlyphBlitVector *gbv;6162jfloat x = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListX);63jfloat y = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListY);64jint len = toGlyph - fromGlyph;65jlongArray glyphImages = (jlongArray)66(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphImages);67jfloatArray glyphPositions =68(*env)->GetBooleanField(env, glyphlist, sunFontIDs.glyphListUsePos)69? (jfloatArray)70(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphListPos)71: NULL;7273bytesNeeded = sizeof(GlyphBlitVector)+sizeof(ImageRef)*len;74gbv = (GlyphBlitVector*)malloc(bytesNeeded);75if (gbv == NULL) {76return NULL;77}78gbv->numGlyphs = len;79gbv->glyphs = (ImageRef*)((unsigned char*)gbv+sizeof(GlyphBlitVector));8081imagePtrs = (*env)->GetPrimitiveArrayCritical(env, glyphImages, NULL);82if (imagePtrs == NULL) {83free(gbv);84return (GlyphBlitVector*)NULL;85}8687if (glyphPositions) {88int n = fromGlyph * 2 - 1;8990positions =91(*env)->GetPrimitiveArrayCritical(env, glyphPositions, NULL);92if (positions == NULL) {93(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,94imagePtrs, JNI_ABORT);95free(gbv);96return (GlyphBlitVector*)NULL;97}9899for (g=0; g<len; g++) {100jfloat px = x + positions[++n];101jfloat py = y + positions[++n];102103ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);104gbv->glyphs[g].glyphInfo = ginfo;105gbv->glyphs[g].pixels = ginfo->image;106gbv->glyphs[g].width = ginfo->width;107gbv->glyphs[g].rowBytes = ginfo->rowBytes;108gbv->glyphs[g].height = ginfo->height;109FLOOR_ASSIGN(gbv->glyphs[g].x, px + ginfo->topLeftX);110FLOOR_ASSIGN(gbv->glyphs[g].y, py + ginfo->topLeftY);111}112(*env)->ReleasePrimitiveArrayCritical(env,glyphPositions,113positions, JNI_ABORT);114} else {115for (g=0; g<len; g++) {116ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);117gbv->glyphs[g].glyphInfo = ginfo;118gbv->glyphs[g].pixels = ginfo->image;119gbv->glyphs[g].width = ginfo->width;120gbv->glyphs[g].rowBytes = ginfo->rowBytes;121gbv->glyphs[g].height = ginfo->height;122FLOOR_ASSIGN(gbv->glyphs[g].x, x + ginfo->topLeftX);123FLOOR_ASSIGN(gbv->glyphs[g].y, y + ginfo->topLeftY);124125/* copy image data into this array at x/y locations */126x += ginfo->advanceX;127y += ginfo->advanceY;128}129}130131(*env)->ReleasePrimitiveArrayCritical(env, glyphImages, imagePtrs,132JNI_ABORT);133134if (!glyphPositions) {135(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListX, x);136(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListY, y);137}138139return gbv;140}141142jint RefineBounds(GlyphBlitVector *gbv, SurfaceDataBounds *bounds) {143int index;144jint dx1, dy1, dx2, dy2;145ImageRef glyphImage;146int num = gbv->numGlyphs;147SurfaceDataBounds glyphs;148149glyphs.x1 = glyphs.y1 = 0x7fffffff;150glyphs.x2 = glyphs.y2 = 0x80000000;151for (index = 0; index < num; index++) {152glyphImage = gbv->glyphs[index];153dx1 = (jint) glyphImage.x;154dy1 = (jint) glyphImage.y;155dx2 = dx1 + glyphImage.width;156dy2 = dy1 + glyphImage.height;157if (glyphs.x1 > dx1) glyphs.x1 = dx1;158if (glyphs.y1 > dy1) glyphs.y1 = dy1;159if (glyphs.x2 < dx2) glyphs.x2 = dx2;160if (glyphs.y2 < dy2) glyphs.y2 = dy2;161}162163SurfaceData_IntersectBounds(bounds, &glyphs);164return (bounds->x1 < bounds->x2 && bounds->y1 < bounds->y2);165}166167168169170/* since the AA and non-AA loop functions share a common method171* signature, can call both through this common function since172* there's no difference except for the inner loop.173* This could be a macro but there's enough of those already.174*/175static void drawGlyphList(JNIEnv *env, jobject self,176jobject sg2d, jobject sData,177GlyphBlitVector *gbv, jint pixel, jint color,178NativePrimitive *pPrim, DrawGlyphListFunc *func) {179180SurfaceDataOps *sdOps;181SurfaceDataRasInfo rasInfo;182CompositeInfo compInfo;183int clipLeft, clipRight, clipTop, clipBottom;184int ret;185186sdOps = SurfaceData_GetOps(env, sData);187if (sdOps == 0) {188return;189}190191if (pPrim->pCompType->getCompInfo != NULL) {192GrPrim_Sg2dGetCompInfo(env, sg2d, pPrim, &compInfo);193}194195GrPrim_Sg2dGetClip(env, sg2d, &rasInfo.bounds);196if (rasInfo.bounds.y2 <= rasInfo.bounds.y1 ||197rasInfo.bounds.x2 <= rasInfo.bounds.x1)198{199return;200}201202ret = sdOps->Lock(env, sdOps, &rasInfo, pPrim->dstflags);203if (ret != SD_SUCCESS) {204if (ret == SD_SLOWLOCK) {205if (!RefineBounds(gbv, &rasInfo.bounds)) {206SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);207return;208}209} else {210return;211}212}213214sdOps->GetRasInfo(env, sdOps, &rasInfo);215if (!rasInfo.rasBase) {216SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);217return;218}219clipLeft = rasInfo.bounds.x1;220clipRight = rasInfo.bounds.x2;221clipTop = rasInfo.bounds.y1;222clipBottom = rasInfo.bounds.y2;223if (clipRight > clipLeft && clipBottom > clipTop) {224225(*func)(&rasInfo,226gbv->glyphs, gbv->numGlyphs,227pixel, color,228clipLeft, clipTop,229clipRight, clipBottom,230pPrim, &compInfo);231SurfaceData_InvokeRelease(env, sdOps, &rasInfo);232233}234SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);235}236237static unsigned char* getLCDGammaLUT(int gamma);238static unsigned char* getInvLCDGammaLUT(int gamma);239240static void drawGlyphListLCD(JNIEnv *env, jobject self,241jobject sg2d, jobject sData,242GlyphBlitVector *gbv, jint pixel, jint color,243jboolean rgbOrder, int contrast,244NativePrimitive *pPrim,245DrawGlyphListLCDFunc *func) {246247SurfaceDataOps *sdOps;248SurfaceDataRasInfo rasInfo;249CompositeInfo compInfo;250int clipLeft, clipRight, clipTop, clipBottom;251int ret;252253sdOps = SurfaceData_GetOps(env, sData);254if (sdOps == 0) {255return;256}257258if (pPrim->pCompType->getCompInfo != NULL) {259GrPrim_Sg2dGetCompInfo(env, sg2d, pPrim, &compInfo);260}261262GrPrim_Sg2dGetClip(env, sg2d, &rasInfo.bounds);263if (rasInfo.bounds.y2 <= rasInfo.bounds.y1 ||264rasInfo.bounds.x2 <= rasInfo.bounds.x1)265{266return;267}268269ret = sdOps->Lock(env, sdOps, &rasInfo, pPrim->dstflags);270if (ret != SD_SUCCESS) {271if (ret == SD_SLOWLOCK) {272if (!RefineBounds(gbv, &rasInfo.bounds)) {273SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);274return;275}276} else {277return;278}279}280281sdOps->GetRasInfo(env, sdOps, &rasInfo);282if (!rasInfo.rasBase) {283SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);284return;285}286clipLeft = rasInfo.bounds.x1;287clipRight = rasInfo.bounds.x2;288clipTop = rasInfo.bounds.y1;289clipBottom = rasInfo.bounds.y2;290291if (clipRight > clipLeft && clipBottom > clipTop) {292293(*func)(&rasInfo,294gbv->glyphs, gbv->numGlyphs,295pixel, color,296clipLeft, clipTop,297clipRight, clipBottom, (jint)rgbOrder,298getLCDGammaLUT(contrast), getInvLCDGammaLUT(contrast),299pPrim, &compInfo);300SurfaceData_InvokeRelease(env, sdOps, &rasInfo);301302}303SurfaceData_InvokeUnlock(env, sdOps, &rasInfo);304}305306/*307* Class: sun_java2d_loops_DrawGlyphList308* Method: DrawGlyphList309* Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;Lsun/java2d/font/GlyphList;II)V310*/311JNIEXPORT void JNICALL312Java_sun_java2d_loops_DrawGlyphList_DrawGlyphList313(JNIEnv *env, jobject self,314jobject sg2d, jobject sData, jobject glyphlist,315jint fromGlyph, jint toGlyph) {316317jint pixel, color;318GlyphBlitVector* gbv;319NativePrimitive *pPrim;320321if ((pPrim = GetNativePrim(env, self)) == NULL) {322return;323}324325if ((gbv = setupBlitVector(env, glyphlist, fromGlyph, toGlyph)) == NULL) {326return;327}328329pixel = GrPrim_Sg2dGetPixel(env, sg2d);330color = GrPrim_Sg2dGetEaRGB(env, sg2d);331drawGlyphList(env, self, sg2d, sData, gbv, pixel, color,332pPrim, pPrim->funcs.drawglyphlist);333free(gbv);334335}336337/*338* Class: sun_java2d_loops_DrawGlyphListAA339* Method: DrawGlyphListAA340* Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;Lsun/java2d/font/GlyphList;II)V341*/342JNIEXPORT void JNICALL343Java_sun_java2d_loops_DrawGlyphListAA_DrawGlyphListAA344(JNIEnv *env, jobject self,345jobject sg2d, jobject sData, jobject glyphlist,346jint fromGlyph, jint toGlyph) {347348jint pixel, color;349GlyphBlitVector* gbv;350NativePrimitive *pPrim;351352if ((pPrim = GetNativePrim(env, self)) == NULL) {353return;354}355356if ((gbv = setupBlitVector(env, glyphlist, fromGlyph, toGlyph)) == NULL) {357return;358}359pixel = GrPrim_Sg2dGetPixel(env, sg2d);360color = GrPrim_Sg2dGetEaRGB(env, sg2d);361drawGlyphList(env, self, sg2d, sData, gbv, pixel, color,362pPrim, pPrim->funcs.drawglyphlistaa);363free(gbv);364}365366/*367* Class: sun_java2d_loops_DrawGlyphListLCD368* Method: DrawGlyphListLCD369* Signature: (Lsun/java2d/SunGraphics2D;Lsun/java2d/SurfaceData;Lsun/java2d/font/GlyphList;II)V370*/371JNIEXPORT void JNICALL372Java_sun_java2d_loops_DrawGlyphListLCD_DrawGlyphListLCD373(JNIEnv *env, jobject self,374jobject sg2d, jobject sData, jobject glyphlist,375jint fromGlyph, jint toGlyph) {376377jint pixel, color, contrast;378jboolean rgbOrder;379GlyphBlitVector* gbv;380NativePrimitive *pPrim;381382if ((pPrim = GetNativePrim(env, self)) == NULL) {383return;384}385386if ((gbv = setupLCDBlitVector(env, glyphlist, fromGlyph, toGlyph))387== NULL) {388return;389}390pixel = GrPrim_Sg2dGetPixel(env, sg2d);391color = GrPrim_Sg2dGetEaRGB(env, sg2d);392contrast = GrPrim_Sg2dGetLCDTextContrast(env, sg2d);393rgbOrder = (*env)->GetBooleanField(env,glyphlist, sunFontIDs.lcdRGBOrder);394drawGlyphListLCD(env, self, sg2d, sData, gbv, pixel, color,395rgbOrder, contrast,396pPrim, pPrim->funcs.drawglyphlistlcd);397free(gbv);398}399400/*401* LCD text utilises a filter which spreads energy to adjacent subpixels.402* So we add 3 bytes (one whole pixel) of padding at the start of every row403* to hold energy from the very leftmost sub-pixel.404* This is to the left of the intended glyph image position so LCD text also405* adjusts the top-left X position of the padded image one pixel to the left406* so a glyph image is drawn in the same place it would be if the padding407* were not present.408*409* So in the glyph cache for LCD text the first two bytes of every row are410* zero.411* We make use of this to be able to adjust the rendering position of the412* text when the client specifies a fractional metrics sub-pixel positioning413* rendering hint.414*415* So the first 6 bytes in a cache row looks like :416* 00 00 Ex G0 G1 G2417*418* where419* 00 are the always zero bytes420* Ex is extra energy spread from the glyph into the left padding pixel.421* Gn are the RGB component bytes of the first pixel of the glyph image422* For an RGB display G0 is the red component, etc.423*424* If a glyph is drawn at X=12 then the G0 G1 G2 pixel is placed at that425* position : ie G0 is drawn in the first sub-pixel at X=12426*427* Draw at X=12,0428* PIXEL POS 11 11 11 12 12 12 13 13 13429* SUBPX POS 0 1 2 0 1 2 0 1 2430* 00 00 Ex G0 G1 G2431*432* If a sub-pixel rounded glyph position is calculated as being X=12.33 -433* ie 12 and one-third pixels, we want the result to look like this :434* Draw at X=12,1435* PIXEL POS 11 11 11 12 12 12 13 13 13436* SUBPX POS 0 1 2 0 1 2 0 1 2437* 00 00 Ex G0 G1 G2438*439* ie the G0 byte is moved one sub-pixel to the right.440* To do this we need to make two adjustments :441* - set X=X+1442* - set start of scan row to start+2, ie index past the two zero bytes443* ie we don't need the 00 00 bytes at all any more. Rendering start X444* can skip over those.445*446* Lets look at the final case :447* If a sub-pixel rounded glyph position is calculated as being X=12.67 -448* ie 12 and two-third pixels, we want the result to look like this :449* Draw at X=12,2450* PIXEL POS 11 11 11 12 12 12 13 13 13451* SUBPX POS 0 1 2 0 1 2 0 1 2452* 00 00 Ex G0 G1 G2453*454* ie the G0 byte is moved two sub-pixels to the right, so that the image455* starts at 12.67456* To do this we need to make these two adjustments :457* - set X=X+1458* - set start of scan row to start+1, ie index past the first zero byte459* In this case the second of the 00 bytes is used as a no-op on the first460* red sub-pixel position.461*462* The final adjustment needed to make all this work is note that if463* we moved the start of row one or two bytes in we will go one or two bytes464* past the end of the row. So the glyph cache needs to have 2 bytes of465* zero padding at the end of each row. This is the extra memory cost to466* accommodate this algorithm.467*468* The resulting text is perhaps fractionally better in overall perception469* than rounding to the whole pixel grid, as a few issues arise.470*471* * the improvement in inter-glyph spacing as well as being limited472* to 1/3 pixel resolution, is also limited because the glyphs were hinted473* so they fit to the whole pixel grid. It may be worthwhile to pursue474* disabling x-axis gridfitting.475*476* * an LCD display may have gaps between the pixels that are greater477* than the subpixels. Thus for thin stemmed fonts, if the shift causes478* the "heart" of a stem to span whole pixels it may appear more diffuse -479* less sharp. Eliminating hinting would probably not make this worse - in480* effect we have already doing that here. But it would improve the spacing.481*482* * perhaps contradicting the above point in some ways, more diffuse glyphs483* are better at reducing colour fringing, but what appears to be more484* colour fringing in this FM case is more likely attributable to a greater485* likelihood for glyphs to abutt. In integer metrics or even whole pixel486* rendered fractional metrics, there's typically more space between the487* glyphs. Perhaps disabling X-axis grid-fitting will help with that.488*/489GlyphBlitVector* setupLCDBlitVector(JNIEnv *env, jobject glyphlist,490jint fromGlyph, jint toGlyph) {491492int g;493size_t bytesNeeded;494jlong *imagePtrs;495jfloat* positions = NULL;496GlyphInfo *ginfo;497GlyphBlitVector *gbv;498499jfloat x = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListX);500jfloat y = (*env)->GetFloatField(env, glyphlist, sunFontIDs.glyphListY);501jint len = toGlyph - fromGlyph;502jlongArray glyphImages = (jlongArray)503(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphImages);504jfloatArray glyphPositions =505(*env)->GetBooleanField(env, glyphlist, sunFontIDs.glyphListUsePos)506? (jfloatArray)507(*env)->GetObjectField(env, glyphlist, sunFontIDs.glyphListPos)508: NULL;509jboolean subPixPos =510(*env)->GetBooleanField(env,glyphlist, sunFontIDs.lcdSubPixPos);511512bytesNeeded = sizeof(GlyphBlitVector)+sizeof(ImageRef)*len;513gbv = (GlyphBlitVector*)malloc(bytesNeeded);514if (gbv == NULL) {515return NULL;516}517gbv->numGlyphs = len;518gbv->glyphs = (ImageRef*)((unsigned char*)gbv+sizeof(GlyphBlitVector));519520imagePtrs = (*env)->GetPrimitiveArrayCritical(env, glyphImages, NULL);521if (imagePtrs == NULL) {522free(gbv);523return (GlyphBlitVector*)NULL;524}525526/* The position of the start of the text is adjusted up so527* that we can round it to an integral pixel position for a528* bitmap glyph or non-subpixel positioning, and round it to an529* integral subpixel position for that case, hence 0.5/3 = 0.166667530* Presently subPixPos means FM, and FM disables embedded bitmaps531* Therefore if subPixPos is true we should never get embedded bitmaps532* and the glyphlist will be homogenous. This test and the position533* adjustments will need to be per glyph once this case becomes534* heterogenous.535* Also set subPixPos=false if detect a B&W bitmap as we only536* need to test that on a per glyph basis once the list becomes537* heterogenous538*/539if (subPixPos && len > 0) {540ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[fromGlyph]);541if (ginfo == NULL) {542(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,543imagePtrs, JNI_ABORT);544free(gbv);545return (GlyphBlitVector*)NULL;546}547/* rowBytes==width tests if its a B&W or LCD glyph */548if (ginfo->width == ginfo->rowBytes) {549subPixPos = JNI_FALSE;550}551}552553if (glyphPositions) {554int n = fromGlyph * 2 - 1;555556positions =557(*env)->GetPrimitiveArrayCritical(env, glyphPositions, NULL);558if (positions == NULL) {559(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,560imagePtrs, JNI_ABORT);561free(gbv);562return (GlyphBlitVector*)NULL;563}564565for (g=0; g<len; g++) {566jfloat px, py;567568ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);569if (ginfo == NULL) {570(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,571imagePtrs, JNI_ABORT);572free(gbv);573return (GlyphBlitVector*)NULL;574}575gbv->glyphs[g].glyphInfo = ginfo;576gbv->glyphs[g].pixels = ginfo->image;577gbv->glyphs[g].width = ginfo->width;578gbv->glyphs[g].rowBytes = ginfo->rowBytes;579gbv->glyphs[g].height = ginfo->height;580581px = x + positions[++n];582py = y + positions[++n];583584/*585* Subpixel positioning may be requested for LCD text.586*587* Subpixel positioning can take place only in the direction in588* which the subpixels increase the resolution.589* So this is useful for the typical case of vertical stripes590* increasing the resolution in the direction of the glyph591* advances - ie typical horizontally laid out text.592* If the subpixel stripes are horizontal, subpixel positioning593* can take place only in the vertical direction, which isn't594* as useful - you would have to be drawing rotated text on595* a display which actually had that organisation. A pretty596* unlikely combination.597* So this is supported only for vertical stripes which598* increase the horizontal resolution.599* If in this case the client also rotates the text then there600* will still be some benefit for small rotations. For 90 degree601* rotation there's no horizontal advance and less benefit602* from the subpixel rendering too.603* The test for width==rowBytes detects the case where the glyph604* is a B&W image obtained from an embedded bitmap. In that605* case we cannot apply sub-pixel positioning so ignore it.606* This is handled on a per glyph basis.607*/608if (subPixPos) {609int frac;610float pos;611612px += 0.1666667f - 0.5f;613py += 0.1666667f - 0.5f;614615pos = px + ginfo->topLeftX;616FLOOR_ASSIGN(gbv->glyphs[g].x, pos);617/* Calculate the fractional pixel position - ie the subpixel618* position within the RGB/BGR triple. We are rounding to619* the nearest, even though we just do (int) since at the620* start of the loop the position was already adjusted by621* 0.5 (sub)pixels to get rounding.622* Thus the "fractional" position will be 0, 1 or 2.623* eg 0->0.32 is 0, 0.33->0.66 is 1, > 0.66->0.99 is 2.624* We can use an (int) cast here since the floor operation625* above guarantees us that the value is positive.626*/627frac = (int)((pos - gbv->glyphs[g].x)*3);628if (frac == 0) {629/* frac rounded down to zero, so this is equivalent630* to no sub-pixel positioning.631*/632gbv->glyphs[g].rowBytesOffset = 0;633} else {634/* In this case we need to adjust both the position at635* which the glyph will be positioned by one pixel to the636* left and adjust the position in the glyph image row637* from which to extract the data638* Every glyph image row has 2 bytes padding639* on the right to account for this.640*/641gbv->glyphs[g].rowBytesOffset = 3-frac;642gbv->glyphs[g].x += 1;643}644} else {645FLOOR_ASSIGN(gbv->glyphs[g].x, px + ginfo->topLeftX);646gbv->glyphs[g].rowBytesOffset = 0;647}648FLOOR_ASSIGN(gbv->glyphs[g].y, py + ginfo->topLeftY);649}650(*env)->ReleasePrimitiveArrayCritical(env,glyphPositions,651positions, JNI_ABORT);652} else {653for (g=0; g<len; g++) {654jfloat px = x;655jfloat py = y;656ginfo = (GlyphInfo*)((uintptr_t)imagePtrs[g + fromGlyph]);657if (ginfo == NULL) {658(*env)->ReleasePrimitiveArrayCritical(env, glyphImages,659imagePtrs, JNI_ABORT);660free(gbv);661return (GlyphBlitVector*)NULL;662}663gbv->glyphs[g].glyphInfo = ginfo;664gbv->glyphs[g].pixels = ginfo->image;665gbv->glyphs[g].width = ginfo->width;666gbv->glyphs[g].rowBytes = ginfo->rowBytes;667gbv->glyphs[g].height = ginfo->height;668669if (subPixPos) {670int frac;671float pos;672673px += 0.1666667f - 0.5f;674py += 0.1666667f - 0.5f;675676pos = px + ginfo->topLeftX;677FLOOR_ASSIGN(gbv->glyphs[g].x, pos);678frac = (int)((pos - gbv->glyphs[g].x)*3);679if (frac == 0) {680gbv->glyphs[g].rowBytesOffset = 0;681} else {682gbv->glyphs[g].rowBytesOffset = 3-frac;683gbv->glyphs[g].x += 1;684}685} else {686FLOOR_ASSIGN(gbv->glyphs[g].x, px + ginfo->topLeftX);687gbv->glyphs[g].rowBytesOffset = 0;688}689FLOOR_ASSIGN(gbv->glyphs[g].y, py + ginfo->topLeftY);690691/* copy image data into this array at x/y locations */692x += ginfo->advanceX;693y += ginfo->advanceY;694}695}696697(*env)->ReleasePrimitiveArrayCritical(env, glyphImages, imagePtrs,698JNI_ABORT);699if (!glyphPositions) {700(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListX, x);701(*env)->SetFloatField(env, glyphlist, sunFontIDs.glyphListY, y);702}703704return gbv;705}706707/* LCD text needs to go through a gamma (contrast) adjustment.708* Gamma is constrained to the range 1.0->2.2 with a quantization of709* 0.01 (more than good enough). Representing as an integer with that710* precision yields a range 100->250 thus we need to store up to 151 LUTs711* and inverse LUTs.712* We allocate the actual LUTs on an as needed basis. Typically zero or713* one is what will be needed.714* Colour component values are in the range 0.0->1.0 represented as an integer715* in the range 0->255 (ie in a byte). It is assumed that even if we have 5716* bit colour components these are presented mapped on to 8 bit components.717* lcdGammaLUT references LUTs which convert linear colour components718* to a gamma adjusted space, and719* lcdInvGammaLUT references LUTs which convert gamma adjusted colour720* components to a linear space.721*/722#define MIN_GAMMA 100723#define MAX_GAMMA 250724#define LCDLUTCOUNT (MAX_GAMMA-MIN_GAMMA+1)725UInt8 *lcdGammaLUT[LCDLUTCOUNT];726UInt8 *lcdInvGammaLUT[LCDLUTCOUNT];727728void initLUT(int gamma) {729int i,index;730double ig,g;731732index = gamma-MIN_GAMMA;733734lcdGammaLUT[index] = (UInt8*)malloc(256);735lcdInvGammaLUT[index] = (UInt8*)malloc(256);736if (gamma==100) {737for (i=0;i<256;i++) {738lcdGammaLUT[index][i] = (UInt8)i;739lcdInvGammaLUT[index][i] = (UInt8)i;740}741return;742}743744ig = ((double)gamma)/100.0;745g = 1.0/ig;746lcdGammaLUT[index][0] = (UInt8)0;747lcdInvGammaLUT[index][0] = (UInt8)0;748lcdGammaLUT[index][255] = (UInt8)255;749lcdInvGammaLUT[index][255] = (UInt8)255;750for (i=1;i<255;i++) {751double val = ((double)i)/255.0;752double gval = pow(val, g);753double igval = pow(val, ig);754lcdGammaLUT[index][i] = (UInt8)(255*gval);755lcdInvGammaLUT[index][i] = (UInt8)(255*igval);756}757}758759static unsigned char* getLCDGammaLUT(int gamma) {760int index;761762if (gamma<MIN_GAMMA) {763gamma = MIN_GAMMA;764} else if (gamma>MAX_GAMMA) {765gamma = MAX_GAMMA;766}767index = gamma-MIN_GAMMA;768if (!lcdGammaLUT[index]) {769initLUT(gamma);770}771return (unsigned char*)lcdGammaLUT[index];772}773774static unsigned char* getInvLCDGammaLUT(int gamma) {775int index;776777if (gamma<MIN_GAMMA) {778gamma = MIN_GAMMA;779} else if (gamma>MAX_GAMMA) {780gamma = MAX_GAMMA;781}782index = gamma-MIN_GAMMA;783if (!lcdInvGammaLUT[index]) {784initLUT(gamma);785}786return (unsigned char*)lcdInvGammaLUT[index];787}788789#if 0790void printDefaultTables(int gamma) {791int i;792UInt8 *g, *ig;793lcdGammaLUT[gamma-MIN_GAMMA] = NULL;794lcdInvGammaLUT[gamma-MIN_GAMMA] = NULL;795g = getLCDGammaLUT(gamma);796ig = getInvLCDGammaLUT(gamma);797printf("UInt8 defaultGammaLUT[256] = {\n");798for (i=0;i<256;i++) {799if (i % 8 == 0) {800printf(" /* %3d */ ", i);801}802printf("%4d, ",(int)(g[i]&0xff));803if ((i+1) % 8 == 0) {804printf("\n");805}806}807printf("};\n");808809printf("UInt8 defaultInvGammaLUT[256] = {\n");810for (i=0;i<256;i++) {811if (i % 8 == 0) {812printf(" /* %3d */ ", i);813}814printf("%4d, ",(int)(ig[i]&0xff));815if ((i+1) % 8 == 0) {816printf("\n");817}818}819printf("};\n");820}821#endif822823/* These tables are generated for a Gamma adjustment of 1.4 */824UInt8 defaultGammaLUT[256] = {825/* 0 */ 0, 4, 7, 10, 13, 15, 17, 19,826/* 8 */ 21, 23, 25, 27, 28, 30, 32, 33,827/* 16 */ 35, 36, 38, 39, 41, 42, 44, 45,828/* 24 */ 47, 48, 49, 51, 52, 53, 55, 56,829/* 32 */ 57, 59, 60, 61, 62, 64, 65, 66,830/* 40 */ 67, 69, 70, 71, 72, 73, 75, 76,831/* 48 */ 77, 78, 79, 80, 81, 83, 84, 85,832/* 56 */ 86, 87, 88, 89, 90, 91, 92, 93,833/* 64 */ 94, 96, 97, 98, 99, 100, 101, 102,834/* 72 */ 103, 104, 105, 106, 107, 108, 109, 110,835/* 80 */ 111, 112, 113, 114, 115, 116, 117, 118,836/* 88 */ 119, 120, 121, 122, 123, 124, 125, 125,837/* 96 */ 126, 127, 128, 129, 130, 131, 132, 133,838/* 104 */ 134, 135, 136, 137, 138, 138, 139, 140,839/* 112 */ 141, 142, 143, 144, 145, 146, 147, 147,840/* 120 */ 148, 149, 150, 151, 152, 153, 154, 154,841/* 128 */ 155, 156, 157, 158, 159, 160, 161, 161,842/* 136 */ 162, 163, 164, 165, 166, 167, 167, 168,843/* 144 */ 169, 170, 171, 172, 172, 173, 174, 175,844/* 152 */ 176, 177, 177, 178, 179, 180, 181, 181,845/* 160 */ 182, 183, 184, 185, 186, 186, 187, 188,846/* 168 */ 189, 190, 190, 191, 192, 193, 194, 194,847/* 176 */ 195, 196, 197, 198, 198, 199, 200, 201,848/* 184 */ 201, 202, 203, 204, 205, 205, 206, 207,849/* 192 */ 208, 208, 209, 210, 211, 212, 212, 213,850/* 200 */ 214, 215, 215, 216, 217, 218, 218, 219,851/* 208 */ 220, 221, 221, 222, 223, 224, 224, 225,852/* 216 */ 226, 227, 227, 228, 229, 230, 230, 231,853/* 224 */ 232, 233, 233, 234, 235, 236, 236, 237,854/* 232 */ 238, 239, 239, 240, 241, 242, 242, 243,855/* 240 */ 244, 244, 245, 246, 247, 247, 248, 249,856/* 248 */ 249, 250, 251, 252, 252, 253, 254, 255,857};858859UInt8 defaultInvGammaLUT[256] = {860/* 0 */ 0, 0, 0, 0, 0, 1, 1, 1,861/* 8 */ 2, 2, 2, 3, 3, 3, 4, 4,862/* 16 */ 5, 5, 6, 6, 7, 7, 8, 8,863/* 24 */ 9, 9, 10, 10, 11, 12, 12, 13,864/* 32 */ 13, 14, 15, 15, 16, 17, 17, 18,865/* 40 */ 19, 19, 20, 21, 21, 22, 23, 23,866/* 48 */ 24, 25, 26, 26, 27, 28, 29, 29,867/* 56 */ 30, 31, 32, 32, 33, 34, 35, 36,868/* 64 */ 36, 37, 38, 39, 40, 40, 41, 42,869/* 72 */ 43, 44, 45, 45, 46, 47, 48, 49,870/* 80 */ 50, 51, 52, 52, 53, 54, 55, 56,871/* 88 */ 57, 58, 59, 60, 61, 62, 63, 64,872/* 96 */ 64, 65, 66, 67, 68, 69, 70, 71,873/* 104 */ 72, 73, 74, 75, 76, 77, 78, 79,874/* 112 */ 80, 81, 82, 83, 84, 85, 86, 87,875/* 120 */ 88, 89, 90, 91, 92, 93, 95, 96,876/* 128 */ 97, 98, 99, 100, 101, 102, 103, 104,877/* 136 */ 105, 106, 107, 109, 110, 111, 112, 113,878/* 144 */ 114, 115, 116, 117, 119, 120, 121, 122,879/* 152 */ 123, 124, 125, 127, 128, 129, 130, 131,880/* 160 */ 132, 133, 135, 136, 137, 138, 139, 140,881/* 168 */ 142, 143, 144, 145, 146, 148, 149, 150,882/* 176 */ 151, 152, 154, 155, 156, 157, 159, 160,883/* 184 */ 161, 162, 163, 165, 166, 167, 168, 170,884/* 192 */ 171, 172, 173, 175, 176, 177, 178, 180,885/* 200 */ 181, 182, 184, 185, 186, 187, 189, 190,886/* 208 */ 191, 193, 194, 195, 196, 198, 199, 200,887/* 216 */ 202, 203, 204, 206, 207, 208, 210, 211,888/* 224 */ 212, 214, 215, 216, 218, 219, 220, 222,889/* 232 */ 223, 224, 226, 227, 228, 230, 231, 232,890/* 240 */ 234, 235, 236, 238, 239, 241, 242, 243,891/* 248 */ 245, 246, 248, 249, 250, 252, 253, 255,892};893894895/* Since our default is 140, here we can populate that from pre-calculated896* data, it needs only 512 bytes - plus a few more of overhead - and saves897* about that many intrinsic function calls plus other FP calculations.898*/899void initLCDGammaTables() {900memset(lcdGammaLUT, 0, LCDLUTCOUNT * sizeof(UInt8*));901memset(lcdInvGammaLUT, 0, LCDLUTCOUNT * sizeof(UInt8*));902/* printDefaultTables(140); */903lcdGammaLUT[40] = defaultGammaLUT;904lcdInvGammaLUT[40] = defaultInvGammaLUT;905}906907908