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
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(text);
35 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
36 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
39 /***********************************************************************
43 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
44 const RECT *lprect, LPCWSTR wstr, UINT count,
45 const INT *lpDx, INT breakExtra )
49 INT width, ascent, descent, xwidth, ywidth;
52 char dfBreakChar, lfUnderline, lfStrikeOut;
54 XChar2b *str2b = NULL;
55 BOOL dibUpdateFlag = FALSE;
57 HRGN saved_region = 0;
62 if(physDev->has_gdi_font)
63 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx, breakExtra);
65 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
67 align = GetTextAlign( physDev->hdc );
68 charExtra = GetTextCharacterExtra( physDev->hdc );
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 physDev->hdc, (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 (align & TA_UPDATECP)
95 GetCurrentPositionEx( physDev->hdc, &pt );
100 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
102 if (!lprect) /* not always */
105 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
107 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
110 rect.right = x + sz.cx;
112 rect.bottom = y + sz.cy;
118 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
120 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
121 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
126 LPtoDP(physDev->hdc, &pt, 1);
130 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
131 x, y, rect.left, rect.top, rect.right, rect.bottom);
133 /* Draw the rectangle */
135 if (flags & ETO_OPAQUE)
137 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
138 dibUpdateFlag = TRUE;
140 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
141 XFillRectangle( 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 );
146 if (!count) goto END; /* Nothing more to do */
148 /* Compute text starting position */
150 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
152 for (i = width = 0; i < count; i++) width += lpDx[i];
153 width = X11DRV_XWStoDS(physDev, width);
158 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
163 width = X11DRV_XWStoDS(physDev, sz.cx);
165 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
166 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
167 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
168 pfo->lpX11Trans->pixelsize : width;
169 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
170 pfo->lpX11Trans->pixelsize : 0;
172 switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) )
175 if (align & TA_UPDATECP) {
178 DPtoLP(physDev->hdc, &pt, 1);
179 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
185 if (align & TA_UPDATECP) {
188 DPtoLP(physDev->hdc, &pt, 1);
189 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
198 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
201 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
202 pfo->lpX11Trans->pixelsize : 0;
203 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
204 pfo->lpX11Trans->pixelsize : ascent;
207 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
208 pfo->lpX11Trans->pixelsize : 0;
209 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
210 pfo->lpX11Trans->pixelsize : descent;
216 /* Set the clip region */
218 if (flags & ETO_CLIPPED)
221 RECT clip_rect = *lprect;
223 LPtoDP( physDev->hdc, (POINT *)&clip_rect, 2 );
224 clip_region = CreateRectRgnIndirect( &clip_rect );
225 /* make a copy of the current device region */
226 saved_region = CreateRectRgn( 0, 0, 0, 0 );
227 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
228 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
229 DeleteObject( clip_region );
232 /* Draw the text background if necessary */
236 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
237 dibUpdateFlag = TRUE;
240 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
242 /* If rectangle is opaque and clipped, do nothing */
243 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
245 /* Only draw if rectangle is not opaque or if some */
246 /* text is outside the rectangle */
247 if (!(flags & ETO_OPAQUE) ||
249 (x + width >= rect.right) ||
250 (y - ascent < rect.top) ||
251 (y + descent >= rect.bottom))
254 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
255 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
256 physDev->org.x + x, physDev->org.y + y - ascent,
257 width, ascent + descent );
263 /* Draw the text (count > 0 verified) */
264 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
268 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
272 if (!charExtra && !breakExtra && !lpDx)
274 X11DRV_cptable[pfo->fi->cptable].pDrawString(
275 pfo, gdi_display, physDev->drawable, physDev->gc,
276 physDev->org.x + x, physDev->org.y + y, str2b, count );
278 else /* Now the fun begins... */
280 XTextItem16 *items, *pitem;
283 /* allocate max items */
285 pitem = items = HeapAlloc( GetProcessHeap(), 0,
286 count * sizeof(XTextItem16) );
287 if(items == NULL) goto FAIL;
289 if( lpDx ) /* explicit character widths */
292 unsigned short err = 0;
294 ve_we = X11DRV_XWStoDS( physDev, 0x10000 );
298 /* initialize text item with accumulated delta */
303 pitem->chars = str2b + i;
304 pitem->delta = delta;
309 /* add characters to the same XTextItem
310 * until new delta becomes non-zero */
315 fSum = sum*ve_we+err;
316 delta = (short)HIWORD(fSum)
317 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
318 pfo, pitem->chars, pitem->nchars+1);
320 } while ((++i < count) && !delta);
325 else /* charExtra or breakExtra */
329 pitem->chars = str2b + i;
330 pitem->delta = delta;
338 if (str2b[i].byte2 == (char)dfBreakChar)
341 } while ((++i < count) && !delta);
346 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
347 physDev->drawable, physDev->gc,
348 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
349 HeapFree( GetProcessHeap(), 0, items );
354 /* have to render character by character. */
358 for (i=0; i<count; i++)
360 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
361 - font->min_char_or_byte2;
362 int x_i = IROUND((double) (physDev->org.x + x) + offset *
363 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
364 int y_i = IROUND((double) (physDev->org.y + y) - offset *
365 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
367 X11DRV_cptable[pfo->fi->cptable].pDrawString(
368 pfo, gdi_display, physDev->drawable, physDev->gc,
369 x_i, y_i, &str2b[i], 1);
372 offset += X11DRV_XWStoDS(physDev, lpDx[i]);
376 offset += (double) (font->per_char ?
377 font->per_char[char_metric_offset].attributes:
378 font->min_bounds.attributes)
379 * pfo->lpX11Trans->pixelsize / 1000.0;
381 if (str2b[i].byte2 == (char)dfBreakChar)
382 offset += breakExtra;
386 HeapFree( GetProcessHeap(), 0, str2b );
388 /* Draw underline and strike-out if needed */
393 long linePos, lineWidth;
395 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
396 linePos = descent - 1;
397 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
399 else if (lineWidth == 1) lineWidth = 0;
400 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
401 LineSolid, CapRound, JoinBevel );
402 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
403 physDev->org.x + x, physDev->org.y + y + linePos,
404 physDev->org.x + x + width, physDev->org.y + y + linePos );
408 long lineAscent, lineDescent;
409 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
410 lineAscent = ascent / 2;
411 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
412 lineDescent = -lineAscent * 2 / 3;
413 XSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
414 LineSolid, CapRound, JoinBevel );
415 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
416 physDev->org.x + x, physDev->org.y + y - lineAscent,
417 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
421 if (flags & ETO_CLIPPED)
423 /* restore the device region */
424 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
425 DeleteObject( saved_region );
430 HeapFree( GetProcessHeap(), 0, str2b );
434 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
439 /***********************************************************************
440 * X11DRV_GetTextExtentPoint
442 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
445 fontObject* pfo = XFONT_GetFontObject( physDev->font );
447 TRACE("%s %d\n", debugstr_wn(str,count), count);
449 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
450 if (!p) return FALSE;
451 if( !pfo->lpX11Trans ) {
452 int dir, ascent, descent;
454 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
455 count, &dir, &ascent, &descent, &info_width );
457 size->cx = info_width;
458 size->cy = pfo->fs->ascent + pfo->fs->descent;
461 float x = 0.0, y = 0.0;
462 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
463 for(i = 0; i < count; i++) {
464 x += pfo->fs->per_char ?
465 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
466 pfo->fs->min_bounds.attributes;
468 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
469 TRACE("x = %f y = %f\n", x, y);
470 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
471 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
473 size->cx *= pfo->rescale;
474 size->cy *= pfo->rescale;
475 HeapFree( GetProcessHeap(), 0, p );