0N/A/*
2362N/A * Copyright (c) 2001, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <string.h>
0N/A#include <stdlib.h>
0N/A
0N/A#include <ctype.h>
0N/A#include <sys/utsname.h>
0N/A
0N/A#include <jni.h>
0N/A#include <jni_util.h>
0N/A#include "fontscalerdefs.h"
0N/A#include "X11FontScaler.h"
0N/A
0N/A#ifndef HEADLESS
0N/A
0N/A#include <X11/Xlib.h>
0N/A#include <X11/Xutil.h>
0N/A#include <awt.h>
0N/A
0N/Astatic GC pixmapGC = 0;
0N/Astatic Pixmap pixmap = 0;
0N/Astatic Atom psAtom = 0;
0N/Astatic Atom fullNameAtom = 0;
0N/Astatic int pixmapWidth = 0;
0N/Astatic int pixmapHeight = 0;
0N/A
0N/A#define FONT_AWT_LOCK() \
0N/Aenv = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); \
0N/AAWT_LOCK();
0N/A
0N/Aint CreatePixmapAndGC (int width, int height)
0N/A{
0N/A /* REMIND: use the actual screen, not the default screen */
0N/A Window awt_defaultRoot =
0N/A RootWindow(awt_display, DefaultScreen(awt_display));
0N/A
0N/A if (width < 100) {
0N/A width = 100;
0N/A }
0N/A if (height < 100) {
0N/A height = 100;
0N/A }
0N/A pixmapHeight = height;
0N/A pixmapWidth = width;
0N/A if (pixmap != 0) {
0N/A XFreePixmap (awt_display, pixmap);
0N/A }
0N/A if (pixmapGC != NULL) {
0N/A XFreeGC (awt_display, pixmapGC);
0N/A }
0N/A pixmap = XCreatePixmap (awt_display, awt_defaultRoot, pixmapWidth,
0N/A pixmapHeight, 1);
0N/A if (pixmap == 0) {
0N/A return BadAlloc;
0N/A }
0N/A pixmapGC = XCreateGC (awt_display, pixmap, 0, 0);
0N/A if (pixmapGC == NULL) {
0N/A return BadAlloc;
0N/A }
0N/A XFillRectangle (awt_display, pixmap, pixmapGC, 0, 0, pixmapWidth,
0N/A pixmapHeight);
0N/A XSetForeground (awt_display, pixmapGC, 1);
0N/A return Success;
0N/A}
0N/A
0N/A#ifdef DUMP_IMAGES
0N/A
0N/Astatic void dumpXImage(XImage *ximage)
0N/A{
0N/A int height = ximage->height;
0N/A int width = ximage->width;
0N/A int row;
0N/A int column;
0N/A
0N/A fprintf(stderr, "-------------------------------------------\n");
0N/A for (row = 0; row < height; ++row) {
0N/A for (column = 0; column < width; ++column) {
0N/A int pixel = ximage->f.get_pixel(ximage, column, row);
0N/A fprintf(stderr, (pixel == 0) ? " " : "XX");
0N/A }
0N/A fprintf(stderr, "\n");
0N/A }
0N/A fprintf(stderr, "-------------------------------------------\n");
0N/A}
0N/A
0N/A#endif
0N/A
0N/A#endif /* !HEADLESS */
0N/A
0N/AJNIEXPORT int JNICALL AWTCountFonts(char* xlfd) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A char **names;
0N/A int count;
0N/A JNIEnv *env;
0N/A FONT_AWT_LOCK();
0N/A names = XListFonts(awt_display, xlfd, 3, &count);
0N/A XFreeFontNames(names);
0N/A AWT_UNLOCK();
0N/A return count;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL AWTLoadFont(char* name, AWTFont *pReturn) {
0N/A JNIEnv *env;
0N/A *pReturn = NULL;
0N/A#ifndef HEADLESS
0N/A FONT_AWT_LOCK();
0N/A *pReturn = (AWTFont)XLoadQueryFont(awt_display, name);
0N/A AWT_UNLOCK();
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL AWTFreeFont(AWTFont font) {
0N/A#ifndef HEADLESS
0N/A JNIEnv *env;
0N/A FONT_AWT_LOCK();
0N/A XFreeFont(awt_display, (XFontStruct *)font);
0N/A AWT_UNLOCK();
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT unsigned JNICALL AWTFontMinByte1(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XFontStruct *)font)->min_byte1;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT unsigned JNICALL AWTFontMaxByte1(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XFontStruct *)font)->max_byte1;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT unsigned JNICALL AWTFontMinCharOrByte2(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XFontStruct *)font)->min_char_or_byte2;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT unsigned JNICALL AWTFontMaxCharOrByte2(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XFontStruct *)font)->max_char_or_byte2;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT unsigned JNICALL AWTFontDefaultChar(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XFontStruct *)font)->default_char;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT AWTChar JNICALL AWTFontPerChar(AWTFont font, int index) {
0N/A#ifdef HEADLESS
0N/A return NULL;
0N/A#else
0N/A XFontStruct *fXFont = (XFontStruct *)font;
0N/A XCharStruct *perChar = fXFont->per_char;
0N/A if (perChar == NULL) {
0N/A return NULL;
0N/A }
0N/A return (AWTChar)&(perChar[index]);
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT AWTChar JNICALL AWTFontMaxBounds(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return (AWTChar)&((XFontStruct *)font)->max_bounds;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/A
0N/AJNIEXPORT int JNICALL AWTFontAscent(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XFontStruct *)font)->ascent;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/A
0N/AJNIEXPORT int JNICALL AWTFontDescent(AWTFont font) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XFontStruct *)font)->descent;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL AWTFontTextExtents16(AWTFont font,
0N/A AWTChar2b* xChar,
0N/A AWTChar* overall) {
0N/A#ifndef HEADLESS
0N/A JNIEnv *env;
0N/A int ascent, descent, direction;
0N/A XFontStruct* xFont = (XFontStruct*)font;
0N/A XCharStruct* newChar = (XCharStruct*)malloc(sizeof(XCharStruct));
0N/A *overall = (AWTChar)newChar;
0N/A /* There is a claim from the pre 1.5 source base that the info in the
0N/A * XFontStruct is flaky for 16 byte chars. This seems plausible as
0N/A * for info to be valid, that struct would need a large number of
0N/A * XCharStructs. But there's nothing in the X APIs which warns you of
0N/A * this. If it really is flaky you must question why there's an
0N/A * XTextExtents16 API call. Try XTextExtents16 for now and if it fails
0N/A * go back to XQueryTextExtents16 in this function.
0N/A * Indeed the metrics from the Solaris 9 JA font
0N/A * -ricoh-gothic-medium-r-normal--*-140-72-72-m-*-jisx0208.1983-0
0N/A * do appear different so revert to the query api
0N/A */
0N/A FONT_AWT_LOCK();
0N/A XQueryTextExtents16(awt_display,xFont->fid, xChar, 1,
0N/A &direction, &ascent, &descent, newChar);
0N/A/* XTextExtents16(xFont, xChar, 1, &direction, &ascent, &descent, newChar); */
0N/A AWT_UNLOCK();
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL AWTFreeChar(AWTChar xChar) {
0N/A#ifndef HEADLESS
0N/A free(xChar);
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT jlong JNICALL AWTFontGenerateImage(AWTFont pFont, AWTChar2b* xChar) {
0N/A
0N/A#ifndef HEADLESS
0N/A
0N/A int width, height, direction, ascent, descent;
0N/A GlyphInfo *glyphInfo;
0N/A XFontStruct* xFont = (XFontStruct*)pFont;
0N/A XCharStruct xcs;
0N/A XImage *ximage;
0N/A int h, i, j, nbytes;
0N/A unsigned char *srcRow, *dstRow, *dstByte;
0N/A int wholeByteCount, remainingBitsCount;
0N/A unsigned int imageSize;
0N/A JNIEnv *env;
0N/A
0N/A FONT_AWT_LOCK();
0N/A/* XTextExtents16(xFont, xChar, 1, &direction, &ascent, &descent, &xcs); */
0N/A XQueryTextExtents16(awt_display,xFont->fid, xChar, 1,
0N/A &direction, &ascent, &descent, &xcs);
0N/A width = xcs.rbearing - xcs.lbearing;
0N/A height = xcs.ascent+xcs.descent;
0N/A imageSize = width*height;
0N/A
0N/A glyphInfo = (GlyphInfo*)malloc(sizeof(GlyphInfo)+imageSize);
0N/A glyphInfo->cellInfo = NULL;
0N/A glyphInfo->width = width;
0N/A glyphInfo->height = height;
0N/A glyphInfo->topLeftX = xcs.lbearing;
0N/A glyphInfo->topLeftY = -xcs.ascent;
0N/A glyphInfo->advanceX = xcs.width;
0N/A glyphInfo->advanceY = 0;
0N/A
0N/A if (imageSize == 0) {
0N/A glyphInfo->image = NULL;
0N/A AWT_UNLOCK();
0N/A return (jlong)(uintptr_t)glyphInfo;
0N/A } else {
0N/A glyphInfo->image = (unsigned char*)glyphInfo+sizeof(GlyphInfo);
0N/A }
0N/A
0N/A if ((pixmap == 0) || (width > pixmapWidth) || (height > pixmapHeight)) {
0N/A if (CreatePixmapAndGC(width, height) != Success) {
0N/A glyphInfo->image = NULL;
0N/A AWT_UNLOCK();
0N/A return (jlong)(uintptr_t)glyphInfo;
0N/A }
0N/A }
0N/A
0N/A XSetFont(awt_display, pixmapGC, xFont->fid);
0N/A XSetForeground(awt_display, pixmapGC, 0);
0N/A XFillRectangle(awt_display, pixmap, pixmapGC, 0, 0,
0N/A pixmapWidth, pixmapHeight);
0N/A XSetForeground(awt_display, pixmapGC, 1);
0N/A XDrawString16(awt_display, pixmap, pixmapGC,
0N/A -xcs.lbearing, xcs.ascent, xChar, 1);
0N/A ximage = XGetImage(awt_display, pixmap, 0, 0, width, height,
0N/A AllPlanes, XYPixmap);
0N/A
0N/A if (ximage == NULL) {
0N/A glyphInfo->image = NULL;
0N/A AWT_UNLOCK();
0N/A return (jlong)(uintptr_t)glyphInfo;
0N/A }
0N/A
0N/A#ifdef DUMP_IMAGES
0N/A dumpXImage(ximage);
0N/A#endif
0N/A
0N/A nbytes = ximage->bytes_per_line;
0N/A srcRow = (unsigned char*)ximage->data;
0N/A dstRow = (unsigned char*)glyphInfo->image;
0N/A wholeByteCount = width >> 3;
0N/A remainingBitsCount = width & 7;
0N/A
0N/A for (h=0; h<height; h++) {
0N/A const UInt8* src8 = srcRow;
0N/A UInt8 *dstByte = dstRow;
0N/A UInt32 srcValue;
0N/A
0N/A srcRow += nbytes;
0N/A dstRow += width;
0N/A
0N/A for (i = 0; i < wholeByteCount; i++) {
0N/A srcValue = *src8++;
0N/A for (j = 0; j < 8; j++) {
0N/A if (ximage->bitmap_bit_order == LSBFirst) {
0N/A *dstByte++ = (srcValue & 0x01) ? 0xFF : 0;
0N/A srcValue >>= 1;
0N/A } else { /* MSBFirst */
0N/A *dstByte++ = (srcValue & 0x80) ? 0xFF : 0;
0N/A srcValue <<= 1;
0N/A }
0N/A }
0N/A }
0N/A if (remainingBitsCount) {
0N/A srcValue = *src8;
0N/A for (j = 0; j < remainingBitsCount; j++) {
0N/A if (ximage->bitmap_bit_order == LSBFirst) {
0N/A *dstByte++ = (srcValue & 0x01) ? 0xFF : 0;
0N/A srcValue >>= 1;
0N/A } else { /* MSBFirst */
0N/A *dstByte++ = (srcValue & 0x80) ? 0xFF : 0;
0N/A srcValue <<= 1;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A XDestroyImage (ximage);
0N/A AWT_UNLOCK();
0N/A return (jlong)(uintptr_t)glyphInfo;
0N/A#else
0N/A return (jlong)0;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT short JNICALL AWTCharAdvance(AWTChar xChar) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XCharStruct *)xChar)->width;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT short JNICALL AWTCharLBearing(AWTChar xChar) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XCharStruct *)xChar)->lbearing;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT short JNICALL AWTCharRBearing(AWTChar xChar) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XCharStruct *)xChar)->rbearing;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT short JNICALL AWTCharAscent(AWTChar xChar) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XCharStruct *)xChar)->ascent;
0N/A#endif /* !HEADLESS */
0N/A}
0N/A
0N/AJNIEXPORT short JNICALL AWTCharDescent(AWTChar xChar) {
0N/A#ifdef HEADLESS
0N/A return 0;
0N/A#else
0N/A return ((XCharStruct *)xChar)->descent;
0N/A#endif /* !HEADLESS */
0N/A}