Added a first-cut version of MapVirtualKeyExW() that has the same
[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 "debugtools.h"
22
23 DEFAULT_DEBUG_CHANNEL(text);
24
25 #define SWAP_INT(a,b)  { int t = a; a = b; b = t; }
26 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
27
28
29 /***********************************************************************
30  *           X11DRV_ExtTextOut
31  */
32 BOOL
33 X11DRV_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
34                    const RECT *lprect, LPCWSTR wstr, UINT count,
35                    const INT *lpDx )
36 {
37     int                 i;
38     fontObject*         pfo;
39     INT                 width, ascent, descent, xwidth, ywidth;
40     XFontStruct*        font;
41     RECT                rect;
42     char                dfBreakChar, lfUnderline, lfStrikeOut;
43     BOOL                rotated = FALSE;
44     X11DRV_PDEVICE      *physDev = (X11DRV_PDEVICE *)dc->physDev;
45     XChar2b             *str2b = NULL;
46     BOOL                dibUpdateFlag = FALSE;
47     BOOL                result = TRUE; 
48
49     if (!X11DRV_SetupGCForText( dc )) return TRUE;
50
51     pfo = XFONT_GetFontObject( physDev->font );
52     font = pfo->fs;
53      
54     if (pfo->lf.lfEscapement && pfo->lpX11Trans)
55         rotated = TRUE;
56     dfBreakChar = (char)pfo->fi->df.dfBreakChar;
57     lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
58     lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
59
60     TRACE("hdc=%04x df=%04x %d,%d %s, %d  flags=%d lpDx=%p\n",
61           dc->hSelf, (UINT16)(physDev->font), x, y,
62           debugstr_wn (wstr, count), count, flags, lpDx);
63
64     /* some strings sent here end in a newline for whatever reason.  I have no
65        clue what the right treatment should be in general, but ignoring
66        terminating newlines seems ok.  MW, April 1998.  */
67     if (count > 0 && wstr[count - 1] == '\n') count--;
68
69     if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
70                                      lprect->left, lprect->top,
71                                      lprect->right, lprect->bottom );
72       /* Setup coordinates */
73
74     if (dc->textAlign & TA_UPDATECP)
75     {
76         x = dc->CursPosX;
77         y = dc->CursPosY;
78     }
79
80     if (flags & (ETO_OPAQUE | ETO_CLIPPED))  /* there's a rectangle */
81     {
82         if (!lprect)  /* not always */
83         {
84             SIZE sz;
85             if (flags & ETO_CLIPPED)  /* Can't clip with no rectangle */
86               return FALSE;
87             if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
88               return FALSE;
89             rect.left   = XLPTODP( dc, x );
90             rect.right  = XLPTODP( dc, x+sz.cx );
91             rect.top    = YLPTODP( dc, y );
92             rect.bottom = YLPTODP( dc, y+sz.cy );
93         }
94         else
95         {
96             rect.left   = XLPTODP( dc, lprect->left );
97             rect.right  = XLPTODP( dc, lprect->right );
98             rect.top    = YLPTODP( dc, lprect->top );
99             rect.bottom = YLPTODP( dc, lprect->bottom );
100         }
101         if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
102         if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
103     }
104
105     x = XLPTODP( dc, x );
106     y = YLPTODP( dc, y );
107
108     TRACE("\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
109                           x, y, rect.left, rect.top, rect.right, rect.bottom);
110
111       /* Draw the rectangle */
112
113     if (flags & ETO_OPAQUE)
114     {
115         X11DRV_DIB_UpdateDIBSection( dc, FALSE );
116         dibUpdateFlag = TRUE;
117         TSXSetForeground( display, physDev->gc, physDev->backgroundPixel );
118         TSXFillRectangle( display, physDev->drawable, physDev->gc,
119                         dc->DCOrgX + rect.left, dc->DCOrgY + rect.top,
120                         rect.right-rect.left, rect.bottom-rect.top );
121     }
122     if (!count) goto END;  /* Nothing more to do */
123
124       /* Compute text starting position */
125
126     if (lpDx) /* have explicit character cell x offsets in logical coordinates */
127     {
128         int extra = dc->wndExtX / 2;
129         for (i = width = 0; i < count; i++) width += lpDx[i];
130         width = (width * dc->vportExtX + extra ) / dc->wndExtX;
131     }
132     else
133     {
134         SIZE sz;
135         if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
136             return FALSE;
137         width = XLSTODS(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 = XDPTOLP( dc, x + xwidth );
151               dc->CursPosY = YDPTOLP( dc, y - ywidth );
152           }
153           break;
154       case TA_RIGHT:
155           x -= xwidth;
156           y += ywidth;
157           if (dc->textAlign & TA_UPDATECP) {
158               dc->CursPosX = XDPTOLP( dc, x );
159               dc->CursPosY = YDPTOLP( dc, 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_DIB_UpdateDIBSection( dc, 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             const long ve_we = dc->vportExtX*0x10000 / dc->wndExtX;
254             unsigned short err = 0;
255
256             while (i < count)
257             {
258                 /* initialize text item with accumulated delta */
259
260                 long sum;
261                 long fSum;
262                 sum = 0;
263                 pitem->chars  = str2b + i;
264                 pitem->delta  = delta;
265                 pitem->nchars = 0;
266                 pitem->font   = None;
267                 delta = 0;
268
269                 /* add characters to the same XTextItem
270                  * until new delta becomes non-zero */
271
272                 do
273                 {
274                     sum += lpDx[i];
275                     fSum = sum*ve_we+err;
276                     delta = SHIWORD(fSum)
277                       - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
278                                                 pfo, pitem->chars, pitem->nchars+1);
279                     pitem->nchars++;
280                 } while ((++i < count) && !delta);
281                 pitem++;
282                 err = LOWORD(fSum);
283            }
284         }
285         else /* charExtra or breakExtra */
286         {
287             while (i < count)
288             {
289                 pitem->chars  = str2b + i;
290                 pitem->delta  = delta;
291                 pitem->nchars = 0;
292                 pitem->font   = None;
293                 delta = 0;
294
295                 do
296                 {
297                     delta += dc->charExtra;
298                     if (str2b[i].byte2 == (char)dfBreakChar)
299                       delta += dc->breakExtra;
300                     pitem->nchars++;
301                 } while ((++i < count) && !delta);
302                 pitem++;
303             } 
304         }
305
306         X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, display,
307                 physDev->drawable, physDev->gc,
308                 dc->DCOrgX + x, dc->DCOrgY + y, items, pitem - items );
309         HeapFree( GetProcessHeap(), 0, items );
310       }
311     }
312     else /* rotated */
313     {  
314       /* have to render character by character. */
315       double offset = 0.0;
316       int i;
317
318       for (i=0; i<count; i++)
319       {
320         int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8) 
321           - font->min_char_or_byte2;
322         int x_i = IROUND((double) (dc->DCOrgX + x) + offset *
323                          pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
324         int y_i = IROUND((double) (dc->DCOrgY + y) - offset *
325                          pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
326
327         X11DRV_cptable[pfo->fi->cptable].pDrawString(
328                 pfo, display, physDev->drawable, physDev->gc,
329                 x_i, y_i, &str2b[i], 1);
330         if (lpDx)
331           offset += XLSTODS(dc, lpDx[i]);
332         else
333         {
334           offset += (double) (font->per_char ?
335                               font->per_char[char_metric_offset].attributes:
336                               font->min_bounds.attributes)
337                           * pfo->lpX11Trans->pixelsize / 1000.0;
338           offset += dc->charExtra;
339           if (str2b[i].byte2 == (char)dfBreakChar)
340             offset += dc->breakExtra;
341         }
342       }
343     }
344     HeapFree( GetProcessHeap(), 0, str2b );
345
346       /* Draw underline and strike-out if needed */
347
348     if (lfUnderline)
349     {
350         long linePos, lineWidth;       
351
352         if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
353             linePos = descent - 1;
354         if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
355             lineWidth = 0;
356         else if (lineWidth == 1) lineWidth = 0;
357         TSXSetLineAttributes( display, physDev->gc, lineWidth,
358                               LineSolid, CapRound, JoinBevel ); 
359         TSXDrawLine( display, physDev->drawable, physDev->gc,
360                      dc->DCOrgX + x, dc->DCOrgY + y + linePos,
361                      dc->DCOrgX + x + width, dc->DCOrgY + y + linePos );
362     }
363     if (lfStrikeOut)
364     {
365         long lineAscent, lineDescent;
366         if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
367             lineAscent = ascent / 2;
368         if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
369             lineDescent = -lineAscent * 2 / 3;
370         TSXSetLineAttributes( display, physDev->gc, lineAscent + lineDescent,
371                             LineSolid, CapRound, JoinBevel ); 
372         TSXDrawLine( display, physDev->drawable, physDev->gc,
373                    dc->DCOrgX + x, dc->DCOrgY + y - lineAscent,
374                    dc->DCOrgX + x + width, dc->DCOrgY + y - lineAscent );
375     }
376
377     if (flags & ETO_CLIPPED) 
378         RestoreVisRgn16( dc->hSelf );
379
380     goto END;
381             
382 FAIL:
383     if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
384     result = FALSE;
385     
386 END:
387     if (dibUpdateFlag) X11DRV_DIB_UpdateDIBSection( dc, TRUE );
388     return result;
389 }
390
391
392 /***********************************************************************
393  *           X11DRV_GetTextExtentPoint
394  */
395 BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
396                                   LPSIZE size )
397 {
398     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
399     fontObject* pfo = XFONT_GetFontObject( physDev->font );
400
401     TRACE("%s %d\n", debugstr_wn(str,count), count);
402     if( pfo ) {
403         XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
404         if (!p) return FALSE;
405         if( !pfo->lpX11Trans ) {
406             int dir, ascent, descent;
407             int info_width;
408             X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
409                                 count, &dir, &ascent, &descent, &info_width );
410             size->cx = abs((info_width + dc->breakRem + count * 
411                             dc->charExtra) * dc->wndExtX / dc->vportExtX);
412             size->cy = abs((pfo->fs->ascent + pfo->fs->descent) * 
413                            dc->wndExtY / dc->vportExtY);
414         } else {
415             INT i;
416             float x = 0.0, y = 0.0;
417             /* FIXME: Deal with *_char_or_byte2 != 0 situations */
418             for(i = 0; i < count; i++) {
419                 x += pfo->fs->per_char ? 
420            pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes : 
421            pfo->fs->min_bounds.attributes;
422             }
423             y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
424             TRACE("x = %f y = %f\n", x, y);
425             x *= pfo->lpX11Trans->pixelsize / 1000.0;
426             y *= pfo->lpX11Trans->pixelsize / 1000.0; 
427             size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
428                              dc->wndExtX / dc->vportExtX);
429             size->cy = fabs(y * dc->wndExtY / dc->vportExtY);
430         }
431         size->cx *= pfo->rescale;
432         size->cy *= pfo->rescale;
433         HeapFree( GetProcessHeap(), 0, p );
434         return TRUE;
435     }
436     return FALSE;
437 }