2 * X11 graphics driver text functions
4 * Copyright 1993,1994 Alexandre Julliard
9 #ifndef X_DISPLAY_MISSING
11 #include <X11/Xatom.h>
23 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
24 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
26 /***********************************************************************
30 X11DRV_ExtTextOut( DC *dc, INT32 x, INT32 y, UINT32 flags,
31 const RECT32 *lprect, LPCSTR str, UINT32 count,
36 INT32 width, ascent, descent, xwidth, ywidth;
39 char dfBreakChar, lfUnderline, lfStrikeOut;
40 BOOL32 rotated = FALSE;
41 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
43 if (!X11DRV_SetupGCForText( dc )) return TRUE;
45 pfo = XFONT_GetFontObject( physDev->font );
48 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
50 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
51 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
52 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
54 TRACE(text,"hdc=%04x df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
55 dc->hSelf, (UINT16)(physDev->font), x, y,
56 debugstr_an (str, count), count, flags, lpDx);
58 /* some strings sent here end in a newline for whatever reason. I have no
59 clue what the right treatment should be in general, but ignoring
60 terminating newlines seems ok. MW, April 1998. */
61 if (count > 0 && str[count - 1] == '\n') count--;
63 if (lprect != NULL) TRACE(text, "\trect=(%d,%d - %d,%d)\n",
64 lprect->left, lprect->top,
65 lprect->right, lprect->bottom );
66 /* Setup coordinates */
68 if (dc->w.textAlign & TA_UPDATECP)
74 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
76 if (!lprect) /* not always */
79 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
81 if (!X11DRV_GetTextExtentPoint( dc, str, count, &sz ))
83 rect.left = XLPTODP( dc, x );
84 rect.right = XLPTODP( dc, x+sz.cx );
85 rect.top = YLPTODP( dc, y );
86 rect.bottom = YLPTODP( dc, y+sz.cy );
90 rect.left = XLPTODP( dc, lprect->left );
91 rect.right = XLPTODP( dc, lprect->right );
92 rect.top = YLPTODP( dc, lprect->top );
93 rect.bottom = YLPTODP( dc, lprect->bottom );
95 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
96 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
100 y = YLPTODP( dc, y );
102 TRACE(text,"\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
103 x, y, rect.left, rect.top, rect.right, rect.bottom);
105 /* Draw the rectangle */
107 if (flags & ETO_OPAQUE)
109 TSXSetForeground( display, physDev->gc, physDev->backgroundPixel );
110 TSXFillRectangle( display, physDev->drawable, physDev->gc,
111 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
112 rect.right-rect.left, rect.bottom-rect.top );
114 if (!count) return TRUE; /* Nothing more to do */
116 /* Compute text starting position */
118 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
120 int extra = dc->wndExtX / 2;
121 for (i = width = 0; i < count; i++) width += lpDx[i];
122 width = (width * dc->vportExtX + extra ) / dc->wndExtX;
127 if (!X11DRV_GetTextExtentPoint( dc, str, count, &sz ))
129 width = XLSTODS(dc, sz.cx);
131 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
132 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
133 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
134 pfo->lpX11Trans->pixelsize : width;
135 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
136 pfo->lpX11Trans->pixelsize : 0;
138 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
141 if (dc->w.textAlign & TA_UPDATECP) {
142 dc->w.CursPosX = XDPTOLP( dc, x + xwidth );
143 dc->w.CursPosY = YDPTOLP( dc, y - ywidth );
149 if (dc->w.textAlign & TA_UPDATECP) {
150 dc->w.CursPosX = XDPTOLP( dc, x );
151 dc->w.CursPosY = YDPTOLP( dc, y );
160 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
163 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
164 pfo->lpX11Trans->pixelsize : 0;
165 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
166 pfo->lpX11Trans->pixelsize : ascent;
169 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
170 pfo->lpX11Trans->pixelsize : 0;
171 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
172 pfo->lpX11Trans->pixelsize : descent;
178 /* Set the clip region */
180 if (flags & ETO_CLIPPED)
182 SaveVisRgn( dc->hSelf );
183 CLIPPING_IntersectVisRect( dc, rect.left, rect.top, rect.right,
184 rect.bottom, FALSE );
187 /* Draw the text background if necessary */
189 if (dc->w.backgroundMode != TRANSPARENT)
191 /* If rectangle is opaque and clipped, do nothing */
192 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
194 /* Only draw if rectangle is not opaque or if some */
195 /* text is outside the rectangle */
196 if (!(flags & ETO_OPAQUE) ||
198 (x + width >= rect.right) ||
199 (y - ascent < rect.top) ||
200 (y + descent >= rect.bottom))
202 TSXSetForeground( display, physDev->gc,
203 physDev->backgroundPixel );
204 TSXFillRectangle( display, physDev->drawable, physDev->gc,
206 dc->w.DCOrgY + y - ascent,
213 /* Draw the text (count > 0 verified) */
215 TSXSetForeground( display, physDev->gc, physDev->textPixel );
218 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
220 TSXDrawString( display, physDev->drawable, physDev->gc,
221 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
223 else /* Now the fun begins... */
225 XTextItem *items, *pitem;
228 /* allocate max items */
230 pitem = items = HEAP_xalloc( GetProcessHeap(), 0,
231 count * sizeof(XTextItem) );
233 if( lpDx ) /* explicit character widths */
235 int extra = dc->wndExtX / 2;
239 /* initialize text item with accumulated delta */
241 pitem->chars = (char *)str + i;
242 pitem->delta = delta;
247 /* add characters to the same XTextItem until new delta
248 * becomes non-zero */
252 delta += (lpDx[i] * dc->vportExtX + extra) / dc->wndExtX
253 - TSXTextWidth( font, str + i, 1);
255 } while ((++i < count) && !delta);
259 else /* charExtra or breakExtra */
263 pitem->chars = (char *)str + i;
264 pitem->delta = delta;
271 delta += dc->w.charExtra;
272 if (str[i] == (char)dfBreakChar) delta += dc->w.breakExtra;
274 } while ((++i < count) && !delta);
279 TSXDrawText( display, physDev->drawable, physDev->gc,
280 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
281 HeapFree( GetProcessHeap(), 0, items );
286 /* have to render character by character. */
290 for (i=0; i<count; i++)
292 int char_metric_offset = (unsigned char) str[i]
293 - font->min_char_or_byte2;
294 int x_i = IROUND((double) (dc->w.DCOrgX + x) + offset *
295 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
296 int y_i = IROUND((double) (dc->w.DCOrgY + y) - offset *
297 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
299 TSXDrawString( display, physDev->drawable, physDev->gc,
300 x_i, y_i, &str[i], 1);
302 offset += XLSTODS(dc, lpDx[i]);
305 offset += (double) (font->per_char ?
306 font->per_char[char_metric_offset].attributes:
307 font->min_bounds.attributes)
308 * pfo->lpX11Trans->pixelsize / 1000.0;
309 offset += dc->w.charExtra;
310 if (str[i] == (char)dfBreakChar)
311 offset += dc->w.breakExtra;
316 /* Draw underline and strike-out if needed */
320 long linePos, lineWidth;
322 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
323 linePos = descent - 1;
324 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
326 else if (lineWidth == 1) lineWidth = 0;
327 TSXSetLineAttributes( display, physDev->gc, lineWidth,
328 LineSolid, CapRound, JoinBevel );
329 TSXDrawLine( display, physDev->drawable, physDev->gc,
330 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
331 dc->w.DCOrgX + x + width, dc->w.DCOrgY + y + linePos );
335 long lineAscent, lineDescent;
336 if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
337 lineAscent = ascent / 2;
338 if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
339 lineDescent = -lineAscent * 2 / 3;
340 TSXSetLineAttributes( display, physDev->gc, lineAscent + lineDescent,
341 LineSolid, CapRound, JoinBevel );
342 TSXDrawLine( display, physDev->drawable, physDev->gc,
343 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
344 dc->w.DCOrgX + x + width, dc->w.DCOrgY + y - lineAscent );
347 if (flags & ETO_CLIPPED)
348 RestoreVisRgn( dc->hSelf );
353 #endif /* !defined(X_DISPLAY_MISSING) */