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
))
66 if (str
[i
] == CR
|| str
[i
] == 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
)
162 *count
= wb_count
- 1;
179 /***********************************************************************
182 int DrawText( HDC hdc
, LPSTR str
, int count
, LPRECT rect
, WORD flags
)
186 static char line
[1024];
187 int len
, lh
, prefix_x
, prefix_end
;
189 int x
= rect
->left
, y
= rect
->top
;
190 int width
= rect
->right
- rect
->left
;
192 dprintf_text(stddeb
,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str
, count
,
193 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
195 if (count
== -1) count
= strlen(str
);
198 GetTextMetrics(hdc
, &tm
);
199 if (flags
& DT_EXTERNALLEADING
)
200 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
204 if (flags
& DT_TABSTOP
)
205 tabstop
= flags
>> 8;
207 if (flags
& DT_EXPANDTABS
)
209 GetTextExtentPoint(hdc
, " ", 1, &size
);
210 spacewidth
= size
.cx
;
211 GetTextExtentPoint(hdc
, "o", 1, &size
);
212 tabwidth
= size
.cx
* tabstop
;
218 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
220 if (prefix_offset
!= -1)
222 GetTextExtentPoint(hdc
, line
, prefix_offset
, &size
);
224 GetTextExtentPoint(hdc
, line
, prefix_offset
+ 1, &size
);
225 prefix_end
= size
.cx
- 1;
228 if (!GetTextExtentPoint(hdc
, line
, len
, &size
)) return 0;
229 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
231 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
233 if (flags
& DT_SINGLELINE
)
235 if (flags
& DT_VCENTER
) y
= rect
->top
+
236 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
237 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
239 if (!(flags
& DT_CALCRECT
))
240 if (!ExtTextOut( hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
241 rect
, line
, len
, NULL
)) return 0;
243 if (prefix_offset
!= -1)
245 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
246 HPEN oldPen
= SelectObject( hdc
, hpen
);
247 MoveTo(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
248 LineTo(hdc
, x
+ prefix_end
, y
+ tm
.tmAscent
+ 1 );
249 SelectObject( hdc
, oldPen
);
250 DeleteObject( hpen
);
256 if (!(flags
& DT_NOCLIP
))
258 if (y
> rect
->bottom
- lh
)
264 if (flags
& DT_CALCRECT
) rect
->bottom
= y
;
269 /***********************************************************************
270 * ExtTextOut (GDI.351)
272 BOOL
ExtTextOut( HDC hdc
, short x
, short y
, WORD flags
, LPRECT lprect
,
273 LPSTR str
, WORD count
, LPINT lpDx
)
275 int dir
, ascent
, descent
, i
;
280 DC
* dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
283 dc
= (DC
*)GDI_GetObjPtr( hdc
, METAFILE_DC_MAGIC
);
284 if (!dc
) return FALSE
;
285 MF_TextOut( dc
, x
, y
, str
, count
);
289 if (!DC_SetupGCForText( dc
)) return TRUE
;
290 font
= dc
->u
.x
.font
.fstruct
;
292 dprintf_text(stddeb
,"ExtTextOut: %d,%d '%s', %d flags=%d rect=%d,%d,%d,%d\n",
293 x
, y
, str
, count
, flags
,
294 lprect
->left
, lprect
->top
, lprect
->right
, lprect
->bottom
);
296 /* Setup coordinates */
298 if (dc
->w
.textAlign
& TA_UPDATECP
)
303 x
= XLPTODP( dc
, x
);
304 y
= YLPTODP( dc
, y
);
305 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* There's a rectangle */
307 rect
.left
= XLPTODP( dc
, lprect
->left
);
308 rect
.right
= XLPTODP( dc
, lprect
->right
);
309 rect
.top
= YLPTODP( dc
, lprect
->top
);
310 rect
.bottom
= YLPTODP( dc
, lprect
->bottom
);
311 if (rect
.right
< rect
.left
) SWAP_INT( rect
.left
, rect
.right
);
312 if (rect
.bottom
< rect
.top
) SWAP_INT( rect
.top
, rect
.bottom
);
315 /* Draw the rectangle */
317 if (flags
& ETO_OPAQUE
)
319 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
320 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
321 dc
->w
.DCOrgX
+ rect
.left
, dc
->w
.DCOrgY
+ rect
.top
,
322 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
324 if (!count
) return TRUE
; /* Nothing more to do */
326 /* Compute text starting position */
328 XTextExtents( font
, str
, count
, &dir
, &ascent
, &descent
, &info
);
329 info
.width
+= count
*dc
->w
.charExtra
+ dc
->w
.breakExtra
*dc
->w
.breakCount
;
330 if (lpDx
) for (i
= 0; i
< count
; i
++) info
.width
+= lpDx
[i
];
332 switch( dc
->w
.textAlign
& (TA_LEFT
| TA_RIGHT
| TA_CENTER
) )
335 if (dc
->w
.textAlign
& TA_UPDATECP
)
336 dc
->w
.CursPosX
= XDPTOLP( dc
, x
+ info
.width
);
340 if (dc
->w
.textAlign
& TA_UPDATECP
) dc
->w
.CursPosX
= XDPTOLP( dc
, x
);
346 switch( dc
->w
.textAlign
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
358 /* Set the clip region */
360 if (flags
& ETO_CLIPPED
)
363 IntersectVisRect( hdc
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
366 /* Draw the text background if necessary */
368 if (dc
->w
.backgroundMode
!= TRANSPARENT
)
370 /* If rectangle is opaque and clipped, do nothing */
371 if (!(flags
& ETO_CLIPPED
) || !(flags
& ETO_OPAQUE
))
373 /* Only draw if rectangle is not opaque or if some */
374 /* text is outside the rectangle */
375 if (!(flags
& ETO_OPAQUE
) ||
377 (x
+ info
.width
>= rect
.right
) ||
378 (y
-font
->ascent
< rect
.top
) ||
379 (y
+font
->descent
>= rect
.bottom
))
381 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.backgroundPixel
);
382 XFillRectangle( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
384 dc
->w
.DCOrgY
+ y
- font
->ascent
,
386 font
->ascent
+ font
->descent
);
393 XSetForeground( display
, dc
->u
.x
.gc
, dc
->w
.textPixel
);
394 if (!dc
->w
.charExtra
&& !dc
->w
.breakExtra
&& !lpDx
)
396 XDrawString( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
397 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, str
, count
);
399 else /* Now the fun begins... */
401 XTextItem
*items
, *pitem
;
403 items
= malloc( count
* sizeof(XTextItem
) );
404 for (i
= 0, pitem
= items
; i
< count
; i
++, pitem
++)
406 pitem
->chars
= str
+ i
;
412 continue; /* First iteration -> no delta */
414 pitem
->delta
= dc
->w
.charExtra
;
415 if (str
[i
] == dc
->u
.x
.font
.metrics
.tmBreakChar
)
416 pitem
->delta
+= dc
->w
.breakExtra
;
420 GetCharWidth( hdc
, str
[i
], str
[i
], &width
);
421 pitem
->delta
+= lpDx
[i
-1] - width
;
424 XDrawText( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
425 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, items
, count
);
429 /* Draw underline and strike-out if needed */
431 if (dc
->u
.x
.font
.metrics
.tmUnderlined
)
433 long linePos
, lineWidth
;
434 if (!XGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
435 linePos
= font
->descent
-1;
436 if (!XGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
438 else if (lineWidth
== 1) lineWidth
= 0;
439 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineWidth
,
440 LineSolid
, CapRound
, JoinBevel
);
441 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
442 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
+ linePos
,
443 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
+ linePos
);
445 if (dc
->u
.x
.font
.metrics
.tmStruckOut
)
447 long lineAscent
, lineDescent
;
448 if (!XGetFontProperty( font
, XA_STRIKEOUT_ASCENT
, &lineAscent
))
449 lineAscent
= font
->ascent
/ 3;
450 if (!XGetFontProperty( font
, XA_STRIKEOUT_DESCENT
, &lineDescent
))
451 lineDescent
= -lineAscent
;
452 XSetLineAttributes( display
, dc
->u
.x
.gc
, lineAscent
+ lineDescent
,
453 LineSolid
, CapRound
, JoinBevel
);
454 XDrawLine( display
, dc
->u
.x
.drawable
, dc
->u
.x
.gc
,
455 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
- lineAscent
,
456 dc
->w
.DCOrgX
+ x
+ info
.width
, dc
->w
.DCOrgY
+ y
- lineAscent
);
458 if (flags
& ETO_CLIPPED
) RestoreVisRgn( hdc
);
463 /***********************************************************************
466 BOOL
TextOut( HDC hdc
, short x
, short y
, LPSTR str
, short count
)
468 return ExtTextOut( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
472 /***********************************************************************
473 * GrayString (USER.185)
475 BOOL
GrayString(HDC hdc
, HBRUSH hbr
, FARPROC gsprc
, LPARAM lParam
,
476 INT cch
, INT x
, INT y
, INT cx
, INT cy
)
478 int s
, current_color
;
481 return CallGrayStringProc(gsprc
, hdc
, lParam
,
482 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
484 current_color
= GetTextColor(hdc
);
485 SetTextColor(hdc
, GetSysColor(COLOR_GRAYTEXT
) );
486 s
= TextOut(hdc
, x
, y
, (LPSTR
) lParam
,
487 cch
? cch
: lstrlen((LPCSTR
) lParam
) );
488 SetTextColor(hdc
, current_color
);
495 /***********************************************************************
496 * TabbedTextOut [USER.196]
498 LONG
TabbedTextOut(HDC hDC
, short x
, short y
, LPSTR lpStr
, short nCount
,
499 short nTabCount
, LPINT lpTabPos
, short nTabOrg
)
502 dprintf_text(stdnimp
,"EMPTY STUB !!! TabbedTextOut(); ! call TextOut() for now !\n");
503 height
= HIWORD(GetTextExtent(hDC
, lpStr
, nCount
));
504 width
= LOWORD(GetTextExtent(hDC
, lpStr
, nCount
));
505 TextOut(hDC
, x
, y
, lpStr
, nCount
);
506 return MAKELONG(width
, height
);
510 /***********************************************************************
511 * GetTabbedTextExtent [USER.197]
513 DWORD
GetTabbedTextExtent(HDC hDC
, LPSTR lpString
, int nCount
,
514 int nTabPositions
, LPINT lpnTabStopPositions
)
516 dprintf_text(stdnimp
,"EMPTY STUB !!! GetTabbedTextExtent(); !\n");
518 return (18 << 16) | (nCount
* 18);