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