Release 20010305.
[wine] / graphics / x11drv / text.c
1 /*
2  * X11 graphics driver text functions
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <X11/Xatom.h>
10
11 #include "ts_xlib.h"
12
13 #include <stdlib.h>
14 #include <math.h>
15
16 #include "windef.h"
17 #include "winnls.h"
18 #include "gdi.h"
19 #include "heap.h"
20 #include "x11font.h"
21 #include "bitmap.h"
22 #include "debugtools.h"
23
24 DEFAULT_DEBUG_CHANNEL(text);
25
26 #define SWAP_INT(a,b)  { int t = a; a = b; b = t; }
27 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
28
29
30 /***********************************************************************
31  *           X11DRV_ExtTextOut
32  */
33 BOOL
34 X11DRV_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
35                    const RECT *lprect, LPCWSTR wstr, UINT count,
36                    const INT *lpDx )
37 {
38     int                 i;
39     fontObject*         pfo;
40     INT                 width, ascent, descent, xwidth, ywidth;
41     XFontStruct*        font;
42     RECT                rect;
43     char                dfBreakChar, lfUnderline, lfStrikeOut;
44     BOOL                rotated = FALSE;
45     X11DRV_PDEVICE      *physDev = (X11DRV_PDEVICE *)dc->physDev;
46     XChar2b             *str2b = NULL;
47     BOOL                dibUpdateFlag = FALSE;
48     BOOL                result = TRUE; 
49
50     if (!X11DRV_SetupGCForText( dc )) return TRUE;
51
52     pfo = XFONT_GetFontObject( physDev->font );
53     font = pfo->fs;
54      
55     if (pfo->lf.lfEscapement && pfo->lpX11Trans)
56         rotated = TRUE;
57     dfBreakChar = (char)pfo->fi->df.dfBreakChar;
58     lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
59     lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
60
61     TRACE("hdc=%04x df=%04x %d,%d %s, %d  flags=%d lpDx=%p\n",
62           dc->hSelf, (UINT16)(physDev->font), x, y,
63           debugstr_wn (wstr, count), count, flags, lpDx);
64
65     /* some strings sent here end in a newline for whatever reason.  I have no
66        clue what the right treatment should be in general, but ignoring
67        terminating newlines seems ok.  MW, April 1998.  */
68     if (count > 0 && wstr[count - 1] == '\n') count--;
69
70     if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
71                                      lprect->left, lprect->top,
72                                      lprect->right, lprect->bottom );
73       /* Setup coordinates */
74
75     if (dc->textAlign & TA_UPDATECP)
76     {
77         x = dc->CursPosX;
78         y = dc->CursPosY;
79     }
80
81     if (flags & (ETO_OPAQUE | ETO_CLIPPED))  /* there's a rectangle */
82     {
83         if (!lprect)  /* not always */
84         {
85             SIZE sz;
86             if (flags & ETO_CLIPPED)  /* Can't clip with no rectangle */
87               return FALSE;
88             if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
89               return FALSE;
90             rect.left   = INTERNAL_XWPTODP( dc, x, y );
91             rect.right  = INTERNAL_XWPTODP( dc, x+sz.cx, y+sz.cy );
92             rect.top    = INTERNAL_YWPTODP( dc, x, y );
93             rect.bottom = INTERNAL_YWPTODP( dc, x+sz.cx, y+sz.cy );
94         }
95         else
96         {
97             rect.left   = INTERNAL_XWPTODP( dc, lprect->left, lprect->top );
98             rect.right  = INTERNAL_XWPTODP( dc, lprect->right, lprect->bottom );
99             rect.top    = INTERNAL_YWPTODP( dc, lprect->left, lprect->top );
100             rect.bottom = INTERNAL_YWPTODP( dc, lprect->right, lprect->bottom );
101         }
102         if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
103         if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
104     }
105
106     x = INTERNAL_XWPTODP( dc, x, y );
107     y = INTERNAL_YWPTODP( dc, x, y );
108
109     TRACE("\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
110                           x, y, rect.left, rect.top, rect.right, rect.bottom);
111
112       /* Draw the rectangle */
113
114     if (flags & ETO_OPAQUE)
115     {
116         X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
117         dibUpdateFlag = TRUE;
118         TSXSetForeground( display, physDev->gc, physDev->backgroundPixel );
119         TSXFillRectangle( display, physDev->drawable, physDev->gc,
120                         dc->DCOrgX + rect.left, dc->DCOrgY + rect.top,
121                         rect.right-rect.left, rect.bottom-rect.top );
122     }
123     if (!count) goto END;  /* Nothing more to do */
124
125       /* Compute text starting position */
126
127     if (lpDx) /* have explicit character cell x offsets in logical coordinates */
128     {
129         for (i = width = 0; i < count; i++) width += lpDx[i];
130         width = INTERNAL_XWSTODS(dc, width);
131     }
132     else
133     {
134         SIZE sz;
135         if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
136             return FALSE;
137         width = INTERNAL_XWSTODS(dc, sz.cx);
138     }
139     ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
140     descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
141     xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
142       pfo->lpX11Trans->pixelsize : width;
143     ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
144       pfo->lpX11Trans->pixelsize : 0;
145
146     switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
147     {
148       case TA_LEFT:
149           if (dc->textAlign & TA_UPDATECP) {
150               dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
151               dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
152           }
153           break;
154       case TA_RIGHT:
155           x -= xwidth;
156           y += ywidth;
157           if (dc->textAlign & TA_UPDATECP) {
158               dc->CursPosX = INTERNAL_XDPTOWP( dc, x, y );
159               dc->CursPosY = INTERNAL_YDPTOWP( dc, x, y );
160           }
161           break;
162       case TA_CENTER:
163           x -= xwidth / 2;
164           y += ywidth / 2;
165           break;
166     }
167
168     switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
169     {
170       case TA_TOP:
171           x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
172             pfo->lpX11Trans->pixelsize : 0;
173           y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
174             pfo->lpX11Trans->pixelsize : ascent;
175           break;
176       case TA_BOTTOM:
177           x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
178             pfo->lpX11Trans->pixelsize : 0;
179           y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
180             pfo->lpX11Trans->pixelsize : descent;
181           break;
182       case TA_BASELINE:
183           break;
184     }
185
186       /* Set the clip region */
187
188     if (flags & ETO_CLIPPED)
189     {
190         SaveVisRgn16( dc->hSelf );
191         CLIPPING_IntersectVisRect( dc, rect.left, rect.top, rect.right,
192                                    rect.bottom, FALSE );
193     }
194
195       /* Draw the text background if necessary */
196
197     if (!dibUpdateFlag)
198     {
199         X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
200         dibUpdateFlag = TRUE;
201     }
202
203     if (dc->backgroundMode != TRANSPARENT)
204     {
205           /* If rectangle is opaque and clipped, do nothing */
206         if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
207         {
208               /* Only draw if rectangle is not opaque or if some */
209               /* text is outside the rectangle */
210             if (!(flags & ETO_OPAQUE) ||
211                 (x < rect.left) ||
212                 (x + width >= rect.right) ||
213                 (y - ascent < rect.top) ||
214                 (y + descent >= rect.bottom))
215             {
216                 TSXSetForeground( display, physDev->gc,
217                                   physDev->backgroundPixel );
218                 TSXFillRectangle( display, physDev->drawable, physDev->gc,
219                                 dc->DCOrgX + x,
220                                 dc->DCOrgY + y - ascent,
221                                 width,
222                                 ascent + descent );
223             }
224         }
225     }
226     
227     /* Draw the text (count > 0 verified) */
228     if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
229         goto FAIL;
230
231     TSXSetForeground( display, physDev->gc, physDev->textPixel );
232     if(!rotated)
233     {
234       if (!dc->charExtra && !dc->breakExtra && !lpDx)
235       {
236         X11DRV_cptable[pfo->fi->cptable].pDrawString(
237                 pfo, display, physDev->drawable, physDev->gc,
238                 dc->DCOrgX + x, dc->DCOrgY + y, str2b, count );
239       }
240       else  /* Now the fun begins... */
241       {
242         XTextItem16 *items, *pitem;
243         int delta;
244
245         /* allocate max items */
246
247         pitem = items = HeapAlloc( GetProcessHeap(), 0,
248                                    count * sizeof(XTextItem16) );
249         if(items == NULL) goto FAIL; 
250         delta = i = 0;
251         if( lpDx ) /* explicit character widths */
252         {
253             long ve_we;
254             unsigned short err = 0;
255
256             ve_we = (LONG)(dc->xformWorld2Vport.eM11 * 0x10000);
257
258             while (i < count)
259             {
260                 /* initialize text item with accumulated delta */
261
262                 long sum;
263                 long fSum;
264                 sum = 0;
265                 pitem->chars  = str2b + i;
266                 pitem->delta  = delta;
267                 pitem->nchars = 0;
268                 pitem->font   = None;
269                 delta = 0;
270
271                 /* add characters to the same XTextItem
272                  * until new delta becomes non-zero */
273
274                 do
275                 {
276                     sum += lpDx[i];
277                     fSum = sum*ve_we+err;
278                     delta = SHIWORD(fSum)
279                       - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
280                                                 pfo, pitem->chars, pitem->nchars+1);
281                     pitem->nchars++;
282                 } while ((++i < count) && !delta);
283                 pitem++;
284                 err = LOWORD(fSum);
285            }
286         }
287         else /* charExtra or breakExtra */
288         {
289             while (i < count)
290             {
291                 pitem->chars  = str2b + i;
292                 pitem->delta  = delta;
293                 pitem->nchars = 0;
294                 pitem->font   = None;
295                 delta = 0;
296
297                 do
298                 {
299                     delta += dc->charExtra;
300                     if (str2b[i].byte2 == (char)dfBreakChar)
301                       delta += dc->breakExtra;
302                     pitem->nchars++;
303                 } while ((++i < count) && !delta);
304                 pitem++;
305             } 
306         }
307
308         X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, display,
309                 physDev->drawable, physDev->gc,
310                 dc->DCOrgX + x, dc->DCOrgY + y, items, pitem - items );
311         HeapFree( GetProcessHeap(), 0, items );
312       }
313     }
314     else /* rotated */
315     {  
316       /* have to render character by character. */
317       double offset = 0.0;
318       int i;
319
320       for (i=0; i<count; i++)
321       {
322         int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8) 
323           - font->min_char_or_byte2;
324         int x_i = IROUND((double) (dc->DCOrgX + x) + offset *
325                          pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
326         int y_i = IROUND((double) (dc->DCOrgY + y) - offset *
327                          pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
328
329         X11DRV_cptable[pfo->fi->cptable].pDrawString(
330                 pfo, display, physDev->drawable, physDev->gc,
331                 x_i, y_i, &str2b[i], 1);
332         if (lpDx)
333         {
334           offset += INTERNAL_XWSTODS(dc, lpDx[i]);
335         }
336         else
337         {
338           offset += (double) (font->per_char ?
339                               font->per_char[char_metric_offset].attributes:
340                               font->min_bounds.attributes)
341                           * pfo->lpX11Trans->pixelsize / 1000.0;
342           offset += dc->charExtra;
343           if (str2b[i].byte2 == (char)dfBreakChar)
344             offset += dc->breakExtra;
345         }
346       }
347     }
348     HeapFree( GetProcessHeap(), 0, str2b );
349
350       /* Draw underline and strike-out if needed */
351
352     if (lfUnderline)
353     {
354         long linePos, lineWidth;       
355
356         if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
357             linePos = descent - 1;
358         if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
359             lineWidth = 0;
360         else if (lineWidth == 1) lineWidth = 0;
361         TSXSetLineAttributes( display, physDev->gc, lineWidth,
362                               LineSolid, CapRound, JoinBevel ); 
363         TSXDrawLine( display, physDev->drawable, physDev->gc,
364                      dc->DCOrgX + x, dc->DCOrgY + y + linePos,
365                      dc->DCOrgX + x + width, dc->DCOrgY + y + linePos );
366     }
367     if (lfStrikeOut)
368     {
369         long lineAscent, lineDescent;
370         if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
371             lineAscent = ascent / 2;
372         if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
373             lineDescent = -lineAscent * 2 / 3;
374         TSXSetLineAttributes( display, physDev->gc, lineAscent + lineDescent,
375                             LineSolid, CapRound, JoinBevel ); 
376         TSXDrawLine( display, physDev->drawable, physDev->gc,
377                    dc->DCOrgX + x, dc->DCOrgY + y - lineAscent,
378                    dc->DCOrgX + x + width, dc->DCOrgY + y - lineAscent );
379     }
380
381     if (flags & ETO_CLIPPED) 
382         RestoreVisRgn16( dc->hSelf );
383
384     goto END;
385             
386 FAIL:
387     if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
388     result = FALSE;
389     
390 END:
391     if (dibUpdateFlag) X11DRV_UnlockDIBSection( dc, TRUE );
392     return result;
393 }
394
395
396 /***********************************************************************
397  *           X11DRV_GetTextExtentPoint
398  */
399 BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
400                                   LPSIZE size )
401 {
402     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
403     fontObject* pfo = XFONT_GetFontObject( physDev->font );
404
405     TRACE("%s %d\n", debugstr_wn(str,count), count);
406     if( pfo ) {
407         XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
408         if (!p) return FALSE;
409         if( !pfo->lpX11Trans ) {
410             int dir, ascent, descent;
411             int info_width;
412             X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
413                                 count, &dir, &ascent, &descent, &info_width );
414
415           size->cx = fabs((FLOAT)(info_width + dc->breakRem + count *
416                                   dc->charExtra) * dc->xformVport2World.eM11);
417           size->cy = fabs((FLOAT)(pfo->fs->ascent + pfo->fs->descent) *
418                           dc->xformVport2World.eM22);
419         } else {
420             INT i;
421             float x = 0.0, y = 0.0;
422             /* FIXME: Deal with *_char_or_byte2 != 0 situations */
423             for(i = 0; i < count; i++) {
424                 x += pfo->fs->per_char ? 
425            pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes : 
426            pfo->fs->min_bounds.attributes;
427             }
428             y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
429             TRACE("x = %f y = %f\n", x, y);
430             x *= pfo->lpX11Trans->pixelsize / 1000.0;
431             y *= pfo->lpX11Trans->pixelsize / 1000.0; 
432             size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
433                             dc->xformVport2World.eM11);
434             size->cy = fabs(y * dc->xformVport2World.eM22);
435         }
436         size->cx *= pfo->rescale;
437         size->cy *= pfo->rescale;
438         HeapFree( GetProcessHeap(), 0, p );
439         return TRUE;
440     }
441     return FALSE;
442 }