Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/font/sunFont.c
38829 views
/*1* Copyright (c) 2007, 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#include "stdlib.h"26#include "string.h"27#include "gdefs.h"28#include "jlong.h"29#include "jni_util.h"30#include "sunfontids.h"31#include "fontscalerdefs.h"32#include "sun_font_SunFontManager.h"33#include "sun_font_NullFontScaler.h"34#include "sun_font_StrikeCache.h"3536static void *theNullScalerContext = NULL;37extern void AccelGlyphCache_RemoveAllCellInfos(GlyphInfo *glyph);383940JNIEXPORT jlong JNICALL41Java_sun_font_NullFontScaler_getNullScalerContext42(JNIEnv *env, jclass scalerClass) {4344if (theNullScalerContext == NULL) {45theNullScalerContext = malloc(1);46}47return ptr_to_jlong(theNullScalerContext);48}4950int isNullScalerContext(void *context) {51return theNullScalerContext == context;52}5354/* Eventually we may rework it to be a singleton.55* This will require additional checks in freeLongMemory/freeIntMemory56* and on other hand malformed fonts (main source of null glyph images)57* are supposed to be collected fast.58* But perhaps it is still right thing to do.59* Even better is to eliminate the need to have this native method60* but for this it is necessary to rework Strike and drawing logic61* to be able to live with NULL pointers without performance hit.62*/63JNIEXPORT jlong JNICALL Java_sun_font_NullFontScaler_getGlyphImage64(JNIEnv *env, jobject scaler, jlong pContext, jint glyphCode) {65void *nullscaler = calloc(sizeof(GlyphInfo), 1);66return ptr_to_jlong(nullscaler);67}68697071void initLCDGammaTables();7273/* placeholder for extern variable */74static int initialisedFontIDs = 0;75FontManagerNativeIDs sunFontIDs;7677static void initFontIDs(JNIEnv *env) {7879jclass tmpClass;8081if (initialisedFontIDs) {82return;83}84CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/TrueTypeFont"));85CHECK_NULL(sunFontIDs.ttReadBlockMID =86(*env)->GetMethodID(env, tmpClass, "readBlock",87"(Ljava/nio/ByteBuffer;II)I"));88CHECK_NULL(sunFontIDs.ttReadBytesMID =89(*env)->GetMethodID(env, tmpClass, "readBytes", "(II)[B"));9091CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/Type1Font"));92CHECK_NULL(sunFontIDs.readFileMID =93(*env)->GetMethodID(env, tmpClass,94"readFile", "(Ljava/nio/ByteBuffer;)V"));9596CHECK_NULL(tmpClass =97(*env)->FindClass(env, "java/awt/geom/Point2D$Float"));98sunFontIDs.pt2DFloatClass = (jclass)(*env)->NewGlobalRef(env, tmpClass);99CHECK_NULL(sunFontIDs.pt2DFloatCtr =100(*env)->GetMethodID(env, sunFontIDs.pt2DFloatClass, "<init>","(FF)V"));101102CHECK_NULL(sunFontIDs.xFID =103(*env)->GetFieldID(env, sunFontIDs.pt2DFloatClass, "x", "F"));104CHECK_NULL(sunFontIDs.yFID =105(*env)->GetFieldID(env, sunFontIDs.pt2DFloatClass, "y", "F"));106107CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/StrikeMetrics"));108CHECK_NULL(sunFontIDs.strikeMetricsClass =109(jclass)(*env)->NewGlobalRef(env, tmpClass));110111CHECK_NULL(sunFontIDs.strikeMetricsCtr =112(*env)->GetMethodID(env, sunFontIDs.strikeMetricsClass,113"<init>", "(FFFFFFFFFF)V"));114115CHECK_NULL(tmpClass =116(*env)->FindClass(env, "java/awt/geom/Rectangle2D$Float"));117sunFontIDs.rect2DFloatClass = (jclass)(*env)->NewGlobalRef(env, tmpClass);118CHECK_NULL(sunFontIDs.rect2DFloatCtr =119(*env)->GetMethodID(env, sunFontIDs.rect2DFloatClass, "<init>", "()V"));120CHECK_NULL(sunFontIDs.rect2DFloatCtr4 =121(*env)->GetMethodID(env, sunFontIDs.rect2DFloatClass,122"<init>", "(FFFF)V"));123CHECK_NULL(sunFontIDs.rectF2DX =124(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "x", "F"));125CHECK_NULL(sunFontIDs.rectF2DY =126(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "y", "F"));127CHECK_NULL(sunFontIDs.rectF2DWidth =128(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "width", "F"));129CHECK_NULL(sunFontIDs.rectF2DHeight =130(*env)->GetFieldID(env, sunFontIDs.rect2DFloatClass, "height", "F"));131132CHECK_NULL(tmpClass = (*env)->FindClass(env, "java/awt/geom/GeneralPath"));133sunFontIDs.gpClass = (jclass)(*env)->NewGlobalRef(env, tmpClass);134CHECK_NULL(sunFontIDs.gpCtr =135(*env)->GetMethodID(env, sunFontIDs.gpClass, "<init>", "(I[BI[FI)V"));136CHECK_NULL(sunFontIDs.gpCtrEmpty =137(*env)->GetMethodID(env, sunFontIDs.gpClass, "<init>", "()V"));138139CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/Font2D"));140CHECK_NULL(sunFontIDs.f2dCharToGlyphMID =141(*env)->GetMethodID(env, tmpClass, "charToGlyph", "(I)I"));142CHECK_NULL(sunFontIDs.getMapperMID =143(*env)->GetMethodID(env, tmpClass, "getMapper",144"()Lsun/font/CharToGlyphMapper;"));145CHECK_NULL(sunFontIDs.getTableBytesMID =146(*env)->GetMethodID(env, tmpClass, "getTableBytes", "(I)[B"));147CHECK_NULL(sunFontIDs.canDisplayMID =148(*env)->GetMethodID(env, tmpClass, "canDisplay", "(C)Z"));149150CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/CharToGlyphMapper"));151CHECK_NULL(sunFontIDs.charToGlyphMID =152(*env)->GetMethodID(env, tmpClass, "charToGlyph", "(I)I"));153154CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/PhysicalStrike"));155CHECK_NULL(sunFontIDs.getGlyphMetricsMID =156(*env)->GetMethodID(env, tmpClass, "getGlyphMetrics",157"(I)Ljava/awt/geom/Point2D$Float;"));158CHECK_NULL(sunFontIDs.getGlyphPointMID =159(*env)->GetMethodID(env, tmpClass, "getGlyphPoint",160"(II)Ljava/awt/geom/Point2D$Float;"));161CHECK_NULL(sunFontIDs.adjustPointMID =162(*env)->GetMethodID(env, tmpClass, "adjustPoint",163"(Ljava/awt/geom/Point2D$Float;)V"));164CHECK_NULL(sunFontIDs.pScalerContextFID =165(*env)->GetFieldID(env, tmpClass, "pScalerContext", "J"));166167CHECK_NULL(tmpClass = (*env)->FindClass(env, "sun/font/GlyphList"));168CHECK_NULL(sunFontIDs.glyphListX =169(*env)->GetFieldID(env, tmpClass, "x", "F"));170CHECK_NULL(sunFontIDs.glyphListY =171(*env)->GetFieldID(env, tmpClass, "y", "F"));172CHECK_NULL(sunFontIDs.glyphListLen =173(*env)->GetFieldID(env, tmpClass, "len", "I"));174CHECK_NULL(sunFontIDs.glyphImages =175(*env)->GetFieldID(env, tmpClass, "images", "[J"));176CHECK_NULL(sunFontIDs.glyphListUsePos =177(*env)->GetFieldID(env, tmpClass, "usePositions", "Z"));178CHECK_NULL(sunFontIDs.glyphListPos =179(*env)->GetFieldID(env, tmpClass, "positions", "[F"));180CHECK_NULL(sunFontIDs.lcdRGBOrder =181(*env)->GetFieldID(env, tmpClass, "lcdRGBOrder", "Z"));182CHECK_NULL(sunFontIDs.lcdSubPixPos =183(*env)->GetFieldID(env, tmpClass, "lcdSubPixPos", "Z"));184185initLCDGammaTables();186187initialisedFontIDs = 1;188}189190JNIEXPORT void JNICALL191Java_sun_font_SunFontManager_initIDs192(JNIEnv *env, jclass cls) {193194initFontIDs(env);195}196197JNIEXPORT FontManagerNativeIDs getSunFontIDs(JNIEnv *env) {198199initFontIDs(env);200return sunFontIDs;201}202203/*204* Class: sun_font_StrikeCache205* Method: freeIntPointer206* Signature: (I)V207*/208JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeIntPointer209(JNIEnv *env, jclass cacheClass, jint ptr) {210211/* Note this is used for freeing a glyph which was allocated212* but never placed into the glyph cache. The caller holds the213* only reference, therefore it is unnecessary to invalidate any214* accelerated glyph cache cells as we do in freeInt/LongMemory().215*/216if (ptr != 0) {217free((void*)ptr);218}219}220221/*222* Class: sun_font_StrikeCache223* Method: freeLongPointer224* Signature: (J)V225*/226JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeLongPointer227(JNIEnv *env, jclass cacheClass, jlong ptr) {228229/* Note this is used for freeing a glyph which was allocated230* but never placed into the glyph cache. The caller holds the231* only reference, therefore it is unnecessary to invalidate any232* accelerated glyph cache cells as we do in freeInt/LongMemory().233*/234if (ptr != 0L) {235free(jlong_to_ptr(ptr));236}237}238239/*240* Class: sun_font_StrikeCache241* Method: freeIntMemory242* Signature: ([I)V243*/244JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeIntMemory245(JNIEnv *env, jclass cacheClass, jintArray jmemArray, jlong pContext) {246247int len = (*env)->GetArrayLength(env, jmemArray);248jint* ptrs =249(jint*)(*env)->GetPrimitiveArrayCritical(env, jmemArray, NULL);250int i;251252if (ptrs) {253for (i=0; i< len; i++) {254if (ptrs[i] != 0) {255GlyphInfo *ginfo = (GlyphInfo *)ptrs[i];256if (ginfo->cellInfo != NULL &&257ginfo->managed == MANAGED_GLYPH) {258// invalidate this glyph's accelerated cache cell259AccelGlyphCache_RemoveAllCellInfos(ginfo);260}261free((void*)ginfo);262}263}264(*env)->ReleasePrimitiveArrayCritical(env, jmemArray, ptrs, JNI_ABORT);265}266if (!isNullScalerContext(jlong_to_ptr(pContext))) {267free(jlong_to_ptr(pContext));268}269}270271/*272* Class: sun_font_StrikeCache273* Method: freeLongMemory274* Signature: ([J)V275*/276JNIEXPORT void JNICALL Java_sun_font_StrikeCache_freeLongMemory277(JNIEnv *env, jclass cacheClass, jlongArray jmemArray, jlong pContext) {278279int len = (*env)->GetArrayLength(env, jmemArray);280jlong* ptrs =281(jlong*)(*env)->GetPrimitiveArrayCritical(env, jmemArray, NULL);282int i;283284if (ptrs) {285for (i=0; i< len; i++) {286if (ptrs[i] != 0L) {287GlyphInfo *ginfo = (GlyphInfo *) jlong_to_ptr(ptrs[i]);288if (ginfo->cellInfo != NULL &&289ginfo->managed == MANAGED_GLYPH) {290AccelGlyphCache_RemoveAllCellInfos(ginfo);291}292free((void*)ginfo);293}294}295(*env)->ReleasePrimitiveArrayCritical(env, jmemArray, ptrs, JNI_ABORT);296}297if (!isNullScalerContext(jlong_to_ptr(pContext))) {298free(jlong_to_ptr(pContext));299}300}301302JNIEXPORT void JNICALL303Java_sun_font_StrikeCache_getGlyphCacheDescription304(JNIEnv *env, jclass cls, jlongArray results) {305306jlong* nresults;307GlyphInfo *info;308size_t baseAddr;309310if ((*env)->GetArrayLength(env, results) < 13) {311return;312}313314nresults = (jlong*)(*env)->GetPrimitiveArrayCritical(env, results, NULL);315if (nresults == NULL) {316return;317}318info = (GlyphInfo*) calloc(1, sizeof(GlyphInfo));319if (info == NULL) {320(*env)->ReleasePrimitiveArrayCritical(env, results, nresults, 0);321return;322}323baseAddr = (size_t)info;324nresults[0] = sizeof(void*);325nresults[1] = sizeof(GlyphInfo);326nresults[2] = 0;327nresults[3] = (size_t)&(info->advanceY)-baseAddr;328nresults[4] = (size_t)&(info->width)-baseAddr;329nresults[5] = (size_t)&(info->height)-baseAddr;330nresults[6] = (size_t)&(info->rowBytes)-baseAddr;331nresults[7] = (size_t)&(info->topLeftX)-baseAddr;332nresults[8] = (size_t)&(info->topLeftY)-baseAddr;333nresults[9] = (size_t)&(info->image)-baseAddr;334nresults[10] = (jlong)(uintptr_t)info; /* invisible glyph */335nresults[11] = (size_t)&(info->cellInfo)-baseAddr;336nresults[12] = (size_t)&(info->managed)-baseAddr;337338(*env)->ReleasePrimitiveArrayCritical(env, results, nresults, 0);339}340341JNIEXPORT TTLayoutTableCache* newLayoutTableCache() {342TTLayoutTableCache* ltc = calloc(1, sizeof(TTLayoutTableCache));343if (ltc) {344int i;345for(i=0;i<LAYOUTCACHE_ENTRIES;i++) {346ltc->entries[i].len = -1;347}348}349return ltc;350}351352JNIEXPORT void freeLayoutTableCache(TTLayoutTableCache* ltc) {353if (ltc) {354int i;355for(i=0;i<LAYOUTCACHE_ENTRIES;i++) {356if(ltc->entries[i].ptr) free (ltc->entries[i].ptr);357}358if (ltc->kernPairs) free(ltc->kernPairs);359free(ltc);360}361}362363364