libjava/ChangeLog:
[official-gcc.git] / libjava / gnu / gcj / xlib / natFont.cc
blobbe842d0a9b4705165c7069ae935330c0b16408d0
1 /* Copyright (C) 2000, 2003 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 #include <X11/Xlib.h>
11 #include <gcj/cni.h>
12 #include <gnu/gcj/RawData.h>
13 #include <java/lang/String.h>
15 #include <gnu/gcj/xlib/Display.h>
16 #include <gnu/gcj/xlib/Font.h>
17 #include <gnu/gcj/xlib/XException.h>
19 gnu::gcj::RawData* gnu::gcj::xlib::Font::loadFontImpl(Display* display,
20 jstring lfdNamePattern)
22 ::Display* dpy = (::Display*) display->display;
23 int len = JvGetStringUTFLength(lfdNamePattern);
24 char cName[len+1];
25 JvGetStringUTFRegion(lfdNamePattern, 0, lfdNamePattern->length(),
26 cName);
27 cName[len] = '\0';
29 XFontStruct* fontStruct = XLoadQueryFont(dpy, cName);
30 if (fontStruct == 0)
32 throw new XException(JvNewStringLatin1("font not found"));
35 return reinterpret_cast<gnu::gcj::RawData*>(fontStruct);
38 jint gnu::gcj::xlib::Font::getXIDFromStruct(gnu::gcj::RawData* structure)
40 XFontStruct* fontStruct = (XFontStruct*) structure;
41 return fontStruct->fid;
44 jint gnu::gcj::xlib::Font::getMaxAscent()
46 XFontStruct* fontStruct = (XFontStruct*) structure;
47 return fontStruct->max_bounds.ascent+1; // +1 to include the baseline
50 jint gnu::gcj::xlib::Font::getMaxDescent()
52 XFontStruct* fontStruct = (XFontStruct*) structure;
53 return fontStruct->max_bounds.descent-1; // -1 to exclude the baseline
56 jint gnu::gcj::xlib::Font::getAscent()
58 XFontStruct* fontStruct = (XFontStruct*) structure;
59 jint returnValue = fontStruct->ascent;
60 if (fontStruct->min_byte1==0 && fontStruct->min_char_or_byte2<=(unsigned)'O')
61 returnValue = fontStruct
62 ->per_char[(unsigned)'O'-fontStruct->min_char_or_byte2]
63 .ascent;
64 return returnValue+1; // +1 to include the baseline
67 jint gnu::gcj::xlib::Font::getDescent()
69 XFontStruct* fontStruct = (XFontStruct*) structure;
70 jint returnValue = fontStruct->descent;
71 if (fontStruct->min_byte1==0 && fontStruct->min_char_or_byte2<=(unsigned)'y')
72 returnValue = fontStruct
73 ->per_char[(unsigned)'y'-fontStruct->min_char_or_byte2]
74 .descent;
75 return returnValue-1; // -1 to exclude the baseline
78 jint gnu::gcj::xlib::Font::getStringWidth(::java::lang::String* text)
80 XFontStruct* fontStruct = (XFontStruct*) structure;
82 // FIXME: Convert to the character set used in the font, which may
83 // or may not be unicode. For now, treat everything as 16-bit and
84 // use character codes directly, which should be OK for unicode or
85 // 8-bit ascii fonts.
86 jint length = text->length();
87 jchar* txt = JvGetStringChars(text);
88 XChar2b xwchars[length];
89 for (int i=0; i<length; i++)
91 XChar2b* xc = &(xwchars[i]);
92 jchar jc = txt[i];
93 xc->byte1 = (jc >> 8) & 0xff;
94 xc->byte2 = jc & 0xff;
96 return XTextWidth16(fontStruct, xwchars, length);
99 void gnu::gcj::xlib::Font::finalize()
101 if (structure != 0)
103 ::Display* dpy = (::Display*) display->display;
104 XFontStruct* fontStruct = (XFontStruct*) structure;
105 int result = XFreeFont(dpy, fontStruct);
107 if (result == BadFont)
108 throw new XException(display, result);
110 structure = 0; xid = 0;