Make the testing framework thread safe.
[wine/gsoc_dplay.git] / graphics / x11drv / text.c
blobfbf5d961caa87019779878982ecc09e362c23239
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 "gdi.h"
33 #include "x11font.h"
34 #include "bitmap.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(text);
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)
43 /***********************************************************************
44 * X11DRV_ExtTextOut
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 )
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;
63 if(dc->gdiFont)
64 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count,
65 lpDx);
68 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
70 pfo = XFONT_GetFontObject( physDev->font );
71 font = pfo->fs;
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;
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);
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--;
88 if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
89 lprect->left, lprect->top,
90 lprect->right, lprect->bottom );
91 /* Setup coordinates */
93 if (dc->textAlign & TA_UPDATECP)
95 x = dc->CursPosX;
96 y = dc->CursPosY;
99 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
101 if (!lprect) /* not always */
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 );
113 else
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 );
120 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
121 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
124 x = INTERNAL_XWPTODP( dc, x, y );
125 y = INTERNAL_YWPTODP( dc, x, y );
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);
130 /* Draw the rectangle */
132 if (flags & ETO_OPAQUE)
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 dc->DCOrgX + rect.left, dc->DCOrgY + rect.top,
139 rect.right-rect.left, rect.bottom-rect.top );
141 if (!count) goto END; /* Nothing more to do */
143 /* Compute text starting position */
145 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
147 for (i = width = 0; i < count; i++) width += lpDx[i];
148 width = INTERNAL_XWSTODS(dc, width);
150 else
152 SIZE sz;
153 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
154 return FALSE;
155 width = INTERNAL_XWSTODS(dc, sz.cx);
157 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
158 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
159 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
160 pfo->lpX11Trans->pixelsize : width;
161 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
162 pfo->lpX11Trans->pixelsize : 0;
164 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
166 case TA_LEFT:
167 if (dc->textAlign & TA_UPDATECP) {
168 dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
169 dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
171 break;
172 case TA_RIGHT:
173 x -= xwidth;
174 y += ywidth;
175 if (dc->textAlign & TA_UPDATECP) {
176 dc->CursPosX = INTERNAL_XDPTOWP( dc, x, y );
177 dc->CursPosY = INTERNAL_YDPTOWP( dc, x, y );
179 break;
180 case TA_CENTER:
181 x -= xwidth / 2;
182 y += ywidth / 2;
183 break;
186 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
188 case TA_TOP:
189 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
190 pfo->lpX11Trans->pixelsize : 0;
191 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
192 pfo->lpX11Trans->pixelsize : ascent;
193 break;
194 case TA_BOTTOM:
195 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
196 pfo->lpX11Trans->pixelsize : 0;
197 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
198 pfo->lpX11Trans->pixelsize : descent;
199 break;
200 case TA_BASELINE:
201 break;
204 /* Set the clip region */
206 if (flags & ETO_CLIPPED)
208 SaveVisRgn16( dc->hSelf );
209 CLIPPING_IntersectVisRect( dc, rect.left, rect.top, rect.right,
210 rect.bottom, FALSE );
213 /* Draw the text background if necessary */
215 if (!dibUpdateFlag)
217 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
218 dibUpdateFlag = TRUE;
221 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
223 /* If rectangle is opaque and clipped, do nothing */
224 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
226 /* Only draw if rectangle is not opaque or if some */
227 /* text is outside the rectangle */
228 if (!(flags & ETO_OPAQUE) ||
229 (x < rect.left) ||
230 (x + width >= rect.right) ||
231 (y - ascent < rect.top) ||
232 (y + descent >= rect.bottom))
234 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
235 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
236 dc->DCOrgX + x,
237 dc->DCOrgY + y - ascent,
238 width,
239 ascent + descent );
244 /* Draw the text (count > 0 verified) */
245 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
246 goto FAIL;
248 TSXSetForeground( gdi_display, physDev->gc, physDev->textPixel );
249 if(!rotated)
251 if (!dc->charExtra && !dc->breakExtra && !lpDx)
253 X11DRV_cptable[pfo->fi->cptable].pDrawString(
254 pfo, gdi_display, physDev->drawable, physDev->gc,
255 dc->DCOrgX + x, dc->DCOrgY + y, str2b, count );
257 else /* Now the fun begins... */
259 XTextItem16 *items, *pitem;
260 int delta;
262 /* allocate max items */
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 */
270 long ve_we;
271 unsigned short err = 0;
273 ve_we = (LONG)(dc->xformWorld2Vport.eM11 * 0x10000);
275 while (i < count)
277 /* initialize text item with accumulated delta */
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;
288 /* add characters to the same XTextItem
289 * until new delta becomes non-zero */
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);
304 else /* charExtra or breakExtra */
306 while (i < count)
308 pitem->chars = str2b + i;
309 pitem->delta = delta;
310 pitem->nchars = 0;
311 pitem->font = None;
312 delta = 0;
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++;
325 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
326 physDev->drawable, physDev->gc,
327 dc->DCOrgX + x, dc->DCOrgY + y, items, pitem - items );
328 HeapFree( GetProcessHeap(), 0, items );
331 else /* rotated */
333 /* have to render character by character. */
334 double offset = 0.0;
335 int i;
337 for (i=0; i<count; i++)
339 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
340 - font->min_char_or_byte2;
341 int x_i = IROUND((double) (dc->DCOrgX + x) + offset *
342 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
343 int y_i = IROUND((double) (dc->DCOrgY + y) - offset *
344 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
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)
351 offset += INTERNAL_XWSTODS(dc, lpDx[i]);
353 else
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;
365 HeapFree( GetProcessHeap(), 0, str2b );
367 /* Draw underline and strike-out if needed */
369 if (lfUnderline)
371 long linePos, lineWidth;
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 dc->DCOrgX + x, dc->DCOrgY + y + linePos,
382 dc->DCOrgX + x + width, dc->DCOrgY + y + linePos );
384 if (lfStrikeOut)
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 dc->DCOrgX + x, dc->DCOrgY + y - lineAscent,
395 dc->DCOrgX + x + width, dc->DCOrgY + y - lineAscent );
398 if (flags & ETO_CLIPPED)
399 RestoreVisRgn16( dc->hSelf );
401 goto END;
403 FAIL:
404 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
405 result = FALSE;
407 END:
408 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
409 return result;
413 /***********************************************************************
414 * X11DRV_GetTextExtentPoint
416 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
417 LPSIZE size )
419 DC *dc = physDev->dc;
420 fontObject* pfo = XFONT_GetFontObject( physDev->font );
422 TRACE("%s %d\n", debugstr_wn(str,count), count);
423 if( pfo ) {
424 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
425 if (!p) return FALSE;
426 if( !pfo->lpX11Trans ) {
427 int dir, ascent, descent;
428 int info_width;
429 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
430 count, &dir, &ascent, &descent, &info_width );
432 size->cx = fabs((FLOAT)(info_width + dc->breakRem + count *
433 dc->charExtra) * dc->xformVport2World.eM11);
434 size->cy = fabs((FLOAT)(pfo->fs->ascent + pfo->fs->descent) *
435 dc->xformVport2World.eM22);
436 } else {
437 INT i;
438 float x = 0.0, y = 0.0;
439 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
440 for(i = 0; i < count; i++) {
441 x += pfo->fs->per_char ?
442 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
443 pfo->fs->min_bounds.attributes;
445 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
446 TRACE("x = %f y = %f\n", x, y);
447 x *= pfo->lpX11Trans->pixelsize / 1000.0;
448 y *= pfo->lpX11Trans->pixelsize / 1000.0;
449 size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
450 dc->xformVport2World.eM11);
451 size->cy = fabs(y * dc->xformVport2World.eM22);
453 size->cx *= pfo->rescale;
454 size->cy *= pfo->rescale;
455 HeapFree( GetProcessHeap(), 0, p );
456 return TRUE;
458 return FALSE;