Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/java2d/x11/X11FontScaler_md.c
32288 views
/*1* Copyright (c) 2001, 2003, 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 <stdio.h>26#include <string.h>27#include <stdlib.h>2829#include <ctype.h>30#include <sys/utsname.h>3132#include <jni.h>33#include <jni_util.h>34#include "fontscalerdefs.h"35#include "X11FontScaler.h"3637#ifndef HEADLESS3839#include <X11/Xlib.h>40#include <X11/Xutil.h>41#include <awt.h>4243static GC pixmapGC = 0;44static Pixmap pixmap = 0;45static Atom psAtom = 0;46static Atom fullNameAtom = 0;47static int pixmapWidth = 0;48static int pixmapHeight = 0;4950#define FONT_AWT_LOCK() \51env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); \52AWT_LOCK();5354int CreatePixmapAndGC (int width, int height)55{56/* REMIND: use the actual screen, not the default screen */57Window awt_defaultRoot =58RootWindow(awt_display, DefaultScreen(awt_display));5960if (width < 100) {61width = 100;62}63if (height < 100) {64height = 100;65}66pixmapHeight = height;67pixmapWidth = width;68if (pixmap != 0) {69XFreePixmap (awt_display, pixmap);70}71if (pixmapGC != NULL) {72XFreeGC (awt_display, pixmapGC);73}74pixmap = XCreatePixmap (awt_display, awt_defaultRoot, pixmapWidth,75pixmapHeight, 1);76if (pixmap == 0) {77return BadAlloc;78}79pixmapGC = XCreateGC (awt_display, pixmap, 0, 0);80if (pixmapGC == NULL) {81return BadAlloc;82}83XFillRectangle (awt_display, pixmap, pixmapGC, 0, 0, pixmapWidth,84pixmapHeight);85XSetForeground (awt_display, pixmapGC, 1);86return Success;87}8889#ifdef DUMP_IMAGES9091static void dumpXImage(XImage *ximage)92{93int height = ximage->height;94int width = ximage->width;95int row;96int column;9798fprintf(stderr, "-------------------------------------------\n");99for (row = 0; row < height; ++row) {100for (column = 0; column < width; ++column) {101int pixel = ximage->f.get_pixel(ximage, column, row);102fprintf(stderr, (pixel == 0) ? " " : "XX");103}104fprintf(stderr, "\n");105}106fprintf(stderr, "-------------------------------------------\n");107}108109#endif110111#endif /* !HEADLESS */112113JNIEXPORT int JNICALL AWTCountFonts(char* xlfd) {114#ifdef HEADLESS115return 0;116#else117char **names;118int count;119JNIEnv *env;120FONT_AWT_LOCK();121names = XListFonts(awt_display, xlfd, 3, &count);122XFreeFontNames(names);123AWT_UNLOCK();124return count;125#endif /* !HEADLESS */126}127128JNIEXPORT void JNICALL AWTLoadFont(char* name, AWTFont *pReturn) {129JNIEnv *env;130*pReturn = NULL;131#ifndef HEADLESS132FONT_AWT_LOCK();133*pReturn = (AWTFont)XLoadQueryFont(awt_display, name);134AWT_UNLOCK();135#endif /* !HEADLESS */136}137138JNIEXPORT void JNICALL AWTFreeFont(AWTFont font) {139#ifndef HEADLESS140JNIEnv *env;141FONT_AWT_LOCK();142XFreeFont(awt_display, (XFontStruct *)font);143AWT_UNLOCK();144#endif /* !HEADLESS */145}146147JNIEXPORT unsigned JNICALL AWTFontMinByte1(AWTFont font) {148#ifdef HEADLESS149return 0;150#else151return ((XFontStruct *)font)->min_byte1;152#endif /* !HEADLESS */153}154155JNIEXPORT unsigned JNICALL AWTFontMaxByte1(AWTFont font) {156#ifdef HEADLESS157return 0;158#else159return ((XFontStruct *)font)->max_byte1;160#endif /* !HEADLESS */161}162163JNIEXPORT unsigned JNICALL AWTFontMinCharOrByte2(AWTFont font) {164#ifdef HEADLESS165return 0;166#else167return ((XFontStruct *)font)->min_char_or_byte2;168#endif /* !HEADLESS */169}170171JNIEXPORT unsigned JNICALL AWTFontMaxCharOrByte2(AWTFont font) {172#ifdef HEADLESS173return 0;174#else175return ((XFontStruct *)font)->max_char_or_byte2;176#endif /* !HEADLESS */177}178179JNIEXPORT unsigned JNICALL AWTFontDefaultChar(AWTFont font) {180#ifdef HEADLESS181return 0;182#else183return ((XFontStruct *)font)->default_char;184#endif /* !HEADLESS */185}186187JNIEXPORT AWTChar JNICALL AWTFontPerChar(AWTFont font, int index) {188#ifdef HEADLESS189return NULL;190#else191XFontStruct *fXFont = (XFontStruct *)font;192XCharStruct *perChar = fXFont->per_char;193if (perChar == NULL) {194return NULL;195}196return (AWTChar)&(perChar[index]);197#endif /* !HEADLESS */198}199200JNIEXPORT AWTChar JNICALL AWTFontMaxBounds(AWTFont font) {201#ifdef HEADLESS202return 0;203#else204return (AWTChar)&((XFontStruct *)font)->max_bounds;205#endif /* !HEADLESS */206}207208209JNIEXPORT int JNICALL AWTFontAscent(AWTFont font) {210#ifdef HEADLESS211return 0;212#else213return ((XFontStruct *)font)->ascent;214#endif /* !HEADLESS */215}216217218JNIEXPORT int JNICALL AWTFontDescent(AWTFont font) {219#ifdef HEADLESS220return 0;221#else222return ((XFontStruct *)font)->descent;223#endif /* !HEADLESS */224}225226JNIEXPORT void JNICALL AWTFontTextExtents16(AWTFont font,227AWTChar2b* xChar,228AWTChar* overall) {229#ifndef HEADLESS230JNIEnv *env;231int ascent, descent, direction;232XFontStruct* xFont = (XFontStruct*)font;233XCharStruct* newChar = (XCharStruct*)malloc(sizeof(XCharStruct));234*overall = (AWTChar)newChar;235/* There is a claim from the pre 1.5 source base that the info in the236* XFontStruct is flaky for 16 byte chars. This seems plausible as237* for info to be valid, that struct would need a large number of238* XCharStructs. But there's nothing in the X APIs which warns you of239* this. If it really is flaky you must question why there's an240* XTextExtents16 API call. Try XTextExtents16 for now and if it fails241* go back to XQueryTextExtents16 in this function.242* Indeed the metrics from the Solaris 9 JA font243* -ricoh-gothic-medium-r-normal--*-140-72-72-m-*-jisx0208.1983-0244* do appear different so revert to the query api245*/246FONT_AWT_LOCK();247XQueryTextExtents16(awt_display,xFont->fid, xChar, 1,248&direction, &ascent, &descent, newChar);249/* XTextExtents16(xFont, xChar, 1, &direction, &ascent, &descent, newChar); */250AWT_UNLOCK();251#endif /* !HEADLESS */252}253254JNIEXPORT void JNICALL AWTFreeChar(AWTChar xChar) {255#ifndef HEADLESS256free(xChar);257#endif /* !HEADLESS */258}259260JNIEXPORT jlong JNICALL AWTFontGenerateImage(AWTFont pFont, AWTChar2b* xChar) {261262#ifndef HEADLESS263264int width, height, direction, ascent, descent;265GlyphInfo *glyphInfo;266XFontStruct* xFont = (XFontStruct*)pFont;267XCharStruct xcs;268XImage *ximage;269int h, i, j, nbytes;270unsigned char *srcRow, *dstRow, *dstByte;271int wholeByteCount, remainingBitsCount;272unsigned int imageSize;273JNIEnv *env;274275276FONT_AWT_LOCK();277/* XTextExtents16(xFont, xChar, 1, &direction, &ascent, &descent, &xcs); */278XQueryTextExtents16(awt_display,xFont->fid, xChar, 1,279&direction, &ascent, &descent, &xcs);280width = xcs.rbearing - xcs.lbearing;281height = xcs.ascent+xcs.descent;282imageSize = width*height;283glyphInfo = (GlyphInfo*)malloc(sizeof(GlyphInfo)+imageSize);284if (glyphInfo == NULL) {285AWT_UNLOCK();286return (jlong)(uintptr_t)NULL;287}288glyphInfo->cellInfo = NULL;289glyphInfo->width = width;290glyphInfo->height = height;291glyphInfo->topLeftX = xcs.lbearing;292glyphInfo->topLeftY = -xcs.ascent;293glyphInfo->advanceX = xcs.width;294glyphInfo->advanceY = 0;295296if (imageSize == 0) {297glyphInfo->image = NULL;298AWT_UNLOCK();299return (jlong)(uintptr_t)glyphInfo;300} else {301glyphInfo->image = (unsigned char*)glyphInfo+sizeof(GlyphInfo);302}303304if ((pixmap == 0) || (width > pixmapWidth) || (height > pixmapHeight)) {305if (CreatePixmapAndGC(width, height) != Success) {306glyphInfo->image = NULL;307AWT_UNLOCK();308return (jlong)(uintptr_t)glyphInfo;309}310}311312XSetFont(awt_display, pixmapGC, xFont->fid);313XSetForeground(awt_display, pixmapGC, 0);314XFillRectangle(awt_display, pixmap, pixmapGC, 0, 0,315pixmapWidth, pixmapHeight);316XSetForeground(awt_display, pixmapGC, 1);317XDrawString16(awt_display, pixmap, pixmapGC,318-xcs.lbearing, xcs.ascent, xChar, 1);319ximage = XGetImage(awt_display, pixmap, 0, 0, width, height,320AllPlanes, XYPixmap);321322if (ximage == NULL) {323glyphInfo->image = NULL;324AWT_UNLOCK();325return (jlong)(uintptr_t)glyphInfo;326}327328#ifdef DUMP_IMAGES329dumpXImage(ximage);330#endif331332nbytes = ximage->bytes_per_line;333srcRow = (unsigned char*)ximage->data;334dstRow = (unsigned char*)glyphInfo->image;335wholeByteCount = width >> 3;336remainingBitsCount = width & 7;337338for (h=0; h<height; h++) {339const UInt8* src8 = srcRow;340UInt8 *dstByte = dstRow;341UInt32 srcValue;342343srcRow += nbytes;344dstRow += width;345346for (i = 0; i < wholeByteCount; i++) {347srcValue = *src8++;348for (j = 0; j < 8; j++) {349if (ximage->bitmap_bit_order == LSBFirst) {350*dstByte++ = (srcValue & 0x01) ? 0xFF : 0;351srcValue >>= 1;352} else { /* MSBFirst */353*dstByte++ = (srcValue & 0x80) ? 0xFF : 0;354srcValue <<= 1;355}356}357}358if (remainingBitsCount) {359srcValue = *src8;360for (j = 0; j < remainingBitsCount; j++) {361if (ximage->bitmap_bit_order == LSBFirst) {362*dstByte++ = (srcValue & 0x01) ? 0xFF : 0;363srcValue >>= 1;364} else { /* MSBFirst */365*dstByte++ = (srcValue & 0x80) ? 0xFF : 0;366srcValue <<= 1;367}368}369}370}371372XDestroyImage (ximage);373AWT_UNLOCK();374return (jlong)(uintptr_t)glyphInfo;375#else376return (jlong)0;377#endif /* !HEADLESS */378}379380JNIEXPORT short JNICALL AWTCharAdvance(AWTChar xChar) {381#ifdef HEADLESS382return 0;383#else384return ((XCharStruct *)xChar)->width;385#endif /* !HEADLESS */386}387388JNIEXPORT short JNICALL AWTCharLBearing(AWTChar xChar) {389#ifdef HEADLESS390return 0;391#else392return ((XCharStruct *)xChar)->lbearing;393#endif /* !HEADLESS */394}395396JNIEXPORT short JNICALL AWTCharRBearing(AWTChar xChar) {397#ifdef HEADLESS398return 0;399#else400return ((XCharStruct *)xChar)->rbearing;401#endif /* !HEADLESS */402}403404JNIEXPORT short JNICALL AWTCharAscent(AWTChar xChar) {405#ifdef HEADLESS406return 0;407#else408return ((XCharStruct *)xChar)->ascent;409#endif /* !HEADLESS */410}411412JNIEXPORT short JNICALL AWTCharDescent(AWTChar xChar) {413#ifdef HEADLESS414return 0;415#else416return ((XCharStruct *)xChar)->descent;417#endif /* !HEADLESS */418}419420421