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