Release 960717
[wine/multimedia.git] / objects / text.c
blob5c892d8cc7040947f14ec7f7bea83854a4e5dc43
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;
35 static const char *TEXT_NextLine( HDC hdc, const char *str, int *count,
36 char *dest, int *len, int width, WORD format)
38 /* Return next line of text from a string.
40 * hdc - handle to DC.
41 * str - string to parse into lines.
42 * count - length of str.
43 * dest - destination in which to return line.
44 * len - length of resultant line in dest in chars.
45 * width - maximum width of line in pixels.
46 * format - format type passed to DrawText.
48 * Returns pointer to next char in str after end of the line
49 * or NULL if end of str reached.
52 int i = 0, j = 0, k;
53 int plen = 0;
54 int numspaces;
55 SIZE16 size;
56 int lasttab = 0;
57 int wb_i = 0, wb_j = 0, wb_count = 0;
59 while (*count)
61 switch (str[i])
63 case CR:
64 case LF:
65 if (!(format & DT_SINGLELINE))
67 if (str[i] == CR && str[i+1] == LF)
68 i++;
69 i++;
70 *len = j;
71 (*count)--;
72 return (&str[i]);
74 dest[j++] = str[i++];
75 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
76 (format & DT_WORDBREAK))
78 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
79 return NULL;
80 plen += size.cx;
82 break;
84 case PREFIX:
85 if (!(format & DT_NOPREFIX))
87 prefix_offset = j;
88 i++;
90 else
92 dest[j++] = str[i++];
93 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
95 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
96 return NULL;
97 plen += size.cx;
100 break;
102 case TAB:
103 if (format & DT_EXPANDTABS)
105 wb_i = ++i;
106 wb_j = j;
107 wb_count = *count;
109 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
110 &size))
111 return NULL;
113 numspaces = (tabwidth - size.cx) / spacewidth;
114 for (k = 0; k < numspaces; k++)
115 dest[j++] = SPACE;
116 plen += tabwidth - size.cx;
117 lasttab = wb_j + numspaces;
119 else
121 dest[j++] = str[i++];
122 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
123 (format & DT_WORDBREAK))
125 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
126 return NULL;
127 plen += size.cx;
130 break;
132 case SPACE:
133 dest[j++] = str[i++];
134 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
135 (format & DT_WORDBREAK))
137 wb_i = i;
138 wb_j = j - 1;
139 wb_count = *count;
140 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
141 return NULL;
142 plen += size.cx;
144 break;
146 default:
147 dest[j++] = str[i++];
148 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
149 (format & DT_WORDBREAK))
151 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
152 return NULL;
153 plen += size.cx;
157 (*count)--;
158 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
160 if (plen > width)
162 if (format & DT_WORDBREAK)
164 if (wb_j)
166 *len = wb_j;
167 *count = wb_count - 1;
168 return (&str[wb_i]);
171 else
173 *len = j;
174 return (&str[i]);
180 *len = j;
181 return NULL;
185 /***********************************************************************
186 * DrawText16 (USER.85)
188 INT16 DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
189 LPRECT16 rect, UINT16 flags )
191 SIZE16 size;
192 const char *strPtr;
193 static char line[1024];
194 int len, lh, count=i_count;
195 int prefix_x = 0;
196 int prefix_end = 0;
197 TEXTMETRIC16 tm;
198 int x = rect->left, y = rect->top;
199 int width = rect->right - rect->left;
200 int max_width = 0;
202 dprintf_text(stddeb,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str,
203 count, rect->left, rect->top, rect->right, rect->bottom);
205 if (count == -1) count = strlen(str);
206 strPtr = str;
208 GetTextMetrics16(hdc, &tm);
209 if (flags & DT_EXTERNALLEADING)
210 lh = tm.tmHeight + tm.tmExternalLeading;
211 else
212 lh = tm.tmHeight;
214 if (flags & DT_TABSTOP)
215 tabstop = flags >> 8;
217 if (flags & DT_EXPANDTABS)
219 GetTextExtentPoint16(hdc, " ", 1, &size);
220 spacewidth = size.cx;
221 GetTextExtentPoint16(hdc, "o", 1, &size);
222 tabwidth = size.cx * tabstop;
227 prefix_offset = -1;
228 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
230 if (prefix_offset != -1)
232 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
233 prefix_x = size.cx;
234 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
235 prefix_end = size.cx - 1;
238 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
239 if (flags & DT_CENTER) x = (rect->left + rect->right -
240 size.cx) / 2;
241 else if (flags & DT_RIGHT) x = rect->right - size.cx;
243 if (flags & DT_SINGLELINE)
245 if (flags & DT_VCENTER) y = rect->top +
246 (rect->bottom - rect->top) / 2 - size.cy / 2;
247 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
249 if (!(flags & DT_CALCRECT))
251 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
252 rect, line, len, NULL )) return 0;
253 if (prefix_offset != -1)
255 HPEN16 hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
256 HPEN16 oldPen = SelectObject( hdc, hpen );
257 MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
258 LineTo(hdc, x + prefix_end, y + tm.tmAscent + 1 );
259 SelectObject( hdc, oldPen );
260 DeleteObject( hpen );
263 else if (size.cx > max_width)
264 max_width = size.cx;
266 y += lh;
267 if (strPtr)
269 if (!(flags & DT_NOCLIP) && !(flags & DT_CALCRECT))
271 if (y > rect->bottom - lh)
272 break;
276 while (strPtr);
277 if (flags & DT_CALCRECT)
279 rect->right = rect->left + max_width;
280 rect->bottom = y;
282 return 1;
286 /***********************************************************************
287 * DrawText32A (USER32.163)
289 INT32 DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
290 LPRECT32 rect, UINT32 flags )
292 RECT16 rect16;
293 INT16 ret;
295 if (!rect)
296 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
297 CONV_RECT32TO16( rect, &rect16 );
298 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
299 CONV_RECT16TO32( &rect16, rect );
300 return ret;
304 /***********************************************************************
305 * DrawText32W (USER32.166)
307 INT32 DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
308 LPRECT32 rect, UINT32 flags )
310 char *p = STRING32_DupUniToAnsi( str );
311 INT32 ret = DrawText32A( hdc, p, count, rect, flags );
312 free(p);
313 return ret;
317 /***********************************************************************
318 * ExtTextOut16 (GDI.351)
320 BOOL16 ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
321 const RECT16 *lprect, LPCSTR str, UINT16 count,
322 const INT16 *lpDx )
324 int dir, ascent, descent, i;
325 XCharStruct info;
326 XFontStruct *font;
327 RECT16 rect;
329 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
330 if (!dc)
332 dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
333 if (!dc) return FALSE;
334 MF_TextOut( dc, x, y, str, count );
335 return TRUE;
338 if (!DC_SetupGCForText( dc )) return TRUE;
339 font = dc->u.x.font.fstruct;
341 dprintf_text(stddeb,"ExtTextOut: hdc=%04x %d,%d '%*.*s', %d flags=%d\n",
342 hdc, x, y, count, count, str, count, flags);
343 if (lprect != NULL) dprintf_text(stddeb, "\trect=(%d,%d- %d,%d)\n",
344 lprect->left, lprect->top,
345 lprect->right, lprect->bottom );
347 /* Setup coordinates */
349 if (dc->w.textAlign & TA_UPDATECP)
351 x = dc->w.CursPosX;
352 y = dc->w.CursPosY;
355 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
357 if (!lprect) /* not always */
359 SIZE16 sz;
360 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
361 return FALSE;
362 if (!GetTextExtentPoint16( hdc, str, count, &sz ))
363 return FALSE;
364 rect.left = XLPTODP( dc, x );
365 rect.right = XLPTODP( dc, x+sz.cx );
366 rect.top = YLPTODP( dc, y );
367 rect.bottom = YLPTODP( dc, y+sz.cy );
369 else
371 rect.left = XLPTODP( dc, lprect->left );
372 rect.right = XLPTODP( dc, lprect->right );
373 rect.top = YLPTODP( dc, lprect->top );
374 rect.bottom = YLPTODP( dc, lprect->bottom );
376 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
377 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
380 x = XLPTODP( dc, x );
381 y = YLPTODP( dc, y );
383 dprintf_text(stddeb,"\treal coord: x=%i, y=%i, rect=(%d,%d-%d,%d)\n",
384 x, y, rect.left, rect.top, rect.right, rect.bottom);
386 /* Draw the rectangle */
388 if (flags & ETO_OPAQUE)
390 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
391 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
392 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
393 rect.right-rect.left, rect.bottom-rect.top );
395 if (!count) return TRUE; /* Nothing more to do */
397 /* Compute text starting position */
399 XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
401 if (lpDx) /* have explicit character cell x offsets */
403 /* sum lpDx array and add the width of last character */
405 info.width = XTextWidth( font, str + count - 1, 1) + dc->w.charExtra;
406 if (str[count-1] == (char)dc->u.x.font.metrics.tmBreakChar)
407 info.width += dc->w.breakExtra;
409 for (i = 0; i < count; i++) info.width += lpDx[i];
411 else
412 info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
414 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
416 case TA_LEFT:
417 if (dc->w.textAlign & TA_UPDATECP)
418 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
419 break;
420 case TA_RIGHT:
421 x -= info.width;
422 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
423 break;
424 case TA_CENTER:
425 x -= info.width / 2;
426 break;
429 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
431 case TA_TOP:
432 y += font->ascent;
433 break;
434 case TA_BOTTOM:
435 y -= font->descent;
436 break;
437 case TA_BASELINE:
438 break;
441 /* Set the clip region */
443 if (flags & ETO_CLIPPED)
445 SaveVisRgn( hdc );
446 IntersectVisRect( hdc, rect.left, rect.top, rect.right, rect.bottom );
449 /* Draw the text background if necessary */
451 if (dc->w.backgroundMode != TRANSPARENT)
453 /* If rectangle is opaque and clipped, do nothing */
454 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
456 /* Only draw if rectangle is not opaque or if some */
457 /* text is outside the rectangle */
458 if (!(flags & ETO_OPAQUE) ||
459 (x < rect.left) ||
460 (x + info.width >= rect.right) ||
461 (y-font->ascent < rect.top) ||
462 (y+font->descent >= rect.bottom))
464 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
465 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
466 dc->w.DCOrgX + x,
467 dc->w.DCOrgY + y - font->ascent,
468 info.width,
469 font->ascent + font->descent );
474 /* Draw the text (count > 0 verified) */
476 XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
477 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
479 XDrawString( display, dc->u.x.drawable, dc->u.x.gc,
480 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
482 else /* Now the fun begins... */
484 XTextItem *items, *pitem;
485 int delta;
487 /* allocate max items */
489 pitem = items = xmalloc( count * sizeof(XTextItem) );
490 delta = i = 0;
491 while (i < count)
493 /* initialize text item with accumulated delta */
495 pitem->chars = (char *)str + i;
496 pitem->delta = delta;
497 pitem->nchars = 0;
498 pitem->font = None;
499 delta = 0;
501 /* stuff characters into the same XTextItem until new delta
502 * becomes non-zero */
506 if (lpDx) delta += lpDx[i] - XTextWidth( font, str + i, 1);
507 else
509 delta += dc->w.charExtra;
510 if (str[i] == (char)dc->u.x.font.metrics.tmBreakChar)
511 delta += dc->w.breakExtra;
513 pitem->nchars++;
515 while ((++i < count) && !delta);
516 pitem++;
519 XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
520 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
521 free( items );
524 /* Draw underline and strike-out if needed */
526 if (dc->u.x.font.metrics.tmUnderlined)
528 long linePos, lineWidth;
529 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
530 linePos = font->descent-1;
531 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
532 lineWidth = 0;
533 else if (lineWidth == 1) lineWidth = 0;
534 XSetLineAttributes( display, dc->u.x.gc, lineWidth,
535 LineSolid, CapRound, JoinBevel );
536 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
537 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
538 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
540 if (dc->u.x.font.metrics.tmStruckOut)
542 long lineAscent, lineDescent;
543 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
544 lineAscent = font->ascent / 3;
545 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
546 lineDescent = -lineAscent;
547 XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
548 LineSolid, CapRound, JoinBevel );
549 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
550 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
551 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
553 if (flags & ETO_CLIPPED) RestoreVisRgn( hdc );
554 return TRUE;
558 /***********************************************************************
559 * ExtTextOut32A (GDI32.98)
561 BOOL32 ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
562 const RECT32 *lprect, LPCSTR str, UINT32 count,
563 const INT32 *lpDx )
565 RECT16 rect16;
567 if (lpDx) fprintf( stderr, "ExtTextOut32A: lpDx not implemented\n" );
568 if (!lprect)
569 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
570 NULL, str, (UINT16)count, NULL );
571 CONV_RECT32TO16( lprect, &rect16 );
572 return ExtTextOut16( (HDC16)hdc, (INT16)x, (INT16)y, (UINT16)flags,
573 &rect16, str, (UINT16)count, NULL );
577 /***********************************************************************
578 * ExtTextOut32W (GDI32.99)
580 BOOL32 ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
581 const RECT32 *lprect, LPCWSTR str, UINT32 count,
582 const INT32 *lpDx )
584 char *p = STRING32_DupUniToAnsi( str );
585 INT32 ret = ExtTextOut32A( hdc, x, y, flags, lprect, p, count, lpDx );
586 free(p);
587 return ret;
591 /***********************************************************************
592 * TextOut16 (GDI.33)
594 BOOL16 TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
596 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
600 /***********************************************************************
601 * TextOut32A (GDI32.355)
603 BOOL32 TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
605 return ExtTextOut32A( hdc, x, y, 0, NULL, str, count, NULL );
609 /***********************************************************************
610 * TextOut32W (GDI32.356)
612 BOOL32 TextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count )
614 return ExtTextOut32W( hdc, x, y, 0, NULL, str, count, NULL );
618 /***********************************************************************
619 * GrayString (USER.185)
621 BOOL GrayString(HDC hdc, HBRUSH hbr, FARPROC16 gsprc, LPARAM lParam,
622 INT cch, INT x, INT y, INT cx, INT cy)
624 BOOL ret;
625 COLORREF current_color;
627 if (!cch) cch = lstrlen16( (LPCSTR)PTR_SEG_TO_LIN(lParam) );
628 if (gsprc) return CallGrayStringProc( gsprc, hdc, lParam, cch );
629 current_color = GetTextColor( hdc );
630 SetTextColor( hdc, GetSysColor(COLOR_GRAYTEXT) );
631 ret = TextOut16( hdc, x, y, (LPCSTR)PTR_SEG_TO_LIN(lParam), cch );
632 SetTextColor( hdc, current_color );
633 return ret;
637 /***********************************************************************
638 * TEXT_TabbedTextOut
640 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
641 * Note: this doesn't work too well for text-alignment modes other
642 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
644 LONG TEXT_TabbedTextOut( HDC hdc, int x, int y, LPSTR lpstr, int count,
645 int cTabStops, LPINT16 lpTabPos, int nTabOrg,
646 BOOL fDisplayText)
648 WORD defWidth;
649 DWORD extent = 0;
650 int i, tabPos = x;
651 int start = x;
653 if (cTabStops == 1)
655 defWidth = *lpTabPos;
656 cTabStops = 0;
658 else
660 TEXTMETRIC16 tm;
661 GetTextMetrics16( hdc, &tm );
662 defWidth = 8 * tm.tmAveCharWidth;
665 while (count > 0)
667 for (i = 0; i < count; i++)
668 if (lpstr[i] == '\t') break;
669 extent = GetTextExtent( hdc, lpstr, i );
670 while ((cTabStops > 0) && (nTabOrg + *lpTabPos <= x + LOWORD(extent)))
672 lpTabPos++;
673 cTabStops--;
675 if (i == count)
676 tabPos = x + LOWORD(extent);
677 else if (cTabStops > 0)
678 tabPos = nTabOrg + *lpTabPos;
679 else
680 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
681 if (fDisplayText)
683 RECT16 r;
684 SetRect16( &r, x, y, tabPos, y+HIWORD(extent) );
685 ExtTextOut16( hdc, x, y,
686 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
687 &r, lpstr, i, NULL );
689 x = tabPos;
690 count -= i+1;
691 lpstr += i+1;
693 return MAKELONG(tabPos - start, HIWORD(extent));
697 /***********************************************************************
698 * TabbedTextOut (USER.196)
700 LONG TabbedTextOut( HDC hdc, short x, short y, LPSTR lpstr, short count,
701 short cTabStops, LPINT16 lpTabPos, short nTabOrg )
703 dprintf_text( stddeb, "TabbedTextOut: %04x %d,%d '%*.*s' %d\n",
704 hdc, x, y, count, count, lpstr, count );
705 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
706 lpTabPos, nTabOrg, TRUE );
710 /***********************************************************************
711 * GetTabbedTextExtent (USER.197)
713 DWORD GetTabbedTextExtent( HDC hdc, LPSTR lpstr, int count,
714 int cTabStops, LPINT16 lpTabPos )
716 dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%*.*s' %d\n",
717 hdc, count, count, lpstr, count );
718 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
719 lpTabPos, 0, FALSE );