2 * X11 graphics driver text functions
4 * Copyright 1993,1994 Alexandre Julliard
13 /*#include "callback.h"*/
17 /* #define DEBUG_TEXT */
20 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
23 /***********************************************************************
27 X11DRV_ExtTextOut( DC *dc, INT32 x, INT32 y, UINT32 flags,
28 const RECT32 *lprect, LPCSTR str, UINT32 count,
32 int dir, ascent, descent, i;
37 char dfBreakChar, lfUnderline, lfStrikeOut;
39 if (!DC_SetupGCForText( dc )) return TRUE;
41 pfo = XFONT_GetFontObject( dc->u.x.font );
44 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
45 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
46 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
48 dprintf_text(stddeb,"ExtTextOut: hdc=%04x df=%04x %d,%d '%.*s', %d flags=%d\n",
49 dc->hSelf, (UINT16)(dc->u.x.font), x, y, (int)count, str, count, flags);
51 if (lprect != NULL) dprintf_text(stddeb, "\trect=(%d,%d- %d,%d)\n",
52 lprect->left, lprect->top,
53 lprect->right, lprect->bottom );
54 /* Setup coordinates */
56 if (dc->w.textAlign & TA_UPDATECP)
62 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
64 if (!lprect) /* not always */
67 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
69 if (!X11DRV_GetTextExtentPoint( dc, str, count, &sz ))
71 rect.left = XLPTODP( dc, x );
72 rect.right = XLPTODP( dc, x+sz.cx );
73 rect.top = YLPTODP( dc, y );
74 rect.bottom = YLPTODP( dc, y+sz.cy );
78 rect.left = XLPTODP( dc, lprect->left );
79 rect.right = XLPTODP( dc, lprect->right );
80 rect.top = YLPTODP( dc, lprect->top );
81 rect.bottom = YLPTODP( dc, lprect->bottom );
83 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
84 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
90 dprintf_text(stddeb,"\treal coord: x=%i, y=%i, rect=(%d,%d-%d,%d)\n",
91 x, y, rect.left, rect.top, rect.right, rect.bottom);
93 /* Draw the rectangle */
95 if (flags & ETO_OPAQUE)
97 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
98 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
99 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
100 rect.right-rect.left, rect.bottom-rect.top );
102 if (!count) return TRUE; /* Nothing more to do */
104 /* Compute text starting position */
106 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
108 int extra = dc->wndExtX / 2;
109 for (i = info.width = 0; i < count; i++) info.width += lpDx[i];
110 info.width = (info.width * dc->vportExtX + extra ) / dc->wndExtX;
114 XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
115 info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
118 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
121 if (dc->w.textAlign & TA_UPDATECP)
122 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
126 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
133 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
145 /* Set the clip region */
147 if (flags & ETO_CLIPPED)
149 hRgnClip = dc->w.hClipRgn;
150 CLIPPING_IntersectClipRect( dc, rect.left, rect.top, rect.right,
151 rect.bottom, CLIP_INTERSECT|CLIP_KEEPRGN );
154 /* Draw the text background if necessary */
156 if (dc->w.backgroundMode != TRANSPARENT)
158 /* If rectangle is opaque and clipped, do nothing */
159 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
161 /* Only draw if rectangle is not opaque or if some */
162 /* text is outside the rectangle */
163 if (!(flags & ETO_OPAQUE) ||
165 (x + info.width >= rect.right) ||
166 (y-font->ascent < rect.top) ||
167 (y+font->descent >= rect.bottom))
169 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
170 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
172 dc->w.DCOrgY + y - font->ascent,
174 font->ascent + font->descent );
179 /* Draw the text (count > 0 verified) */
181 XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
182 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
184 XDrawString( display, dc->u.x.drawable, dc->u.x.gc,
185 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
187 else /* Now the fun begins... */
189 XTextItem *items, *pitem;
192 /* allocate max items */
194 pitem = items = HEAP_xalloc( GetProcessHeap(), 0,
195 count * sizeof(XTextItem) );
197 if( lpDx ) /* explicit character widths */
199 int extra = dc->wndExtX / 2;
203 /* initialize text item with accumulated delta */
205 pitem->chars = (char *)str + i;
206 pitem->delta = delta;
211 /* add characters to the same XTextItem until new delta
212 * becomes non-zero */
216 delta += (lpDx[i] * dc->vportExtX + extra) / dc->wndExtX
217 - XTextWidth( font, str + i, 1);
219 } while ((++i < count) && !delta);
223 else /* charExtra or breakExtra */
227 pitem->chars = (char *)str + i;
228 pitem->delta = delta;
235 delta += dc->w.charExtra;
236 if (str[i] == (char)dfBreakChar) delta += dc->w.breakExtra;
238 } while ((++i < count) && !delta);
243 XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
244 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
245 HeapFree( GetProcessHeap(), 0, items );
248 /* Draw underline and strike-out if needed */
252 long linePos, lineWidth;
254 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
255 linePos = font->descent-1;
256 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
258 else if (lineWidth == 1) lineWidth = 0;
259 XSetLineAttributes( display, dc->u.x.gc, lineWidth,
260 LineSolid, CapRound, JoinBevel );
261 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
262 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
263 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
267 long lineAscent, lineDescent;
268 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
269 lineAscent = font->ascent / 2;
270 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
271 lineDescent = -lineAscent * 2 / 3;
272 XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
273 LineSolid, CapRound, JoinBevel );
274 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
275 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
276 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
279 if (flags & ETO_CLIPPED)
281 SelectClipRgn32( dc->hSelf, hRgnClip );
282 DeleteObject32( hRgnClip );