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
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
);
25 JvGetStringUTFRegion(lfdNamePattern
, 0, lfdNamePattern
->length(),
29 XFontStruct
* fontStruct
= XLoadQueryFont(dpy
, cName
);
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
]
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
]
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
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
]);
93 xc
->byte1
= (jc
>> 8) & 0xff;
94 xc
->byte2
= jc
& 0xff;
96 return XTextWidth16(fontStruct
, xwchars
, length
);
99 void gnu::gcj::xlib::Font::finalize()
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;