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;
60 UINT align = GetTextAlign( physDev->hdc );
61 INT charExtra = GetTextCharacterExtra( physDev->hdc );
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 pfo = XFONT_GetFontObject( physDev->font );
71 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
73 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
74 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
75 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
77 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
78 physDev->hdc, (UINT16)(physDev->font), x, y,
79 debugstr_wn (wstr, count), count, flags, lpDx);
81 /* some strings sent here end in a newline for whatever reason. I have no
82 clue what the right treatment should be in general, but ignoring
83 terminating newlines seems ok. MW, April 1998. */
84 if (count > 0 && wstr[count - 1] == '\n') count--;
86 if (lprect != NULL) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
87 lprect->left, lprect->top,
88 lprect->right, lprect->bottom );
89 /* Setup coordinates */
91 if (align & TA_UPDATECP)
93 GetCurrentPositionEx( physDev->hdc, &pt );
98 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
100 if (!lprect) /* not always */
103 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
105 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
108 rect.right = x + sz.cx;
110 rect.bottom = y + sz.cy;
116 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
118 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
119 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
124 LPtoDP(physDev->hdc, &pt, 1);
128 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
129 x, y, rect.left, rect.top, rect.right, rect.bottom);
131 /* Draw the rectangle */
133 if (flags & ETO_OPAQUE)
135 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
136 dibUpdateFlag = TRUE;
138 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
139 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
140 physDev->org.x + rect.left, physDev->org.y + rect.top,
141 rect.right-rect.left, rect.bottom-rect.top );
144 if (!count) goto END; /* Nothing more to do */
146 /* Compute text starting position */
148 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
150 for (i = width = 0; i < count; i++) width += lpDx[i];
151 width = X11DRV_XWStoDS(physDev, width);
156 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
161 width = X11DRV_XWStoDS(physDev, sz.cx);
163 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
164 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
165 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
166 pfo->lpX11Trans->pixelsize : width;
167 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
168 pfo->lpX11Trans->pixelsize : 0;
170 switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) )
173 if (align & TA_UPDATECP) {
176 DPtoLP(physDev->hdc, &pt, 1);
177 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
183 if (align & TA_UPDATECP) {
186 DPtoLP(physDev->hdc, &pt, 1);
187 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
196 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
199 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
200 pfo->lpX11Trans->pixelsize : 0;
201 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
202 pfo->lpX11Trans->pixelsize : ascent;
205 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
206 pfo->lpX11Trans->pixelsize : 0;
207 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
208 pfo->lpX11Trans->pixelsize : descent;
214 /* Set the clip region */
216 if (flags & ETO_CLIPPED)
219 RECT clip_rect = *lprect;
221 LPtoDP( physDev->hdc, (POINT *)&clip_rect, 2 );
222 clip_region = CreateRectRgnIndirect( &clip_rect );
223 /* make a copy of the current device region */
224 saved_region = CreateRectRgn( 0, 0, 0, 0 );
225 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
226 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
227 DeleteObject( clip_region );
230 /* Draw the text background if necessary */
234 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
235 dibUpdateFlag = TRUE;
238 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
240 /* If rectangle is opaque and clipped, do nothing */
241 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
243 /* Only draw if rectangle is not opaque or if some */
244 /* text is outside the rectangle */
245 if (!(flags & ETO_OPAQUE) ||
247 (x + width >= rect.right) ||
248 (y - ascent < rect.top) ||
249 (y + descent >= rect.bottom))
252 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
253 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
254 physDev->org.x + x, physDev->org.y + y - ascent,
255 width, ascent + descent );
261 /* Draw the text (count > 0 verified) */
262 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
266 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
270 if (!charExtra && !breakExtra && !lpDx)
272 X11DRV_cptable[pfo->fi->cptable].pDrawString(
273 pfo, gdi_display, physDev->drawable, physDev->gc,
274 physDev->org.x + x, physDev->org.y + y, str2b, count );
276 else /* Now the fun begins... */
278 XTextItem16 *items, *pitem;
281 /* allocate max items */
283 pitem = items = HeapAlloc( GetProcessHeap(), 0,
284 count * sizeof(XTextItem16) );
285 if(items == NULL) goto FAIL;
287 if( lpDx ) /* explicit character widths */
290 unsigned short err = 0;
292 ve_we = X11DRV_XWStoDS( physDev, 0x10000 );
296 /* initialize text item with accumulated delta */
301 pitem->chars = str2b + i;
302 pitem->delta = delta;
307 /* add characters to the same XTextItem
308 * until new delta becomes non-zero */
313 fSum = sum*ve_we+err;
314 delta = (short)HIWORD(fSum)
315 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
316 pfo, pitem->chars, pitem->nchars+1);
318 } while ((++i < count) && !delta);
323 else /* charExtra or breakExtra */
327 pitem->chars = str2b + i;
328 pitem->delta = delta;
336 if (str2b[i].byte2 == (char)dfBreakChar)
339 } while ((++i < count) && !delta);
344 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
345 physDev->drawable, physDev->gc,
346 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
347 HeapFree( GetProcessHeap(), 0, items );
352 /* have to render character by character. */
356 for (i=0; i<count; i++)
358 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
359 - font->min_char_or_byte2;
360 int x_i = IROUND((double) (physDev->org.x + x) + offset *
361 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
362 int y_i = IROUND((double) (physDev->org.y + y) - offset *
363 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
365 X11DRV_cptable[pfo->fi->cptable].pDrawString(
366 pfo, gdi_display, physDev->drawable, physDev->gc,
367 x_i, y_i, &str2b[i], 1);
370 offset += X11DRV_XWStoDS(physDev, lpDx[i]);
374 offset += (double) (font->per_char ?
375 font->per_char[char_metric_offset].attributes:
376 font->min_bounds.attributes)
377 * pfo->lpX11Trans->pixelsize / 1000.0;
379 if (str2b[i].byte2 == (char)dfBreakChar)
380 offset += breakExtra;
384 HeapFree( GetProcessHeap(), 0, str2b );
386 /* Draw underline and strike-out if needed */
391 long linePos, lineWidth;
393 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
394 linePos = descent - 1;
395 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
397 else if (lineWidth == 1) lineWidth = 0;
398 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
399 LineSolid, CapRound, JoinBevel );
400 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
401 physDev->org.x + x, physDev->org.y + y + linePos,
402 physDev->org.x + x + width, physDev->org.y + y + linePos );
406 long lineAscent, lineDescent;
407 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
408 lineAscent = ascent / 2;
409 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
410 lineDescent = -lineAscent * 2 / 3;
411 XSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
412 LineSolid, CapRound, JoinBevel );
413 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
414 physDev->org.x + x, physDev->org.y + y - lineAscent,
415 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
419 if (flags & ETO_CLIPPED)
421 /* restore the device region */
422 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
423 DeleteObject( saved_region );
428 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
432 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
437 /***********************************************************************
438 * X11DRV_GetTextExtentPoint
440 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
443 fontObject* pfo = XFONT_GetFontObject( physDev->font );
445 TRACE("%s %d\n", debugstr_wn(str,count), count);
447 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
448 if (!p) return FALSE;
449 if( !pfo->lpX11Trans ) {
450 int dir, ascent, descent;
452 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
453 count, &dir, &ascent, &descent, &info_width );
455 size->cx = info_width;
456 size->cy = pfo->fs->ascent + pfo->fs->descent;
459 float x = 0.0, y = 0.0;
460 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
461 for(i = 0; i < count; i++) {
462 x += pfo->fs->per_char ?
463 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
464 pfo->fs->min_bounds.attributes;
466 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
467 TRACE("x = %f y = %f\n", x, y);
468 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
469 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
471 size->cx *= pfo->rescale;
472 size->cy *= pfo->rescale;
473 HeapFree( GetProcessHeap(), 0, p );