Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/font/X11FontScaler.c
32287 views
/*1* Copyright (c) 2003, 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/*26* Important note : All AWTxxx functions are defined in font.h.27* These were added to remove the dependency of this file on X11.28* These functions are used to perform X11 operations and should29* be "stubbed out" in environments that do not support X11.30* The implementation of these functions has been moved from this file31* into X11FontScaler_md.c, which is compiled into another library.32*/33#include <stdio.h>34#include <stdlib.h>35#include <ctype.h>36#include <sys/utsname.h>3738#include <jni.h>39#include <jni_util.h>4041#include "sun_font_NativeFont.h"42#include "sun_font_NativeStrike.h"43#include "sun_font_NativeStrikeDisposer.h"44#include "sunfontids.h"45#include "fontscalerdefs.h"46#include "X11FontScaler.h"4748JNIEXPORT void JNICALL49Java_sun_font_NativeStrikeDisposer_freeNativeScalerContext50(JNIEnv *env, jobject disposer, jlong pScalerContext) {5152NativeScalerContext *context = (NativeScalerContext*)pScalerContext;5354if (context != NULL) {55if (context->xFont != NULL) {56AWTFreeFont(context->xFont);57}58free(context);59}60}6162JNIEXPORT jlong JNICALL63Java_sun_font_NativeStrike_createNullScalerContext64(JNIEnv *env, jobject strike) {6566NativeScalerContext *context =67(NativeScalerContext*)malloc(sizeof(NativeScalerContext));68context->xFont = NULL;69context->minGlyph = 0;70context->maxGlyph = 0;71context->numGlyphs = 0;72context->defaultGlyph = 0;73context->ptSize = NO_POINTSIZE;74return (jlong)(uintptr_t)context;75}7677JNIEXPORT jlong JNICALL78Java_sun_font_NativeStrike_createScalerContext79(JNIEnv *env, jobject strike, jbyteArray xlfdBytes,80jint ptSize, jdouble scale) {8182NativeScalerContext *context;83int len = (*env)->GetArrayLength(env, xlfdBytes);8485char* xlfd = (char*)malloc(len+1);8687if (xlfd == NULL) {88return (jlong)(uintptr_t)0L;89}9091(*env)->GetByteArrayRegion(env, xlfdBytes, 0, len, (jbyte*)xlfd);92xlfd[len] = '\0';93context = (NativeScalerContext*)malloc(sizeof(NativeScalerContext));9495AWTLoadFont (xlfd, &(context->xFont));96free(xlfd);9798if (context->xFont == NULL) { /* NULL means couldn't find the font */99free(context);100context = NULL;101} else {102/* numGlyphs is an estimate : X11 doesn't provide a quick way to103* discover which glyphs are valid: just the range that contains all104* the valid glyphs, and this range may have holes.105*/106context->minGlyph = (AWTFontMinByte1(context->xFont) << 8) +107AWTFontMinCharOrByte2(context->xFont);108context->maxGlyph = (AWTFontMaxByte1(context->xFont) << 8) +109AWTFontMaxCharOrByte2(context->xFont);110context->numGlyphs = context->maxGlyph - context->minGlyph + 1;111context->defaultGlyph = AWTFontDefaultChar(context->xFont);112/* Sometimes the default_char field of the XFontStruct isn't113* initialized to anything, so it can be a large number. So,114* check to see if its less than the largest possible value115* and if so, then use it. Otherwise, just use the minGlyph.116*/117if (context->defaultGlyph < context->minGlyph ||118context->defaultGlyph > context->maxGlyph) {119context->defaultGlyph = context->minGlyph;120}121context->ptSize = ptSize;122context->scale = scale;123}124125/*126* REMIND: freeing of native resources? XID, XFontStruct etc??127*/128return (jlong)(uintptr_t)context;129}130131132/* JNIEXPORT jint JNICALL */133/* Java_sun_font_NativeFont_getItalicAngle */134/* (JNIEnv *env, jobject font) { */135136/* UInt32 angle; */137/* AWTGetFontItalicAngle(xFont, &angle); */138/*X11 reports italic angle as 1/64ths of a degree, relative to 3 o'clock139* with anti-clockwise being the +ve rotation direction.140* We return141XGetFontProperty(xFont,XA_ITALIC_ANGLE, &angle);142*/143144/* return (jint)angle; */145/* } */146147JNIEXPORT jboolean JNICALL148Java_sun_font_NativeFont_fontExists149(JNIEnv *env, jclass fontClass, jbyteArray xlfdBytes) {150151int count = 0;152int len = (*env)->GetArrayLength(env, xlfdBytes);153char* xlfd = (char*)malloc(len+1);154155if (xlfd == NULL) {156return JNI_FALSE;157}158159(*env)->GetByteArrayRegion(env, xlfdBytes, 0, len, (jbyte*)xlfd);160xlfd[len] = '\0';161162count = AWTCountFonts(xlfd);163free(xlfd);164if (count > 0) {165return JNI_TRUE;166} else {167return JNI_FALSE;168}169}170171JNIEXPORT jboolean JNICALL172Java_sun_font_NativeFont_haveBitmapFonts173(JNIEnv *env, jclass fontClass, jbyteArray xlfdBytes) {174175int count = 0;176int len = (*env)->GetArrayLength(env, xlfdBytes);177char* xlfd = (char*)malloc(len+1);178179if (xlfd == NULL) {180return JNI_FALSE;181}182183(*env)->GetByteArrayRegion(env, xlfdBytes, 0, len, (jbyte*)xlfd);184xlfd[len] = '\0';185186count = AWTCountFonts(xlfd);187free(xlfd);188if (count > 2) {189return JNI_TRUE;190} else {191return JNI_FALSE;192}193}194195// CountGlyphs doubles as way of getting a native font reference196// and telling if its valid. So far as I can tell GenerateImage etc197// just return if this "initialisation method" hasn't been called.198// So clients of this class need to call CountGlyphs() right after199// construction to be safe.200JNIEXPORT jint JNICALL201Java_sun_font_NativeFont_countGlyphs202(JNIEnv *env, jobject font, jbyteArray xlfdBytes, jint ptSize) {203204NativeScalerContext *context = (NativeScalerContext*)205Java_sun_font_NativeStrike_createScalerContext206(env, NULL, xlfdBytes, ptSize, 1);207208if (context == NULL) {209return 0;210} else {211int numGlyphs = context->numGlyphs;212AWTFreeFont(context->xFont);213free(context);214return numGlyphs;215}216}217218JNIEXPORT jint JNICALL219Java_sun_font_NativeStrike_getMaxGlyph220(JNIEnv *env, jobject strike, jlong pScalerContext) {221222NativeScalerContext *context = (NativeScalerContext*)pScalerContext;223if (context == NULL) {224return (jint)0;225} else {226return (jint)context->maxGlyph+1;227}228}229230JNIEXPORT jfloat JNICALL231Java_sun_font_NativeFont_getGlyphAdvance232(JNIEnv *env, jobject font2D, jlong pScalerContext, jint glyphCode) {233234NativeScalerContext *context = (NativeScalerContext*)pScalerContext;235AWTFont xFont = (AWTFont)context->xFont;236AWTChar xcs = NULL;237jfloat advance = 0.0f;238239if (xFont == NULL || context->ptSize == NO_POINTSIZE) {240return advance;241}242243if (glyphCode < context->minGlyph || glyphCode > context->maxGlyph) {244glyphCode = context->defaultGlyph;245}246247/* If number of glyphs is 256 or less, the metrics are248* stored correctly in the XFontStruct for each249* character. If the # characters is more (double byte250* case), then these metrics seem flaky and there's no251* way to determine if they have been set or not.252*/253if ((context->maxGlyph <= 256) && (AWTFontPerChar(xFont, 0) != NULL)) {254xcs = AWTFontPerChar(xFont, glyphCode - context->minGlyph);255advance = AWTCharAdvance(xcs);256} else {257int direction, ascent, descent;258AWTChar2b xChar;259260xChar.byte1 = (unsigned char) (glyphCode >> 8);261xChar.byte2 = (unsigned char) glyphCode;262AWTFontTextExtents16(xFont, &xChar, &xcs);263advance = AWTCharAdvance(xcs);264AWTFreeChar(xcs);265}266return (jfloat)(advance/context->scale);267}268269JNIEXPORT jlong JNICALL270Java_sun_font_NativeFont_getGlyphImageNoDefault271(JNIEnv *env, jobject font2D, jlong pScalerContext, jint glyphCode) {272273NativeScalerContext *context = (NativeScalerContext*)pScalerContext;274AWTFont xFont = context->xFont;275AWTChar2b xChar;276277if (xFont == NULL || context->ptSize == NO_POINTSIZE) {278return (jlong)0;279}280281if (glyphCode < context->minGlyph || glyphCode > context->maxGlyph) {282return (jlong)0;283}284285xChar.byte1 = (unsigned char)(glyphCode >> 8);286xChar.byte2 = (unsigned char)glyphCode;287return AWTFontGenerateImage(xFont, &xChar);288}289290JNIEXPORT jlong JNICALL291Java_sun_font_NativeFont_getGlyphImage292(JNIEnv *env, jobject font2D, jlong pScalerContext, jint glyphCode) {293294NativeScalerContext *context = (NativeScalerContext*)pScalerContext;295AWTFont xFont = context->xFont;296AWTChar2b xChar;297298if (xFont == NULL || context->ptSize == NO_POINTSIZE) {299return (jlong)0;300}301302if (glyphCode < context->minGlyph || glyphCode > context->maxGlyph) {303glyphCode = context->defaultGlyph;304}305306xChar.byte1 = (unsigned char)(glyphCode >> 8);307xChar.byte2 = (unsigned char)glyphCode;308return AWTFontGenerateImage(xFont, &xChar);309}310311JNIEXPORT jobject JNICALL312Java_sun_font_NativeFont_getFontMetrics313(JNIEnv *env, jobject font2D, jlong pScalerContext) {314315NativeScalerContext *context = (NativeScalerContext*)pScalerContext;316AWTFont xFont = (AWTFont)context->xFont;317jfloat j0=0, j1=1, ay=j0, dy=j0, mx=j0;318jobject metrics;319320if (xFont == NULL) {321return NULL;322}323324/* the commented out lines are the old 1.4.x behaviour which used max325* bounds instead of the font's designed ascent/descent */326/* ay = (jfloat)-AWTCharAscent(AWTFontMaxBounds(xFont)); */327/* dy = (jfloat)AWTCharDescent(AWTFontMaxBounds(xFont)); */328329ay = (jfloat)-AWTFontAscent(xFont);330dy = (jfloat)AWTFontDescent(xFont);331mx = (jfloat)AWTCharAdvance(AWTFontMaxBounds(xFont));332333/* ascent : no need to set ascentX - it will be zero334* descent : no need to set descentX - it will be zero335* baseline : old releases "made up" a number and also seemed to336* make it up for "X" and set "Y" to 0.337* leadingX : no need to set leadingX - it will be zero.338* leadingY : made-up number, but being compatible with what 1.4.x did339* advance : no need to set yMaxLinearAdvanceWidth - it will be zero.340*/341metrics = (*env)->NewObject(env, sunFontIDs.strikeMetricsClass,342sunFontIDs.strikeMetricsCtr,343j0, ay, j0, dy, j1, j0, j0, j1, mx, j0);344/* printf("X11 asc=%f dsc=%f adv=%f scale=%f\n", */345/* ay, dy, mx, (float)context->scale); */346return metrics;347}348349350