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;
35 static const char *TEXT_NextLine( HDC hdc, const char *str, int *count,
36 char *dest, int *len, int width, WORD format)
38 /* Return next line of text from a string.
41 * str - string to parse into lines.
42 * count - length of str.
43 * dest - destination in which to return line.
44 * len - length of resultant line in dest in chars.
45 * width - maximum width of line in pixels.
46 * format - format type passed to DrawText.
48 * Returns pointer to next char in str after end of the line
49 * or NULL if end of str reached.
57 int wb_i = 0, wb_j = 0, wb_count = 0;
65 if (!(format & DT_SINGLELINE))
67 if (str[i] == CR && str[i+1] == LF)
75 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
76 (format & DT_WORDBREAK))
78 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
85 if (!(format & DT_NOPREFIX))
93 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
95 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
103 if (format & DT_EXPANDTABS)
109 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
113 numspaces = (tabwidth - size.cx) / spacewidth;
114 for (k = 0; k < numspaces; k++)
116 plen += tabwidth - size.cx;
117 lasttab = wb_j + numspaces;
121 dest[j++] = str[i++];
122 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
123 (format & DT_WORDBREAK))
125 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
133 dest[j++] = str[i++];
134 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
135 (format & DT_WORDBREAK))
140 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
147 dest[j++] = str[i++];
148 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
149 (format & DT_WORDBREAK))
151 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
158 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
162 if (format & DT_WORDBREAK)
167 *count = wb_count - 1;
185 /***********************************************************************
186 * DrawText16 (USER.85)
188 INT16 DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
189 LPRECT16 rect, UINT16 flags )
193 static char line[1024];
194 int len, lh, count=i_count;
198 int x = rect->left, y = rect->top;
199 int width = rect->right - rect->left;
202 dprintf_text(stddeb,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str,
203 count, rect->left, rect->top, rect->right, rect->bottom);
205 if (count == -1) count = strlen(str);
208 GetTextMetrics16(hdc, &tm);
209 if (flags & DT_EXTERNALLEADING)
210 lh = tm.tmHeight + tm.tmExternalLeading;
214 if (flags & DT_TABSTOP)
215 tabstop = flags >> 8;
217 if (flags & DT_EXPANDTABS)
219 GetTextExtentPoint16(hdc, " ", 1, &size);
220 spacewidth = size.cx;
221 GetTextExtentPoint16(hdc, "o", 1, &size);
222 tabwidth = size.cx * tabstop;
228 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
230 if (prefix_offset != -1)
232 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
234 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
235 prefix_end = size.cx - 1;
238 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
239 if (flags & DT_CENTER) x = (rect->left + rect->right -
241 else if (flags & DT_RIGHT) x = rect->right - size.cx;
243 if (flags & DT_SINGLELINE)
245 if (flags & DT_VCENTER) y = rect->top +
246 (rect->bottom - rect->top) / 2 - size.cy / 2;
247 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
249 if (!(flags & DT_CALCRECT))
251 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
252 rect, line, len, NULL )) return 0;
253 if (prefix_offset != -1)
255 HPEN16 hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
256 HPEN16 oldPen = SelectObject( hdc, hpen );
257 MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
258 LineTo(hdc, x + prefix_end, y + tm.tmAscent + 1 );
259 SelectObject( hdc, oldPen );
260 DeleteObject( hpen );
263 else if (size.cx > max_width)
269 if (!(flags & DT_NOCLIP) && !(flags & DT_CALCRECT))
271 if (y > rect->bottom - lh)
277 if (flags & DT_CALCRECT)
279 rect->right = rect->left + max_width;
286 /***********************************************************************
287 * DrawText32A (USER32.163)
289 INT32 DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
290 LPRECT32 rect, UINT32 flags )
296 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
297 CONV_RECT32TO16( rect, &rect16 );
298 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
299 CONV_RECT16TO32( &rect16, rect );
304 /***********************************************************************
305 * DrawText32W (USER32.166)
307 INT32 DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
308 LPRECT32 rect, UINT32 flags )
310 char *p = STRING32_DupUniToAnsi( str );
311 INT32 ret = DrawText32A( hdc, p, count, rect, flags );
317 /***********************************************************************
318 * ExtTextOut16 (GDI.351)
320 BOOL16 ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
321 const RECT16 *lprect, LPCSTR str, UINT16 count,
324 int dir, ascent, descent, i;
329 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
332 dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
333 if (!dc) return FALSE;
334 MF_ExtTextOut( dc, x, y, flags, lprect, str, count, lpDx );
338 if (!DC_SetupGCForText( dc )) return TRUE;
339 font = dc->u.x.font.fstruct;
341 dprintf_text(stddeb,"ExtTextOut: hdc=%04x %d,%d '%*.*s', %d flags=%d\n",
342 hdc, x, y, count, count, str, count, flags);
343 if (lprect != NULL) dprintf_text(stddeb, "\trect=(%d,%d- %d,%d)\n",
344 lprect->left, lprect->top,
345 lprect->right, lprect->bottom );
347 /* Setup coordinates */
349 if (dc->w.textAlign & TA_UPDATECP)
355 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
357 if (!lprect) /* not always */
360 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
362 if (!GetTextExtentPoint16( hdc, str, count, &sz ))
364 rect.left = XLPTODP( dc, x );
365 rect.right = XLPTODP( dc, x+sz.cx );
366 rect.top = YLPTODP( dc, y );
367 rect.bottom = YLPTODP( dc, y+sz.cy );
371 rect.left = XLPTODP( dc, lprect->left );
372 rect.right = XLPTODP( dc, lprect->right );
373 rect.top = YLPTODP( dc, lprect->top );
374 rect.bottom = YLPTODP( dc, lprect->bottom );
376 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
377 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
380 x = XLPTODP( dc, x );
381 y = YLPTODP( dc, y );
383 dprintf_text(stddeb,"\treal coord: x=%i, y=%i, rect=(%d,%d-%d,%d)\n",
384 x, y, rect.left, rect.top, rect.right, rect.bottom);
386 /* Draw the rectangle */
388 if (flags & ETO_OPAQUE)
390 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
391 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
392 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
393 rect.right-rect.left, rect.bottom-rect.top );
395 if (!count) return TRUE; /* Nothing more to do */
397 /* Compute text starting position */
399 XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
401 if (lpDx) /* have explicit character cell x offsets */
403 /* sum lpDx array and add the width of last character */
405 info.width = XTextWidth( font, str + count - 1, 1) + dc->w.charExtra;
406 if (str[count-1] == (char)dc->u.x.font.metrics.tmBreakChar)
407 info.width += dc->w.breakExtra;
409 for (i = 0; i < count; i++) info.width += lpDx[i];
412 info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
414 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
417 if (dc->w.textAlign & TA_UPDATECP)
418 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
422 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
429 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
441 /* Set the clip region */
443 if (flags & ETO_CLIPPED)
446 IntersectVisRect( hdc, rect.left, rect.top, rect.right, rect.bottom );
449 /* Draw the text background if necessary */
451 if (dc->w.backgroundMode != TRANSPARENT)
453 /* If rectangle is opaque and clipped, do nothing */
454 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
456 /* Only draw if rectangle is not opaque or if some */
457 /* text is outside the rectangle */
458 if (!(flags & ETO_OPAQUE) ||
460 (x + info.width >= rect.right) ||
461 (y-font->ascent < rect.top) ||
462 (y+font->descent >= rect.bottom))
464 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
465 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
467 dc->w.DCOrgY + y - font->ascent,
469 font->ascent + font->descent );
474 /* Draw the text (count > 0 verified) */
476 XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
477 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
479 XDrawString( display, dc->u.x.drawable, dc->u.x.gc,
480 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
482 else /* Now the fun begins... */
484 XTextItem *items, *pitem;
487 /* allocate max items */
489 pitem = items = xmalloc( count * sizeof(XTextItem) );
493 /* initialize text item with accumulated delta */
495 pitem->chars = (char *)str + i;
496 pitem->delta = delta;
501 /* stuff characters into the same XTextItem until new delta
502 * becomes non-zero */
506 if (lpDx) delta += lpDx[i] - XTextWidth( font, str + i, 1);
509 delta += dc->w.charExtra;
510 if (str[i] == (char)dc->u.x.font.metrics.tmBreakChar)
511 delta += dc->w.breakExtra;
515 while ((++i < count) && !delta);
519 XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
520 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
524 /* Draw underline and strike-out if needed */
526 if (dc->u.x.font.metrics.tmUnderlined)
528 long linePos, lineWidth;
529 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
530 linePos = font->descent-1;
531 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
533 else if (lineWidth == 1) lineWidth = 0;
534 XSetLineAttributes( display, dc->u.x.gc, lineWidth,
535 LineSolid, CapRound, JoinBevel );
536 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
537 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
538 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
540 if (dc->u.x.font.metrics.tmStruckOut)
542 long lineAscent, lineDescent;
543 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
544 lineAscent = font->ascent / 3;
545 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
546 lineDescent = -lineAscent;
547 XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
548 LineSolid, CapRound, JoinBevel );
549 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
550 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
551 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
553 if (flags & ETO_CLIPPED) RestoreVisRgn( hdc );
558 /***********************************************************************
559 * ExtTextOut32A (GDI32.98)
561 BOOL32 ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
562 const RECT32 *lprect, LPCSTR str, UINT32 count,
567 if (lpDx) fprintf( stderr, "ExtTextOut32A: lpDx not implemented\n" );
569 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
570 NULL, str, (UINT16)count, NULL );
571 CONV_RECT32TO16( lprect, &rect16 );
572 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
573 &rect16, str, (UINT16)count, NULL );
577 /***********************************************************************
578 * ExtTextOut32W (GDI32.99)
580 BOOL32 ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
581 const RECT32 *lprect, LPCWSTR str, UINT32 count,
584 char *p = STRING32_DupUniToAnsi( str );
585 INT32 ret = ExtTextOut32A( hdc, x, y, flags, lprect, p, count, lpDx );
591 /***********************************************************************
594 BOOL16 TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
596 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
600 /***********************************************************************
601 * TextOut32A (GDI32.355)
603 BOOL32 TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
605 return ExtTextOut32A( hdc, x, y, 0, NULL, str, count, NULL );
609 /***********************************************************************
610 * TextOut32W (GDI32.356)
612 BOOL32 TextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count )
614 return ExtTextOut32W( hdc, x, y, 0, NULL, str, count, NULL );
618 /***********************************************************************
619 * GrayString (USER.185)
621 BOOL GrayString(HDC hdc, HBRUSH hbr, GRAYSTRINGPROC16 gsprc, LPARAM lParam,
622 INT cch, INT x, INT y, INT cx, INT cy)
625 COLORREF current_color;
627 if (!cch) cch = lstrlen16( (LPCSTR)PTR_SEG_TO_LIN(lParam) );
628 if (gsprc) return gsprc( hdc, lParam, cch );
629 current_color = GetTextColor( hdc );
630 SetTextColor( hdc, GetSysColor(COLOR_GRAYTEXT) );
631 ret = TextOut16( hdc, x, y, (LPCSTR)PTR_SEG_TO_LIN(lParam), cch );
632 SetTextColor( hdc, current_color );
637 /***********************************************************************
640 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
641 * Note: this doesn't work too well for text-alignment modes other
642 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
644 LONG TEXT_TabbedTextOut( HDC hdc, int x, int y, LPSTR lpstr, int count,
645 int cTabStops, LPINT16 lpTabPos, int nTabOrg,
655 defWidth = *lpTabPos;
661 GetTextMetrics16( hdc, &tm );
662 defWidth = 8 * tm.tmAveCharWidth;
667 for (i = 0; i < count; i++)
668 if (lpstr[i] == '\t') break;
669 extent = GetTextExtent( hdc, lpstr, i );
670 while ((cTabStops > 0) && (nTabOrg + *lpTabPos <= x + LOWORD(extent)))
676 tabPos = x + LOWORD(extent);
677 else if (cTabStops > 0)
678 tabPos = nTabOrg + *lpTabPos;
680 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
684 SetRect16( &r, x, y, tabPos, y+HIWORD(extent) );
685 ExtTextOut16( hdc, x, y,
686 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
687 &r, lpstr, i, NULL );
693 return MAKELONG(tabPos - start, HIWORD(extent));
697 /***********************************************************************
698 * TabbedTextOut (USER.196)
700 LONG TabbedTextOut( HDC hdc, short x, short y, LPSTR lpstr, short count,
701 short cTabStops, LPINT16 lpTabPos, short nTabOrg )
703 dprintf_text( stddeb, "TabbedTextOut: %04x %d,%d '%*.*s' %d\n",
704 hdc, x, y, count, count, lpstr, count );
705 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
706 lpTabPos, nTabOrg, TRUE );
710 /***********************************************************************
711 * GetTabbedTextExtent (USER.197)
713 DWORD GetTabbedTextExtent( HDC hdc, LPSTR lpstr, int count,
714 int cTabStops, LPINT16 lpTabPos )
716 dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%*.*s' %d\n",
717 hdc, count, count, lpstr, count );
718 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
719 lpTabPos, 0, FALSE );