Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CTextPipe.m
41152 views
/*1* Copyright (c) 2011, 2013, 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// Native side of the Quartz text pipe, paints on Quartz Surface Datas.26// Interesting Docs : /Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/FontHandling/FontHandling.html2728#import "sun_awt_SunHints.h"29#import "sun_lwawt_macosx_CTextPipe.h"30#import "sun_java2d_OSXSurfaceData.h"3132#import "CoreTextSupport.h"33#import "QuartzSurfaceData.h"34#include "AWTStrike.h"35#import "JNIUtilities.h"3637static const CGAffineTransform sInverseTX = { 1, 0, 0, -1, 0, 0 };383940#pragma mark --- CoreText Support ---414243// Translates a Unicode into a CGGlyph/CTFontRef pair44// Returns the substituted font, and places the appropriate glyph into "glyphRef"45CTFontRef JavaCT_CopyCTFallbackFontAndGlyphForUnicode46(const AWTFont *font, const UTF16Char *charRef, CGGlyph *glyphRef, int count) {47CTFontRef fallback = JRSFontCreateFallbackFontForCharacters((CTFontRef)font->fFont, charRef, count);48if (fallback == NULL)49{50// use the original font if we somehow got duped into trying to fallback something we can't51fallback = (CTFontRef)font->fFont;52CFRetain(fallback);53}5455CTFontGetGlyphsForCharacters(fallback, charRef, glyphRef, count);56return fallback;57}5859// Translates a Java glyph code int (might be a negative unicode value) into a CGGlyph/CTFontRef pair60// Returns the substituted font, and places the appropriate glyph into "glyph"61CTFontRef JavaCT_CopyCTFallbackFontAndGlyphForJavaGlyphCode62(const AWTFont *font, const jint glyphCode, CGGlyph *glyphRef)63{64// negative glyph codes are really unicodes, which were placed there by the mapper65// to indicate we should use CoreText to substitute the character66if (glyphCode >= 0)67{68*glyphRef = glyphCode;69CFRetain(font->fFont);70return (CTFontRef)font->fFont;71}7273UTF16Char character = -glyphCode;74return JavaCT_CopyCTFallbackFontAndGlyphForUnicode(font, &character, glyphRef, 1);75}7677// Breakup a 32 bit unicode value into the component surrogate pairs78void JavaCT_BreakupUnicodeIntoSurrogatePairs(int uniChar, UTF16Char charRef[]) {79int value = uniChar - 0x10000;80UTF16Char low_surrogate = (value & 0x3FF) | LO_SURROGATE_START;81UTF16Char high_surrogate = (((int)(value & 0xFFC00)) >> 10) | HI_SURROGATE_START;82charRef[0] = high_surrogate;83charRef[1] = low_surrogate;84}85868788/*89* Callback for CoreText which uses the CoreTextProviderStruct to feed CT UniChars90* We only use it for one-off lines, and don't attempt to fragment our strings91*/92const UniChar *Java_CTProvider93(CFIndex stringIndex, CFIndex *charCount, CFDictionaryRef *attributes, void *refCon)94{95// if we have a zero length string we can just return NULL for the string96// or if the index anything other than 0 we are not using core text97// correctly since we only have one run.98if (stringIndex != 0)99{100return NULL;101}102103CTS_ProviderStruct *ctps = (CTS_ProviderStruct *)refCon;104*charCount = ctps->length;105*attributes = ctps->attributes;106return ctps->unicodes;107}108109110/*111* Gets a Dictionary filled with common details we want to use for CoreText when we are interacting112* with it from Java.113*/114static NSDictionary* ctsDictionaryFor(const NSFont *font, BOOL useFractionalMetrics)115{116NSNumber *gZeroNumber = [NSNumber numberWithInt:0];117NSNumber *gOneNumber = [NSNumber numberWithInt:1];118119return [NSDictionary dictionaryWithObjectsAndKeys:120font, NSFontAttributeName,121gOneNumber, (id)kCTForegroundColorFromContextAttributeName,122useFractionalMetrics ? gZeroNumber : gOneNumber, @"CTIntegerMetrics", // force integer hack in CoreText to help with Java's integer assumptions123gZeroNumber, NSLigatureAttributeName,124gZeroNumber, NSKernAttributeName,125nil];126}127128// Itterates though each glyph, and if a transform is present for that glyph, apply it to the CGContext, and strike the glyph.129// If there is no per-glyph transform, just strike the glyph. Advances must also be transformed on-the-spot as well.130void JavaCT_DrawGlyphVector131(const QuartzSDOps *qsdo, const AWTStrike *strike, const BOOL useSubstituion, const int uniChars[], const CGGlyph glyphs[], CGSize advances[], const jint g_gvTXIndicesAsInts[], const jdouble g_gvTransformsAsDoubles[], const CFIndex length)132{133CGPoint pt = { 0, 0 };134135// get our baseline transform and font136CGContextRef cgRef = qsdo->cgRef;137CGAffineTransform ctmText = CGContextGetTextMatrix(cgRef);138139BOOL saved = false;140141CGAffineTransform invTx = CGAffineTransformInvert(strike->fTx);142143NSInteger i;144for (i = 0; i < length; i++)145{146CGGlyph glyph = glyphs[i];147int uniChar = uniChars[i];148// if we found a unichar instead of a glyph code, get the fallback font,149// find the glyph code for the fallback font, and set the font on the current context150if (uniChar != 0)151{152CTFontRef fallback;153if (uniChar > 0xFFFF) {154UTF16Char charRef[2];155JavaCT_BreakupUnicodeIntoSurrogatePairs(uniChar, charRef);156CGGlyph glyphTmp[2];157fallback = JavaCT_CopyCTFallbackFontAndGlyphForUnicode(strike->fAWTFont, (const UTF16Char *)&charRef, (CGGlyph *)&glyphTmp, 2);158glyph = glyphTmp[0];159} else {160const UTF16Char u = uniChar;161fallback = JavaCT_CopyCTFallbackFontAndGlyphForUnicode(strike->fAWTFont, &u, (CGGlyph *)&glyph, 1);162}163if (fallback) {164const CGFontRef cgFallback = CTFontCopyGraphicsFont(fallback, NULL);165CFRelease(fallback);166167if (cgFallback) {168if (!saved) {169CGContextSaveGState(cgRef);170saved = true;171}172CGContextSetFont(cgRef, cgFallback);173CFRelease(cgFallback);174}175}176} else {177if (saved) {178CGContextRestoreGState(cgRef);179saved = false;180}181}182183// if we have per-glyph transformations184int tin = (g_gvTXIndicesAsInts == NULL) ? -1 : (g_gvTXIndicesAsInts[i] - 1) * 6;185if (tin < 0)186{187CGContextShowGlyphsAtPoint(cgRef, pt.x, pt.y, &glyph, 1);188}189else190{191CGAffineTransform tx = CGAffineTransformMake(192(CGFloat)g_gvTransformsAsDoubles[tin + 0], (CGFloat)g_gvTransformsAsDoubles[tin + 2],193(CGFloat)g_gvTransformsAsDoubles[tin + 1], (CGFloat)g_gvTransformsAsDoubles[tin + 3],1940, 0);195196CGPoint txOffset = { (CGFloat)g_gvTransformsAsDoubles[tin + 4], (CGFloat)g_gvTransformsAsDoubles[tin + 5] };197198txOffset = CGPointApplyAffineTransform(txOffset, invTx);199200// apply the transform, strike the glyph, can change the transform back201CGContextSetTextMatrix(cgRef, CGAffineTransformConcat(ctmText, tx));202CGContextShowGlyphsAtPoint(cgRef, txOffset.x + pt.x, txOffset.y + pt.y, &glyph, 1);203CGContextSetTextMatrix(cgRef, ctmText);204205// transform the measured advance for this strike206advances[i] = CGSizeApplyAffineTransform(advances[i], tx);207advances[i].width += txOffset.x;208advances[i].height += txOffset.y;209}210211// move our next x,y212pt.x += advances[i].width;213pt.y += advances[i].height;214215}216// reset the font on the context after striking a unicode with CoreText217if (saved) {218CGContextRestoreGState(cgRef);219}220}221222// Using the Quartz Surface Data context, draw a hot-substituted character run223void JavaCT_DrawTextUsingQSD(JNIEnv *env, const QuartzSDOps *qsdo, const AWTStrike *strike, const jchar *chars, const jsize length)224{225CGContextRef cgRef = qsdo->cgRef;226227AWTFont *awtFont = strike->fAWTFont;228CGFloat ptSize = strike->fSize;229CGAffineTransform tx = strike->fFontTx;230231NSFont *nsFont = [NSFont fontWithName:[awtFont->fFont fontName] size:ptSize];232233if (ptSize != 0) {234CGFloat invScale = 1 / ptSize;235tx = CGAffineTransformConcat(tx, CGAffineTransformMakeScale(invScale, invScale));236CGContextConcatCTM(cgRef, tx);237}238239CGContextSetTextMatrix(cgRef, CGAffineTransformIdentity); // resets the damage from CoreText240241NSString *string = [NSString stringWithCharacters:chars length:length];242/*243The calls below were used previously but for unknown reason did not244render using the right font (see bug 7183516) when attribString is not245initialized with font dictionary attributes. It seems that "options"246in CTTypesetterCreateWithAttributedStringAndOptions which contains the247font dictionary is ignored.248249NSAttributedString *attribString = [[NSAttributedString alloc] initWithString:string];250251CTTypesetterRef typeSetterRef = CTTypesetterCreateWithAttributedStringAndOptions((CFAttributedStringRef) attribString, (CFDictionaryRef) ctsDictionaryFor(nsFont, JRSFontStyleUsesFractionalMetrics(strike->fStyle)));252*/253NSAttributedString *attribString = [[NSAttributedString alloc]254initWithString:string255attributes:ctsDictionaryFor(nsFont, JRSFontStyleUsesFractionalMetrics(strike->fStyle))];256257CTTypesetterRef typeSetterRef = CTTypesetterCreateWithAttributedString((CFAttributedStringRef) attribString);258259CFRange range = {0, length};260CTLineRef lineRef = CTTypesetterCreateLine(typeSetterRef, range);261262CTLineDraw(lineRef, cgRef);263264[attribString release];265CFRelease(lineRef);266CFRelease(typeSetterRef);267}268269270/*----------------------271DrawTextContext is the funnel for all of our CoreText drawing.272All three JNI apis call through this method.273----------------------*/274static void DrawTextContext275(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, const jchar *chars, const jsize length, const jdouble x, const jdouble y)276{277if (length == 0)278{279return;280}281282qsdo->BeginSurface(env, qsdo, SD_Text);283if (qsdo->cgRef == NULL)284{285qsdo->FinishSurface(env, qsdo);286return;287}288289CGContextRef cgRef = qsdo->cgRef;290291292CGContextSaveGState(cgRef);293JRSFontSetRenderingStyleOnContext(cgRef, strike->fStyle);294295// we want to translate before we transform (scale or rotate) <rdar://4042541> (vm)296CGContextTranslateCTM(cgRef, x, y);297298AWTFont *awtfont = strike->fAWTFont; //(AWTFont *)(qsdo->fontInfo.awtfont);299NSCharacterSet *charSet = [awtfont->fFont coveredCharacterSet];300301JavaCT_DrawTextUsingQSD(env, qsdo, strike, chars, length); // Draw with CoreText302303CGContextRestoreGState(cgRef);304305qsdo->FinishSurface(env, qsdo);306}307308#pragma mark --- Glyph Vector Pipeline ---309310/*-----------------------------------311Glyph Vector Pipeline312313doDrawGlyphs() has been separated into several pipelined functions to increase performance,314and improve accountability for JNI resources, malloc'd memory, and error handling.315316Each stage of the pipeline is responsible for doing only one major thing, like allocating buffers,317aquiring transform arrays from JNI, filling buffers, or striking glyphs. All resources or memory318acquired at a given stage, must be released in that stage. Any error that occurs (like a failed malloc)319is to be handled in the stage it occurs in, and is to return immediatly after freeing it's resources.320321-----------------------------------*/322323static jclass jc_StandardGlyphVector = NULL;324#define GET_SGV_CLASS() GET_CLASS(jc_StandardGlyphVector, "sun/font/StandardGlyphVector");325326// Checks the GlyphVector Java object for any transforms that were applied to individual characters. If none are present,327// strike the glyphs immediately in Core Graphics. Otherwise, obtain the arrays, and defer to above.328static inline void doDrawGlyphsPipe_checkForPerGlyphTransforms329(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector, BOOL useSubstituion, int *uniChars, CGGlyph *glyphs, CGSize *advances, size_t length)330{331// if we have no character substitution, and no per-glyph transformations - strike now!332GET_SGV_CLASS();333DECLARE_FIELD(jm_StandardGlyphVector_gti, jc_StandardGlyphVector, "gti", "Lsun/font/StandardGlyphVector$GlyphTransformInfo;");334jobject gti = (*env)->GetObjectField(env, gVector, jm_StandardGlyphVector_gti);335if (gti == 0)336{337if (useSubstituion)338{339// quasi-simple case, substitution, but no per-glyph transforms340JavaCT_DrawGlyphVector(qsdo, strike, TRUE, uniChars, glyphs, advances, NULL, NULL, length);341}342else343{344// fast path, straight to CG without per-glyph transforms345CGContextShowGlyphsWithAdvances(qsdo->cgRef, glyphs, advances, length);346}347return;348}349350DECLARE_CLASS(jc_StandardGlyphVector_GlyphTransformInfo, "sun/font/StandardGlyphVector$GlyphTransformInfo");351DECLARE_FIELD(jm_StandardGlyphVector_GlyphTransformInfo_transforms, jc_StandardGlyphVector_GlyphTransformInfo, "transforms", "[D");352jdoubleArray g_gtiTransformsArray = (*env)->GetObjectField(env, gti, jm_StandardGlyphVector_GlyphTransformInfo_transforms); //(*env)->GetObjectField(env, gti, g_gtiTransforms);353if (g_gtiTransformsArray == NULL) {354return;355}356jdouble *g_gvTransformsAsDoubles = (*env)->GetPrimitiveArrayCritical(env, g_gtiTransformsArray, NULL);357if (g_gvTransformsAsDoubles == NULL) {358(*env)->DeleteLocalRef(env, g_gtiTransformsArray);359return;360}361362DECLARE_FIELD(jm_StandardGlyphVector_GlyphTransformInfo_indices, jc_StandardGlyphVector_GlyphTransformInfo, "indices", "[I");363jintArray g_gtiTXIndicesArray = (*env)->GetObjectField(env, gti, jm_StandardGlyphVector_GlyphTransformInfo_indices);364jint *g_gvTXIndicesAsInts = (*env)->GetPrimitiveArrayCritical(env, g_gtiTXIndicesArray, NULL);365if (g_gvTXIndicesAsInts == NULL) {366(*env)->ReleasePrimitiveArrayCritical(env, g_gtiTransformsArray, g_gvTransformsAsDoubles, JNI_ABORT);367(*env)->DeleteLocalRef(env, g_gtiTransformsArray);368(*env)->DeleteLocalRef(env, g_gtiTXIndicesArray);369return;370}371// slowest case, we have per-glyph transforms, and possibly glyph substitution as well372JavaCT_DrawGlyphVector(qsdo, strike, useSubstituion, uniChars, glyphs, advances, g_gvTXIndicesAsInts, g_gvTransformsAsDoubles, length);373374(*env)->ReleasePrimitiveArrayCritical(env, g_gtiTransformsArray, g_gvTransformsAsDoubles, JNI_ABORT);375(*env)->ReleasePrimitiveArrayCritical(env, g_gtiTXIndicesArray, g_gvTXIndicesAsInts, JNI_ABORT);376377(*env)->DeleteLocalRef(env, g_gtiTransformsArray);378(*env)->DeleteLocalRef(env, g_gtiTXIndicesArray);379}380381// Retrieves advances for translated unicodes382// Uses "glyphs" as a temporary buffer for the glyph-to-unicode translation383void JavaCT_GetAdvancesForUnichars384(const NSFont *font, const int uniChars[], CGGlyph glyphs[], const size_t length, CGSize advances[])385{386// cycle over each spot, and if we discovered a unicode to substitute, we have to calculate the advance for it387size_t i;388for (i = 0; i < length; i++)389{390UniChar uniChar = uniChars[i];391if (uniChar == 0) continue;392393CGGlyph glyph = 0;394const CTFontRef fallback = JRSFontCreateFallbackFontForCharacters((CTFontRef)font, &uniChar, 1);395if (fallback) {396CTFontGetGlyphsForCharacters(fallback, &uniChar, &glyph, 1);397CTFontGetAdvancesForGlyphs(fallback, kCTFontDefaultOrientation, &glyph, &(advances[i]), 1);398CFRelease(fallback);399}400401glyphs[i] = glyph;402}403}404405// Fills the glyph buffer with glyphs from the GlyphVector object. Also checks to see if the glyph's positions have been406// already caculated from GlyphVector, or we simply ask Core Graphics to make some advances for us. Pre-calculated positions407// are translated into advances, since CG only understands advances.408static inline void doDrawGlyphsPipe_fillGlyphAndAdvanceBuffers409(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector, CGGlyph *glyphs, int *uniChars, CGSize *advances, size_t length, jintArray glyphsArray)410{411// fill the glyph buffer412jint *glyphsAsInts = (*env)->GetPrimitiveArrayCritical(env, glyphsArray, NULL);413if (glyphsAsInts == NULL) {414return;415}416417// if a glyph code from Java is negative, that means it is really a unicode value418// which we can use in CoreText to strike the character in another font419size_t i;420BOOL complex = NO;421for (i = 0; i < length; i++)422{423jint code = glyphsAsInts[i];424if (code < 0)425{426complex = YES;427uniChars[i] = -code;428glyphs[i] = 0;429}430else431{432uniChars[i] = 0;433glyphs[i] = code;434}435}436437(*env)->ReleasePrimitiveArrayCritical(env, glyphsArray, glyphsAsInts, JNI_ABORT);438439// fill the advance buffer440GET_SGV_CLASS();441DECLARE_FIELD(jm_StandardGlyphVector_positions, jc_StandardGlyphVector, "positions", "[F");442jfloatArray posArray = (*env)->GetObjectField(env, gVector, jm_StandardGlyphVector_positions);443jfloat *positions = NULL;444if (posArray != NULL) {445// in this case, the positions have already been pre-calculated for us on the Java side446positions = (*env)->GetPrimitiveArrayCritical(env, posArray, NULL);447if (positions == NULL) {448(*env)->DeleteLocalRef(env, posArray);449}450}451if (positions != NULL) {452CGPoint prev;453prev.x = positions[0];454prev.y = positions[1];455456// <rdar://problem/4294061> take the first point, and move the context to that location457CGContextTranslateCTM(qsdo->cgRef, prev.x, prev.y);458459CGAffineTransform invTx = CGAffineTransformInvert(strike->fFontTx);460461// for each position, figure out the advance (since CG won't take positions directly)462size_t i;463for (i = 0; i < length - 1; i++)464{465size_t i2 = (i+1) * 2;466CGPoint pt;467pt.x = positions[i2];468pt.y = positions[i2+1];469pt = CGPointApplyAffineTransform(pt, invTx);470advances[i].width = pt.x - prev.x;471advances[i].height = -(pt.y - prev.y); // negative to translate to device space472prev.x = pt.x;473prev.y = pt.y;474}475476(*env)->ReleasePrimitiveArrayCritical(env, posArray, positions, JNI_ABORT);477(*env)->DeleteLocalRef(env, posArray);478}479else480{481// in this case, we have to go and calculate the positions ourselves482// there were no pre-calculated positions from the glyph buffer on the Java side483AWTFont *awtFont = strike->fAWTFont;484CTFontGetAdvancesForGlyphs((CTFontRef)awtFont->fFont, kCTFontDefaultOrientation, glyphs, advances, length);485486if (complex)487{488JavaCT_GetAdvancesForUnichars(awtFont->fFont, uniChars, glyphs, length, advances);489}490}491492// continue on to the next stage of the pipe493doDrawGlyphsPipe_checkForPerGlyphTransforms(env, qsdo, strike, gVector, complex, uniChars, glyphs, advances, length);494}495496// Obtains the glyph array to determine the number of glyphs we are dealing with. If we are dealing a large number of glyphs,497// we malloc a buffer to hold the glyphs and their advances, otherwise we use stack allocated buffers.498static inline void doDrawGlyphsPipe_getGlyphVectorLengthAndAlloc499(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector)500{501GET_SGV_CLASS();502DECLARE_FIELD(jm_StandardGlyphVector_glyphs, jc_StandardGlyphVector, "glyphs", "[I");503jintArray glyphsArray = (*env)->GetObjectField(env, gVector, jm_StandardGlyphVector_glyphs);504jsize length = (*env)->GetArrayLength(env, glyphsArray);505506if (length == 0)507{508// nothing to draw509(*env)->DeleteLocalRef(env, glyphsArray);510return;511}512513if (length < MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE)514{515// if we are small enough, fit everything onto the stack516CGGlyph glyphs[length];517int uniChars[length];518CGSize advances[length];519doDrawGlyphsPipe_fillGlyphAndAdvanceBuffers(env, qsdo, strike, gVector, glyphs, uniChars, advances, length, glyphsArray);520}521else522{523// otherwise, we should malloc and free buffers for this large run524CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * length);525int *uniChars = (int *)malloc(sizeof(int) * length);526CGSize *advances = (CGSize *)malloc(sizeof(CGSize) * length);527528if (glyphs == NULL || uniChars == NULL || advances == NULL)529{530(*env)->DeleteLocalRef(env, glyphsArray);531[NSException raise:NSMallocException format:@"%s-%s:%d", __FILE__, __FUNCTION__, __LINE__];532if (glyphs)533{534free(glyphs);535}536if (uniChars)537{538free(uniChars);539}540if (advances)541{542free(advances);543}544return;545}546547doDrawGlyphsPipe_fillGlyphAndAdvanceBuffers(env, qsdo, strike, gVector, glyphs, uniChars, advances, length, glyphsArray);548549free(glyphs);550free(uniChars);551free(advances);552}553554(*env)->DeleteLocalRef(env, glyphsArray);555}556557// Setup and save the state of the CGContext, and apply any java.awt.Font transforms to the context.558static inline void doDrawGlyphsPipe_applyFontTransforms559(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector, const jfloat x, const jfloat y)560{561CGContextRef cgRef = qsdo->cgRef;562CGContextSetFontSize(cgRef, 1.0);563CGContextSetFont(cgRef, strike->fAWTFont->fNativeCGFont);564CGContextSetTextMatrix(cgRef, CGAffineTransformIdentity);565566CGAffineTransform tx = strike->fFontTx;567tx.tx += x;568tx.ty += y;569CGContextConcatCTM(cgRef, tx);570571doDrawGlyphsPipe_getGlyphVectorLengthAndAlloc(env, qsdo, strike, gVector);572}573574575#pragma mark --- CTextPipe JNI ---576577578/*579* Class: sun_lwawt_macosx_CTextPipe580* Method: doDrawString581* Signature: (Lsun/java2d/SurfaceData;JLjava/lang/String;DD)V582*/583JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doDrawString584(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jstring str, jdouble x, jdouble y)585{586if (str == NULL) {587return;588}589QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata);590AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr);591592JNI_COCOA_ENTER(env);593594jsize len = (*env)->GetStringLength(env, str);595596if (len < MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE) // optimized for stack allocation <rdar://problem/4285041>597{598jchar unichars[len];599(*env)->GetStringRegion(env, str, 0, len, unichars);600CHECK_EXCEPTION();601602// Draw the text context603DrawTextContext(env, qsdo, awtStrike, unichars, len, x, y);604}605else606{607// Get string to draw and the length608const jchar *unichars = (*env)->GetStringChars(env, str, NULL);609if (unichars == NULL) {610JNU_ThrowOutOfMemoryError(env, "Could not get string chars");611return;612}613614// Draw the text context615DrawTextContext(env, qsdo, awtStrike, unichars, len, x, y);616617(*env)->ReleaseStringChars(env, str, unichars);618}619620JNI_COCOA_RENDERER_EXIT(env);621}622623624/*625* Class: sun_lwawt_macosx_CTextPipe626* Method: doUnicodes627* Signature: (Lsun/java2d/SurfaceData;J[CIIFF)V628*/629JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doUnicodes630(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jcharArray unicodes, jint offset, jint length, jfloat x, jfloat y)631{632QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata);633AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr);634635JNI_COCOA_ENTER(env);636637// Setup the text context638if (length < MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE) // optimized for stack allocation639{640jchar copyUnichars[length];641(*env)->GetCharArrayRegion(env, unicodes, offset, length, copyUnichars);642CHECK_EXCEPTION();643DrawTextContext(env, qsdo, awtStrike, copyUnichars, length, x, y);644}645else646{647jchar *copyUnichars = malloc(length * sizeof(jchar));648if (!copyUnichars) {649JNU_ThrowOutOfMemoryError(env, "Failed to malloc memory to create the glyphs for string drawing");650return;651}652653@try {654(*env)->GetCharArrayRegion(env, unicodes, offset, length, copyUnichars);655CHECK_EXCEPTION();656DrawTextContext(env, qsdo, awtStrike, copyUnichars, length, x, y);657} @finally {658free(copyUnichars);659}660}661662JNI_COCOA_RENDERER_EXIT(env);663}664665/*666* Class: sun_lwawt_macosx_CTextPipe667* Method: doOneUnicode668* Signature: (Lsun/java2d/SurfaceData;JCFF)V669*/670JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doOneUnicode671(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jchar aUnicode, jfloat x, jfloat y)672{673QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata);674AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr);675676JNI_COCOA_ENTER(env);677678DrawTextContext(env, qsdo, awtStrike, &aUnicode, 1, x, y);679680JNI_COCOA_RENDERER_EXIT(env);681}682683/*684* Class: sun_lwawt_macosx_CTextPipe685* Method: doDrawGlyphs686* Signature: (Lsun/java2d/SurfaceData;JLjava/awt/font/GlyphVector;FF)V687*/688JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doDrawGlyphs689(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jobject gVector, jfloat x, jfloat y)690{691QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata);692AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr);693694JNI_COCOA_ENTER(env);695696qsdo->BeginSurface(env, qsdo, SD_Text);697if (qsdo->cgRef == NULL)698{699qsdo->FinishSurface(env, qsdo);700return;701}702703CGContextSaveGState(qsdo->cgRef);704JRSFontSetRenderingStyleOnContext(qsdo->cgRef, JRSFontGetRenderingStyleForHints(sun_awt_SunHints_INTVAL_FRACTIONALMETRICS_ON, sun_awt_SunHints_INTVAL_TEXT_ANTIALIAS_ON));705706doDrawGlyphsPipe_applyFontTransforms(env, qsdo, awtStrike, gVector, x, y);707708CGContextRestoreGState(qsdo->cgRef);709710qsdo->FinishSurface(env, qsdo);711712JNI_COCOA_RENDERER_EXIT(env);713}714715716