4 * Copyright 1993, 1994 Alexandre Julliard
17 /* #define DEBUG_TEXT */
27 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
29 static int tabstop = 8;
31 static int spacewidth;
32 static int prefix_offset;
34 extern int CLIPPING_IntersectClipRect( DC * dc, short left, short top,
35 short right, short bottom, UINT16 flags);
37 static const char *TEXT_NextLine( HDC16 hdc, const char *str, int *count,
38 char *dest, int *len, int width, WORD format)
40 /* Return next line of text from a string.
43 * str - string to parse into lines.
44 * count - length of str.
45 * dest - destination in which to return line.
46 * len - length of resultant line in dest in chars.
47 * width - maximum width of line in pixels.
48 * format - format type passed to DrawText.
50 * Returns pointer to next char in str after end of the line
51 * or NULL if end of str reached.
59 int wb_i = 0, wb_j = 0, wb_count = 0;
67 if (!(format & DT_SINGLELINE))
69 if (str[i] == CR && str[i+1] == LF)
77 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
78 (format & DT_WORDBREAK))
80 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
87 if (!(format & DT_NOPREFIX))
95 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
97 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
105 if (format & DT_EXPANDTABS)
111 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
115 numspaces = (tabwidth - size.cx) / spacewidth;
116 for (k = 0; k < numspaces; k++)
118 plen += tabwidth - size.cx;
119 lasttab = wb_j + numspaces;
123 dest[j++] = str[i++];
124 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
125 (format & DT_WORDBREAK))
127 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
135 dest[j++] = str[i++];
136 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
137 (format & DT_WORDBREAK))
142 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
149 dest[j++] = str[i++];
150 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
151 (format & DT_WORDBREAK))
153 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
160 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
164 if (format & DT_WORDBREAK)
169 *count = wb_count - 1;
187 /***********************************************************************
188 * DrawText16 (USER.85)
190 INT16 DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
191 LPRECT16 rect, UINT16 flags )
195 static char line[1024];
196 int len, lh, count=i_count;
200 int x = rect->left, y = rect->top;
201 int width = rect->right - rect->left;
204 dprintf_text(stddeb,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str,
205 count, rect->left, rect->top, rect->right, rect->bottom);
207 if (count == -1) count = strlen(str);
210 GetTextMetrics16(hdc, &tm);
211 if (flags & DT_EXTERNALLEADING)
212 lh = tm.tmHeight + tm.tmExternalLeading;
216 if (flags & DT_TABSTOP)
217 tabstop = flags >> 8;
219 if (flags & DT_EXPANDTABS)
221 GetTextExtentPoint16(hdc, " ", 1, &size);
222 spacewidth = size.cx;
223 GetTextExtentPoint16(hdc, "o", 1, &size);
224 tabwidth = size.cx * tabstop;
227 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
232 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
234 if (prefix_offset != -1)
236 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
238 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
239 prefix_end = size.cx - 1;
242 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
243 if (flags & DT_CENTER) x = (rect->left + rect->right -
245 else if (flags & DT_RIGHT) x = rect->right - size.cx;
247 if (flags & DT_SINGLELINE)
249 if (flags & DT_VCENTER) y = rect->top +
250 (rect->bottom - rect->top) / 2 - size.cy / 2;
251 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
253 if (!(flags & DT_CALCRECT))
255 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
256 rect, line, len, NULL )) return 0;
257 if (prefix_offset != -1)
259 HPEN32 hpen = CreatePen32( PS_SOLID, 1, GetTextColor(hdc) );
260 HPEN32 oldPen = SelectObject32( hdc, hpen );
261 MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
262 LineTo32(hdc, x + prefix_end, y + tm.tmAscent + 1 );
263 SelectObject32( hdc, oldPen );
264 DeleteObject32( hpen );
267 else if (size.cx > max_width)
273 if (!(flags & DT_NOCLIP))
275 if (y > rect->bottom - lh)
281 if (flags & DT_CALCRECT)
283 rect->right = rect->left + max_width;
286 return y - rect->top;
290 /***********************************************************************
291 * DrawText32A (USER32.163)
293 INT32 DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
294 LPRECT32 rect, UINT32 flags )
300 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
301 CONV_RECT32TO16( rect, &rect16 );
302 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
303 CONV_RECT16TO32( &rect16, rect );
308 /***********************************************************************
309 * DrawText32W (USER32.166)
311 INT32 DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
312 LPRECT32 rect, UINT32 flags )
314 char *p = STRING32_DupUniToAnsi( str );
315 INT32 ret = DrawText32A( hdc, p, count, rect, flags );
321 /***********************************************************************
322 * ExtTextOut16 (GDI.351)
324 BOOL16 ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
325 const RECT16 *lprect, LPCSTR str, UINT16 count,
329 int dir, ascent, descent, i;
334 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
337 dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
338 if (!dc) return FALSE;
339 MF_ExtTextOut( dc, x, y, flags, lprect, str, count, lpDx );
343 if (!DC_SetupGCForText( dc )) return TRUE;
344 font = dc->u.x.font.fstruct;
346 dprintf_text(stddeb,"ExtTextOut: hdc=%04x %d,%d '%*.*s', %d flags=%d\n",
347 hdc, x, y, count, count, str, count, flags);
348 if (lprect != NULL) dprintf_text(stddeb, "\trect=(%d,%d- %d,%d)\n",
349 lprect->left, lprect->top,
350 lprect->right, lprect->bottom );
352 /* Setup coordinates */
354 if (dc->w.textAlign & TA_UPDATECP)
360 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
362 if (!lprect) /* not always */
365 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
367 if (!GetTextExtentPoint16( hdc, str, count, &sz ))
369 rect.left = XLPTODP( dc, x );
370 rect.right = XLPTODP( dc, x+sz.cx );
371 rect.top = YLPTODP( dc, y );
372 rect.bottom = YLPTODP( dc, y+sz.cy );
376 rect.left = XLPTODP( dc, lprect->left );
377 rect.right = XLPTODP( dc, lprect->right );
378 rect.top = YLPTODP( dc, lprect->top );
379 rect.bottom = YLPTODP( dc, lprect->bottom );
381 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
382 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
385 x = XLPTODP( dc, x );
386 y = YLPTODP( dc, y );
388 dprintf_text(stddeb,"\treal coord: x=%i, y=%i, rect=(%d,%d-%d,%d)\n",
389 x, y, rect.left, rect.top, rect.right, rect.bottom);
391 /* Draw the rectangle */
393 if (flags & ETO_OPAQUE)
395 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
396 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
397 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
398 rect.right-rect.left, rect.bottom-rect.top );
400 if (!count) return TRUE; /* Nothing more to do */
402 /* Compute text starting position */
404 XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
406 if (lpDx) /* have explicit character cell x offsets */
408 /* sum lpDx array and add the width of last character */
410 info.width = XTextWidth( font, str + count - 1, 1) + dc->w.charExtra;
411 if (str[count-1] == (char)dc->u.x.font.metrics.tmBreakChar)
412 info.width += dc->w.breakExtra;
414 for (i = 0; i < count; i++) info.width += lpDx[i];
417 info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
419 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
422 if (dc->w.textAlign & TA_UPDATECP)
423 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
427 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
434 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
446 /* Set the clip region */
448 if (flags & ETO_CLIPPED)
450 hRgnClip = dc->w.hClipRgn;
451 CLIPPING_IntersectClipRect( dc, rect.left, rect.top, rect.right,
452 rect.bottom, CLIP_INTERSECT|CLIP_KEEPRGN );
455 /* Draw the text background if necessary */
457 if (dc->w.backgroundMode != TRANSPARENT)
459 /* If rectangle is opaque and clipped, do nothing */
460 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
462 /* Only draw if rectangle is not opaque or if some */
463 /* text is outside the rectangle */
464 if (!(flags & ETO_OPAQUE) ||
466 (x + info.width >= rect.right) ||
467 (y-font->ascent < rect.top) ||
468 (y+font->descent >= rect.bottom))
470 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
471 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
473 dc->w.DCOrgY + y - font->ascent,
475 font->ascent + font->descent );
480 /* Draw the text (count > 0 verified) */
482 XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
483 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
485 XDrawString( display, dc->u.x.drawable, dc->u.x.gc,
486 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
488 else /* Now the fun begins... */
490 XTextItem *items, *pitem;
493 /* allocate max items */
495 pitem = items = xmalloc( count * sizeof(XTextItem) );
499 /* initialize text item with accumulated delta */
501 pitem->chars = (char *)str + i;
502 pitem->delta = delta;
507 /* stuff characters into the same XTextItem until new delta
508 * becomes non-zero */
512 if (lpDx) delta += lpDx[i] - XTextWidth( font, str + i, 1);
515 delta += dc->w.charExtra;
516 if (str[i] == (char)dc->u.x.font.metrics.tmBreakChar)
517 delta += dc->w.breakExtra;
521 while ((++i < count) && !delta);
525 XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
526 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
530 /* Draw underline and strike-out if needed */
532 if (dc->u.x.font.metrics.tmUnderlined)
534 long linePos, lineWidth;
535 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
536 linePos = font->descent-1;
537 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
539 else if (lineWidth == 1) lineWidth = 0;
540 XSetLineAttributes( display, dc->u.x.gc, lineWidth,
541 LineSolid, CapRound, JoinBevel );
542 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
543 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
544 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
546 if (dc->u.x.font.metrics.tmStruckOut)
548 long lineAscent, lineDescent;
549 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
550 lineAscent = font->ascent / 3;
551 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
552 lineDescent = -lineAscent;
553 XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
554 LineSolid, CapRound, JoinBevel );
555 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
556 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
557 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
560 if (flags & ETO_CLIPPED) SelectClipRgn32( hdc, hRgnClip );
565 /***********************************************************************
566 * ExtTextOut32A (GDI32.98)
568 BOOL32 ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
569 const RECT32 *lprect, LPCSTR str, UINT32 count,
574 if (lpDx) fprintf( stderr, "ExtTextOut32A: lpDx not implemented\n" );
576 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
577 NULL, str, (UINT16)count, NULL );
578 CONV_RECT32TO16( lprect, &rect16 );
579 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
580 &rect16, str, (UINT16)count, NULL );
584 /***********************************************************************
585 * ExtTextOut32W (GDI32.99)
587 BOOL32 ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
588 const RECT32 *lprect, LPCWSTR str, UINT32 count,
591 char *p = STRING32_DupUniToAnsi( str );
592 INT32 ret = ExtTextOut32A( hdc, x, y, flags, lprect, p, count, lpDx );
598 /***********************************************************************
601 BOOL16 TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
603 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
607 /***********************************************************************
608 * TextOut32A (GDI32.355)
610 BOOL32 TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
612 return ExtTextOut32A( hdc, x, y, 0, NULL, str, count, NULL );
616 /***********************************************************************
617 * TextOut32W (GDI32.356)
619 BOOL32 TextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count )
621 return ExtTextOut32W( hdc, x, y, 0, NULL, str, count, NULL );
625 /***********************************************************************
626 * GrayString (USER.185)
628 BOOL GrayString(HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc, LPARAM lParam,
629 INT cch, INT x, INT y, INT cx, INT cy)
632 COLORREF current_color;
634 if (!cch) cch = lstrlen16( (LPCSTR)PTR_SEG_TO_LIN(lParam) );
635 if (gsprc) return gsprc( hdc, lParam, cch );
636 current_color = GetTextColor( hdc );
637 SetTextColor( hdc, GetSysColor(COLOR_GRAYTEXT) );
638 ret = TextOut16( hdc, x, y, (LPCSTR)PTR_SEG_TO_LIN(lParam), cch );
639 SetTextColor( hdc, current_color );
644 /***********************************************************************
647 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
648 * Note: this doesn't work too well for text-alignment modes other
649 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
651 LONG TEXT_TabbedTextOut( HDC16 hdc, int x, int y, LPSTR lpstr, int count,
652 int cTabStops, LPINT16 lpTabPos, int nTabOrg,
662 defWidth = *lpTabPos;
668 GetTextMetrics16( hdc, &tm );
669 defWidth = 8 * tm.tmAveCharWidth;
674 for (i = 0; i < count; i++)
675 if (lpstr[i] == '\t') break;
676 extent = GetTextExtent( hdc, lpstr, i );
677 while ((cTabStops > 0) && (nTabOrg + *lpTabPos <= x + LOWORD(extent)))
683 tabPos = x + LOWORD(extent);
684 else if (cTabStops > 0)
685 tabPos = nTabOrg + *lpTabPos;
687 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
691 SetRect16( &r, x, y, tabPos, y+HIWORD(extent) );
692 ExtTextOut16( hdc, x, y,
693 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
694 &r, lpstr, i, NULL );
700 return MAKELONG(tabPos - start, HIWORD(extent));
704 /***********************************************************************
705 * TabbedTextOut (USER.196)
707 LONG TabbedTextOut( HDC16 hdc, short x, short y, LPSTR lpstr, short count,
708 short cTabStops, LPINT16 lpTabPos, short nTabOrg )
710 dprintf_text( stddeb, "TabbedTextOut: %04x %d,%d '%*.*s' %d\n",
711 hdc, x, y, count, count, lpstr, count );
712 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
713 lpTabPos, nTabOrg, TRUE );
717 /***********************************************************************
718 * GetTabbedTextExtent (USER.197)
720 DWORD GetTabbedTextExtent( HDC16 hdc, LPSTR lpstr, int count,
721 int cTabStops, LPINT16 lpTabPos )
723 dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%*.*s' %d\n",
724 hdc, count, count, lpstr, count );
725 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
726 lpTabPos, 0, FALSE );