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