4 * Copyright 1993, 1994 Alexandre Julliard
7 static char Copyright
[] = "Copyright Alexandre Julliard, 1993, 1994";
10 #include <X11/Xatom.h>
15 /* #define DEBUG_TEXT /* */
16 /* #undef 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
;
63 if (!(format
& DT_SINGLELINE
))
65 if (str
[i
] == CR
&& str
[i
+1] == LF
)
72 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
73 (format
& DT_WORDBREAK
))
75 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
82 if (!(format
& DT_NOPREFIX
))
90 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
92 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
100 if (format
& DT_EXPANDTABS
)
106 if (!GetTextExtentPoint(hdc
, &dest
[lasttab
], j
- lasttab
,
110 numspaces
= (tabwidth
- size
.cx
) / spacewidth
;
111 for (k
= 0; k
< numspaces
; k
++)
113 plen
+= tabwidth
- size
.cx
;
114 lasttab
= wb_j
+ numspaces
;
118 dest
[j
++] = str
[i
++];
119 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
120 (format
& DT_WORDBREAK
))
122 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
130 dest
[j
++] = str
[i
++];
131 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
132 (format
& DT_WORDBREAK
))
137 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
144 dest
[j
++] = str
[i
++];
145 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
146 (format
& DT_WORDBREAK
))
148 if (!GetTextExtentPoint(hdc
, &dest
[j
-1], 1, &size
))
155 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
159 if (format
& DT_WORDBREAK
)
164 *count
= wb_count
- 1;
182 /***********************************************************************
185 int DrawText( HDC hdc
, LPSTR str
, int count
, LPRECT rect
, WORD flags
)
189 static char line
[1024];
190 int len
, lh
, prefix_x
, prefix_end
;
192 int x
= rect
->left
, y
= rect
->top
;
193 int width
= rect
->right
- rect
->left
;
196 dprintf_text(stddeb
,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str
, count
,
197 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
199 if (count
== -1) count
= strlen(str
);
202 GetTextMetrics(hdc
, &tm
);
203 if (flags
& DT_EXTERNALLEADING
)
204 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
208 if (flags
& DT_TABSTOP
)
209 tabstop
= flags
>> 8;
211 if (flags
& DT_EXPANDTABS
)
213 GetTextExtentPoint(hdc
, " ", 1, &size
);
214 spacewidth
= size
.cx
;
215 GetTextExtentPoint(hdc
, "o", 1, &size
);
216 tabwidth
= size
.cx
* tabstop
;
222 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
224 if (prefix_offset
!= -1)
226 GetTextExtentPoint(hdc
, line
, prefix_offset
, &size
);
228 GetTextExtentPoint(hdc
, line
, prefix_offset
+ 1, &size
);
229 prefix_end
= size
.cx
- 1;
232 if (!GetTextExtentPoint(hdc
, line
, len
, &size
)) return 0;
233 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
235 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
237 if (flags
& DT_SINGLELINE
)
239 if (flags
& DT_VCENTER
) y
= rect
->top
+
240 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
241 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
243 if (!(flags
& DT_CALCRECT
))
245 if (!ExtTextOut( hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
246 rect
, line
, len
, NULL
)) return 0;
248 else if (size
.cx
> max_width
)
251 if (prefix_offset
!= -1)
253 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
254 HPEN oldPen
= SelectObject( hdc
, hpen
);
255 MoveTo(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
256 LineTo(hdc
, x
+ prefix_end
, y
+ tm
.tmAscent
+ 1 );
257 SelectObject( hdc
, oldPen
);
258 DeleteObject( hpen
);
264 if (!(flags
& DT_NOCLIP
))
266 if (y
> rect
->bottom
- lh
)
272 if (flags
& DT_CALCRECT
)
274 rect
->right
= rect
->left
+ max_width
;
281 /***********************************************************************
282 * ExtTextOut (GDI.351)
284 BOOL
ExtTextOut( HDC hdc
, short x
, short y
, WORD flags
, LPRECT lprect
,
285 LPSTR str
, WORD count
, LPINT lpDx
)
287 int dir
, ascent
, descent
, i
;
292 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
295 dc
= (DC
*)GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
);
296 if (!dc
) return FALSE
;
297 MF_TextOut( dc
, x
, y
, str
, count
);
301 if (!DC_SetupGCForText( dc
)) return TRUE
;
302 font
= dc
->u
.x
.font
.fstruct
;
304 dprintf_text(stddeb
,"ExtTextOut: %d,%d '%s', %d flags=%d rect=%d,%d,%d,%d\n",
305 x
, y
, str
, count
, flags
,
306 lprect
->left
, lprect
->top
, lprect
->right
, lprect
->bottom
);
308 /* Setup coordinates */
310 if (dc
->w
.textAlign
& TA_UPDATECP
)
315 x
= XLPTODP( dc
, x
);
316 y
= YLPTODP( dc
, y
);
317 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* There's a rectangle */
319 rect
.left
= XLPTODP( dc
, lprect
->left
);
320 rect
.right
= XLPTODP( dc
, lprect
->right
);
321 rect
.top
= YLPTODP( dc
, lprect
->top
);
322 rect
.bottom
= YLPTODP( dc
, lprect
->bottom
);
323 if (rect
.right
< rect
.left
) SWAP_INT( rect
.left
, rect
.right
);
324 if (rect
.bottom
< rect
.top
) SWAP_INT( rect
.top
, rect
.bottom
);
327 /* Draw the rectangle */
329 if (flags
& ETO_OPAQUE
)
331 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
332 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
333 dc
->w
.DCOrgX
+ rect
.left
, dc
->w
.DCOrgY
+ rect
.top
,
334 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
336 if (!count
) return TRUE
; /* Nothing more to do */
338 /* Compute text starting position */
340 XTextExtents( font
, str
, count
, &dir
, &ascent
, &descent
, &info
);
341 info
.width
+= count
*dc
->w
.charExtra
+ dc
->w
.breakExtra
*dc
->w
.breakCount
;
342 if (lpDx
) for (i
= 0; i
< count
; i
++) info
.width
+= lpDx
[i
];
344 switch( dc
->w
.textAlign
& (TA_LEFT
| TA_RIGHT
| TA_CENTER
) )
347 if (dc
->w
.textAlign
& TA_UPDATECP
)
348 dc
->w
.CursPosX
= XDPTOLP( dc
, x
+ info
.width
);
352 if (dc
->w
.textAlign
& TA_UPDATECP
) dc
->w
.CursPosX
= XDPTOLP( dc
, x
);
358 switch( dc
->w
.textAlign
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
370 /* Set the clip region */
372 if (flags
& ETO_CLIPPED
)
375 IntersectVisRect( hdc
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
378 /* Draw the text background if necessary */
380 if (dc
->w
.backgroundMode
!= TRANSPARENT
)
382 /* If rectangle is opaque and clipped, do nothing */
383 if (!(flags
& ETO_CLIPPED
) || !(flags
& ETO_OPAQUE
))
385 /* Only draw if rectangle is not opaque or if some */
386 /* text is outside the rectangle */
387 if (!(flags
& ETO_OPAQUE
) ||
389 (x
+ info
.width
>= rect
.right
) ||
390 (y
-font
->ascent
< rect
.top
) ||
391 (y
+font
->descent
>= rect
.bottom
))
393 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
394 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
396 dc
->w
.DCOrgY
+ y
- font
->ascent
,
398 font
->ascent
+ font
->descent
);
405 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.textPixel
);
406 if (!dc
->w
.charExtra
&& !dc
->w
.breakExtra
&& !lpDx
)
408 XDrawString( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
409 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, str
, count
);
411 else /* Now the fun begins... */
413 XTextItem
*items
, *pitem
;
415 items
= malloc( count
* sizeof(XTextItem
) );
416 for (i
= 0, pitem
= items
; i
< count
; i
++, pitem
++)
418 pitem
->chars
= str
+ i
;
424 continue; /* First iteration -> no delta */
426 pitem
->delta
= dc
->w
.charExtra
;
427 if (str
[i
] == dc
->u
.x
.font
.metrics
.tmBreakChar
)
428 pitem
->delta
+= dc
->w
.breakExtra
;
432 GetCharWidth( hdc
, str
[i
], str
[i
], &width
);
433 pitem
->delta
+= lpDx
[i
-1] - width
;
436 XDrawText( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
437 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, items
, count
);
441 /* Draw underline and strike-out if needed */
443 if (dc
->u
.x
.font
.metrics
.tmUnderlined
)
445 long linePos
, lineWidth
;
446 if (!XGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
447 linePos
= font
->descent
-1;
448 if (!XGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
450 else if (lineWidth
== 1) lineWidth
= 0;
451 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineWidth
,
452 LineSolid
, CapRound
, JoinBevel
);
453 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
454 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
+ linePos
,
455 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
+ linePos
);
457 if (dc
->u
.x
.font
.metrics
.tmStruckOut
)
459 long lineAscent
, lineDescent
;
460 if (!XGetFontProperty( font
, XA_STRIKEOUT_ASCENT
, &lineAscent
))
461 lineAscent
= font
->ascent
/ 3;
462 if (!XGetFontProperty( font
, XA_STRIKEOUT_DESCENT
, &lineDescent
))
463 lineDescent
= -lineAscent
;
464 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineAscent
+ lineDescent
,
465 LineSolid
, CapRound
, JoinBevel
);
466 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
467 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
- lineAscent
,
468 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
- lineAscent
);
470 if (flags
& ETO_CLIPPED
) RestoreVisRgn( hdc
);
475 /***********************************************************************
478 BOOL
TextOut( HDC hdc
, short x
, short y
, LPSTR str
, short count
)
480 return ExtTextOut( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
484 /***********************************************************************
485 * GrayString (USER.185)
487 BOOL
GrayString(HDC hdc
, HBRUSH hbr
, FARPROC gsprc
, LPARAM lParam
,
488 INT cch
, INT x
, INT y
, INT cx
, INT cy
)
490 int s
, current_color
;
493 return CallGrayStringProc(gsprc
, hdc
, lParam
,
494 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
496 current_color
= GetTextColor(hdc
);
497 SetTextColor(hdc
, GetSysColor(COLOR_GRAYTEXT
) );
498 s
= TextOut(hdc
, x
, y
, (LPSTR
) lParam
,
499 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
500 SetTextColor(hdc
, current_color
);
507 /***********************************************************************
508 * TabbedTextOut [USER.196]
510 LONG
TabbedTextOut(HDC hDC
, short x
, short y
, LPSTR lpStr
, short nCount
,
511 short nTabCount
, LPINT lpTabPos
, short nTabOrg
)
514 dprintf_text(stdnimp
,"EMPTY STUB !!! TabbedTextOut(); ! call TextOut() for now !\n");
515 height
= HIWORD(GetTextExtent(hDC
, lpStr
, nCount
));
516 width
= LOWORD(GetTextExtent(hDC
, lpStr
, nCount
));
517 TextOut(hDC
, x
, y
, lpStr
, nCount
);
518 return MAKELONG(width
, height
);
522 /***********************************************************************
523 * GetTabbedTextExtent [USER.197]
525 DWORD
GetTabbedTextExtent(HDC hDC
, LPSTR lpString
, int nCount
,
526 int nTabPositions
, LPINT lpnTabStopPositions
)
528 dprintf_text(stdnimp
,"EMPTY STUB !!! GetTabbedTextExtent(); !\n");
530 return (18 << 16) | (nCount
* 18);