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
23 #include <X11/Xatom.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 /***********************************************************************
47 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
48 const RECT *lprect, LPCWSTR wstr, UINT count,
53 INT width, ascent, descent, xwidth, ywidth;
56 char dfBreakChar, lfUnderline, lfStrikeOut;
58 XChar2b *str2b = NULL;
59 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=%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)
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 ))
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 );
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 physDev->org.x + rect.left, physDev->org.y + 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);
153 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
158 width = INTERNAL_XWSTODS(dc, sz.cx);
160 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
161 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
162 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
163 pfo->lpX11Trans->pixelsize : width;
164 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
165 pfo->lpX11Trans->pixelsize : 0;
167 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
170 if (dc->textAlign & TA_UPDATECP) {
171 dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
172 dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
178 if (dc->textAlign & TA_UPDATECP) {
179 dc->CursPosX = INTERNAL_XDPTOWP( dc, x, y );
180 dc->CursPosY = INTERNAL_YDPTOWP( dc, x, y );
189 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
192 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
193 pfo->lpX11Trans->pixelsize : 0;
194 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
195 pfo->lpX11Trans->pixelsize : ascent;
198 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
199 pfo->lpX11Trans->pixelsize : 0;
200 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
201 pfo->lpX11Trans->pixelsize : descent;
207 /* Set the clip region */
209 if (flags & ETO_CLIPPED)
211 SaveVisRgn16( dc->hSelf );
212 IntersectVisRect16( dc->hSelf, lprect->left, lprect->top, lprect->right, lprect->bottom );
215 /* Draw the text background if necessary */
219 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
220 dibUpdateFlag = TRUE;
223 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
225 /* If rectangle is opaque and clipped, do nothing */
226 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
228 /* Only draw if rectangle is not opaque or if some */
229 /* text is outside the rectangle */
230 if (!(flags & ETO_OPAQUE) ||
232 (x + width >= rect.right) ||
233 (y - ascent < rect.top) ||
234 (y + descent >= rect.bottom))
236 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
237 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
238 physDev->org.x + x, physDev->org.y + y - ascent,
239 width, ascent + descent );
244 /* Draw the text (count > 0 verified) */
245 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
248 TSXSetForeground( gdi_display, physDev->gc, physDev->textPixel );
251 if (!dc->charExtra && !dc->breakExtra && !lpDx)
253 X11DRV_cptable[pfo->fi->cptable].pDrawString(
254 pfo, gdi_display, physDev->drawable, physDev->gc,
255 physDev->org.x + x, physDev->org.y + y, str2b, count );
257 else /* Now the fun begins... */
259 XTextItem16 *items, *pitem;
262 /* allocate max items */
264 pitem = items = HeapAlloc( GetProcessHeap(), 0,
265 count * sizeof(XTextItem16) );
266 if(items == NULL) goto FAIL;
268 if( lpDx ) /* explicit character widths */
271 unsigned short err = 0;
273 ve_we = (LONG)(dc->xformWorld2Vport.eM11 * 0x10000);
277 /* initialize text item with accumulated delta */
282 pitem->chars = str2b + i;
283 pitem->delta = delta;
288 /* add characters to the same XTextItem
289 * until new delta becomes non-zero */
294 fSum = sum*ve_we+err;
295 delta = SHIWORD(fSum)
296 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
297 pfo, pitem->chars, pitem->nchars+1);
299 } while ((++i < count) && !delta);
304 else /* charExtra or breakExtra */
308 pitem->chars = str2b + i;
309 pitem->delta = delta;
316 delta += dc->charExtra;
317 if (str2b[i].byte2 == (char)dfBreakChar)
318 delta += dc->breakExtra;
320 } while ((++i < count) && !delta);
325 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
326 physDev->drawable, physDev->gc,
327 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
328 HeapFree( GetProcessHeap(), 0, items );
333 /* have to render character by character. */
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) (physDev->org.x + x) + offset *
342 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
343 int y_i = IROUND((double) (physDev->org.y + 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);
351 offset += INTERNAL_XWSTODS(dc, lpDx[i]);
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 */
371 long linePos, lineWidth;
373 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
374 linePos = descent - 1;
375 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
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 physDev->org.x + x, physDev->org.y + y + linePos,
382 physDev->org.x + x + width, physDev->org.y + y + linePos );
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 physDev->org.x + x, physDev->org.y + y - lineAscent,
395 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
398 if (flags & ETO_CLIPPED) RestoreVisRgn16( dc->hSelf );
402 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
406 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
411 /***********************************************************************
412 * X11DRV_GetTextExtentPoint
414 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
417 DC *dc = physDev->dc;
418 fontObject* pfo = XFONT_GetFontObject( physDev->font );
420 TRACE("%s %d\n", debugstr_wn(str,count), count);
422 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
423 if (!p) return FALSE;
424 if( !pfo->lpX11Trans ) {
425 int dir, ascent, descent;
427 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
428 count, &dir, &ascent, &descent, &info_width );
430 size->cx = fabs((FLOAT)(info_width + dc->breakRem + count *
431 dc->charExtra) * dc->xformVport2World.eM11);
432 size->cy = fabs((FLOAT)(pfo->fs->ascent + pfo->fs->descent) *
433 dc->xformVport2World.eM22);
436 float x = 0.0, y = 0.0;
437 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
438 for(i = 0; i < count; i++) {
439 x += pfo->fs->per_char ?
440 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
441 pfo->fs->min_bounds.attributes;
443 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
444 TRACE("x = %f y = %f\n", x, y);
445 x *= pfo->lpX11Trans->pixelsize / 1000.0;
446 y *= pfo->lpX11Trans->pixelsize / 1000.0;
447 size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
448 dc->xformVport2World.eM11);
449 size->cy = fabs(y * dc->xformVport2World.eM22);
451 size->cx *= pfo->rescale;
452 size->cy *= pfo->rescale;
453 HeapFree( GetProcessHeap(), 0, p );