Release 960902
[wine/multimedia.git] / objects / text.c
blobfbe88bca41df79beabeba066a636c01361ce4b89
1 /*
2 * text functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 */
8 #include <stdlib.h>
9 #include <X11/Xatom.h>
10 #include "windows.h"
11 #include "dc.h"
12 #include "gdi.h"
13 #include "callback.h"
14 #include "metafile.h"
15 #include "string32.h"
16 #include "stddebug.h"
17 /* #define DEBUG_TEXT */
18 #include "debug.h"
19 #include "xmalloc.h"
21 #define TAB 9
22 #define LF 10
23 #define CR 13
24 #define SPACE 32
25 #define PREFIX 38
27 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
29 static int tabstop = 8;
30 static int tabwidth;
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( HDC hdc, const char *str, int *count,
38 char *dest, int *len, int width, WORD format)
40 /* Return next line of text from a string.
42 * hdc - handle to DC.
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.
54 int i = 0, j = 0, k;
55 int plen = 0;
56 int numspaces;
57 SIZE16 size;
58 int lasttab = 0;
59 int wb_i = 0, wb_j = 0, wb_count = 0;
61 while (*count)
63 switch (str[i])
65 case CR:
66 case LF:
67 if (!(format & DT_SINGLELINE))
69 if (str[i] == CR && str[i+1] == LF)
70 i++;
71 i++;
72 *len = j;
73 (*count)--;
74 return (&str[i]);
76 dest[j++] = str[i++];
77 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
78 (format & DT_WORDBREAK))
80 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
81 return NULL;
82 plen += size.cx;
84 break;
86 case PREFIX:
87 if (!(format & DT_NOPREFIX))
89 prefix_offset = j;
90 i++;
92 else
94 dest[j++] = str[i++];
95 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
97 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
98 return NULL;
99 plen += size.cx;
102 break;
104 case TAB:
105 if (format & DT_EXPANDTABS)
107 wb_i = ++i;
108 wb_j = j;
109 wb_count = *count;
111 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
112 &size))
113 return NULL;
115 numspaces = (tabwidth - size.cx) / spacewidth;
116 for (k = 0; k < numspaces; k++)
117 dest[j++] = SPACE;
118 plen += tabwidth - size.cx;
119 lasttab = wb_j + numspaces;
121 else
123 dest[j++] = str[i++];
124 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
125 (format & DT_WORDBREAK))
127 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
128 return NULL;
129 plen += size.cx;
132 break;
134 case SPACE:
135 dest[j++] = str[i++];
136 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
137 (format & DT_WORDBREAK))
139 wb_i = i;
140 wb_j = j - 1;
141 wb_count = *count;
142 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
143 return NULL;
144 plen += size.cx;
146 break;
148 default:
149 dest[j++] = str[i++];
150 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
151 (format & DT_WORDBREAK))
153 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
154 return NULL;
155 plen += size.cx;
159 (*count)--;
160 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
162 if (plen > width)
164 if (format & DT_WORDBREAK)
166 if (wb_j)
168 *len = wb_j;
169 *count = wb_count - 1;
170 return (&str[wb_i]);
173 else
175 *len = j;
176 return (&str[i]);
182 *len = j;
183 return NULL;
187 /***********************************************************************
188 * DrawText16 (USER.85)
190 INT16 DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
191 LPRECT16 rect, UINT16 flags )
193 SIZE16 size;
194 const char *strPtr;
195 static char line[1024];
196 int len, lh, count=i_count;
197 int prefix_x = 0;
198 int prefix_end = 0;
199 TEXTMETRIC16 tm;
200 int x = rect->left, y = rect->top;
201 int width = rect->right - rect->left;
202 int max_width = 0;
204 dprintf_text(stddeb,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str,
205 count, rect->left, rect->top, rect->right, rect->bottom);
207 if (count == -1) count = strlen(str);
208 strPtr = str;
210 GetTextMetrics16(hdc, &tm);
211 if (flags & DT_EXTERNALLEADING)
212 lh = tm.tmHeight + tm.tmExternalLeading;
213 else
214 lh = tm.tmHeight;
216 if (flags & DT_TABSTOP)
217 tabstop = flags >> 8;
219 if (flags & DT_EXPANDTABS)
221 GetTextExtentPoint16(hdc, " ", 1, &size);
222 spacewidth = size.cx;
223 GetTextExtentPoint16(hdc, "o", 1, &size);
224 tabwidth = size.cx * tabstop;
229 prefix_offset = -1;
230 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
232 if (prefix_offset != -1)
234 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
235 prefix_x = size.cx;
236 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
237 prefix_end = size.cx - 1;
240 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
241 if (flags & DT_CENTER) x = (rect->left + rect->right -
242 size.cx) / 2;
243 else if (flags & DT_RIGHT) x = rect->right - size.cx;
245 if (flags & DT_SINGLELINE)
247 if (flags & DT_VCENTER) y = rect->top +
248 (rect->bottom - rect->top) / 2 - size.cy / 2;
249 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
251 if (!(flags & DT_CALCRECT))
253 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
254 rect, line, len, NULL )) return 0;
255 if (prefix_offset != -1)
257 HPEN16 hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
258 HPEN16 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 );
265 else if (size.cx > max_width)
266 max_width = size.cx;
268 y += lh;
269 if (strPtr)
271 if (!(flags & DT_NOCLIP) && !(flags & DT_CALCRECT))
273 if (y > rect->bottom - lh)
274 break;
278 while (strPtr);
279 if (flags & DT_CALCRECT)
281 rect->right = rect->left + max_width;
282 rect->bottom = y;
284 return 1;
288 /***********************************************************************
289 * DrawText32A (USER32.163)
291 INT32 DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
292 LPRECT32 rect, UINT32 flags )
294 RECT16 rect16;
295 INT16 ret;
297 if (!rect)
298 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
299 CONV_RECT32TO16( rect, &rect16 );
300 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
301 CONV_RECT16TO32( &rect16, rect );
302 return ret;
306 /***********************************************************************
307 * DrawText32W (USER32.166)
309 INT32 DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
310 LPRECT32 rect, UINT32 flags )
312 char *p = STRING32_DupUniToAnsi( str );
313 INT32 ret = DrawText32A( hdc, p, count, rect, flags );
314 free(p);
315 return ret;
319 /***********************************************************************
320 * ExtTextOut16 (GDI.351)
322 BOOL16 ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
323 const RECT16 *lprect, LPCSTR str, UINT16 count,
324 const INT16 *lpDx )
326 HRGN hRgnClip = 0;
327 int dir, ascent, descent, i;
328 XCharStruct info;
329 XFontStruct *font;
330 RECT16 rect;
332 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
333 if (!dc)
335 dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
336 if (!dc) return FALSE;
337 MF_ExtTextOut( dc, x, y, flags, lprect, str, count, lpDx );
338 return TRUE;
341 if (!DC_SetupGCForText( dc )) return TRUE;
342 font = dc->u.x.font.fstruct;
344 dprintf_text(stddeb,"ExtTextOut: hdc=%04x %d,%d '%*.*s', %d flags=%d\n",
345 hdc, x, y, count, count, str, count, flags);
346 if (lprect != NULL) dprintf_text(stddeb, "\trect=(%d,%d- %d,%d)\n",
347 lprect->left, lprect->top,
348 lprect->right, lprect->bottom );
350 /* Setup coordinates */
352 if (dc->w.textAlign & TA_UPDATECP)
354 x = dc->w.CursPosX;
355 y = dc->w.CursPosY;
358 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
360 if (!lprect) /* not always */
362 SIZE16 sz;
363 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
364 return FALSE;
365 if (!GetTextExtentPoint16( hdc, str, count, &sz ))
366 return FALSE;
367 rect.left = XLPTODP( dc, x );
368 rect.right = XLPTODP( dc, x+sz.cx );
369 rect.top = YLPTODP( dc, y );
370 rect.bottom = YLPTODP( dc, y+sz.cy );
372 else
374 rect.left = XLPTODP( dc, lprect->left );
375 rect.right = XLPTODP( dc, lprect->right );
376 rect.top = YLPTODP( dc, lprect->top );
377 rect.bottom = YLPTODP( dc, lprect->bottom );
379 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
380 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
383 x = XLPTODP( dc, x );
384 y = YLPTODP( dc, y );
386 dprintf_text(stddeb,"\treal coord: x=%i, y=%i, rect=(%d,%d-%d,%d)\n",
387 x, y, rect.left, rect.top, rect.right, rect.bottom);
389 /* Draw the rectangle */
391 if (flags & ETO_OPAQUE)
393 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
394 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
395 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
396 rect.right-rect.left, rect.bottom-rect.top );
398 if (!count) return TRUE; /* Nothing more to do */
400 /* Compute text starting position */
402 XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
404 if (lpDx) /* have explicit character cell x offsets */
406 /* sum lpDx array and add the width of last character */
408 info.width = XTextWidth( font, str + count - 1, 1) + dc->w.charExtra;
409 if (str[count-1] == (char)dc->u.x.font.metrics.tmBreakChar)
410 info.width += dc->w.breakExtra;
412 for (i = 0; i < count; i++) info.width += lpDx[i];
414 else
415 info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
417 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
419 case TA_LEFT:
420 if (dc->w.textAlign & TA_UPDATECP)
421 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
422 break;
423 case TA_RIGHT:
424 x -= info.width;
425 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
426 break;
427 case TA_CENTER:
428 x -= info.width / 2;
429 break;
432 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
434 case TA_TOP:
435 y += font->ascent;
436 break;
437 case TA_BOTTOM:
438 y -= font->descent;
439 break;
440 case TA_BASELINE:
441 break;
444 /* Set the clip region */
446 if (flags & ETO_CLIPPED)
448 if (dc->w.flags & DC_MEMORY)
450 hRgnClip = dc->w.hClipRgn;
451 CLIPPING_IntersectClipRect(dc, rect.left, rect.top, rect.right, rect.bottom,
452 CLIP_INTERSECT | CLIP_KEEPRGN);
454 else
456 SaveVisRgn( hdc );
457 IntersectVisRect( hdc, rect.left, rect.top, rect.right, rect.bottom );
461 /* Draw the text background if necessary */
463 if (dc->w.backgroundMode != TRANSPARENT)
465 /* If rectangle is opaque and clipped, do nothing */
466 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
468 /* Only draw if rectangle is not opaque or if some */
469 /* text is outside the rectangle */
470 if (!(flags & ETO_OPAQUE) ||
471 (x < rect.left) ||
472 (x + info.width >= rect.right) ||
473 (y-font->ascent < rect.top) ||
474 (y+font->descent >= rect.bottom))
476 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
477 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
478 dc->w.DCOrgX + x,
479 dc->w.DCOrgY + y - font->ascent,
480 info.width,
481 font->ascent + font->descent );
486 /* Draw the text (count > 0 verified) */
488 XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
489 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
491 XDrawString( display, dc->u.x.drawable, dc->u.x.gc,
492 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
494 else /* Now the fun begins... */
496 XTextItem *items, *pitem;
497 int delta;
499 /* allocate max items */
501 pitem = items = xmalloc( count * sizeof(XTextItem) );
502 delta = i = 0;
503 while (i < count)
505 /* initialize text item with accumulated delta */
507 pitem->chars = (char *)str + i;
508 pitem->delta = delta;
509 pitem->nchars = 0;
510 pitem->font = None;
511 delta = 0;
513 /* stuff characters into the same XTextItem until new delta
514 * becomes non-zero */
518 if (lpDx) delta += lpDx[i] - XTextWidth( font, str + i, 1);
519 else
521 delta += dc->w.charExtra;
522 if (str[i] == (char)dc->u.x.font.metrics.tmBreakChar)
523 delta += dc->w.breakExtra;
525 pitem->nchars++;
527 while ((++i < count) && !delta);
528 pitem++;
531 XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
532 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
533 free( items );
536 /* Draw underline and strike-out if needed */
538 if (dc->u.x.font.metrics.tmUnderlined)
540 long linePos, lineWidth;
541 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
542 linePos = font->descent-1;
543 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
544 lineWidth = 0;
545 else if (lineWidth == 1) lineWidth = 0;
546 XSetLineAttributes( display, dc->u.x.gc, lineWidth,
547 LineSolid, CapRound, JoinBevel );
548 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
549 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
550 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
552 if (dc->u.x.font.metrics.tmStruckOut)
554 long lineAscent, lineDescent;
555 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
556 lineAscent = font->ascent / 3;
557 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
558 lineDescent = -lineAscent;
559 XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
560 LineSolid, CapRound, JoinBevel );
561 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
562 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
563 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
566 if (flags & ETO_CLIPPED)
568 if( dc->w.flags & DC_MEMORY )
569 SelectClipRgn( hdc, hRgnClip );
570 else RestoreVisRgn( hdc );
572 return TRUE;
576 /***********************************************************************
577 * ExtTextOut32A (GDI32.98)
579 BOOL32 ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
580 const RECT32 *lprect, LPCSTR str, UINT32 count,
581 const INT32 *lpDx )
583 RECT16 rect16;
585 if (lpDx) fprintf( stderr, "ExtTextOut32A: lpDx not implemented\n" );
586 if (!lprect)
587 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
588 NULL, str, (UINT16)count, NULL );
589 CONV_RECT32TO16( lprect, &rect16 );
590 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
591 &rect16, str, (UINT16)count, NULL );
595 /***********************************************************************
596 * ExtTextOut32W (GDI32.99)
598 BOOL32 ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
599 const RECT32 *lprect, LPCWSTR str, UINT32 count,
600 const INT32 *lpDx )
602 char *p = STRING32_DupUniToAnsi( str );
603 INT32 ret = ExtTextOut32A( hdc, x, y, flags, lprect, p, count, lpDx );
604 free(p);
605 return ret;
609 /***********************************************************************
610 * TextOut16 (GDI.33)
612 BOOL16 TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
614 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
618 /***********************************************************************
619 * TextOut32A (GDI32.355)
621 BOOL32 TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
623 return ExtTextOut32A( hdc, x, y, 0, NULL, str, count, NULL );
627 /***********************************************************************
628 * TextOut32W (GDI32.356)
630 BOOL32 TextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count )
632 return ExtTextOut32W( hdc, x, y, 0, NULL, str, count, NULL );
636 /***********************************************************************
637 * GrayString (USER.185)
639 BOOL GrayString(HDC hdc, HBRUSH hbr, GRAYSTRINGPROC16 gsprc, LPARAM lParam,
640 INT cch, INT x, INT y, INT cx, INT cy)
642 BOOL ret;
643 COLORREF current_color;
645 if (!cch) cch = lstrlen16( (LPCSTR)PTR_SEG_TO_LIN(lParam) );
646 if (gsprc) return gsprc( hdc, lParam, cch );
647 current_color = GetTextColor( hdc );
648 SetTextColor( hdc, GetSysColor(COLOR_GRAYTEXT) );
649 ret = TextOut16( hdc, x, y, (LPCSTR)PTR_SEG_TO_LIN(lParam), cch );
650 SetTextColor( hdc, current_color );
651 return ret;
655 /***********************************************************************
656 * TEXT_TabbedTextOut
658 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
659 * Note: this doesn't work too well for text-alignment modes other
660 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
662 LONG TEXT_TabbedTextOut( HDC hdc, int x, int y, LPSTR lpstr, int count,
663 int cTabStops, LPINT16 lpTabPos, int nTabOrg,
664 BOOL fDisplayText)
666 WORD defWidth;
667 DWORD extent = 0;
668 int i, tabPos = x;
669 int start = x;
671 if (cTabStops == 1)
673 defWidth = *lpTabPos;
674 cTabStops = 0;
676 else
678 TEXTMETRIC16 tm;
679 GetTextMetrics16( hdc, &tm );
680 defWidth = 8 * tm.tmAveCharWidth;
683 while (count > 0)
685 for (i = 0; i < count; i++)
686 if (lpstr[i] == '\t') break;
687 extent = GetTextExtent( hdc, lpstr, i );
688 while ((cTabStops > 0) && (nTabOrg + *lpTabPos <= x + LOWORD(extent)))
690 lpTabPos++;
691 cTabStops--;
693 if (i == count)
694 tabPos = x + LOWORD(extent);
695 else if (cTabStops > 0)
696 tabPos = nTabOrg + *lpTabPos;
697 else
698 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
699 if (fDisplayText)
701 RECT16 r;
702 SetRect16( &r, x, y, tabPos, y+HIWORD(extent) );
703 ExtTextOut16( hdc, x, y,
704 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
705 &r, lpstr, i, NULL );
707 x = tabPos;
708 count -= i+1;
709 lpstr += i+1;
711 return MAKELONG(tabPos - start, HIWORD(extent));
715 /***********************************************************************
716 * TabbedTextOut (USER.196)
718 LONG TabbedTextOut( HDC hdc, short x, short y, LPSTR lpstr, short count,
719 short cTabStops, LPINT16 lpTabPos, short nTabOrg )
721 dprintf_text( stddeb, "TabbedTextOut: %04x %d,%d '%*.*s' %d\n",
722 hdc, x, y, count, count, lpstr, count );
723 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
724 lpTabPos, nTabOrg, TRUE );
728 /***********************************************************************
729 * GetTabbedTextExtent (USER.197)
731 DWORD GetTabbedTextExtent( HDC hdc, LPSTR lpstr, int count,
732 int cTabStops, LPINT16 lpTabPos )
734 dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%*.*s' %d\n",
735 hdc, count, count, lpstr, count );
736 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
737 lpTabPos, 0, FALSE );