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