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
))
89 if (str
[++i
] != PREFIX
)
96 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
97 (format
& DT_WORDBREAK
))
99 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
106 if (format
& DT_EXPANDTABS
)
112 if (!GetTextExtentPoint16(hdc
, &dest
[lasttab
], j
- lasttab
,
116 numspaces
= (tabwidth
- size
.cx
) / spacewidth
;
117 for (k
= 0; k
< numspaces
; k
++)
119 plen
+= tabwidth
- size
.cx
;
120 lasttab
= wb_j
+ numspaces
;
124 dest
[j
++] = str
[i
++];
125 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
126 (format
& DT_WORDBREAK
))
128 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
136 dest
[j
++] = str
[i
++];
137 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
138 (format
& DT_WORDBREAK
))
143 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
150 dest
[j
++] = str
[i
++];
151 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
152 (format
& DT_WORDBREAK
))
154 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
161 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
165 if (format
& DT_WORDBREAK
)
170 *count
= wb_count
- 1;
188 /***********************************************************************
189 * DrawText16 (USER.85)
191 INT16
DrawText16( HDC16 hdc
, LPCSTR str
, INT16 i_count
,
192 LPRECT16 rect
, UINT16 flags
)
196 static char line
[1024];
197 int len
, lh
, count
=i_count
;
201 int x
= rect
->left
, y
= rect
->top
;
202 int width
= rect
->right
- rect
->left
;
205 dprintf_text(stddeb
,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str
,
206 count
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
208 if (count
== -1) count
= strlen(str
);
211 GetTextMetrics16(hdc
, &tm
);
212 if (flags
& DT_EXTERNALLEADING
)
213 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
217 if (flags
& DT_TABSTOP
)
218 tabstop
= flags
>> 8;
220 if (flags
& DT_EXPANDTABS
)
222 GetTextExtentPoint16(hdc
, " ", 1, &size
);
223 spacewidth
= size
.cx
;
224 GetTextExtentPoint16(hdc
, "o", 1, &size
);
225 tabwidth
= size
.cx
* tabstop
;
228 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
233 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
235 if (prefix_offset
!= -1)
237 GetTextExtentPoint16(hdc
, line
, prefix_offset
, &size
);
239 GetTextExtentPoint16(hdc
, line
, prefix_offset
+ 1, &size
);
240 prefix_end
= size
.cx
- 1;
243 if (!GetTextExtentPoint16(hdc
, line
, len
, &size
)) return 0;
244 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
246 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
248 if (flags
& DT_SINGLELINE
)
250 if (flags
& DT_VCENTER
) y
= rect
->top
+
251 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
252 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
254 if (!(flags
& DT_CALCRECT
))
256 if (!ExtTextOut16(hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
257 rect
, line
, len
, NULL
)) return 0;
258 if (prefix_offset
!= -1)
260 HPEN32 hpen
= CreatePen32( PS_SOLID
, 1, GetTextColor32(hdc
) );
261 HPEN32 oldPen
= SelectObject32( hdc
, hpen
);
262 MoveTo(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
263 LineTo32(hdc
, x
+ prefix_end
, y
+ tm
.tmAscent
+ 1 );
264 SelectObject32( hdc
, oldPen
);
265 DeleteObject32( hpen
);
268 else if (size
.cx
> max_width
)
274 if (!(flags
& DT_NOCLIP
))
276 if (y
> rect
->bottom
- lh
)
282 if (flags
& DT_CALCRECT
)
284 rect
->right
= rect
->left
+ max_width
;
287 return y
- rect
->top
;
291 /***********************************************************************
292 * DrawText32A (USER32.163)
294 INT32
DrawText32A( HDC32 hdc
, LPCSTR str
, INT32 count
,
295 LPRECT32 rect
, UINT32 flags
)
301 return DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, NULL
, (UINT16
)flags
);
302 CONV_RECT32TO16( rect
, &rect16
);
303 ret
= DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, &rect16
, (UINT16
)flags
);
304 CONV_RECT16TO32( &rect16
, rect
);
309 /***********************************************************************
310 * DrawText32W (USER32.166)
312 INT32
DrawText32W( HDC32 hdc
, LPCWSTR str
, INT32 count
,
313 LPRECT32 rect
, UINT32 flags
)
315 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
316 INT32 ret
= DrawText32A( hdc
, p
, count
, rect
, flags
);
317 HeapFree( GetProcessHeap(), 0, p
);
322 /***********************************************************************
323 * ExtTextOut16 (GDI.351)
325 BOOL16
ExtTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, UINT16 flags
,
326 const RECT16
*lprect
, LPCSTR str
, UINT16 count
,
330 int dir
, ascent
, descent
, i
;
335 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
338 dc
= (DC
*)GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
);
339 if (!dc
) return FALSE
;
340 MF_ExtTextOut( dc
, x
, y
, flags
, lprect
, str
, count
, lpDx
);
344 if (!DC_SetupGCForText( dc
)) return TRUE
;
345 font
= dc
->u
.x
.font
.fstruct
;
347 dprintf_text(stddeb
,"ExtTextOut: hdc=%04x %d,%d '%*.*s', %d flags=%d\n",
348 hdc
, x
, y
, count
, count
, str
, count
, flags
);
349 if (lprect
!= NULL
) dprintf_text(stddeb
, "\trect=(%d,%d- %d,%d)\n",
350 lprect
->left
, lprect
->top
,
351 lprect
->right
, lprect
->bottom
);
353 /* Setup coordinates */
355 if (dc
->w
.textAlign
& TA_UPDATECP
)
361 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* there's a rectangle */
363 if (!lprect
) /* not always */
366 if (flags
& ETO_CLIPPED
) /* Can't clip with no rectangle */
368 if (!GetTextExtentPoint16( hdc
, str
, count
, &sz
))
370 rect
.left
= XLPTODP( dc
, x
);
371 rect
.right
= XLPTODP( dc
, x
+sz
.cx
);
372 rect
.top
= YLPTODP( dc
, y
);
373 rect
.bottom
= YLPTODP( dc
, y
+sz
.cy
);
377 rect
.left
= XLPTODP( dc
, lprect
->left
);
378 rect
.right
= XLPTODP( dc
, lprect
->right
);
379 rect
.top
= YLPTODP( dc
, lprect
->top
);
380 rect
.bottom
= YLPTODP( dc
, lprect
->bottom
);
382 if (rect
.right
< rect
.left
) SWAP_INT( rect
.left
, rect
.right
);
383 if (rect
.bottom
< rect
.top
) SWAP_INT( rect
.top
, rect
.bottom
);
386 x
= XLPTODP( dc
, x
);
387 y
= YLPTODP( dc
, y
);
389 dprintf_text(stddeb
,"\treal coord: x=%i, y=%i, rect=(%d,%d-%d,%d)\n",
390 x
, y
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
392 /* Draw the rectangle */
394 if (flags
& ETO_OPAQUE
)
396 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
397 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
398 dc
->w
.DCOrgX
+ rect
.left
, dc
->w
.DCOrgY
+ rect
.top
,
399 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
401 if (!count
) return TRUE
; /* Nothing more to do */
403 /* Compute text starting position */
405 XTextExtents( font
, str
, count
, &dir
, &ascent
, &descent
, &info
);
407 if (lpDx
) /* have explicit character cell x offsets */
409 /* sum lpDx array and add the width of last character */
411 info
.width
= XTextWidth( font
, str
+ count
- 1, 1) + dc
->w
.charExtra
;
412 if (str
[count
-1] == (char)dc
->u
.x
.font
.metrics
.tmBreakChar
)
413 info
.width
+= dc
->w
.breakExtra
;
415 for (i
= 0; i
< count
; i
++) info
.width
+= lpDx
[i
];
418 info
.width
+= count
*dc
->w
.charExtra
+ dc
->w
.breakExtra
*dc
->w
.breakCount
;
420 switch( dc
->w
.textAlign
& (TA_LEFT
| TA_RIGHT
| TA_CENTER
) )
423 if (dc
->w
.textAlign
& TA_UPDATECP
)
424 dc
->w
.CursPosX
= XDPTOLP( dc
, x
+ info
.width
);
428 if (dc
->w
.textAlign
& TA_UPDATECP
) dc
->w
.CursPosX
= XDPTOLP( dc
, x
);
435 switch( dc
->w
.textAlign
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
447 /* Set the clip region */
449 if (flags
& ETO_CLIPPED
)
451 hRgnClip
= dc
->w
.hClipRgn
;
452 CLIPPING_IntersectClipRect( dc
, rect
.left
, rect
.top
, rect
.right
,
453 rect
.bottom
, CLIP_INTERSECT
|CLIP_KEEPRGN
);
456 /* Draw the text background if necessary */
458 if (dc
->w
.backgroundMode
!= TRANSPARENT
)
460 /* If rectangle is opaque and clipped, do nothing */
461 if (!(flags
& ETO_CLIPPED
) || !(flags
& ETO_OPAQUE
))
463 /* Only draw if rectangle is not opaque or if some */
464 /* text is outside the rectangle */
465 if (!(flags
& ETO_OPAQUE
) ||
467 (x
+ info
.width
>= rect
.right
) ||
468 (y
-font
->ascent
< rect
.top
) ||
469 (y
+font
->descent
>= rect
.bottom
))
471 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
472 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
474 dc
->w
.DCOrgY
+ y
- font
->ascent
,
476 font
->ascent
+ font
->descent
);
481 /* Draw the text (count > 0 verified) */
483 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.textPixel
);
484 if (!dc
->w
.charExtra
&& !dc
->w
.breakExtra
&& !lpDx
)
486 XDrawString( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
487 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, str
, count
);
489 else /* Now the fun begins... */
491 XTextItem
*items
, *pitem
;
494 /* allocate max items */
496 pitem
= items
= xmalloc( count
* sizeof(XTextItem
) );
500 /* initialize text item with accumulated delta */
502 pitem
->chars
= (char *)str
+ i
;
503 pitem
->delta
= delta
;
508 /* stuff characters into the same XTextItem until new delta
509 * becomes non-zero */
513 if (lpDx
) delta
+= lpDx
[i
] - XTextWidth( font
, str
+ i
, 1);
516 delta
+= dc
->w
.charExtra
;
517 if (str
[i
] == (char)dc
->u
.x
.font
.metrics
.tmBreakChar
)
518 delta
+= dc
->w
.breakExtra
;
522 while ((++i
< count
) && !delta
);
526 XDrawText( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
527 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, items
, pitem
- items
);
531 /* Draw underline and strike-out if needed */
533 if (dc
->u
.x
.font
.metrics
.tmUnderlined
)
535 long linePos
, lineWidth
;
536 if (!XGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
537 linePos
= font
->descent
-1;
538 if (!XGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
540 else if (lineWidth
== 1) lineWidth
= 0;
541 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineWidth
,
542 LineSolid
, CapRound
, JoinBevel
);
543 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
544 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
+ linePos
,
545 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
+ linePos
);
547 if (dc
->u
.x
.font
.metrics
.tmStruckOut
)
549 long lineAscent
, lineDescent
;
550 if (!XGetFontProperty( font
, XA_STRIKEOUT_ASCENT
, &lineAscent
))
551 lineAscent
= font
->ascent
/ 3;
552 if (!XGetFontProperty( font
, XA_STRIKEOUT_DESCENT
, &lineDescent
))
553 lineDescent
= -lineAscent
;
554 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineAscent
+ lineDescent
,
555 LineSolid
, CapRound
, JoinBevel
);
556 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
557 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
- lineAscent
,
558 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
- lineAscent
);
561 if (flags
& ETO_CLIPPED
) SelectClipRgn32( hdc
, hRgnClip
);
566 /***********************************************************************
567 * ExtTextOut32A (GDI32.98)
569 BOOL32
ExtTextOut32A( HDC32 hdc
, INT32 x
, INT32 y
, UINT32 flags
,
570 const RECT32
*lprect
, LPCSTR str
, UINT32 count
,
575 if (lpDx
) fprintf( stderr
, "ExtTextOut32A: lpDx not implemented\n" );
577 return ExtTextOut16( (HDC16
)hdc
, (INT16
)x
, (INT16
)y
, (UINT16
)flags
,
578 NULL
, str
, (UINT16
)count
, NULL
);
579 CONV_RECT32TO16( lprect
, &rect16
);
580 return ExtTextOut16( (HDC16
)hdc
, (INT16
)x
, (INT16
)y
, (UINT16
)flags
,
581 &rect16
, str
, (UINT16
)count
, NULL
);
585 /***********************************************************************
586 * ExtTextOut32W (GDI32.99)
588 BOOL32
ExtTextOut32W( HDC32 hdc
, INT32 x
, INT32 y
, UINT32 flags
,
589 const RECT32
*lprect
, LPCWSTR str
, UINT32 count
,
592 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
593 INT32 ret
= ExtTextOut32A( hdc
, x
, y
, flags
, lprect
, p
, count
, lpDx
);
594 HeapFree( GetProcessHeap(), 0, p
);
599 /***********************************************************************
602 BOOL16
TextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR str
, INT16 count
)
604 return ExtTextOut16( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
608 /***********************************************************************
609 * TextOut32A (GDI32.355)
611 BOOL32
TextOut32A( HDC32 hdc
, INT32 x
, INT32 y
, LPCSTR str
, INT32 count
)
613 return ExtTextOut32A( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
617 /***********************************************************************
618 * TextOut32W (GDI32.356)
620 BOOL32
TextOut32W( HDC32 hdc
, INT32 x
, INT32 y
, LPCWSTR str
, INT32 count
)
622 return ExtTextOut32W( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
626 /***********************************************************************
627 * GrayString (USER.185)
629 BOOL
GrayString(HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
, LPARAM lParam
,
630 INT cch
, INT x
, INT y
, INT cx
, INT cy
)
633 COLORREF current_color
;
635 if (!cch
) cch
= lstrlen16( (LPCSTR
)PTR_SEG_TO_LIN(lParam
) );
636 if (gsprc
) return gsprc( hdc
, lParam
, cch
);
637 current_color
= GetTextColor32( hdc
);
638 SetTextColor( hdc
, GetSysColor(COLOR_GRAYTEXT
) );
639 ret
= TextOut16( hdc
, x
, y
, (LPCSTR
)PTR_SEG_TO_LIN(lParam
), cch
);
640 SetTextColor( hdc
, current_color
);
645 /***********************************************************************
648 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
649 * Note: this doesn't work too well for text-alignment modes other
650 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
652 LONG
TEXT_TabbedTextOut( HDC16 hdc
, int x
, int y
, LPSTR lpstr
, int count
,
653 int cTabStops
, LPINT16 lpTabPos
, int nTabOrg
,
663 defWidth
= *lpTabPos
;
669 GetTextMetrics16( hdc
, &tm
);
670 defWidth
= 8 * tm
.tmAveCharWidth
;
675 for (i
= 0; i
< count
; i
++)
676 if (lpstr
[i
] == '\t') break;
677 extent
= GetTextExtent( hdc
, lpstr
, i
);
678 while ((cTabStops
> 0) && (nTabOrg
+ *lpTabPos
<= x
+ LOWORD(extent
)))
684 tabPos
= x
+ LOWORD(extent
);
685 else if (cTabStops
> 0)
686 tabPos
= nTabOrg
+ *lpTabPos
;
688 tabPos
= nTabOrg
+ ((x
+ LOWORD(extent
) - nTabOrg
) / defWidth
+ 1) * defWidth
;
692 SetRect16( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
693 ExtTextOut16( hdc
, x
, y
,
694 GetBkMode32(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
695 &r
, lpstr
, i
, NULL
);
701 return MAKELONG(tabPos
- start
, HIWORD(extent
));
705 /***********************************************************************
706 * TabbedTextOut (USER.196)
708 LONG
TabbedTextOut( HDC16 hdc
, short x
, short y
, LPSTR lpstr
, short count
,
709 short cTabStops
, LPINT16 lpTabPos
, short nTabOrg
)
711 dprintf_text( stddeb
, "TabbedTextOut: %04x %d,%d '%*.*s' %d\n",
712 hdc
, x
, y
, count
, count
, lpstr
, count
);
713 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
714 lpTabPos
, nTabOrg
, TRUE
);
718 /***********************************************************************
719 * GetTabbedTextExtent (USER.197)
721 DWORD
GetTabbedTextExtent( HDC16 hdc
, LPSTR lpstr
, int count
,
722 int cTabStops
, LPINT16 lpTabPos
)
724 dprintf_text( stddeb
, "GetTabbedTextExtent: %04x '%*.*s' %d\n",
725 hdc
, count
, count
, lpstr
, count
);
726 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
727 lpTabPos
, 0, FALSE
);