Include <stdlib.h> to obtain a prototype of abs().
[wine] / dlls / wineps / text.c
1 /*
2  *      PostScript driver text functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <string.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <math.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "psdrv.h"
29 #include "winspool.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
33
34 static BOOL PSDRV_Text(PSDRV_PDEVICE *physDev, INT x, INT y, UINT flags,
35                        LPCWSTR str, UINT count,
36                        BOOL bDrawBackground, const INT *lpDx);
37
38 /***********************************************************************
39  *           PSDRV_ExtTextOut
40  */
41 BOOL PSDRV_ExtTextOut( PSDRV_PDEVICE *physDev, INT x, INT y, UINT flags,
42                        const RECT *lprect, LPCWSTR str, UINT count,
43                        const INT *lpDx, INT breakExtra )
44 {
45     BOOL bResult = TRUE;
46     BOOL bClipped = FALSE;
47     BOOL bOpaque = FALSE;
48     RECT rect;
49
50     TRACE("(x=%d, y=%d, flags=0x%08x, str=%s, count=%d, lpDx=%p)\n", x, y,
51           flags, debugstr_wn(str, count), count, lpDx);
52
53     /* write font if not already written */
54     PSDRV_SetFont(physDev);
55
56     PSDRV_SetClip(physDev);
57
58     /* set clipping and/or draw background */
59     if ((flags & (ETO_CLIPPED | ETO_OPAQUE)) && (lprect != NULL))
60     {
61         rect = *lprect;
62         LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
63         PSDRV_WriteGSave(physDev);
64         PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
65                              rect.bottom - rect.top);
66
67         if (flags & ETO_OPAQUE)
68         {
69             bOpaque = TRUE;
70             PSDRV_WriteGSave(physDev);
71             PSDRV_WriteSetColor(physDev, &physDev->bkColor);
72             PSDRV_WriteFill(physDev);
73             PSDRV_WriteGRestore(physDev);
74         }
75
76         if (flags & ETO_CLIPPED)
77         {
78             bClipped = TRUE;
79             PSDRV_WriteClip(physDev);
80         }
81
82         bResult = PSDRV_Text(physDev, x, y, flags, str, count, !(bClipped && bOpaque), lpDx);
83         PSDRV_WriteGRestore(physDev);
84     }
85     else
86     {
87         bResult = PSDRV_Text(physDev, x, y, flags, str, count, TRUE, lpDx);
88     }
89
90     PSDRV_ResetClip(physDev);
91     return bResult;
92 }
93
94 /***********************************************************************
95  *           PSDRV_Text
96  */
97 static BOOL PSDRV_Text(PSDRV_PDEVICE *physDev, INT x, INT y, UINT flags, LPCWSTR str,
98                        UINT count, BOOL bDrawBackground, const INT *lpDx)
99 {
100     SIZE sz;
101     TEXTMETRICW tm;
102     POINT pt;
103     INT ascent, descent;
104     WORD *glyphs = NULL;
105     UINT align = GetTextAlign( physDev->hdc );
106     INT char_extra;
107     INT *deltas = NULL;
108     double cosEsc, sinEsc;
109     LOGFONTW lf;
110
111     if (!count)
112         return TRUE;
113
114     GetObjectW(GetCurrentObject(physDev->hdc, OBJ_FONT), sizeof(lf), &lf);
115     if(lf.lfEscapement != 0) {
116         cosEsc = cos(lf.lfEscapement * M_PI / 1800);
117         sinEsc = sin(lf.lfEscapement * M_PI / 1800);
118     } else {
119         cosEsc = 1;
120         sinEsc = 0;
121     }
122
123     if(physDev->font.fontloc == Download) {
124         if(flags & ETO_GLYPH_INDEX)
125             glyphs = (LPWORD)str;
126         else {
127             glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WORD));
128             GetGlyphIndicesW(physDev->hdc, str, count, glyphs, 0);
129         }
130     }
131
132     pt.x = x;
133     pt.y = y;
134     if(align & TA_UPDATECP) GetCurrentPositionEx( physDev->hdc, &pt );
135     LPtoDP(physDev->hdc, &pt, 1);
136     x = pt.x;
137     y = pt.y;
138
139     if(physDev->font.fontloc == Download)
140         GetTextExtentPointI(physDev->hdc, glyphs, count, &sz);
141     else
142         GetTextExtentPoint32W(physDev->hdc, str, count, &sz);
143
144     if((char_extra = GetTextCharacterExtra(physDev->hdc)) != 0) {
145         INT i;
146         SIZE tmpsz;
147
148         deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
149         for(i = 0; i < count; i++) {
150             if(lpDx)
151                 deltas[i] = lpDx[i] + char_extra;
152             else {
153                 if(physDev->font.fontloc == Download)
154                     GetTextExtentPointI(physDev->hdc, glyphs + i, 1, &tmpsz);
155                 else
156                     GetTextExtentPoint32W(physDev->hdc, str + i, 1, &tmpsz);
157                 deltas[i] = tmpsz.cx;
158             }
159         }
160     } else if(lpDx)
161         deltas = (INT*)lpDx;
162
163     if(deltas) {
164         SIZE tmpsz;
165         INT i;
166         /* Get the width of the last char and add on all the offsets */
167         if(physDev->font.fontloc == Download)
168             GetTextExtentPointI(physDev->hdc, glyphs + count - 1, 1, &tmpsz);
169         else
170             GetTextExtentPoint32W(physDev->hdc, str + count - 1, 1, &tmpsz);
171         for(i = 0; i < count-1; i++)
172             tmpsz.cx += deltas[i];
173         sz.cx = tmpsz.cx; /* sz.cy remains untouched */
174     }
175
176     sz.cx = PSDRV_XWStoDS(physDev, sz.cx);
177     sz.cy = PSDRV_YWStoDS(physDev, sz.cy);
178
179     GetTextMetricsW(physDev->hdc, &tm);
180     ascent = abs(PSDRV_YWStoDS(physDev, tm.tmAscent));
181     descent = abs(PSDRV_YWStoDS(physDev, tm.tmDescent));
182
183     TRACE("textAlign = %x\n", align);
184     switch(align & (TA_LEFT | TA_CENTER | TA_RIGHT) ) {
185     case TA_LEFT:
186         if(align & TA_UPDATECP)
187         {
188             POINT pt;
189             pt.x = x + sz.cx * cosEsc;
190             pt.y = y - sz.cx * sinEsc;
191             DPtoLP( physDev->hdc, &pt, 1 );
192             MoveToEx( physDev->hdc, pt.x, pt.y, NULL );
193         }
194         break;
195
196     case TA_CENTER:
197         x -= sz.cx * cosEsc / 2;
198         y += sz.cx * sinEsc / 2;
199         break;
200
201     case TA_RIGHT:
202         x -= sz.cx * cosEsc;
203         y += sz.cx * sinEsc;
204         if(align & TA_UPDATECP)
205         {
206             POINT pt;
207             pt.x = x;
208             pt.y = y;
209             DPtoLP( physDev->hdc, &pt, 1 );
210             MoveToEx( physDev->hdc, pt.x, pt.y, NULL );
211         }
212         break;
213     }
214
215     switch(align & (TA_TOP | TA_BASELINE | TA_BOTTOM) ) {
216     case TA_TOP:
217         y += ascent * cosEsc;
218         x += ascent * sinEsc;
219         break;
220
221     case TA_BASELINE:
222         break;
223
224     case TA_BOTTOM:
225         y -= descent * cosEsc;
226         x -= descent * sinEsc;
227         break;
228     }
229
230     if ((GetBkMode( physDev->hdc ) != TRANSPARENT) && bDrawBackground)
231     {
232         PSDRV_WriteGSave(physDev);
233         PSDRV_WriteNewPath(physDev);
234         PSDRV_WriteRectangle(physDev, x, y - ascent, sz.cx,
235                              ascent + descent);
236         PSDRV_WriteSetColor(physDev, &physDev->bkColor);
237         PSDRV_WriteFill(physDev);
238         PSDRV_WriteGRestore(physDev);
239     }
240
241     PSDRV_WriteMoveTo(physDev, x, y);
242
243     if(!deltas) {
244         if(physDev->font.fontloc == Download)
245             PSDRV_WriteDownloadGlyphShow(physDev, glyphs, count);
246         else
247             PSDRV_WriteBuiltinGlyphShow(physDev, str, count);
248     }
249     else {
250         INT i;
251         float dx = 0.0, dy = 0.0;
252         float cos_theta = cos(physDev->font.escapement * M_PI / 1800.0);
253         float sin_theta = sin(physDev->font.escapement * M_PI / 1800.0);
254         for(i = 0; i < count-1; i++) {
255             TRACE("lpDx[%d] = %d\n", i, deltas[i]);
256             if(physDev->font.fontloc == Download)
257                 PSDRV_WriteDownloadGlyphShow(physDev, glyphs + i, 1);
258             else
259                 PSDRV_WriteBuiltinGlyphShow(physDev, str + i, 1);
260             dx += deltas[i] * cos_theta;
261             dy -= deltas[i] * sin_theta;
262             PSDRV_WriteMoveTo(physDev, x + PSDRV_XWStoDS(physDev, dx),
263                               y + PSDRV_YWStoDS(physDev, dy));
264         }
265         if(physDev->font.fontloc == Download)
266             PSDRV_WriteDownloadGlyphShow(physDev, glyphs + i, 1);
267         else
268             PSDRV_WriteBuiltinGlyphShow(physDev, str + i, 1);
269         if(deltas != lpDx)
270             HeapFree(GetProcessHeap(), 0, deltas);
271     }
272
273     /*
274      * Underline and strikeout attributes.
275      */
276     if ((tm.tmUnderlined) || (tm.tmStruckOut)) {
277
278         /* Get the thickness and the position for the underline attribute */
279         /* We'll use the same thickness for the strikeout attribute       */
280
281         INT escapement =  physDev->font.escapement;
282
283         /* Do the underline */
284
285         if (tm.tmUnderlined) {
286             PSDRV_WriteNewPath(physDev); /* will be closed by WriteRectangle */
287             if (escapement != 0)  /* rotated text */
288             {
289                 PSDRV_WriteGSave(physDev);  /* save the graphics state */
290                 PSDRV_WriteMoveTo(physDev, x, y); /* move to the start */
291
292                 /* temporarily rotate the coord system */
293                 PSDRV_WriteRotate(physDev, -escapement/10);
294
295                 /* draw the underline relative to the starting point */
296                 PSDRV_WriteRRectangle(physDev, 0, -physDev->font.underlinePosition,
297                                       sz.cx, physDev->font.underlineThickness);
298             }
299             else
300                 PSDRV_WriteRectangle(physDev, x, y - physDev->font.underlinePosition,
301                                      sz.cx, physDev->font.underlineThickness);
302
303             PSDRV_WriteFill(physDev);
304
305             if (escapement != 0)  /* rotated text */
306                 PSDRV_WriteGRestore(physDev);  /* restore the graphics state */
307         }
308
309         /* Do the strikeout */
310
311         if (tm.tmStruckOut) {
312             PSDRV_WriteNewPath(physDev); /* will be closed by WriteRectangle */
313             if (escapement != 0)  /* rotated text */
314             {
315                 PSDRV_WriteGSave(physDev);  /* save the graphics state */
316                 PSDRV_WriteMoveTo(physDev, x, y); /* move to the start */
317
318                 /* temporarily rotate the coord system */
319                 PSDRV_WriteRotate(physDev, -escapement/10);
320
321                 /* draw the line relative to the starting point */
322                 PSDRV_WriteRRectangle(physDev, 0, -physDev->font.strikeoutPosition,
323                                       sz.cx, physDev->font.strikeoutThickness);
324             }
325             else
326                 PSDRV_WriteRectangle(physDev, x, y - physDev->font.strikeoutPosition,
327                                      sz.cx, physDev->font.strikeoutThickness);
328
329             PSDRV_WriteFill(physDev);
330
331             if (escapement != 0)  /* rotated text */
332                 PSDRV_WriteGRestore(physDev);  /* restore the graphics state */
333         }
334     }
335
336     if(glyphs && glyphs != str) HeapFree(GetProcessHeap(), 0, glyphs);
337     return TRUE;
338 }