1 /*
   2  * Copyright (c) 2008, 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 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  27 
  28 #import "AWTFont.h"
  29 #import "CoreTextSupport.h"
  30 
  31 #import "sun_font_CCharToGlyphMapper.h"
  32 
  33 /*
  34  * Class:     sun_font_CCharToGlyphMapper
  35  * Method:    countGlyphs
  36  * Signature: (J)I
  37  */
  38 JNIEXPORT jint JNICALL
  39 Java_sun_font_CCharToGlyphMapper_countGlyphs
  40     (JNIEnv *env, jclass clazz, jlong awtFontPtr)
  41 {
  42     jint numGlyphs = 0;
  43 
  44 JNF_COCOA_ENTER(env);
  45 
  46     AWTFont *awtFont = (AWTFont *)jlong_to_ptr(awtFontPtr);
  47     numGlyphs = [awtFont->fFont numberOfGlyphs];
  48 
  49 JNF_COCOA_EXIT(env);
  50 
  51     return numGlyphs;
  52 }
  53 
  54 static inline void
  55 GetGlyphsFromUnicodes(JNIEnv *env, AWTFont *awtFont,
  56                       jint count, UniChar *unicodes,
  57                       CGGlyph *cgGlyphs, jintArray glyphs)
  58 {
  59     jint *glyphCodeInts = (*env)->GetPrimitiveArrayCritical(env, glyphs, 0);
  60 
  61     CTS_GetGlyphsAsIntsForCharacters(awtFont, unicodes,
  62                                      cgGlyphs, glyphCodeInts, count);
  63 
  64     // Do not use JNI_COMMIT, as that will not free the buffer copy
  65     // when +ProtectJavaHeap is on.
  66     (*env)->ReleasePrimitiveArrayCritical(env, glyphs, glyphCodeInts, 0);
  67 }
  68 
  69 static inline void
  70 AllocateGlyphBuffer(JNIEnv *env, AWTFont *awtFont,
  71                     jint count, UniChar *unicodes, jintArray glyphs)
  72 {
  73     if (count < MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE) {
  74         CGGlyph cgGlyphs[count];
  75         GetGlyphsFromUnicodes(env, awtFont, count, unicodes, cgGlyphs, glyphs);
  76     } else {
  77         CGGlyph *cgGlyphs = (CGGlyph *)malloc(count * sizeof(CGGlyph));
  78         GetGlyphsFromUnicodes(env, awtFont, count, unicodes, cgGlyphs, glyphs);
  79         free(cgGlyphs);
  80     }
  81 }
  82 
  83 /*
  84  * Class:     sun_font_CCharToGlyphMapper
  85  * Method:    nativeCharsToGlyphs
  86  * Signature: (JI[C[I)V
  87  */
  88 JNIEXPORT void JNICALL
  89 Java_sun_font_CCharToGlyphMapper_nativeCharsToGlyphs
  90     (JNIEnv *env, jclass clazz,
  91      jlong awtFontPtr, jint count, jcharArray unicodes, jintArray glyphs)
  92 {
  93 JNF_COCOA_ENTER(env);
  94 
  95     AWTFont *awtFont = (AWTFont *)jlong_to_ptr(awtFontPtr);
  96 
  97     // check the array size
  98     jint len = (*env)->GetArrayLength(env, glyphs);
  99     if (len < count) {
 100         count = len;
 101     }
 102 
 103     jchar *unicodesAsChars =
 104         (*env)->GetPrimitiveArrayCritical(env, unicodes, NULL);
 105 
 106     if (unicodesAsChars != NULL) {
 107         AllocateGlyphBuffer(env, awtFont, count,
 108                            (UniChar *)unicodesAsChars, glyphs);
 109 
 110         (*env)->ReleasePrimitiveArrayCritical(env, unicodes,
 111                                               unicodesAsChars, JNI_ABORT);
 112     }
 113 
 114 JNF_COCOA_EXIT(env);
 115 }