Path: blob/master/src/java.desktop/share/native/libfontmanager/hb-jdk-font.cc
41149 views
/*1* Copyright (c) 2015, 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 "sun_font_SunLayoutEngine.h"2728#include "hb.h"29#include "hb-jdk.h"30#include <stdlib.h>3132#if defined(__GNUC__) && __GNUC__ >= 433#define HB_UNUSED __attribute__((unused))34#else35#define HB_UNUSED36#endif373839static hb_bool_t40hb_jdk_get_nominal_glyph (hb_font_t *font HB_UNUSED,41void *font_data,42hb_codepoint_t unicode,43hb_codepoint_t *glyph,44void *user_data HB_UNUSED)45{4647JDKFontInfo *jdkFontInfo = (JDKFontInfo*)font_data;48JNIEnv* env = jdkFontInfo->env;49jobject font2D = jdkFontInfo->font2D;50*glyph = (hb_codepoint_t)env->CallIntMethod(51font2D, sunFontIDs.f2dCharToGlyphMID, unicode);52if (env->ExceptionOccurred())53{54env->ExceptionClear();55}56if ((int)*glyph < 0) {57*glyph = 0;58}59return (*glyph != 0);60}6162static hb_bool_t63hb_jdk_get_variation_glyph (hb_font_t *font HB_UNUSED,64void *font_data,65hb_codepoint_t unicode,66hb_codepoint_t variation_selector,67hb_codepoint_t *glyph,68void *user_data HB_UNUSED)69{7071JDKFontInfo *jdkFontInfo = (JDKFontInfo*)font_data;72JNIEnv* env = jdkFontInfo->env;73jobject font2D = jdkFontInfo->font2D;74*glyph = (hb_codepoint_t)env->CallIntMethod(75font2D, sunFontIDs.f2dCharToVariationGlyphMID,76unicode, variation_selector);77if (env->ExceptionOccurred())78{79env->ExceptionClear();80}81if ((int)*glyph < 0) {82*glyph = 0;83}84return (*glyph != 0);85}8687static hb_position_t88hb_jdk_get_glyph_h_advance (hb_font_t *font HB_UNUSED,89void *font_data,90hb_codepoint_t glyph,91void *user_data HB_UNUSED)92{9394float fadv = 0.0f;95if ((glyph & 0xfffe) == 0xfffe) {96return 0; // JDK uses this glyph code.97}9899JDKFontInfo *jdkFontInfo = (JDKFontInfo*)font_data;100JNIEnv* env = jdkFontInfo->env;101jobject fontStrike = jdkFontInfo->fontStrike;102jobject pt = env->CallObjectMethod(fontStrike,103sunFontIDs.getGlyphMetricsMID, glyph);104105if (pt == NULL) {106return 0;107}108fadv = env->GetFloatField(pt, sunFontIDs.xFID);109fadv *= jdkFontInfo->devScale;110env->DeleteLocalRef(pt);111112return HBFloatToFixed(fadv);113}114115static hb_position_t116hb_jdk_get_glyph_v_advance (hb_font_t *font HB_UNUSED,117void *font_data,118hb_codepoint_t glyph,119void *user_data HB_UNUSED)120{121122float fadv = 0.0f;123if ((glyph & 0xfffe) == 0xfffe) {124return 0; // JDK uses this glyph code.125}126127JDKFontInfo *jdkFontInfo = (JDKFontInfo*)font_data;128JNIEnv* env = jdkFontInfo->env;129jobject fontStrike = jdkFontInfo->fontStrike;130jobject pt = env->CallObjectMethod(fontStrike,131sunFontIDs.getGlyphMetricsMID, glyph);132133if (pt == NULL) {134return 0;135}136fadv = env->GetFloatField(pt, sunFontIDs.yFID);137env->DeleteLocalRef(pt);138139return HBFloatToFixed(fadv);140141}142143static hb_bool_t144hb_jdk_get_glyph_h_origin (hb_font_t *font HB_UNUSED,145void *font_data HB_UNUSED,146hb_codepoint_t glyph HB_UNUSED,147hb_position_t *x HB_UNUSED,148hb_position_t *y HB_UNUSED,149void *user_data HB_UNUSED)150{151/* We always work in the horizontal coordinates. */152return true;153}154155static hb_bool_t156hb_jdk_get_glyph_v_origin (hb_font_t *font HB_UNUSED,157void *font_data,158hb_codepoint_t glyph,159hb_position_t *x,160hb_position_t *y,161void *user_data HB_UNUSED)162{163return false;164}165166static hb_position_t167hb_jdk_get_glyph_h_kerning (hb_font_t *font,168void *font_data,169hb_codepoint_t lejdk_glyph,170hb_codepoint_t right_glyph,171void *user_data HB_UNUSED)172{173/* Not implemented. This seems to be in the HB API174* as a way to fall back to Freetype's kerning support175* which could be based on some on-the fly glyph analysis.176* But more likely it reads the kern table. That is easy177* enough code to add if we find a need to fall back178* to that instead of using gpos. It seems like if179* there is a gpos table at all, the practice is to180* use that and ignore kern, no matter that gpos does181* not implement the kern feature.182*/183return 0;184}185186static hb_position_t187hb_jdk_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,188void *font_data HB_UNUSED,189hb_codepoint_t top_glyph HB_UNUSED,190hb_codepoint_t bottom_glyph HB_UNUSED,191void *user_data HB_UNUSED)192{193/* OpenType doesn't have vertical-kerning other than GPOS. */194return 0;195}196197static hb_bool_t198hb_jdk_get_glyph_extents (hb_font_t *font HB_UNUSED,199void *font_data,200hb_codepoint_t glyph,201hb_glyph_extents_t *extents,202void *user_data HB_UNUSED)203{204/* TODO */205return false;206}207208static hb_bool_t209hb_jdk_get_glyph_contour_point (hb_font_t *font HB_UNUSED,210void *font_data,211hb_codepoint_t glyph,212unsigned int point_index,213hb_position_t *x,214hb_position_t *y,215void *user_data HB_UNUSED)216{217if ((glyph & 0xfffe) == 0xfffe) {218*x = 0; *y = 0;219return true;220}221222JDKFontInfo *jdkFontInfo = (JDKFontInfo*)font_data;223JNIEnv* env = jdkFontInfo->env;224jobject fontStrike = jdkFontInfo->fontStrike;225jobject pt = env->CallObjectMethod(fontStrike,226sunFontIDs.getGlyphPointMID,227glyph, point_index);228229if (pt == NULL) {230*x = 0; *y = 0;231return true;232}233*x = HBFloatToFixed(env->GetFloatField(pt, sunFontIDs.xFID));234*y = HBFloatToFixed(env->GetFloatField(pt, sunFontIDs.yFID));235env->DeleteLocalRef(pt);236237return true;238}239240static hb_bool_t241hb_jdk_get_glyph_name (hb_font_t *font HB_UNUSED,242void *font_data,243hb_codepoint_t glyph,244char *name, unsigned int size,245void *user_data HB_UNUSED)246{247return false;248}249250static hb_bool_t251hb_jdk_get_glyph_from_name (hb_font_t *font HB_UNUSED,252void *font_data,253const char *name, int len,254hb_codepoint_t *glyph,255void *user_data HB_UNUSED)256{257return false;258}259260// remind : can we initialise this from the code we call261// from the class static method in Java to make it262// completely thread safe.263static hb_font_funcs_t *264_hb_jdk_get_font_funcs (void)265{266static hb_font_funcs_t *jdk_ffuncs = NULL;267hb_font_funcs_t *ff;268269if (!jdk_ffuncs) {270ff = hb_font_funcs_create();271272hb_font_funcs_set_nominal_glyph_func(ff, hb_jdk_get_nominal_glyph, NULL, NULL);273hb_font_funcs_set_variation_glyph_func(ff, hb_jdk_get_variation_glyph, NULL, NULL);274hb_font_funcs_set_glyph_h_advance_func(ff,275hb_jdk_get_glyph_h_advance, NULL, NULL);276hb_font_funcs_set_glyph_v_advance_func(ff,277hb_jdk_get_glyph_v_advance, NULL, NULL);278hb_font_funcs_set_glyph_h_origin_func(ff,279hb_jdk_get_glyph_h_origin, NULL, NULL);280hb_font_funcs_set_glyph_v_origin_func(ff,281hb_jdk_get_glyph_v_origin, NULL, NULL);282hb_font_funcs_set_glyph_h_kerning_func(ff,283hb_jdk_get_glyph_h_kerning, NULL, NULL);284hb_font_funcs_set_glyph_v_kerning_func(ff,285hb_jdk_get_glyph_v_kerning, NULL, NULL);286hb_font_funcs_set_glyph_extents_func(ff,287hb_jdk_get_glyph_extents, NULL, NULL);288hb_font_funcs_set_glyph_contour_point_func(ff,289hb_jdk_get_glyph_contour_point, NULL, NULL);290hb_font_funcs_set_glyph_name_func(ff,291hb_jdk_get_glyph_name, NULL, NULL);292hb_font_funcs_set_glyph_from_name_func(ff,293hb_jdk_get_glyph_from_name, NULL, NULL);294hb_font_funcs_make_immutable(ff); // done setting functions.295jdk_ffuncs = ff;296}297return jdk_ffuncs;298}299300static void _do_nothing(void) {301}302303struct Font2DPtr {304JavaVM* vmPtr;305jweak font2DRef;306};307308static void cleanupFontInfo(void* data) {309Font2DPtr* fontInfo;310JNIEnv* env;311312fontInfo = (Font2DPtr*) data;313fontInfo->vmPtr->GetEnv((void**)&env, JNI_VERSION_1_1);314env->DeleteWeakGlobalRef(fontInfo->font2DRef);315free(data);316}317318static hb_blob_t *319reference_table(hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) {320321Font2DPtr *fontInfo;322JNIEnv* env;323jobject font2D;324jsize length;325void* buffer;326327// HB_TAG_NONE is 0 and is used to get the whole font file.328// It is not expected to be needed for JDK.329if (tag == 0) {330return NULL;331}332333fontInfo = (Font2DPtr*)user_data;334fontInfo->vmPtr->GetEnv((void**)&env, JNI_VERSION_1_1);335if (env == NULL) {336return NULL;337}338font2D = fontInfo->font2DRef;339340jbyteArray tableBytes = (jbyteArray)341env->CallObjectMethod(font2D, sunFontIDs.getTableBytesMID, tag);342if (tableBytes == NULL) {343return NULL;344}345length = env->GetArrayLength(tableBytes);346buffer = calloc(length, sizeof(jbyte));347if (buffer == NULL) {348return NULL;349}350env->GetByteArrayRegion(tableBytes, 0, length, (jbyte*)buffer);351352return hb_blob_create((const char *)buffer, length,353HB_MEMORY_MODE_WRITABLE,354buffer, free);355}356357extern "C" {358359/*360* Class: sun_font_SunLayoutEngine361* Method: createFace362* Signature: (Lsun/font/Font2D;JJ)J363*/364JNIEXPORT jlong JNICALL Java_sun_font_SunLayoutEngine_createFace(JNIEnv *env,365jclass cls,366jobject font2D,367jlong platformFontPtr) {368Font2DPtr *fi = (Font2DPtr*)malloc(sizeof(Font2DPtr));369if (!fi) {370return 0;371}372JavaVM* vmPtr;373env->GetJavaVM(&vmPtr);374fi->vmPtr = vmPtr;375fi->font2DRef = env->NewWeakGlobalRef(font2D);376if (!fi->font2DRef) {377free(fi);378return 0;379}380hb_face_t *face = hb_face_create_for_tables(reference_table, fi,381cleanupFontInfo);382return ptr_to_jlong(face);383}384385/*386* Class: sun_font_SunLayoutEngine387* Method: disposeFace388* Signature: (J)V389*/390JNIEXPORT void JNICALL Java_sun_font_SunLayoutEngine_disposeFace(JNIEnv *env,391jclass cls,392jlong ptr) {393hb_face_t* face = (hb_face_t*) jlong_to_ptr(ptr);394hb_face_destroy(face);395}396397} // extern "C"398399static hb_font_t* _hb_jdk_font_create(hb_face_t* face,400JDKFontInfo *jdkFontInfo,401hb_destroy_func_t destroy) {402403hb_font_t *font;404405font = hb_font_create(face);406hb_font_set_funcs (font,407_hb_jdk_get_font_funcs (),408jdkFontInfo, (hb_destroy_func_t) _do_nothing);409hb_font_set_scale (font,410HBFloatToFixed(jdkFontInfo->ptSize*jdkFontInfo->devScale),411HBFloatToFixed(jdkFontInfo->ptSize*jdkFontInfo->devScale));412return font;413}414415#ifdef MACOSX416static hb_font_t* _hb_jdk_ct_font_create(hb_face_t* face,417JDKFontInfo *jdkFontInfo) {418419hb_font_t *font = NULL;420font = hb_font_create(face);421hb_font_set_scale(font,422HBFloatToFixed(jdkFontInfo->ptSize),423HBFloatToFixed(jdkFontInfo->ptSize));424return font;425}426#endif427428hb_font_t* hb_jdk_font_create(hb_face_t* hbFace,429JDKFontInfo *jdkFontInfo,430hb_destroy_func_t destroy) {431432return _hb_jdk_font_create(hbFace, jdkFontInfo, destroy);433}434435436