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
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(text);
38 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
39 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
42 /***********************************************************************
46 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
47 const RECT *lprect, LPCWSTR wstr, UINT count,
52 INT width, ascent, descent, xwidth, ywidth;
55 char dfBreakChar, lfUnderline, lfStrikeOut;
57 XChar2b *str2b = NULL;
58 BOOL dibUpdateFlag = FALSE;
64 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count,
68 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
70 pfo = XFONT_GetFontObject( physDev->font );
73 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
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=%p 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=(%ld,%ld - %ld,%ld)\n",
89 lprect->left, lprect->top,
90 lprect->right, lprect->bottom );
91 /* Setup coordinates */
93 if (dc->textAlign & TA_UPDATECP)
99 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
101 if (!lprect) /* not always */
104 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
106 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
109 rect.right = x + sz.cx;
111 rect.bottom = y + sz.cy;
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 );
125 LPtoDP(physDev->hdc, &pt, 1);
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;
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 );
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);
157 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
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) )
174 if (dc->textAlign & TA_UPDATECP) {
177 DPtoLP(physDev->hdc, &pt, 1);
185 if (dc->textAlign & TA_UPDATECP) {
188 DPtoLP(physDev->hdc, &pt, 1);
199 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
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;
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;
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 */
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) ||
242 (x + width >= rect.right) ||
243 (y - ascent < rect.top) ||
244 (y + descent >= rect.bottom))
247 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
248 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
249 physDev->org.x + x, physDev->org.y + y - ascent,
250 width, ascent + descent );
256 /* Draw the text (count > 0 verified) */
257 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
261 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
265 if (!dc->charExtra && !dc->breakExtra && !lpDx)
267 X11DRV_cptable[pfo->fi->cptable].pDrawString(
268 pfo, gdi_display, physDev->drawable, physDev->gc,
269 physDev->org.x + x, physDev->org.y + y, str2b, count );
271 else /* Now the fun begins... */
273 XTextItem16 *items, *pitem;
276 /* allocate max items */
278 pitem = items = HeapAlloc( GetProcessHeap(), 0,
279 count * sizeof(XTextItem16) );
280 if(items == NULL) goto FAIL;
282 if( lpDx ) /* explicit character widths */
285 unsigned short err = 0;
287 ve_we = (LONG)(dc->xformWorld2Vport.eM11 * 0x10000);
291 /* initialize text item with accumulated delta */
296 pitem->chars = str2b + i;
297 pitem->delta = delta;
302 /* add characters to the same XTextItem
303 * until new delta becomes non-zero */
308 fSum = sum*ve_we+err;
309 delta = (short)HIWORD(fSum)
310 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
311 pfo, pitem->chars, pitem->nchars+1);
313 } while ((++i < count) && !delta);
318 else /* charExtra or breakExtra */
322 pitem->chars = str2b + i;
323 pitem->delta = delta;
330 delta += dc->charExtra;
331 if (str2b[i].byte2 == (char)dfBreakChar)
332 delta += dc->breakExtra;
334 } while ((++i < count) && !delta);
339 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
340 physDev->drawable, physDev->gc,
341 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
342 HeapFree( GetProcessHeap(), 0, items );
347 /* have to render character by character. */
351 for (i=0; i<count; i++)
353 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
354 - font->min_char_or_byte2;
355 int x_i = IROUND((double) (physDev->org.x + x) + offset *
356 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
357 int y_i = IROUND((double) (physDev->org.y + y) - offset *
358 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
360 X11DRV_cptable[pfo->fi->cptable].pDrawString(
361 pfo, gdi_display, physDev->drawable, physDev->gc,
362 x_i, y_i, &str2b[i], 1);
365 offset += INTERNAL_XWSTODS(dc, lpDx[i]);
369 offset += (double) (font->per_char ?
370 font->per_char[char_metric_offset].attributes:
371 font->min_bounds.attributes)
372 * pfo->lpX11Trans->pixelsize / 1000.0;
373 offset += dc->charExtra;
374 if (str2b[i].byte2 == (char)dfBreakChar)
375 offset += dc->breakExtra;
379 HeapFree( GetProcessHeap(), 0, str2b );
381 /* Draw underline and strike-out if needed */
386 long linePos, lineWidth;
388 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
389 linePos = descent - 1;
390 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
392 else if (lineWidth == 1) lineWidth = 0;
393 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
394 LineSolid, CapRound, JoinBevel );
395 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
396 physDev->org.x + x, physDev->org.y + y + linePos,
397 physDev->org.x + x + width, physDev->org.y + y + linePos );
401 long lineAscent, lineDescent;
402 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
403 lineAscent = ascent / 2;
404 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
405 lineDescent = -lineAscent * 2 / 3;
406 XSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
407 LineSolid, CapRound, JoinBevel );
408 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
409 physDev->org.x + x, physDev->org.y + y - lineAscent,
410 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
414 if (flags & ETO_CLIPPED) RestoreVisRgn16( HDC_16(dc->hSelf) );
418 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
422 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
427 /***********************************************************************
428 * X11DRV_GetTextExtentPoint
430 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
433 DC *dc = physDev->dc;
434 fontObject* pfo = XFONT_GetFontObject( physDev->font );
436 TRACE("%s %d\n", debugstr_wn(str,count), count);
438 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
439 if (!p) return FALSE;
440 if( !pfo->lpX11Trans ) {
441 int dir, ascent, descent;
443 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
444 count, &dir, &ascent, &descent, &info_width );
446 size->cx = fabs((FLOAT)(info_width + dc->breakRem + count *
447 dc->charExtra) * dc->xformVport2World.eM11);
448 size->cy = fabs((FLOAT)(pfo->fs->ascent + pfo->fs->descent) *
449 dc->xformVport2World.eM22);
452 float x = 0.0, y = 0.0;
453 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
454 for(i = 0; i < count; i++) {
455 x += pfo->fs->per_char ?
456 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
457 pfo->fs->min_bounds.attributes;
459 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
460 TRACE("x = %f y = %f\n", x, y);
461 x *= pfo->lpX11Trans->pixelsize / 1000.0;
462 y *= pfo->lpX11Trans->pixelsize / 1000.0;
463 size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
464 dc->xformVport2World.eM11);
465 size->cy = fabs(y * dc->xformVport2World.eM22);
467 size->cx *= pfo->rescale;
468 size->cy *= pfo->rescale;
469 HeapFree( GetProcessHeap(), 0, p );