scx/c/xlib/text.c

195 lines
5.6 KiB
C
Raw Normal View History

2001-05-08 10:21:00 -04:00
#include "xlib.h"
2001-07-19 09:59:42 -04:00
#include "scheme48.h"
2002-02-04 07:44:08 -05:00
#include <stdio.h>
2001-05-08 10:21:00 -04:00
extern XDrawText(), XDrawText16();
/* Calculation of text widths and extents should not be done using
* the Xlib functions. For instance, the values returned by
* XTextExtents() are only shorts and can therefore overflow for
* long strings.
*/
2002-02-04 07:44:08 -05:00
s48_value scx_Text_Width(s48_value Xfontstruct, s48_value text,
s48_value format){
int len = (int)S48_VECTOR_LENGTH(text), i, tmp;
2002-03-17 10:51:21 -05:00
char s[len];
XChar2b s2[len];
XFontStruct* font = SCX_EXTRACT_FONTSTRUCT(Xfontstruct);
2002-02-04 07:44:08 -05:00
if (s48_extract_integer(format) == 1){
2001-07-19 09:59:42 -04:00
for (i = 0; i < len; i++){
tmp = (int)s48_extract_integer(S48_VECTOR_REF(text, i));
s2[i].byte1 = (tmp >> 8) & 0xff;
s2[i].byte2 = tmp & 0xff;
}
i = XTextWidth16(font, s2, len);
}
else{
for (i = 0; i < len; i++){
s[i] = (int)s48_extract_integer(S48_VECTOR_REF(text, i));
2001-05-08 10:21:00 -04:00
}
2001-07-19 09:59:42 -04:00
i = XTextWidth(font, s, len);
}
return s48_enter_fixnum((long)i);
2001-05-08 10:21:00 -04:00
}
2001-07-19 09:59:42 -04:00
s48_value scx_Extents_Text (s48_value Xfontstruct, s48_value text,
2001-07-19 09:59:42 -04:00
s48_value format, s48_value which){
char* s;
XChar2b* s2;
XFontStruct* font = SCX_EXTRACT_FONTSTRUCT(Xfontstruct);
2001-07-19 09:59:42 -04:00
XCharStruct CI;
int len = (int)S48_VECTOR_LENGTH(text), i, tmp, dir, fasc, fdesc;
2002-02-04 07:44:08 -05:00
if (s48_extract_integer(format) == 1){
2001-07-19 09:59:42 -04:00
for (i = 0; i < len; i++){
tmp = (int)s48_extract_integer(S48_VECTOR_REF(text, i));
s2[i].byte1 = (tmp >> 8) & 0xff;
s2[i].byte2 = tmp & 0xff;
}
XTextExtents16(font, s2, len, &dir, &fasc, &fdesc, &CI);
}else{
for (i = 0; i < len; i++){
s[i] = (int)s48_extract_integer(S48_VECTOR_REF(text, i));
}
XTextExtents(font, s, len, &dir, &fasc, &fdesc, &CI);
}
switch(s48_extract_integer(which)){
case 0:
return s48_enter_fixnum((long) CI.lbearing);
2001-07-19 09:59:42 -04:00
case 1:
return s48_enter_fixnum((long) CI.rbearing);
2001-07-19 09:59:42 -04:00
case 2:
return s48_enter_fixnum((long) CI.width);
2001-07-19 09:59:42 -04:00
case 3:
return s48_enter_fixnum((long) CI.ascent);
2001-07-19 09:59:42 -04:00
case 4:
return s48_enter_fixnum((long) CI.descent);
2001-07-19 09:59:42 -04:00
}
return S48_FALSE;
2001-05-08 10:21:00 -04:00
}
s48_value scx_Draw_Image_Text (s48_value Xdisplay, s48_value Xdrawable,
2001-07-19 09:59:42 -04:00
s48_value Xgcontext, s48_value x, s48_value y,
s48_value text, s48_value is_twobyte){
2001-07-19 09:59:42 -04:00
int i, len, tmp;
len = S48_VECTOR_LENGTH(text);
2001-05-08 10:21:00 -04:00
if (!S48_FALSE_P(is_twobyte)) {
XChar2b s2[len];
2001-07-19 09:59:42 -04:00
for (i = 0; i < len; i++) {
tmp = (int)s48_extract_integer(S48_VECTOR_REF(text, i));
s2[i].byte1 = (tmp >> 8) & 0xff;
s2[i].byte2 = tmp & 0xff;
}
XDrawImageString16 (SCX_EXTRACT_DISPLAY(Xdisplay),
SCX_EXTRACT_DRAWABLE(Xdrawable),
SCX_EXTRACT_GCONTEXT(Xgcontext),
2001-07-19 09:59:42 -04:00
(int)s48_extract_integer(x),
(int)s48_extract_integer(y),
s2, len);
} else {
char s[len];
for (i = 0; i < len; i++) {
2001-07-19 09:59:42 -04:00
s[i] = (int)s48_extract_integer(S48_VECTOR_REF(text, i));
2001-05-08 10:21:00 -04:00
}
XDrawImageString (SCX_EXTRACT_DISPLAY(Xdisplay),
SCX_EXTRACT_DRAWABLE(Xdrawable),
SCX_EXTRACT_GCONTEXT(Xgcontext),
2001-07-19 09:59:42 -04:00
(int)s48_extract_integer(x),
(int)s48_extract_integer(y),
s, len);
2001-07-19 09:59:42 -04:00
}
return S48_UNSPECIFIC;
2001-05-08 10:21:00 -04:00
}
// Draw_Poly_Text processes a vector like [[24 23 87 67] Xfont [12 0]] and
// passes it to XDrawPolyText or XDrawPolyText16
s48_value scx_Draw_Poly_Text (s48_value Xdisplay, s48_value Xdrawable,
2001-07-19 09:59:42 -04:00
s48_value Xgcontext, s48_value x, s48_value y,
s48_value text, s48_value is_twobyte) {
int i, len, nitems;
s48_value temp_vec = S48_FALSE;
char twobyte = !S48_FALSE_P(is_twobyte);
len = S48_VECTOR_LENGTH(text);
// Nothing to do with an empty vector.
if (len == 0) return S48_UNSPECIFIC;
// count the strings in text:
nitems = 0;
for (i = 0; i < len; i++) {
if (S48_VECTOR_P(S48_VECTOR_REF(text, i)))
nitems++;
}
2001-05-08 10:21:00 -04:00
{
XTextItem item[nitems];
int set = 0, j, k, tmp;
2001-07-19 09:59:42 -04:00
//Maybe no font as first Element of text?
if (S48_VECTOR_P(S48_VECTOR_REF(text, 0))) {
2001-07-19 09:59:42 -04:00
item[0].delta = 0;
item[0].font = None;
}
// Generate the XTextItem{16}
2001-07-19 09:59:42 -04:00
for (i = 0; i < len; i++) {
if (S48_VECTOR_P(S48_VECTOR_REF(text,i))) {
2001-07-19 09:59:42 -04:00
temp_vec = S48_VECTOR_REF(text,i);
k = S48_VECTOR_LENGTH(temp_vec);
item[set].nchars = k;
if (twobyte) {
2001-08-22 07:56:46 -04:00
XChar2b* s2 = (XChar2b*)malloc(sizeof(XChar2b)*k);
2001-07-19 09:59:42 -04:00
for (j = 0; j < k; j++){
tmp = (int)s48_extract_integer(S48_VECTOR_REF(temp_vec, j));
s2[j].byte1 = (tmp >> 8) & 0xff;
s2[j].byte2 = tmp & 0xff;
}
(XTextItem16*)item[set].chars = s2;
} else {
2001-08-22 07:56:46 -04:00
char* s = (char*)malloc(sizeof(char)*k);
for (j = 0; j < k; j++) {
2001-07-19 09:59:42 -04:00
s[j] = (int)s48_extract_integer(S48_VECTOR_REF(temp_vec, j));
}
item[set].chars = s;
}
set++;
} else {
s48_value fontspec = S48_VECTOR_REF(text, i);
s48_value font = S48_CAR(fontspec);
2002-03-17 10:51:21 -05:00
item[set].font = SCX_EXTRACT_FONT(font);
item[set].delta = s48_extract_integer(S48_CDR(fontspec));
2001-07-19 09:59:42 -04:00
}
}
// No pass it all to the Xlib
if (twobyte) {
XDrawText16(SCX_EXTRACT_DISPLAY(Xdisplay),
SCX_EXTRACT_DRAWABLE(Xdrawable),
SCX_EXTRACT_GCONTEXT(Xgcontext),
(int)s48_extract_integer(x), (int)s48_extract_integer(y),
(XTextItem16*) item, nitems);
} else {
XDrawText(SCX_EXTRACT_DISPLAY(Xdisplay), SCX_EXTRACT_DRAWABLE(Xdrawable),
SCX_EXTRACT_GCONTEXT(Xgcontext), (int)s48_extract_integer(x),
2001-07-19 09:59:42 -04:00
(int)s48_extract_integer(y), item, nitems);
}
// No free all character-arrays
for (i = 0; i < nitems; i++)
free(item[i].chars);
}
return S48_UNSPECIFIC;
2001-05-08 10:21:00 -04:00
}
2001-07-19 09:59:42 -04:00
void scx_init_text(void) {
S48_EXPORT_FUNCTION(scx_Text_Width);
S48_EXPORT_FUNCTION(scx_Extents_Text);
S48_EXPORT_FUNCTION(scx_Draw_Image_Text);
S48_EXPORT_FUNCTION(scx_Draw_Poly_Text);
2001-05-08 10:21:00 -04:00
}