4 * Copyright 1993, 1994 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1993, 1994";
16 /* #define DEBUG_TEXT */
25 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
27 static int tabstop
= 8;
29 static int spacewidth
;
30 static int prefix_offset
;
33 static char *TEXT_NextLine(HDC hdc
, char *str
, int *count
, char *dest
,
34 int *len
, int width
, WORD format
)
36 /* Return next line of text from a string.
39 * str - string to parse into lines.
40 * count - length of str.
41 * dest - destination in which to return line.
42 * len - length of resultant line in dest in chars.
43 * width - maximum width of line in pixels.
44 * format - format type passed to DrawText.
46 * Returns pointer to next char in str after end of the line
47 * or NULL if end of str reached.
55 int wb_i
= 0, wb_j
= 0, wb_count
= 0;
63 if (!(format
& DT_SINGLELINE
))
65 if (str
[i
] == CR
&& str
[i
+1] == LF
)
73 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
74 (format
& DT_WORDBREAK
))
76 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
83 if (!(format
& DT_NOPREFIX
))
91 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
93 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
101 if (format
& DT_EXPANDTABS
)
107 if (!GetTextExtentPoint(hdc
, &dest
[lasttab
], j
- lasttab
,
111 numspaces
= (tabwidth
- size
.cx
) / spacewidth
;
112 for (k
= 0; k
< numspaces
; k
++)
114 plen
+= tabwidth
- size
.cx
;
115 lasttab
= wb_j
+ numspaces
;
119 dest
[j
++] = str
[i
++];
120 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
121 (format
& DT_WORDBREAK
))
123 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
131 dest
[j
++] = str
[i
++];
132 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
133 (format
& DT_WORDBREAK
))
138 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
145 dest
[j
++] = str
[i
++];
146 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
147 (format
& DT_WORDBREAK
))
149 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
156 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
160 if (format
& DT_WORDBREAK
)
165 *count
= wb_count
- 1;
183 /***********************************************************************
186 int DrawText( HDC hdc
, LPSTR str
, int count
, LPRECT rect
, WORD flags
)
190 static char line
[1024];
191 int len
, lh
, prefix_x
, prefix_end
;
193 int x
= rect
->left
, y
= rect
->top
;
194 int width
= rect
->right
- rect
->left
;
197 dprintf_text(stddeb
,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str
, count
,
198 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
200 if (count
== -1) count
= strlen(str
);
203 GetTextMetrics(hdc
, &tm
);
204 if (flags
& DT_EXTERNALLEADING
)
205 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
209 if (flags
& DT_TABSTOP
)
210 tabstop
= flags
>> 8;
212 if (flags
& DT_EXPANDTABS
)
214 GetTextExtentPoint(hdc
, " ", 1, &size
);
215 spacewidth
= size
.cx
;
216 GetTextExtentPoint(hdc
, "o", 1, &size
);
217 tabwidth
= size
.cx
* tabstop
;
223 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
225 if (prefix_offset
!= -1)
227 GetTextExtentPoint(hdc
, line
, prefix_offset
, &size
);
229 GetTextExtentPoint(hdc
, line
, prefix_offset
+ 1, &size
);
230 prefix_end
= size
.cx
- 1;
233 if (!GetTextExtentPoint(hdc
, line
, len
, &size
)) return 0;
234 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
236 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
238 if (flags
& DT_SINGLELINE
)
240 if (flags
& DT_VCENTER
) y
= rect
->top
+
241 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
242 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
244 if (!(flags
& DT_CALCRECT
))
246 if (!ExtTextOut( hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
247 rect
, line
, len
, NULL
)) return 0;
249 else if (size
.cx
> max_width
)
252 if (prefix_offset
!= -1)
254 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
255 HPEN oldPen
= SelectObject( hdc
, hpen
);
256 MoveTo(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
257 LineTo(hdc
, x
+ prefix_end
, y
+ tm
.tmAscent
+ 1 );
258 SelectObject( hdc
, oldPen
);
259 DeleteObject( hpen
);
265 if (!(flags
& DT_NOCLIP
))
267 if (y
> rect
->bottom
- lh
)
273 if (flags
& DT_CALCRECT
)
275 rect
->right
= rect
->left
+ max_width
;
282 /***********************************************************************
283 * ExtTextOut (GDI.351)
285 BOOL
ExtTextOut( HDC hdc
, short x
, short y
, WORD flags
, LPRECT lprect
,
286 LPSTR str
, WORD count
, LPINT lpDx
)
288 int dir
, ascent
, descent
, i
;
293 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
296 dc
= (DC
*)GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
);
297 if (!dc
) return FALSE
;
298 MF_TextOut( dc
, x
, y
, str
, count
);
302 if (!DC_SetupGCForText( dc
)) return TRUE
;
303 font
= dc
->u
.x
.font
.fstruct
;
305 dprintf_text(stddeb
,"ExtTextOut: %d,%d '%*.*s', %d flags=%d rect=%d,%d,%d,%d\n",
306 x
, y
, count
, count
, str
, count
, flags
,
307 lprect
->left
, lprect
->top
, lprect
->right
, lprect
->bottom
);
309 /* Setup coordinates */
311 if (dc
->w
.textAlign
& TA_UPDATECP
)
316 x
= XLPTODP( dc
, x
);
317 y
= YLPTODP( dc
, y
);
318 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* There's a rectangle */
320 rect
.left
= XLPTODP( dc
, lprect
->left
);
321 rect
.right
= XLPTODP( dc
, lprect
->right
);
322 rect
.top
= YLPTODP( dc
, lprect
->top
);
323 rect
.bottom
= YLPTODP( dc
, lprect
->bottom
);
324 if (rect
.right
< rect
.left
) SWAP_INT( rect
.left
, rect
.right
);
325 if (rect
.bottom
< rect
.top
) SWAP_INT( rect
.top
, rect
.bottom
);
328 /* Draw the rectangle */
330 if (flags
& ETO_OPAQUE
)
332 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
333 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
334 dc
->w
.DCOrgX
+ rect
.left
, dc
->w
.DCOrgY
+ rect
.top
,
335 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
337 if (!count
) return TRUE
; /* Nothing more to do */
339 /* Compute text starting position */
341 XTextExtents( font
, str
, count
, &dir
, &ascent
, &descent
, &info
);
342 info
.width
+= count
*dc
->w
.charExtra
+ dc
->w
.breakExtra
*dc
->w
.breakCount
;
343 if (lpDx
) for (i
= 0; i
< count
; i
++) info
.width
+= lpDx
[i
];
345 switch( dc
->w
.textAlign
& (TA_LEFT
| TA_RIGHT
| TA_CENTER
) )
348 if (dc
->w
.textAlign
& TA_UPDATECP
)
349 dc
->w
.CursPosX
= XDPTOLP( dc
, x
+ info
.width
);
353 if (dc
->w
.textAlign
& TA_UPDATECP
) dc
->w
.CursPosX
= XDPTOLP( dc
, x
);
359 switch( dc
->w
.textAlign
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
371 /* Set the clip region */
373 if (flags
& ETO_CLIPPED
)
376 IntersectVisRect( hdc
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
379 /* Draw the text background if necessary */
381 if (dc
->w
.backgroundMode
!= TRANSPARENT
)
383 /* If rectangle is opaque and clipped, do nothing */
384 if (!(flags
& ETO_CLIPPED
) || !(flags
& ETO_OPAQUE
))
386 /* Only draw if rectangle is not opaque or if some */
387 /* text is outside the rectangle */
388 if (!(flags
& ETO_OPAQUE
) ||
390 (x
+ info
.width
>= rect
.right
) ||
391 (y
-font
->ascent
< rect
.top
) ||
392 (y
+font
->descent
>= rect
.bottom
))
394 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
395 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
397 dc
->w
.DCOrgY
+ y
- font
->ascent
,
399 font
->ascent
+ font
->descent
);
406 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.textPixel
);
407 if (!dc
->w
.charExtra
&& !dc
->w
.breakExtra
&& !lpDx
)
409 XDrawString( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
410 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, str
, count
);
412 else /* Now the fun begins... */
414 XTextItem
*items
, *pitem
;
416 items
= malloc( count
* sizeof(XTextItem
) );
417 for (i
= 0, pitem
= items
; i
< count
; i
++, pitem
++)
419 pitem
->chars
= str
+ i
;
425 continue; /* First iteration -> no delta */
427 pitem
->delta
= dc
->w
.charExtra
;
428 if (str
[i
] == (char)dc
->u
.x
.font
.metrics
.tmBreakChar
)
429 pitem
->delta
+= dc
->w
.breakExtra
;
433 GetCharWidth( hdc
, str
[i
], str
[i
], &width
);
434 pitem
->delta
+= lpDx
[i
-1] - width
;
437 XDrawText( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
438 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, items
, count
);
442 /* Draw underline and strike-out if needed */
444 if (dc
->u
.x
.font
.metrics
.tmUnderlined
)
446 long linePos
, lineWidth
;
447 if (!XGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
448 linePos
= font
->descent
-1;
449 if (!XGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
451 else if (lineWidth
== 1) lineWidth
= 0;
452 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineWidth
,
453 LineSolid
, CapRound
, JoinBevel
);
454 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
455 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
+ linePos
,
456 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
+ linePos
);
458 if (dc
->u
.x
.font
.metrics
.tmStruckOut
)
460 long lineAscent
, lineDescent
;
461 if (!XGetFontProperty( font
, XA_STRIKEOUT_ASCENT
, &lineAscent
))
462 lineAscent
= font
->ascent
/ 3;
463 if (!XGetFontProperty( font
, XA_STRIKEOUT_DESCENT
, &lineDescent
))
464 lineDescent
= -lineAscent
;
465 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineAscent
+ lineDescent
,
466 LineSolid
, CapRound
, JoinBevel
);
467 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
468 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
- lineAscent
,
469 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
- lineAscent
);
471 if (flags
& ETO_CLIPPED
) RestoreVisRgn( hdc
);
476 /***********************************************************************
479 BOOL
TextOut( HDC hdc
, short x
, short y
, LPSTR str
, short count
)
481 return ExtTextOut( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
485 /***********************************************************************
486 * GrayString (USER.185)
488 BOOL
GrayString(HDC hdc
, HBRUSH hbr
, FARPROC gsprc
, LPARAM lParam
,
489 INT cch
, INT x
, INT y
, INT cx
, INT cy
)
491 int s
, current_color
;
494 return CallGrayStringProc(gsprc
, hdc
, lParam
,
495 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
497 current_color
= GetTextColor(hdc
);
498 SetTextColor(hdc
, GetSysColor(COLOR_GRAYTEXT
) );
499 s
= TextOut(hdc
, x
, y
, (LPSTR
) lParam
,
500 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
501 SetTextColor(hdc
, current_color
);
508 /***********************************************************************
511 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
512 * Note: this doesn't work too well for text-alignment modes other
513 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
515 LONG
TEXT_TabbedTextOut( HDC hdc
, int x
, int y
, LPSTR lpstr
, int count
,
516 int cTabStops
, LPINT lpTabPos
, int nTabOrg
,
523 if (cTabStops
== 1) defWidth
= *lpTabPos
;
527 GetTextMetrics( hdc
, &tm
);
528 defWidth
= 8 * tm
.tmAveCharWidth
;
533 for (i
= 0; i
< count
; i
++)
534 if (lpstr
[i
] == '\t') break;
535 extent
= GetTextExtent( hdc
, lpstr
, i
);
536 while ((cTabStops
> 0) && (nTabOrg
+ *lpTabPos
< x
+ LOWORD(extent
)))
541 if (lpstr
[i
] != '\t')
542 tabPos
= x
+ LOWORD(extent
);
543 else if (cTabStops
> 0)
544 tabPos
= nTabOrg
+ *lpTabPos
;
546 tabPos
= (x
+ LOWORD(extent
) + defWidth
- 1) / defWidth
* defWidth
;
550 SetRect( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
551 ExtTextOut( hdc
, x
, y
,
552 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
553 &r
, lpstr
, i
, NULL
);
563 /***********************************************************************
564 * TabbedTextOut (USER.196)
566 LONG
TabbedTextOut( HDC hdc
, short x
, short y
, LPSTR lpstr
, short count
,
567 short cTabStops
, LPINT lpTabPos
, short nTabOrg
)
569 dprintf_text( stddeb
, "TabbedTextOut: %x %d,%d '%*.*s' %d\n",
570 hdc
, x
, y
, count
, count
, lpstr
, count
);
571 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
572 lpTabPos
, nTabOrg
, TRUE
);
576 /***********************************************************************
577 * GetTabbedTextExtent (USER.197)
579 DWORD
GetTabbedTextExtent( HDC hdc
, LPSTR lpstr
, int count
,
580 int cTabStops
, LPINT lpTabPos
)
582 dprintf_text( stddeb
, "GetTabbedTextExtent: %x '%*.*s' %d\n",
583 hdc
, count
, count
, lpstr
, count
);
584 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
585 lpTabPos
, 0, FALSE
);