Release 941122
[wine/multimedia.git] / objects / text.c
blobd07d2b9678bb472275f8413c90c13256ab7357d0
1 /*
2 * text functions
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993, 1994";
9 #include <stdlib.h>
10 #include <X11/Xatom.h>
11 #include "windows.h"
12 #include "gdi.h"
13 #include "metafile.h"
14 #include "stddebug.h"
15 /* #define DEBUG_TEXT /* */
16 /* #undef DEBUG_TEXT /* */
17 #include "debug.h"
19 #define TAB 9
20 #define LF 10
21 #define CR 13
22 #define SPACE 32
23 #define PREFIX 38
25 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
27 static int tabstop = 8;
28 static int tabwidth;
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.
38 * hdc - handle to DC.
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.
50 int i = 0, j = 0, k;
51 int plen = 0;
52 int numspaces;
53 SIZE size;
54 int lasttab = 0;
55 int wb_i = 0, wb_j = 0, wb_count;
57 while (*count)
59 switch (str[i])
61 case CR:
62 case LF:
63 if (!(format & DT_SINGLELINE))
65 if (str[i] == CR && str[i+1] == LF)
66 i++;
67 i++;
68 *len = j;
69 return (&str[i]);
71 dest[j++] = str[i++];
72 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
73 (format & DT_WORDBREAK))
75 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
76 return NULL;
77 plen += size.cx;
79 break;
81 case PREFIX:
82 if (!(format & DT_NOPREFIX))
84 prefix_offset = j;
85 i++;
87 else
89 dest[j++] = str[i++];
90 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
92 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
93 return NULL;
94 plen += size.cx;
97 break;
99 case TAB:
100 if (format & DT_EXPANDTABS)
102 wb_i = ++i;
103 wb_j = j;
104 wb_count = *count;
106 if (!GetTextExtentPoint(hdc, &dest[lasttab], j - lasttab,
107 &size))
108 return NULL;
110 numspaces = (tabwidth - size.cx) / spacewidth;
111 for (k = 0; k < numspaces; k++)
112 dest[j++] = SPACE;
113 plen += tabwidth - size.cx;
114 lasttab = wb_j + numspaces;
116 else
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))
123 return NULL;
124 plen += size.cx;
127 break;
129 case SPACE:
130 dest[j++] = str[i++];
131 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
132 (format & DT_WORDBREAK))
134 wb_i = i;
135 wb_j = j - 1;
136 wb_count = *count;
137 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
138 return NULL;
139 plen += size.cx;
141 break;
143 default:
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))
149 return NULL;
150 plen += size.cx;
154 (*count)--;
155 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
157 if (plen > width)
159 if (format & DT_WORDBREAK)
161 if (wb_j)
163 *len = wb_j;
164 *count = wb_count - 1;
165 return (&str[wb_i]);
168 else
170 *len = j;
171 return (&str[i]);
177 *len = j;
178 return NULL;
182 /***********************************************************************
183 * DrawText (USER.85)
185 int DrawText( HDC hdc, LPSTR str, int count, LPRECT rect, WORD flags )
187 SIZE size;
188 char *strPtr;
189 static char line[1024];
190 int len, lh, prefix_x, prefix_end;
191 TEXTMETRIC tm;
192 int x = rect->left, y = rect->top;
193 int width = rect->right - rect->left;
194 int max_width = 0;
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);
200 strPtr = str;
202 GetTextMetrics(hdc, &tm);
203 if (flags & DT_EXTERNALLEADING)
204 lh = tm.tmHeight + tm.tmExternalLeading;
205 else
206 lh = tm.tmHeight;
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;
221 prefix_offset = -1;
222 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
224 if (prefix_offset != -1)
226 GetTextExtentPoint(hdc, line, prefix_offset, &size);
227 prefix_x = size.cx;
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 -
234 size.cx) / 2;
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)
249 max_width = size.cx;
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 );
261 y += lh;
262 if (strPtr)
264 if (!(flags & DT_NOCLIP))
266 if (y > rect->bottom - lh)
267 break;
271 while (strPtr);
272 if (flags & DT_CALCRECT)
274 rect->right = rect->left + max_width;
275 rect->bottom = y;
277 return 1;
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;
288 XCharStruct info;
289 XFontStruct *font;
290 RECT rect;
292 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
293 if (!dc)
295 dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
296 if (!dc) return FALSE;
297 MF_TextOut( dc, x, y, str, count );
298 return TRUE;
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)
312 x = dc->w.CursPosX;
313 y = dc->w.CursPosY;
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) )
346 case TA_LEFT:
347 if (dc->w.textAlign & TA_UPDATECP)
348 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
349 break;
350 case TA_RIGHT:
351 x -= info.width;
352 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
353 break;
354 case TA_CENTER:
355 x -= info.width / 2;
356 break;
358 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
360 case TA_TOP:
361 y += font->ascent;
362 break;
363 case TA_BOTTOM:
364 y -= font->descent;
365 break;
366 case TA_BASELINE:
367 break;
370 /* Set the clip region */
372 if (flags & ETO_CLIPPED)
374 SaveVisRgn( hdc );
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) ||
388 (x < rect.left) ||
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,
395 dc->w.DCOrgX + x,
396 dc->w.DCOrgY + y - font->ascent,
397 info.width,
398 font->ascent + font->descent );
403 /* Draw the text */
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;
419 pitem->nchars = 1;
420 pitem->font = None;
421 if (i == 0)
423 pitem->delta = 0;
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;
429 if (lpDx)
431 INT width;
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 );
438 free( items );
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 ))
449 lineWidth = 0;
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 );
471 return TRUE;
475 /***********************************************************************
476 * TextOut (GDI.33)
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;
492 if (gsprc) {
493 return CallGrayStringProc(gsprc, hdc, lParam,
494 cch ? cch : lstrlen((LPCSTR) lParam) );
495 } else {
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);
502 return s;
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)
513 WORD width, height;
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);