4 * Copyright 1993, 1994 Alexandre Julliard
16 /* #define DEBUG_TEXT */
26 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
28 static int tabstop
= 8;
30 static int spacewidth
;
31 static int prefix_offset
;
34 static const char *TEXT_NextLine( HDC hdc
, const char *str
, int *count
,
35 char *dest
, int *len
, int width
, WORD format
)
37 /* Return next line of text from a string.
40 * str - string to parse into lines.
41 * count - length of str.
42 * dest - destination in which to return line.
43 * len - length of resultant line in dest in chars.
44 * width - maximum width of line in pixels.
45 * format - format type passed to DrawText.
47 * Returns pointer to next char in str after end of the line
48 * or NULL if end of str reached.
56 int wb_i
= 0, wb_j
= 0, wb_count
= 0;
64 if (!(format
& DT_SINGLELINE
))
66 if (str
[i
] == CR
&& str
[i
+1] == LF
)
74 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
75 (format
& DT_WORDBREAK
))
77 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
84 if (!(format
& DT_NOPREFIX
))
92 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
94 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
102 if (format
& DT_EXPANDTABS
)
108 if (!GetTextExtentPoint(hdc
, &dest
[lasttab
], j
- lasttab
,
112 numspaces
= (tabwidth
- size
.cx
) / spacewidth
;
113 for (k
= 0; k
< numspaces
; k
++)
115 plen
+= tabwidth
- size
.cx
;
116 lasttab
= wb_j
+ numspaces
;
120 dest
[j
++] = str
[i
++];
121 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
122 (format
& DT_WORDBREAK
))
124 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
132 dest
[j
++] = str
[i
++];
133 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
134 (format
& DT_WORDBREAK
))
139 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
146 dest
[j
++] = str
[i
++];
147 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
148 (format
& DT_WORDBREAK
))
150 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
157 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
161 if (format
& DT_WORDBREAK
)
166 *count
= wb_count
- 1;
184 /***********************************************************************
187 INT
DrawText( HDC hdc
, LPCTSTR str
, INT i_count
, LPRECT rect
, UINT flags
)
191 static char line
[1024];
192 int len
, lh
, count
=i_count
;
196 int x
= rect
->left
, y
= rect
->top
;
197 int width
= rect
->right
- rect
->left
;
200 dprintf_text(stddeb
,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str
,
201 count
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
203 if (count
== -1) count
= strlen(str
);
206 GetTextMetrics(hdc
, &tm
);
207 if (flags
& DT_EXTERNALLEADING
)
208 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
212 if (flags
& DT_TABSTOP
)
213 tabstop
= flags
>> 8;
215 if (flags
& DT_EXPANDTABS
)
217 GetTextExtentPoint(hdc
, " ", 1, &size
);
218 spacewidth
= size
.cx
;
219 GetTextExtentPoint(hdc
, "o", 1, &size
);
220 tabwidth
= size
.cx
* tabstop
;
226 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
228 if (prefix_offset
!= -1)
230 GetTextExtentPoint(hdc
, line
, prefix_offset
, &size
);
232 GetTextExtentPoint(hdc
, line
, prefix_offset
+ 1, &size
);
233 prefix_end
= size
.cx
- 1;
236 if (!GetTextExtentPoint(hdc
, line
, len
, &size
)) return 0;
237 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
239 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
241 if (flags
& DT_SINGLELINE
)
243 if (flags
& DT_VCENTER
) y
= rect
->top
+
244 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
245 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
247 if (!(flags
& DT_CALCRECT
))
249 if (!ExtTextOut( hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
250 rect
, line
, len
, NULL
)) return 0;
252 else if (size
.cx
> max_width
)
255 if (prefix_offset
!= -1)
257 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
258 HPEN oldPen
= SelectObject( hdc
, hpen
);
259 MoveTo(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
260 LineTo(hdc
, x
+ prefix_end
, y
+ tm
.tmAscent
+ 1 );
261 SelectObject( hdc
, oldPen
);
262 DeleteObject( hpen
);
268 if (!(flags
& DT_NOCLIP
) && !(flags
& DT_CALCRECT
))
270 if (y
> rect
->bottom
- lh
)
276 if (flags
& DT_CALCRECT
)
278 rect
->right
= rect
->left
+ max_width
;
285 /***********************************************************************
286 * ExtTextOut (GDI.351)
288 BOOL
ExtTextOut( HDC hdc
, short x
, short y
, WORD flags
, LPRECT lprect
,
289 LPSTR str
, WORD count
, LPINT lpDx
)
291 int dir
, ascent
, descent
, i
;
296 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
299 dc
= (DC
*)GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
);
300 if (!dc
) return FALSE
;
301 MF_TextOut( dc
, x
, y
, str
, count
);
305 if (!DC_SetupGCForText( dc
)) return TRUE
;
306 font
= dc
->u
.x
.font
.fstruct
;
308 dprintf_text(stddeb
,"ExtTextOut: %d,%d '%*.*s', %d flags=%d\n",
309 x
, y
, count
, count
, str
, count
, flags
);
310 if (lprect
!= NULL
) dprintf_text(stddeb
, "rect %d %d %d %d\n",
311 lprect
->left
, lprect
->top
,
312 lprect
->right
, lprect
->bottom
);
314 /* Setup coordinates */
316 if (dc
->w
.textAlign
& TA_UPDATECP
)
321 x
= XLPTODP( dc
, x
);
322 y
= YLPTODP( dc
, y
);
323 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* There's a rectangle */
325 rect
.left
= XLPTODP( dc
, lprect
->left
);
326 rect
.right
= XLPTODP( dc
, lprect
->right
);
327 rect
.top
= YLPTODP( dc
, lprect
->top
);
328 rect
.bottom
= YLPTODP( dc
, lprect
->bottom
);
329 if (rect
.right
< rect
.left
) SWAP_INT( rect
.left
, rect
.right
);
330 if (rect
.bottom
< rect
.top
) SWAP_INT( rect
.top
, rect
.bottom
);
333 /* Draw the rectangle */
335 if (flags
& ETO_OPAQUE
)
337 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
338 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
339 dc
->w
.DCOrgX
+ rect
.left
, dc
->w
.DCOrgY
+ rect
.top
,
340 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
342 if (!count
) return TRUE
; /* Nothing more to do */
344 /* Compute text starting position */
346 XTextExtents( font
, str
, count
, &dir
, &ascent
, &descent
, &info
);
347 info
.width
+= count
*dc
->w
.charExtra
+ dc
->w
.breakExtra
*dc
->w
.breakCount
;
348 if (lpDx
) for (i
= 0; i
< count
; i
++) info
.width
+= lpDx
[i
];
350 switch( dc
->w
.textAlign
& (TA_LEFT
| TA_RIGHT
| TA_CENTER
) )
353 if (dc
->w
.textAlign
& TA_UPDATECP
)
354 dc
->w
.CursPosX
= XDPTOLP( dc
, x
+ info
.width
);
358 if (dc
->w
.textAlign
& TA_UPDATECP
) dc
->w
.CursPosX
= XDPTOLP( dc
, x
);
364 switch( dc
->w
.textAlign
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
376 /* Set the clip region */
378 if (flags
& ETO_CLIPPED
)
381 IntersectVisRect( hdc
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
384 /* Draw the text background if necessary */
386 if (dc
->w
.backgroundMode
!= TRANSPARENT
)
388 /* If rectangle is opaque and clipped, do nothing */
389 if (!(flags
& ETO_CLIPPED
) || !(flags
& ETO_OPAQUE
))
391 /* Only draw if rectangle is not opaque or if some */
392 /* text is outside the rectangle */
393 if (!(flags
& ETO_OPAQUE
) ||
395 (x
+ info
.width
>= rect
.right
) ||
396 (y
-font
->ascent
< rect
.top
) ||
397 (y
+font
->descent
>= rect
.bottom
))
399 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
400 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
402 dc
->w
.DCOrgY
+ y
- font
->ascent
,
404 font
->ascent
+ font
->descent
);
411 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.textPixel
);
412 if (!dc
->w
.charExtra
&& !dc
->w
.breakExtra
&& !lpDx
)
414 XDrawString( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
415 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, str
, count
);
417 else /* Now the fun begins... */
419 XTextItem
*items
, *pitem
;
421 items
= xmalloc( count
* sizeof(XTextItem
) );
422 for (i
= 0, pitem
= items
; i
< count
; i
++, pitem
++)
424 pitem
->chars
= str
+ i
;
430 continue; /* First iteration -> no delta */
432 pitem
->delta
= dc
->w
.charExtra
;
433 if (str
[i
] == (char)dc
->u
.x
.font
.metrics
.tmBreakChar
)
434 pitem
->delta
+= dc
->w
.breakExtra
;
438 GetCharWidth( hdc
, str
[i
], str
[i
], &width
);
439 pitem
->delta
+= lpDx
[i
-1] - width
;
442 XDrawText( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
443 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, items
, count
);
447 /* Draw underline and strike-out if needed */
449 if (dc
->u
.x
.font
.metrics
.tmUnderlined
)
451 long linePos
, lineWidth
;
452 if (!XGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
453 linePos
= font
->descent
-1;
454 if (!XGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
456 else if (lineWidth
== 1) lineWidth
= 0;
457 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineWidth
,
458 LineSolid
, CapRound
, JoinBevel
);
459 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
460 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
+ linePos
,
461 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
+ linePos
);
463 if (dc
->u
.x
.font
.metrics
.tmStruckOut
)
465 long lineAscent
, lineDescent
;
466 if (!XGetFontProperty( font
, XA_STRIKEOUT_ASCENT
, &lineAscent
))
467 lineAscent
= font
->ascent
/ 3;
468 if (!XGetFontProperty( font
, XA_STRIKEOUT_DESCENT
, &lineDescent
))
469 lineDescent
= -lineAscent
;
470 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineAscent
+ lineDescent
,
471 LineSolid
, CapRound
, JoinBevel
);
472 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
473 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
- lineAscent
,
474 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
- lineAscent
);
476 if (flags
& ETO_CLIPPED
) RestoreVisRgn( hdc
);
481 /***********************************************************************
484 BOOL
TextOut( HDC hdc
, short x
, short y
, LPSTR str
, short count
)
486 return ExtTextOut( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
490 /***********************************************************************
491 * GrayString (USER.185)
493 BOOL
GrayString(HDC hdc
, HBRUSH hbr
, FARPROC gsprc
, LPARAM lParam
,
494 INT cch
, INT x
, INT y
, INT cx
, INT cy
)
496 int s
, current_color
;
499 return CallGrayStringProc(gsprc
, hdc
, lParam
,
500 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
502 current_color
= GetTextColor(hdc
);
503 SetTextColor(hdc
, GetSysColor(COLOR_GRAYTEXT
) );
504 s
= TextOut(hdc
, x
, y
, (LPSTR
) lParam
,
505 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
506 SetTextColor(hdc
, current_color
);
513 /***********************************************************************
516 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
517 * Note: this doesn't work too well for text-alignment modes other
518 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
520 LONG
TEXT_TabbedTextOut( HDC hdc
, int x
, int y
, LPSTR lpstr
, int count
,
521 int cTabStops
, LPINT lpTabPos
, int nTabOrg
,
531 defWidth
= *lpTabPos
;
537 GetTextMetrics( hdc
, &tm
);
538 defWidth
= 8 * tm
.tmAveCharWidth
;
543 for (i
= 0; i
< count
; i
++)
544 if (lpstr
[i
] == '\t') break;
545 extent
= GetTextExtent( hdc
, lpstr
, i
);
546 while ((cTabStops
> 0) && (nTabOrg
+ *lpTabPos
<= x
+ LOWORD(extent
)))
552 tabPos
= x
+ LOWORD(extent
);
553 else if (cTabStops
> 0)
554 tabPos
= nTabOrg
+ *lpTabPos
;
556 tabPos
= nTabOrg
+ ((x
+ LOWORD(extent
) - nTabOrg
) / defWidth
+ 1) * defWidth
;
560 SetRect( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
561 ExtTextOut( hdc
, x
, y
,
562 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
563 &r
, lpstr
, i
, NULL
);
569 return MAKELONG(tabPos
- start
, HIWORD(extent
));
573 /***********************************************************************
574 * TabbedTextOut (USER.196)
576 LONG
TabbedTextOut( HDC hdc
, short x
, short y
, LPSTR lpstr
, short count
,
577 short cTabStops
, LPINT lpTabPos
, short nTabOrg
)
579 dprintf_text( stddeb
, "TabbedTextOut: %04x %d,%d '%*.*s' %d\n",
580 hdc
, x
, y
, count
, count
, lpstr
, count
);
581 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
582 lpTabPos
, nTabOrg
, TRUE
);
586 /***********************************************************************
587 * GetTabbedTextExtent (USER.197)
589 DWORD
GetTabbedTextExtent( HDC hdc
, LPSTR lpstr
, int count
,
590 int cTabStops
, LPINT lpTabPos
)
592 dprintf_text( stddeb
, "GetTabbedTextExtent: %04x '%*.*s' %d\n",
593 hdc
, count
, count
, lpstr
, count
);
594 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
595 lpTabPos
, 0, FALSE
);