Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtTerm / TermPrim / TermPrimRenderFont.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: TermPrimRenderFont.c /main/1 1996/04/21 19:18:47 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 "TermPrimRenderFont.h"
41
42 static void
43 FontRenderFunction(
44     Widget                w,
45     TermFont              font,
46     Pixel                 fg,
47     Pixel                 bg,
48     unsigned long         flags,
49     int                   x,
50     int                   y,
51     unsigned char        *string,
52     int                   len
53 )
54 {
55     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
56     struct termData *tpd = tw->term.tpd;
57     XGCValues values;
58     unsigned long valueMask;
59     XFontStruct *fontStruct = (XFontStruct *) font->fontInfo;
60
61     /* set the renderGC... */
62     valueMask = (unsigned long) 0;
63
64     /* set the foreground... */
65     if (TermIS_SECURE(flags)) {
66         if (tpd->renderGC.foreground != bg) {
67             tpd->renderGC.foreground = bg;
68             values.foreground = bg;
69             valueMask |= GCForeground;
70         }
71     } else {
72         if (tpd->renderGC.foreground != fg) {
73             tpd->renderGC.foreground = fg;
74             values.foreground = fg;
75             valueMask |= GCForeground;
76         }
77     }
78
79     /* set background... */
80     if (tpd->renderGC.background != bg) {
81         tpd->renderGC.background = bg;
82         values.background = bg;
83         valueMask |= GCBackground;
84     }
85
86     /* set the font for renderGC if necessary */
87     if (tpd->renderGC.fid != fontStruct->fid) {
88         tpd->renderGC.fid = fontStruct->fid;
89         values.font = fontStruct->fid;
90         valueMask |= GCFont;
91     }
92
93     if (valueMask) {
94         (void) XChangeGC(XtDisplay(w), tpd->renderGC.gc, valueMask,
95                 &values);
96     }
97
98     /* draw image string a line of text... */
99     if (isDebugFSet('t', 1)) {
100 #ifdef  BBA
101 #pragma BBA_IGNORE
102 #endif  /*BBA*/
103         /* Fill in the text area so we can see what is going to
104          * be displayed...
105          */
106         (void) XFillRectangle(XtDisplay(w),
107                 XtWindow(w),
108                 tpd->renderGC.gc,
109                 x,
110                 y,
111                 tpd->cellWidth * len,
112                 tpd->cellHeight);
113         (void) XSync(XtDisplay(w), False);
114         (void) shortSleep(100000);
115     }
116                         
117     (void) XDrawImageString(XtDisplay(w),       /* Display              */
118             XtWindow(w),                        /* Drawable             */
119             tpd->renderGC.gc,                   /* GC                   */
120             x,                                  /* x                    */
121             y + fontStruct->ascent,             /* y                    */
122             (char *) string,                    /* string               */
123             len);                               /* length               */
124
125     /* handle overstrike... */
126     if (TermIS_OVERSTRIKE(flags)) {
127         (void) XDrawString(XtDisplay(w),        /* Display              */
128                 XtWindow(w),                    /* Drawable             */
129                 tpd->renderGC.gc,               /* GC                   */
130                 x + 1,                          /* x                    */
131                 y + fontStruct->ascent,         /* y                    */
132                 (char *) string,                /* string               */
133                 len);                           /* length               */
134     }
135
136     /* handle the underline enhancement... */
137     /* draw the underline... */
138     if (TermIS_UNDERLINE(flags)) {
139         XDrawLine(XtDisplay(w),                 /* Display              */
140                 XtWindow(w),                    /* Window               */
141                 tpd->renderGC.gc,               /* GC                   */
142                 x,                              /* X1                   */
143                 y + tpd->cellHeight - 1,                /* Y1                   */
144                 x + len * tpd->cellWidth,       /* X2                   */
145                 y + tpd->cellHeight - 1);       /* Y2                   */
146     }
147 }
148
149 static void
150 FontDestroyFunction(
151     Widget                w,
152     TermFont              font
153 )
154 {
155     (void) XtFree((char *) font);
156 }
157
158 static void
159 FontExtentsFunction(
160     Widget                w,
161     TermFont              font,
162     unsigned char        *string,
163     int                   len,
164     int                  *widthReturn,
165     int                  *heightReturn,
166     int                  *ascentReturn
167 )
168 {
169     XFontStruct *fontStruct = (XFontStruct *) font->fontInfo;
170
171     if (widthReturn) {
172         *widthReturn = len * fontStruct->max_bounds.width;
173     }
174     if (heightReturn) {
175         *heightReturn = fontStruct->ascent + fontStruct->descent;
176     }
177     if (ascentReturn) {
178         *ascentReturn = fontStruct->ascent;
179     }
180     return;
181 }
182
183 TermFont
184 _DtTermPrimRenderFontCreate(
185     Widget                w,
186     XFontStruct          *fontStruct
187 )
188 {
189     TermFont termFont;
190
191     termFont = (TermFont) XtMalloc(sizeof(TermFontRec));
192     termFont->renderFunction = FontRenderFunction;
193     termFont->destroyFunction = FontDestroyFunction;
194     termFont->extentsFunction = FontExtentsFunction;
195     termFont->fontInfo = (XtPointer) fontStruct;
196
197     return(termFont);
198 }