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
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(text);
36 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
37 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
40 /***********************************************************************
44 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
45 const RECT *lprect, LPCWSTR wstr, UINT count,
46 const INT *lpDx, INT breakExtra )
50 INT width, ascent, descent, xwidth, ywidth;
53 char dfBreakChar, lfUnderline, lfStrikeOut;
55 XChar2b *str2b = NULL;
56 BOOL dibUpdateFlag = FALSE;
58 HRGN saved_region = 0;
63 if(physDev->has_gdi_font)
64 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx, breakExtra);
66 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
68 align = GetTextAlign( physDev->hdc );
69 charExtra = GetTextCharacterExtra( physDev->hdc );
71 pfo = XFONT_GetFontObject( physDev->font );
74 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
76 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
77 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
78 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
80 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
81 physDev->hdc, (UINT16)(physDev->font), x, y,
82 debugstr_wn (wstr, count), count, flags, lpDx);
84 /* some strings sent here end in a newline for whatever reason. I have no
85 clue what the right treatment should be in general, but ignoring
86 terminating newlines seems ok. MW, April 1998. */
87 if (count > 0 && wstr[count - 1] == '\n') count--;
89 if (lprect != NULL) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
90 lprect->left, lprect->top,
91 lprect->right, lprect->bottom );
92 /* Setup coordinates */
94 if (align & TA_UPDATECP)
96 GetCurrentPositionEx( physDev->hdc, &pt );
101 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
103 if (!lprect) /* not always */
106 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
108 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
111 rect.right = x + sz.cx;
113 rect.bottom = y + sz.cy;
119 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
121 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
122 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
127 LPtoDP(physDev->hdc, &pt, 1);
131 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
132 x, y, rect.left, rect.top, rect.right, rect.bottom);
134 /* Draw the rectangle */
136 if (flags & ETO_OPAQUE)
138 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
139 dibUpdateFlag = TRUE;
141 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
142 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
143 physDev->org.x + rect.left, physDev->org.y + rect.top,
144 rect.right-rect.left, rect.bottom-rect.top );
147 if (!count) goto END; /* Nothing more to do */
149 /* Compute text starting position */
151 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
153 for (i = width = 0; i < count; i++) width += lpDx[i];
154 width = X11DRV_XWStoDS(physDev, width);
159 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
164 width = X11DRV_XWStoDS(physDev, sz.cx);
166 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
167 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
168 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
169 pfo->lpX11Trans->pixelsize : width;
170 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
171 pfo->lpX11Trans->pixelsize : 0;
173 switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) )
176 if (align & TA_UPDATECP) {
179 DPtoLP(physDev->hdc, &pt, 1);
180 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
186 if (align & TA_UPDATECP) {
189 DPtoLP(physDev->hdc, &pt, 1);
190 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
199 switch( align & (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)
222 RECT clip_rect = *lprect;
224 LPtoDP( physDev->hdc, (POINT *)&clip_rect, 2 );
225 clip_region = CreateRectRgnIndirect( &clip_rect );
226 /* make a copy of the current device region */
227 saved_region = CreateRectRgn( 0, 0, 0, 0 );
228 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
229 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
230 DeleteObject( clip_region );
233 /* Draw the text background if necessary */
237 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
238 dibUpdateFlag = TRUE;
241 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
243 /* If rectangle is opaque and clipped, do nothing */
244 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
246 /* Only draw if rectangle is not opaque or if some */
247 /* text is outside the rectangle */
248 if (!(flags & ETO_OPAQUE) ||
250 (x + width >= rect.right) ||
251 (y - ascent < rect.top) ||
252 (y + descent >= rect.bottom))
255 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
256 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
257 physDev->org.x + x, physDev->org.y + y - ascent,
258 width, ascent + descent );
264 /* Draw the text (count > 0 verified) */
265 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
269 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
273 if (!charExtra && !breakExtra && !lpDx)
275 X11DRV_cptable[pfo->fi->cptable].pDrawString(
276 pfo, gdi_display, physDev->drawable, physDev->gc,
277 physDev->org.x + x, physDev->org.y + y, str2b, count );
279 else /* Now the fun begins... */
281 XTextItem16 *items, *pitem;
284 /* allocate max items */
286 pitem = items = HeapAlloc( GetProcessHeap(), 0,
287 count * sizeof(XTextItem16) );
288 if(items == NULL) goto FAIL;
290 if( lpDx ) /* explicit character widths */
293 unsigned short err = 0;
295 ve_we = X11DRV_XWStoDS( physDev, 0x10000 );
299 /* initialize text item with accumulated delta */
304 pitem->chars = str2b + i;
305 pitem->delta = delta;
310 /* add characters to the same XTextItem
311 * until new delta becomes non-zero */
316 fSum = sum*ve_we+err;
317 delta = (short)HIWORD(fSum)
318 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
319 pfo, pitem->chars, pitem->nchars+1);
321 } while ((++i < count) && !delta);
326 else /* charExtra or breakExtra */
330 pitem->chars = str2b + i;
331 pitem->delta = delta;
339 if (str2b[i].byte2 == (char)dfBreakChar)
342 } while ((++i < count) && !delta);
347 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
348 physDev->drawable, physDev->gc,
349 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
350 HeapFree( GetProcessHeap(), 0, items );
355 /* have to render character by character. */
359 for (i=0; i<count; i++)
361 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
362 - font->min_char_or_byte2;
363 int x_i = IROUND((double) (physDev->org.x + x) + offset *
364 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
365 int y_i = IROUND((double) (physDev->org.y + y) - offset *
366 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
368 X11DRV_cptable[pfo->fi->cptable].pDrawString(
369 pfo, gdi_display, physDev->drawable, physDev->gc,
370 x_i, y_i, &str2b[i], 1);
373 offset += X11DRV_XWStoDS(physDev, lpDx[i]);
377 offset += (double) (font->per_char ?
378 font->per_char[char_metric_offset].attributes:
379 font->min_bounds.attributes)
380 * pfo->lpX11Trans->pixelsize / 1000.0;
382 if (str2b[i].byte2 == (char)dfBreakChar)
383 offset += breakExtra;
387 HeapFree( GetProcessHeap(), 0, str2b );
389 /* Draw underline and strike-out if needed */
394 long linePos, lineWidth;
396 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
397 linePos = descent - 1;
398 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
400 else if (lineWidth == 1) lineWidth = 0;
401 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
402 LineSolid, CapRound, JoinBevel );
403 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
404 physDev->org.x + x, physDev->org.y + y + linePos,
405 physDev->org.x + x + width, physDev->org.y + y + linePos );
409 long lineAscent, lineDescent;
410 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
411 lineAscent = ascent / 2;
412 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
413 lineDescent = -lineAscent * 2 / 3;
414 XSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
415 LineSolid, CapRound, JoinBevel );
416 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
417 physDev->org.x + x, physDev->org.y + y - lineAscent,
418 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
422 if (flags & ETO_CLIPPED)
424 /* restore the device region */
425 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
426 DeleteObject( saved_region );
431 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
435 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
440 /***********************************************************************
441 * X11DRV_GetTextExtentPoint
443 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
446 fontObject* pfo = XFONT_GetFontObject( physDev->font );
448 TRACE("%s %d\n", debugstr_wn(str,count), count);
450 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
451 if (!p) return FALSE;
452 if( !pfo->lpX11Trans ) {
453 int dir, ascent, descent;
455 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
456 count, &dir, &ascent, &descent, &info_width );
458 size->cx = info_width;
459 size->cy = pfo->fs->ascent + pfo->fs->descent;
462 float x = 0.0, y = 0.0;
463 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
464 for(i = 0; i < count; i++) {
465 x += pfo->fs->per_char ?
466 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
467 pfo->fs->min_bounds.attributes;
469 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
470 TRACE("x = %f y = %f\n", x, y);
471 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
472 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
474 size->cx *= pfo->rescale;
475 size->cy *= pfo->rescale;
476 HeapFree( GetProcessHeap(), 0, p );