Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtTerm / TermPrim / TermPrimRenderFontSet.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 #ifndef lint
24 #ifdef  VERBOSE_REV_INFO
25 static char rcs_id[] = "$XConsortium: TermPrimRenderFontSet.c /main/1 1996/04/21 19:18:53 drk $";
26 #endif  /* VERBOSE_REV_INFO */
27 #endif  /* lint */
28
29 /*                                                                      *
30  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
31  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
32  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
33  * (c) Copyright 1993, 1994 Novell, Inc.                                *
34  */
35
36 #include "TermHeader.h"
37 #include "TermPrimP.h"
38 #include "TermPrimDebug.h"
39 #include "TermPrimRenderP.h"
40 #include "TermPrimRenderFontSet.h"
41
42 typedef struct _TermFontSetRec {
43     XFontSet fontSet;
44     int ascent;
45     int height;
46     int width;
47 } TermFontSetRec, *TermFontSet;
48
49 static void
50 FontSetRenderFunction(
51     Widget                w,
52     TermFont              font,
53     Pixel                 fg,
54     Pixel                 bg,
55     unsigned long         flags,
56     int                   x,
57     int                   y,
58     unsigned char        *string,
59     int                   len
60 )
61 {
62     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
63     struct termData *tpd = tw->term.tpd;
64     XGCValues values;
65     unsigned long valueMask;
66     TermFontSet termFontSet = (TermFontSet) font->fontInfo;
67
68     /* set the renderGC... */
69     valueMask = (unsigned long) 0;
70
71     /* set the foreground... */
72     if (TermIS_SECURE(flags)) {
73         if (tpd->renderGC.foreground != bg) {
74             tpd->renderGC.foreground = bg;
75             values.foreground = bg;
76             valueMask |= GCForeground;
77         }
78     } else {
79         if (tpd->renderGC.foreground != fg) {
80             tpd->renderGC.foreground = fg;
81             values.foreground = fg;
82             valueMask |= GCForeground;
83         }
84     }
85
86     /* set background... */
87     if (tpd->renderGC.background != bg) {
88         tpd->renderGC.background = bg;
89         values.background = bg;
90         valueMask |= GCBackground;
91     }
92
93     /* since Xlib will be mucking with the GC's font under us, we need to
94      * make sure we trash the cached value...
95      */
96     tpd->renderGC.fid = (Font) 0;
97
98     if (valueMask) {
99         (void) XChangeGC(XtDisplay(w), tpd->renderGC.gc, valueMask,
100                 &values);
101     }
102
103     /* draw image string a line of text... */
104     if (isDebugFSet('t', 1)) {
105 #ifdef  BBA
106 #pragma BBA_IGNORE
107 #endif  /*BBA*/
108         /* Fill in the text area so we can see what is going to
109          * be displayed...
110          */
111         (void) XFillRectangle(XtDisplay(w),
112                 XtWindow(w),
113                 tpd->renderGC.gc,
114                 x,
115                 y,
116                 (tpd->mbCurMax == 1) ?
117                 XmbTextEscapement(termFontSet->fontSet, (char *) string, len) :
118                 XwcTextEscapement(termFontSet->fontSet, (wchar_t *) string,
119                 len),
120                 tpd->cellHeight);
121         (void) XSync(XtDisplay(w), False);
122         (void) shortSleep(100000);
123     }
124                         
125     if (tpd->mbCurMax == 1)
126     {
127          (void) XmbDrawImageString(XtDisplay(w),/* Display              */
128                  XtWindow(w),                   /* Drawable             */
129                  termFontSet->fontSet,          /* XFontSet             */
130                  tpd->renderGC.gc,              /* GC                   */
131                  x,                             /* x                    */
132                  y + termFontSet->ascent,       /* y                    */
133                  (char *)string,                /* string               */
134                  len);                          /* length               */
135
136          /* handle overstrike... */
137          if (TermIS_OVERSTRIKE(flags)) {
138              (void) XmbDrawString(XtDisplay(w), /* Display              */
139                      XtWindow(w),               /* Drawable             */
140                      termFontSet->fontSet,      /* XFontSet             */
141                      tpd->renderGC.gc,          /* GC                   */
142                      x + 1,                     /* x                    */
143                      y + termFontSet->ascent,   /* y                    */
144                      (char *)string,            /* string               */
145                      len);                      /* length               */
146          }
147          /* handle the underline enhancement... */
148          /* draw the underline... */
149          if (TermIS_UNDERLINE(flags)) {
150              XDrawLine(XtDisplay(w),            /* Display              */
151                        XtWindow(w),             /* Window               */
152                        tpd->renderGC.gc,        /* GC                   */
153                        x,                       /* X1                   */
154                        y + tpd->cellHeight - 1, /* Y1                   */
155                        x - 1 + XmbTextEscapement(termFontSet->fontSet,
156                                                  (char *) string, len), /* X2  */
157                        y + tpd->cellHeight - 1);/* Y2                   */
158          }
159     }
160     else
161     {
162         (void) XwcDrawImageString(XtDisplay(w), /* Display              */
163                 XtWindow(w),                    /* Drawable             */
164                 termFontSet->fontSet,           /* XFontSet             */
165                 tpd->renderGC.gc,               /* GC                   */
166                 x,                              /* x                    */
167                 y + termFontSet->ascent,        /* y                    */
168                 (wchar_t *) string,             /* string               */
169                 len);                           /* length               */
170
171         /* handle overstrike... */
172         if (TermIS_OVERSTRIKE(flags)) {
173             (void) XwcDrawString(XtDisplay(w),  /* Display              */
174                     XtWindow(w),                /* Drawable             */
175                     termFontSet->fontSet,       /* XFontSet             */
176                     tpd->renderGC.gc,           /* GC                   */
177                     x + 1,                      /* x                    */
178                     y + termFontSet->ascent,    /* y                    */
179                     (wchar_t *) string,         /* string               */
180                     len);                       /* length               */
181         }
182         /* handle the underline enhancement... */
183         /* draw the underline... */
184         if (TermIS_UNDERLINE(flags)) {
185             XDrawLine(XtDisplay(w),             /* Display              */
186                       XtWindow(w),              /* Window               */
187                       tpd->renderGC.gc,         /* GC                   */
188                       x,                        /* X1                   */
189                       y + tpd->cellHeight - 1,  /* Y1                   */
190                       x - 1 + XwcTextEscapement(termFontSet->fontSet,
191                                                 (wchar_t *) string, len), /* X2 */
192                       y + tpd->cellHeight - 1); /* Y2                   */
193         }
194     }
195 }
196
197 static void
198 FontSetDestroyFunction(
199     Widget                w,
200     TermFont              font
201 )
202 {
203     (void) XtFree((char *) font->fontInfo);
204     (void) XtFree((char *) font);
205 }
206
207 static void
208 FontSetExtentsFunction(
209     Widget                w,
210     TermFont              font,
211     unsigned char        *string,
212     int                   len,
213     int                  *widthReturn,
214     int                  *heightReturn,
215     int                  *ascentReturn
216 )
217 {
218     TermFontSet termFontSet = (TermFontSet) font->fontInfo;
219
220     if (widthReturn) {
221         *widthReturn = len * termFontSet->width;
222     }
223     if (heightReturn) {
224         *heightReturn = termFontSet->height;
225     }
226     if (ascentReturn) {
227         *ascentReturn = termFontSet->ascent;
228     }
229     return;
230 }
231
232 TermFont
233 _DtTermPrimRenderFontSetCreate(
234     Widget                w,
235     XFontSet              fontSet
236 )
237 {
238     TermFont termFont;
239     TermFontSet termFontSet;
240     XFontSetExtents *fontSetExtents;
241
242     termFont = (TermFont) XtMalloc(sizeof(TermFontRec));
243     termFont->renderFunction = FontSetRenderFunction;
244     termFont->destroyFunction = FontSetDestroyFunction;
245     termFont->extentsFunction = FontSetExtentsFunction;
246
247     termFontSet = (TermFontSet) XtMalloc(sizeof(TermFontSetRec));
248     termFontSet->fontSet = fontSet;
249     fontSetExtents = XExtentsOfFontSet(fontSet);
250     termFontSet->width = fontSetExtents->max_logical_extent.width;
251     termFontSet->height = fontSetExtents->max_logical_extent.height;
252     termFontSet->ascent = -fontSetExtents->max_logical_extent.y;
253     termFont->fontInfo = (XtPointer) termFontSet;
254
255     return(termFont);
256 }