Pass the breakExtra value in the ExtTextOut driver call, since there
[wine/multimedia.git] / dlls / x11drv / text.c
blob4b0fbf18fd53cb28f972ce883d57c2c04f13ae4c
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 <stdarg.h>
24 #include <stdlib.h>
25 #include <math.h>
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"
35 WINE_DEFAULT_DEBUG_CHANNEL(text);
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)
41 /***********************************************************************
42 * X11DRV_ExtTextOut
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, INT breakExtra )
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 HRGN saved_region = 0;
60 POINT pt;
61 UINT align = GetTextAlign( physDev->hdc );
62 INT charExtra = GetTextCharacterExtra( physDev->hdc );
64 if(physDev->dc->gdiFont)
65 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx, breakExtra);
67 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
69 pfo = XFONT_GetFontObject( physDev->font );
70 font = pfo->fs;
72 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
73 rotated = TRUE;
74 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
75 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
76 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
78 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
79 physDev->hdc, (UINT16)(physDev->font), x, y,
80 debugstr_wn (wstr, count), count, flags, lpDx);
82 /* some strings sent here end in a newline for whatever reason. I have no
83 clue what the right treatment should be in general, but ignoring
84 terminating newlines seems ok. MW, April 1998. */
85 if (count > 0 && wstr[count - 1] == '\n') count--;
87 if (lprect != NULL) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
88 lprect->left, lprect->top,
89 lprect->right, lprect->bottom );
90 /* Setup coordinates */
92 if (align & TA_UPDATECP)
94 GetCurrentPositionEx( physDev->hdc, &pt );
95 x = pt.x;
96 y = pt.y;
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 = x;
109 rect.right = x + sz.cx;
110 rect.top = y;
111 rect.bottom = y + sz.cy;
113 else
115 rect = *lprect;
117 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
119 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
120 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
123 pt.x = x;
124 pt.y = y;
125 LPtoDP(physDev->hdc, &pt, 1);
126 x = pt.x;
127 y = pt.y;
129 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
130 x, y, rect.left, rect.top, rect.right, rect.bottom);
132 /* Draw the rectangle */
134 if (flags & ETO_OPAQUE)
136 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
137 dibUpdateFlag = TRUE;
138 wine_tsx11_lock();
139 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
140 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
141 physDev->org.x + rect.left, physDev->org.y + rect.top,
142 rect.right-rect.left, rect.bottom-rect.top );
143 wine_tsx11_unlock();
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 = X11DRV_XWStoDS(physDev, width);
154 else
156 SIZE sz;
157 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
159 result = FALSE;
160 goto END;
162 width = X11DRV_XWStoDS(physDev, 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( align & (TA_LEFT | TA_RIGHT | TA_CENTER) )
173 case TA_LEFT:
174 if (align & TA_UPDATECP) {
175 pt.x = x + xwidth;
176 pt.y = y - ywidth;
177 DPtoLP(physDev->hdc, &pt, 1);
178 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
180 break;
181 case TA_RIGHT:
182 x -= xwidth;
183 y += ywidth;
184 if (align & TA_UPDATECP) {
185 pt.x = x;
186 pt.y = y;
187 DPtoLP(physDev->hdc, &pt, 1);
188 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
190 break;
191 case TA_CENTER:
192 x -= xwidth / 2;
193 y += ywidth / 2;
194 break;
197 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
199 case TA_TOP:
200 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
201 pfo->lpX11Trans->pixelsize : 0;
202 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
203 pfo->lpX11Trans->pixelsize : ascent;
204 break;
205 case TA_BOTTOM:
206 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
207 pfo->lpX11Trans->pixelsize : 0;
208 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
209 pfo->lpX11Trans->pixelsize : descent;
210 break;
211 case TA_BASELINE:
212 break;
215 /* Set the clip region */
217 if (flags & ETO_CLIPPED)
219 HRGN clip_region;
220 RECT clip_rect = *lprect;
222 LPtoDP( physDev->hdc, (POINT *)&clip_rect, 2 );
223 clip_region = CreateRectRgnIndirect( &clip_rect );
224 /* make a copy of the current device region */
225 saved_region = CreateRectRgn( 0, 0, 0, 0 );
226 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
227 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
228 DeleteObject( clip_region );
231 /* Draw the text background if necessary */
233 if (!dibUpdateFlag)
235 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
236 dibUpdateFlag = TRUE;
239 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
241 /* If rectangle is opaque and clipped, do nothing */
242 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
244 /* Only draw if rectangle is not opaque or if some */
245 /* text is outside the rectangle */
246 if (!(flags & ETO_OPAQUE) ||
247 (x < rect.left) ||
248 (x + width >= rect.right) ||
249 (y - ascent < rect.top) ||
250 (y + descent >= rect.bottom))
252 wine_tsx11_lock();
253 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
254 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
255 physDev->org.x + x, physDev->org.y + y - ascent,
256 width, ascent + descent );
257 wine_tsx11_unlock();
262 /* Draw the text (count > 0 verified) */
263 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
264 goto FAIL;
266 wine_tsx11_lock();
267 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
268 wine_tsx11_unlock();
269 if(!rotated)
271 if (!charExtra && !breakExtra && !lpDx)
273 X11DRV_cptable[pfo->fi->cptable].pDrawString(
274 pfo, gdi_display, physDev->drawable, physDev->gc,
275 physDev->org.x + x, physDev->org.y + y, str2b, count );
277 else /* Now the fun begins... */
279 XTextItem16 *items, *pitem;
280 int delta;
282 /* allocate max items */
284 pitem = items = HeapAlloc( GetProcessHeap(), 0,
285 count * sizeof(XTextItem16) );
286 if(items == NULL) goto FAIL;
287 delta = i = 0;
288 if( lpDx ) /* explicit character widths */
290 long ve_we;
291 unsigned short err = 0;
293 ve_we = (LONG)(physDev->dc->xformWorld2Vport.eM11 * 0x10000);
295 while (i < count)
297 /* initialize text item with accumulated delta */
299 long sum;
300 long fSum;
301 sum = 0;
302 pitem->chars = str2b + i;
303 pitem->delta = delta;
304 pitem->nchars = 0;
305 pitem->font = None;
306 delta = 0;
308 /* add characters to the same XTextItem
309 * until new delta becomes non-zero */
313 sum += lpDx[i];
314 fSum = sum*ve_we+err;
315 delta = (short)HIWORD(fSum)
316 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
317 pfo, pitem->chars, pitem->nchars+1);
318 pitem->nchars++;
319 } while ((++i < count) && !delta);
320 pitem++;
321 err = LOWORD(fSum);
324 else /* charExtra or breakExtra */
326 while (i < count)
328 pitem->chars = str2b + i;
329 pitem->delta = delta;
330 pitem->nchars = 0;
331 pitem->font = None;
332 delta = 0;
336 delta += charExtra;
337 if (str2b[i].byte2 == (char)dfBreakChar)
338 delta += breakExtra;
339 pitem->nchars++;
340 } while ((++i < count) && !delta);
341 pitem++;
345 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
346 physDev->drawable, physDev->gc,
347 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
348 HeapFree( GetProcessHeap(), 0, items );
351 else /* rotated */
353 /* have to render character by character. */
354 double offset = 0.0;
355 int i;
357 for (i=0; i<count; i++)
359 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
360 - font->min_char_or_byte2;
361 int x_i = IROUND((double) (physDev->org.x + x) + offset *
362 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
363 int y_i = IROUND((double) (physDev->org.y + y) - offset *
364 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
366 X11DRV_cptable[pfo->fi->cptable].pDrawString(
367 pfo, gdi_display, physDev->drawable, physDev->gc,
368 x_i, y_i, &str2b[i], 1);
369 if (lpDx)
371 offset += X11DRV_XWStoDS(physDev, lpDx[i]);
373 else
375 offset += (double) (font->per_char ?
376 font->per_char[char_metric_offset].attributes:
377 font->min_bounds.attributes)
378 * pfo->lpX11Trans->pixelsize / 1000.0;
379 offset += charExtra;
380 if (str2b[i].byte2 == (char)dfBreakChar)
381 offset += breakExtra;
385 HeapFree( GetProcessHeap(), 0, str2b );
387 /* Draw underline and strike-out if needed */
389 wine_tsx11_lock();
390 if (lfUnderline)
392 long linePos, lineWidth;
394 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
395 linePos = descent - 1;
396 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
397 lineWidth = 0;
398 else if (lineWidth == 1) lineWidth = 0;
399 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
400 LineSolid, CapRound, JoinBevel );
401 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
402 physDev->org.x + x, physDev->org.y + y + linePos,
403 physDev->org.x + x + width, physDev->org.y + y + linePos );
405 if (lfStrikeOut)
407 long lineAscent, lineDescent;
408 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
409 lineAscent = ascent / 2;
410 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
411 lineDescent = -lineAscent * 2 / 3;
412 XSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
413 LineSolid, CapRound, JoinBevel );
414 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
415 physDev->org.x + x, physDev->org.y + y - lineAscent,
416 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
418 wine_tsx11_unlock();
420 if (flags & ETO_CLIPPED)
422 /* restore the device region */
423 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
424 DeleteObject( saved_region );
426 goto END;
428 FAIL:
429 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
430 result = FALSE;
432 END:
433 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
434 return result;
438 /***********************************************************************
439 * X11DRV_GetTextExtentPoint
441 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
442 LPSIZE size )
444 fontObject* pfo = XFONT_GetFontObject( physDev->font );
446 TRACE("%s %d\n", debugstr_wn(str,count), count);
447 if( pfo ) {
448 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
449 if (!p) return FALSE;
450 if( !pfo->lpX11Trans ) {
451 int dir, ascent, descent;
452 int info_width;
453 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
454 count, &dir, &ascent, &descent, &info_width );
456 size->cx = info_width;
457 size->cy = pfo->fs->ascent + pfo->fs->descent;
458 } else {
459 INT i;
460 float x = 0.0, y = 0.0;
461 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
462 for(i = 0; i < count; i++) {
463 x += pfo->fs->per_char ?
464 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
465 pfo->fs->min_bounds.attributes;
467 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
468 TRACE("x = %f y = %f\n", x, y);
469 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
470 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
472 size->cx *= pfo->rescale;
473 size->cy *= pfo->rescale;
474 HeapFree( GetProcessHeap(), 0, p );
475 return TRUE;
477 return FALSE;