Release 951212
[wine/multimedia.git] / objects / text.c
blob3b6ec2240b1e7a1cdcbe2691938ccb9b71be6cd4
1 /*
2 * text functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1993, 1994";
7 */
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 "stddebug.h"
16 /* #define DEBUG_TEXT */
17 #include "debug.h"
18 #include "xmalloc.h"
20 #define TAB 9
21 #define LF 10
22 #define CR 13
23 #define SPACE 32
24 #define PREFIX 38
26 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
28 static int tabstop = 8;
29 static int tabwidth;
30 static int spacewidth;
31 static int prefix_offset;
34 static char *TEXT_NextLine(HDC hdc, char *str, int *count, char *dest,
35 int *len, int width, WORD format)
37 /* Return next line of text from a string.
39 * hdc - handle to DC.
40 * str - string to parse into lines.
41 * count - length of str.
42 * dest - destination in which to return line.
43 * len - length of resultant line in dest in chars.
44 * width - maximum width of line in pixels.
45 * format - format type passed to DrawText.
47 * Returns pointer to next char in str after end of the line
48 * or NULL if end of str reached.
51 int i = 0, j = 0, k;
52 int plen = 0;
53 int numspaces;
54 SIZE size;
55 int lasttab = 0;
56 int wb_i = 0, wb_j = 0, wb_count = 0;
58 while (*count)
60 switch (str[i])
62 case CR:
63 case LF:
64 if (!(format & DT_SINGLELINE))
66 if (str[i] == CR && str[i+1] == LF)
67 i++;
68 i++;
69 *len = j;
70 (*count)--;
71 return (&str[i]);
73 dest[j++] = str[i++];
74 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
75 (format & DT_WORDBREAK))
77 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
78 return NULL;
79 plen += size.cx;
81 break;
83 case PREFIX:
84 if (!(format & DT_NOPREFIX))
86 prefix_offset = j;
87 i++;
89 else
91 dest[j++] = str[i++];
92 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
94 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
95 return NULL;
96 plen += size.cx;
99 break;
101 case TAB:
102 if (format & DT_EXPANDTABS)
104 wb_i = ++i;
105 wb_j = j;
106 wb_count = *count;
108 if (!GetTextExtentPoint(hdc, &dest[lasttab], j - lasttab,
109 &size))
110 return NULL;
112 numspaces = (tabwidth - size.cx) / spacewidth;
113 for (k = 0; k < numspaces; k++)
114 dest[j++] = SPACE;
115 plen += tabwidth - size.cx;
116 lasttab = wb_j + numspaces;
118 else
120 dest[j++] = str[i++];
121 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
122 (format & DT_WORDBREAK))
124 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
125 return NULL;
126 plen += size.cx;
129 break;
131 case SPACE:
132 dest[j++] = str[i++];
133 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
134 (format & DT_WORDBREAK))
136 wb_i = i;
137 wb_j = j - 1;
138 wb_count = *count;
139 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
140 return NULL;
141 plen += size.cx;
143 break;
145 default:
146 dest[j++] = str[i++];
147 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
148 (format & DT_WORDBREAK))
150 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
151 return NULL;
152 plen += size.cx;
156 (*count)--;
157 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
159 if (plen > width)
161 if (format & DT_WORDBREAK)
163 if (wb_j)
165 *len = wb_j;
166 *count = wb_count - 1;
167 return (&str[wb_i]);
170 else
172 *len = j;
173 return (&str[i]);
179 *len = j;
180 return NULL;
184 /***********************************************************************
185 * DrawText (USER.85)
187 int DrawText( HDC hdc, LPSTR str, int count, LPRECT rect, WORD flags )
189 SIZE size;
190 char *strPtr;
191 static char line[1024];
192 int len, lh;
193 int prefix_x = 0;
194 int prefix_end = 0;
195 TEXTMETRIC tm;
196 int x = rect->left, y = rect->top;
197 int width = rect->right - rect->left;
198 int max_width = 0;
200 dprintf_text(stddeb,"DrawText: '%s', %d , [(%ld,%ld),(%ld,%ld)]\n", str,
201 count, (LONG)rect->left, (LONG)rect->top, (LONG)rect->right,
202 (LONG)rect->bottom);
204 if (count == -1) count = strlen(str);
205 strPtr = str;
207 GetTextMetrics(hdc, &tm);
208 if (flags & DT_EXTERNALLEADING)
209 lh = tm.tmHeight + tm.tmExternalLeading;
210 else
211 lh = tm.tmHeight;
213 if (flags & DT_TABSTOP)
214 tabstop = flags >> 8;
216 if (flags & DT_EXPANDTABS)
218 GetTextExtentPoint(hdc, " ", 1, &size);
219 spacewidth = size.cx;
220 GetTextExtentPoint(hdc, "o", 1, &size);
221 tabwidth = size.cx * tabstop;
226 prefix_offset = -1;
227 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
229 if (prefix_offset != -1)
231 GetTextExtentPoint(hdc, line, prefix_offset, &size);
232 prefix_x = size.cx;
233 GetTextExtentPoint(hdc, line, prefix_offset + 1, &size);
234 prefix_end = size.cx - 1;
237 if (!GetTextExtentPoint(hdc, line, len, &size)) return 0;
238 if (flags & DT_CENTER) x = (rect->left + rect->right -
239 size.cx) / 2;
240 else if (flags & DT_RIGHT) x = rect->right - size.cx;
242 if (flags & DT_SINGLELINE)
244 if (flags & DT_VCENTER) y = rect->top +
245 (rect->bottom - rect->top) / 2 - size.cy / 2;
246 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
248 if (!(flags & DT_CALCRECT))
250 if (!ExtTextOut( hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
251 rect, line, len, NULL )) return 0;
253 else if (size.cx > max_width)
254 max_width = size.cx;
256 if (prefix_offset != -1)
258 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
259 HPEN oldPen = SelectObject( hdc, hpen );
260 MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
261 LineTo(hdc, x + prefix_end, y + tm.tmAscent + 1 );
262 SelectObject( hdc, oldPen );
263 DeleteObject( hpen );
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 * ExtTextOut (GDI.351)
289 BOOL ExtTextOut( HDC hdc, short x, short y, WORD flags, LPRECT lprect,
290 LPSTR str, WORD count, LPINT lpDx )
292 int dir, ascent, descent, i;
293 XCharStruct info;
294 XFontStruct *font;
295 RECT rect;
297 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
298 if (!dc)
300 dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
301 if (!dc) return FALSE;
302 MF_TextOut( dc, x, y, str, count );
303 return TRUE;
306 if (!DC_SetupGCForText( dc )) return TRUE;
307 font = dc->u.x.font.fstruct;
309 dprintf_text(stddeb,"ExtTextOut: %d,%d '%*.*s', %d flags=%d\n",
310 x, y, count, count, str, count, flags);
311 if (lprect != NULL) {
312 dprintf_text(stddeb, "rect %ld %ld %ld %ld\n", (LONG)lprect->left,
313 (LONG)lprect->top, (LONG)lprect->right,
314 (LONG)lprect->bottom );
317 /* Setup coordinates */
319 if (dc->w.textAlign & TA_UPDATECP)
321 x = dc->w.CursPosX;
322 y = dc->w.CursPosY;
324 x = XLPTODP( dc, x );
325 y = YLPTODP( dc, y );
326 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* There's a rectangle */
328 rect.left = XLPTODP( dc, lprect->left );
329 rect.right = XLPTODP( dc, lprect->right );
330 rect.top = YLPTODP( dc, lprect->top );
331 rect.bottom = YLPTODP( dc, lprect->bottom );
332 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
333 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
336 /* Draw the rectangle */
338 if (flags & ETO_OPAQUE)
340 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
341 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
342 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
343 rect.right-rect.left, rect.bottom-rect.top );
345 if (!count) return TRUE; /* Nothing more to do */
347 /* Compute text starting position */
349 XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
350 info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
351 if (lpDx) for (i = 0; i < count; i++) info.width += lpDx[i];
353 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
355 case TA_LEFT:
356 if (dc->w.textAlign & TA_UPDATECP)
357 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
358 break;
359 case TA_RIGHT:
360 x -= info.width;
361 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
362 break;
363 case TA_CENTER:
364 x -= info.width / 2;
365 break;
367 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
369 case TA_TOP:
370 y += font->ascent;
371 break;
372 case TA_BOTTOM:
373 y -= font->descent;
374 break;
375 case TA_BASELINE:
376 break;
379 /* Set the clip region */
381 if (flags & ETO_CLIPPED)
383 SaveVisRgn( hdc );
384 IntersectVisRect( hdc, rect.left, rect.top, rect.right, rect.bottom );
387 /* Draw the text background if necessary */
389 if (dc->w.backgroundMode != TRANSPARENT)
391 /* If rectangle is opaque and clipped, do nothing */
392 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
394 /* Only draw if rectangle is not opaque or if some */
395 /* text is outside the rectangle */
396 if (!(flags & ETO_OPAQUE) ||
397 (x < rect.left) ||
398 (x + info.width >= rect.right) ||
399 (y-font->ascent < rect.top) ||
400 (y+font->descent >= rect.bottom))
402 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
403 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
404 dc->w.DCOrgX + x,
405 dc->w.DCOrgY + y - font->ascent,
406 info.width,
407 font->ascent + font->descent );
412 /* Draw the text */
414 XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
415 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
417 XDrawString( display, dc->u.x.drawable, dc->u.x.gc,
418 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
420 else /* Now the fun begins... */
422 XTextItem *items, *pitem;
424 items = xmalloc( count * sizeof(XTextItem) );
425 for (i = 0, pitem = items; i < count; i++, pitem++)
427 pitem->chars = str + i;
428 pitem->nchars = 1;
429 pitem->font = None;
430 if (i == 0)
432 pitem->delta = 0;
433 continue; /* First iteration -> no delta */
435 pitem->delta = dc->w.charExtra;
436 if (str[i] == (char)dc->u.x.font.metrics.tmBreakChar)
437 pitem->delta += dc->w.breakExtra;
438 if (lpDx)
440 INT width;
441 GetCharWidth( hdc, str[i], str[i], &width );
442 pitem->delta += lpDx[i-1] - width;
445 XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
446 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, count );
447 free( items );
450 /* Draw underline and strike-out if needed */
452 if (dc->u.x.font.metrics.tmUnderlined)
454 long linePos, lineWidth;
455 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
456 linePos = font->descent-1;
457 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
458 lineWidth = 0;
459 else if (lineWidth == 1) lineWidth = 0;
460 XSetLineAttributes( display, dc->u.x.gc, lineWidth,
461 LineSolid, CapRound, JoinBevel );
462 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
463 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
464 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
466 if (dc->u.x.font.metrics.tmStruckOut)
468 long lineAscent, lineDescent;
469 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
470 lineAscent = font->ascent / 3;
471 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
472 lineDescent = -lineAscent;
473 XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
474 LineSolid, CapRound, JoinBevel );
475 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
476 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
477 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
479 if (flags & ETO_CLIPPED) RestoreVisRgn( hdc );
480 return TRUE;
484 /***********************************************************************
485 * TextOut (GDI.33)
487 BOOL TextOut( HDC hdc, short x, short y, LPSTR str, short count )
489 return ExtTextOut( hdc, x, y, 0, NULL, str, count, NULL );
493 /***********************************************************************
494 * GrayString (USER.185)
496 BOOL GrayString(HDC hdc, HBRUSH hbr, FARPROC gsprc, LPARAM lParam,
497 INT cch, INT x, INT y, INT cx, INT cy)
499 int s, current_color;
501 if (gsprc) {
502 return CallGrayStringProc(gsprc, hdc, lParam,
503 cch ? cch : lstrlen((LPCSTR) lParam) );
504 } else {
505 current_color = GetTextColor(hdc);
506 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT) );
507 s = TextOut(hdc, x, y, (LPSTR) lParam,
508 cch ? cch : lstrlen((LPCSTR) lParam) );
509 SetTextColor(hdc, current_color);
511 return s;
516 /***********************************************************************
517 * TEXT_TabbedTextOut
519 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
520 * Note: this doesn't work too well for text-alignment modes other
521 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
523 LONG TEXT_TabbedTextOut( HDC hdc, int x, int y, LPSTR lpstr, int count,
524 int cTabStops, LPINT lpTabPos, int nTabOrg,
525 BOOL fDisplayText)
527 WORD defWidth;
528 DWORD extent;
529 int i, tabPos = 0;
531 if (cTabStops == 1) defWidth = *lpTabPos;
532 else
534 TEXTMETRIC tm;
535 GetTextMetrics( hdc, &tm );
536 defWidth = 8 * tm.tmAveCharWidth;
539 while (count > 0)
541 for (i = 0; i < count; i++)
542 if (lpstr[i] == '\t') break;
543 extent = GetTextExtent( hdc, lpstr, i );
544 while ((cTabStops > 0) && (nTabOrg + *lpTabPos < x + LOWORD(extent)))
546 lpTabPos++;
547 cTabStops--;
549 if (lpstr[i] != '\t')
550 tabPos = x + LOWORD(extent);
551 else if (cTabStops > 0)
552 tabPos = nTabOrg + *lpTabPos;
553 else
554 tabPos = (x + LOWORD(extent) + defWidth - 1) / defWidth * defWidth;
555 if (fDisplayText)
557 RECT r;
558 SetRect( &r, x, y, tabPos, y+HIWORD(extent) );
559 ExtTextOut( hdc, x, y,
560 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
561 &r, lpstr, i, NULL );
563 x = tabPos;
564 count -= i+1;
565 lpstr += i+1;
567 return tabPos;
571 /***********************************************************************
572 * TabbedTextOut (USER.196)
574 LONG TabbedTextOut( HDC hdc, short x, short y, LPSTR lpstr, short count,
575 short cTabStops, LPINT lpTabPos, short nTabOrg )
577 dprintf_text( stddeb, "TabbedTextOut: "NPFMT" %d,%d '%*.*s' %d\n",
578 hdc, x, y, count, count, lpstr, count );
579 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
580 lpTabPos, nTabOrg, TRUE );
584 /***********************************************************************
585 * GetTabbedTextExtent (USER.197)
587 DWORD GetTabbedTextExtent( HDC hdc, LPSTR lpstr, int count,
588 int cTabStops, LPINT lpTabPos )
590 dprintf_text( stddeb, "GetTabbedTextExtent: "NPFMT" '%*.*s' %d\n",
591 hdc, count, count, lpstr, count );
592 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
593 lpTabPos, 0, FALSE );