Path: blob/master/src/java.desktop/unix/native/libfontmanager/X11FontScaler.c
41149 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*)(uintptr_t)(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));68if (context == NULL) {69return (jlong)(uintptr_t)0L;70}71context->xFont = NULL;72context->minGlyph = 0;73context->maxGlyph = 0;74context->numGlyphs = 0;75context->defaultGlyph = 0;76context->ptSize = NO_POINTSIZE;77return (jlong)(uintptr_t)context;78}7980JNIEXPORT jlong JNICALL81Java_sun_font_NativeStrike_createScalerContext82(JNIEnv *env, jobject strike, jbyteArray xlfdBytes,83jint ptSize, jdouble scale) {8485NativeScalerContext *context;86int len = (*env)->GetArrayLength(env, xlfdBytes);8788char* xlfd = (char*)malloc(len+1);8990if (xlfd == NULL) {91return (jlong)(uintptr_t)0L;92}9394(*env)->GetByteArrayRegion(env, xlfdBytes, 0, len, (jbyte*)xlfd);95xlfd[len] = '\0';96context = (NativeScalerContext*)malloc(sizeof(NativeScalerContext));97if (context == NULL) {98free(xlfd);99return (jlong)(uintptr_t)0L;100}101102AWTLoadFont (xlfd, &(context->xFont));103free(xlfd);104105if (context->xFont == NULL) { /* NULL means couldn't find the font */106free(context);107context = NULL;108} else {109/* numGlyphs is an estimate : X11 doesn't provide a quick way to110* discover which glyphs are valid: just the range that contains all111* the valid glyphs, and this range may have holes.112*/113context->minGlyph = (AWTFontMinByte1(context->xFont) << 8) +114AWTFontMinCharOrByte2(context->xFont);115context->maxGlyph = (AWTFontMaxByte1(context->xFont) << 8) +116AWTFontMaxCharOrByte2(context->xFont);117context->numGlyphs = context->maxGlyph - context->minGlyph + 1;118context->defaultGlyph = AWTFontDefaultChar(context->xFont);119/* Sometimes the default_char field of the XFontStruct isn't120* initialized to anything, so it can be a large number. So,121* check to see if its less than the largest possible value122* and if so, then use it. Otherwise, just use the minGlyph.123*/124if (context->defaultGlyph < context->minGlyph ||125context->defaultGlyph > context->maxGlyph) {126context->defaultGlyph = context->minGlyph;127}128context->ptSize = ptSize;129context->scale = scale;130}131132/*133* REMIND: freeing of native resources? XID, XFontStruct etc??134*/135return (jlong)(uintptr_t)context;136}137138139/* JNIEXPORT jint JNICALL */140/* Java_sun_font_NativeFont_getItalicAngle */141/* (JNIEnv *env, jobject font) { */142143/* UInt32 angle; */144/* AWTGetFontItalicAngle(xFont, &angle); */145/*X11 reports italic angle as 1/64ths of a degree, relative to 3 o'clock146* with anti-clockwise being the +ve rotation direction.147* We return148XGetFontProperty(xFont,XA_ITALIC_ANGLE, &angle);149*/150151/* return (jint)angle; */152/* } */153154JNIEXPORT jboolean JNICALL155Java_sun_font_NativeFont_fontExists156(JNIEnv *env, jclass fontClass, jbyteArray xlfdBytes) {157158int count = 0;159int len = (*env)->GetArrayLength(env, xlfdBytes);160char* xlfd = (char*)malloc(len+1);161162if (xlfd == NULL) {163return JNI_FALSE;164}165166(*env)->GetByteArrayRegion(env, xlfdBytes, 0, len, (jbyte*)xlfd);167xlfd[len] = '\0';168169count = AWTCountFonts(xlfd);170free(xlfd);171if (count > 0) {172return JNI_TRUE;173} else {174return JNI_FALSE;175}176}177178JNIEXPORT jboolean JNICALL179Java_sun_font_NativeFont_haveBitmapFonts180(JNIEnv *env, jclass fontClass, jbyteArray xlfdBytes) {181182int count = 0;183int len = (*env)->GetArrayLength(env, xlfdBytes);184char* xlfd = (char*)malloc(len+1);185186if (xlfd == NULL) {187return JNI_FALSE;188}189190(*env)->GetByteArrayRegion(env, xlfdBytes, 0, len, (jbyte*)xlfd);191xlfd[len] = '\0';192193count = AWTCountFonts(xlfd);194free(xlfd);195if (count > 2) {196return JNI_TRUE;197} else {198return JNI_FALSE;199}200}201202// CountGlyphs doubles as way of getting a native font reference203// and telling if its valid. So far as I can tell GenerateImage etc204// just return if this "initialisation method" hasn't been called.205// So clients of this class need to call CountGlyphs() right after206// construction to be safe.207JNIEXPORT jint JNICALL208Java_sun_font_NativeFont_countGlyphs209(JNIEnv *env, jobject font, jbyteArray xlfdBytes, jint ptSize) {210211NativeScalerContext *context = (NativeScalerContext*)212(uintptr_t)(Java_sun_font_NativeStrike_createScalerContext213(env, NULL, xlfdBytes, ptSize, 1));214215if (context == NULL) {216return 0;217} else {218int numGlyphs = context->numGlyphs;219AWTFreeFont(context->xFont);220free(context);221return numGlyphs;222}223}224225JNIEXPORT jint JNICALL226Java_sun_font_NativeStrike_getMaxGlyph227(JNIEnv *env, jobject strike, jlong pScalerContext) {228229NativeScalerContext *context = (NativeScalerContext*)(uintptr_t)(pScalerContext);230if (context == NULL) {231return (jint)0;232} else {233return (jint)context->maxGlyph+1;234}235}236237JNIEXPORT jfloat JNICALL238Java_sun_font_NativeFont_getGlyphAdvance239(JNIEnv *env, jobject font2D, jlong pScalerContext, jint glyphCode) {240241AWTChar xcs = NULL;242jfloat advance = 0.0f;243AWTFont xFont;244NativeScalerContext *context = (NativeScalerContext*)(uintptr_t)(pScalerContext);245if (context == NULL) {246return advance;247} else {248xFont = (AWTFont)context->xFont;249}250251if (xFont == NULL || context->ptSize == NO_POINTSIZE) {252return advance;253}254255if (glyphCode < context->minGlyph || glyphCode > context->maxGlyph) {256glyphCode = context->defaultGlyph;257}258259/* If number of glyphs is 256 or less, the metrics are260* stored correctly in the XFontStruct for each261* character. If the # characters is more (double byte262* case), then these metrics seem flaky and there's no263* way to determine if they have been set or not.264*/265if ((context->maxGlyph <= 256) && (AWTFontPerChar(xFont, 0) != NULL)) {266xcs = AWTFontPerChar(xFont, glyphCode - context->minGlyph);267advance = AWTCharAdvance(xcs);268} else {269int direction, ascent, descent;270AWTChar2b xChar;271272xChar.byte1 = (unsigned char) (glyphCode >> 8);273xChar.byte2 = (unsigned char) glyphCode;274AWTFontTextExtents16(xFont, &xChar, &xcs);275advance = AWTCharAdvance(xcs);276AWTFreeChar(xcs);277}278return (jfloat)(advance/context->scale);279}280281JNIEXPORT jlong JNICALL282Java_sun_font_NativeFont_getGlyphImageNoDefault283(JNIEnv *env, jobject font2D, jlong pScalerContext, jint glyphCode) {284285AWTChar2b xChar;286AWTFont xFont;287NativeScalerContext *context = (NativeScalerContext*)(uintptr_t)(pScalerContext);288if (context == NULL) {289return (jlong)0;290} else {291xFont = (AWTFont)context->xFont;292}293294if (xFont == NULL || context->ptSize == NO_POINTSIZE) {295return (jlong)0;296}297298if (glyphCode < context->minGlyph || glyphCode > context->maxGlyph) {299return (jlong)0;300}301302xChar.byte1 = (unsigned char)(glyphCode >> 8);303xChar.byte2 = (unsigned char)glyphCode;304return AWTFontGenerateImage(xFont, &xChar);305}306307JNIEXPORT jlong JNICALL308Java_sun_font_NativeFont_getGlyphImage309(JNIEnv *env, jobject font2D, jlong pScalerContext, jint glyphCode) {310311AWTChar2b xChar;312AWTFont xFont;313NativeScalerContext *context = (NativeScalerContext*)(uintptr_t)(pScalerContext);314if (context == NULL) {315return (jlong)0;316} else {317xFont = (AWTFont)context->xFont;318}319320if (xFont == NULL || context->ptSize == NO_POINTSIZE) {321return (jlong)0;322}323324if (glyphCode < context->minGlyph || glyphCode > context->maxGlyph) {325glyphCode = context->defaultGlyph;326}327328xChar.byte1 = (unsigned char)(glyphCode >> 8);329xChar.byte2 = (unsigned char)glyphCode;330return AWTFontGenerateImage(xFont, &xChar);331}332333JNIEXPORT jobject JNICALL334Java_sun_font_NativeFont_getFontMetrics335(JNIEnv *env, jobject font2D, jlong pScalerContext) {336337jfloat j0=0, j1=1, ay=j0, dy=j0, mx=j0;338jobject metrics;339AWTFont xFont;340NativeScalerContext *context = (NativeScalerContext*)(uintptr_t)(pScalerContext);341if (context == NULL) {342return NULL;343} else {344xFont = (AWTFont)context->xFont;345}346347if (xFont == NULL) {348return NULL;349}350351/* the commented out lines are the old 1.4.x behaviour which used max352* bounds instead of the font's designed ascent/descent */353/* ay = (jfloat)-AWTCharAscent(AWTFontMaxBounds(xFont)); */354/* dy = (jfloat)AWTCharDescent(AWTFontMaxBounds(xFont)); */355356ay = (jfloat)-AWTFontAscent(xFont);357dy = (jfloat)AWTFontDescent(xFont);358mx = (jfloat)AWTCharAdvance(AWTFontMaxBounds(xFont));359360/* ascent : no need to set ascentX - it will be zero361* descent : no need to set descentX - it will be zero362* baseline : old releases "made up" a number and also seemed to363* make it up for "X" and set "Y" to 0.364* leadingX : no need to set leadingX - it will be zero.365* leadingY : made-up number, but being compatible with what 1.4.x did366* advance : no need to set yMaxLinearAdvanceWidth - it will be zero.367*/368metrics = (*env)->NewObject(env, sunFontIDs.strikeMetricsClass,369sunFontIDs.strikeMetricsCtr,370j0, ay, j0, dy, j1, j0, j0, j1, mx, j0);371/* printf("X11 asc=%f dsc=%f adv=%f scale=%f\n", */372/* ay, dy, mx, (float)context->scale); */373return metrics;374}375376377