Authors: Ove Kaaven <ovek@transgaming.com>, Andrew Lewycky <andrew@transgaming.com...
[wine/multimedia.git] / graphics / x11drv / text.c
blob98b01155d2af49a96a4015cc358d787936b2283b
1 /*
2 * X11 graphics driver text functions
4 * Copyright 1993,1994 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <X11/Xatom.h>
11 #include "ts_xlib.h"
13 #include <stdlib.h>
14 #include <math.h>
16 #include "windef.h"
17 #include "winnls.h"
18 #include "gdi.h"
19 #include "heap.h"
20 #include "x11font.h"
21 #include "bitmap.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(text);
26 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
27 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
30 /***********************************************************************
31 * X11DRV_ExtTextOut
33 BOOL
34 X11DRV_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
35 const RECT *lprect, LPCWSTR wstr, UINT count,
36 const INT *lpDx )
38 int i;
39 fontObject* pfo;
40 INT width, ascent, descent, xwidth, ywidth;
41 XFontStruct* font;
42 RECT rect;
43 char dfBreakChar, lfUnderline, lfStrikeOut;
44 BOOL rotated = FALSE;
45 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
46 XChar2b *str2b = NULL;
47 BOOL dibUpdateFlag = FALSE;
48 BOOL result = TRUE;
50 if (!X11DRV_SetupGCForText( dc )) return TRUE;
52 pfo = XFONT_GetFontObject( physDev->font );
53 font = pfo->fs;
55 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
56 rotated = TRUE;
57 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
58 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
59 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
61 TRACE("hdc=%04x df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
62 dc->hSelf, (UINT16)(physDev->font), x, y,
63 debugstr_wn (wstr, count), count, flags, lpDx);
65 /* some strings sent here end in a newline for whatever reason. I have no
66 clue what the right treatment should be in general, but ignoring
67 terminating newlines seems ok. MW, April 1998. */
68 if (count > 0 && wstr[count - 1] == '\n') count--;
70 if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
71 lprect->left, lprect->top,
72 lprect->right, lprect->bottom );
73 /* Setup coordinates */
75 if (dc->textAlign & TA_UPDATECP)
77 x = dc->CursPosX;
78 y = dc->CursPosY;
81 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
83 if (!lprect) /* not always */
85 SIZE sz;
86 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
87 return FALSE;
88 if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
89 return FALSE;
90 rect.left = XLPTODP( dc, x );
91 rect.right = XLPTODP( dc, x+sz.cx );
92 rect.top = YLPTODP( dc, y );
93 rect.bottom = YLPTODP( dc, y+sz.cy );
95 else
97 rect.left = XLPTODP( dc, lprect->left );
98 rect.right = XLPTODP( dc, lprect->right );
99 rect.top = YLPTODP( dc, lprect->top );
100 rect.bottom = YLPTODP( dc, lprect->bottom );
102 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
103 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
106 x = XLPTODP( dc, x );
107 y = YLPTODP( dc, y );
109 TRACE("\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
110 x, y, rect.left, rect.top, rect.right, rect.bottom);
112 /* Draw the rectangle */
114 if (flags & ETO_OPAQUE)
116 X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
117 dibUpdateFlag = TRUE;
118 TSXSetForeground( display, physDev->gc, physDev->backgroundPixel );
119 TSXFillRectangle( display, physDev->drawable, physDev->gc,
120 dc->DCOrgX + rect.left, dc->DCOrgY + rect.top,
121 rect.right-rect.left, rect.bottom-rect.top );
123 if (!count) goto END; /* Nothing more to do */
125 /* Compute text starting position */
127 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
129 int extra = dc->wndExtX / 2;
130 for (i = width = 0; i < count; i++) width += lpDx[i];
131 width = (width * dc->vportExtX + extra ) / dc->wndExtX;
133 else
135 SIZE sz;
136 if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
137 return FALSE;
138 width = XLSTODS(dc, sz.cx);
140 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
141 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
142 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
143 pfo->lpX11Trans->pixelsize : width;
144 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
145 pfo->lpX11Trans->pixelsize : 0;
147 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
149 case TA_LEFT:
150 if (dc->textAlign & TA_UPDATECP) {
151 dc->CursPosX = XDPTOLP( dc, x + xwidth );
152 dc->CursPosY = YDPTOLP( dc, y - ywidth );
154 break;
155 case TA_RIGHT:
156 x -= xwidth;
157 y += ywidth;
158 if (dc->textAlign & TA_UPDATECP) {
159 dc->CursPosX = XDPTOLP( dc, x );
160 dc->CursPosY = YDPTOLP( dc, y );
162 break;
163 case TA_CENTER:
164 x -= xwidth / 2;
165 y += ywidth / 2;
166 break;
169 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
171 case TA_TOP:
172 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
173 pfo->lpX11Trans->pixelsize : 0;
174 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
175 pfo->lpX11Trans->pixelsize : ascent;
176 break;
177 case TA_BOTTOM:
178 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
179 pfo->lpX11Trans->pixelsize : 0;
180 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
181 pfo->lpX11Trans->pixelsize : descent;
182 break;
183 case TA_BASELINE:
184 break;
187 /* Set the clip region */
189 if (flags & ETO_CLIPPED)
191 SaveVisRgn16( dc->hSelf );
192 CLIPPING_IntersectVisRect( dc, rect.left, rect.top, rect.right,
193 rect.bottom, FALSE );
196 /* Draw the text background if necessary */
198 if (!dibUpdateFlag)
200 X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
201 dibUpdateFlag = TRUE;
204 if (dc->backgroundMode != TRANSPARENT)
206 /* If rectangle is opaque and clipped, do nothing */
207 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
209 /* Only draw if rectangle is not opaque or if some */
210 /* text is outside the rectangle */
211 if (!(flags & ETO_OPAQUE) ||
212 (x < rect.left) ||
213 (x + width >= rect.right) ||
214 (y - ascent < rect.top) ||
215 (y + descent >= rect.bottom))
217 TSXSetForeground( display, physDev->gc,
218 physDev->backgroundPixel );
219 TSXFillRectangle( display, physDev->drawable, physDev->gc,
220 dc->DCOrgX + x,
221 dc->DCOrgY + y - ascent,
222 width,
223 ascent + descent );
228 /* Draw the text (count > 0 verified) */
229 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
230 goto FAIL;
232 TSXSetForeground( display, physDev->gc, physDev->textPixel );
233 if(!rotated)
235 if (!dc->charExtra && !dc->breakExtra && !lpDx)
237 X11DRV_cptable[pfo->fi->cptable].pDrawString(
238 pfo, display, physDev->drawable, physDev->gc,
239 dc->DCOrgX + x, dc->DCOrgY + y, str2b, count );
241 else /* Now the fun begins... */
243 XTextItem16 *items, *pitem;
244 int delta;
246 /* allocate max items */
248 pitem = items = HeapAlloc( GetProcessHeap(), 0,
249 count * sizeof(XTextItem16) );
250 if(items == NULL) goto FAIL;
251 delta = i = 0;
252 if( lpDx ) /* explicit character widths */
254 const long ve_we = dc->vportExtX*0x10000 / dc->wndExtX;
255 unsigned short err = 0;
257 while (i < count)
259 /* initialize text item with accumulated delta */
261 long sum;
262 long fSum;
263 sum = 0;
264 pitem->chars = str2b + i;
265 pitem->delta = delta;
266 pitem->nchars = 0;
267 pitem->font = None;
268 delta = 0;
270 /* add characters to the same XTextItem
271 * until new delta becomes non-zero */
275 sum += lpDx[i];
276 fSum = sum*ve_we+err;
277 delta = SHIWORD(fSum)
278 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
279 pfo, pitem->chars, pitem->nchars+1);
280 pitem->nchars++;
281 } while ((++i < count) && !delta);
282 pitem++;
283 err = LOWORD(fSum);
286 else /* charExtra or breakExtra */
288 while (i < count)
290 pitem->chars = str2b + i;
291 pitem->delta = delta;
292 pitem->nchars = 0;
293 pitem->font = None;
294 delta = 0;
298 delta += dc->charExtra;
299 if (str2b[i].byte2 == (char)dfBreakChar)
300 delta += dc->breakExtra;
301 pitem->nchars++;
302 } while ((++i < count) && !delta);
303 pitem++;
307 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, display,
308 physDev->drawable, physDev->gc,
309 dc->DCOrgX + x, dc->DCOrgY + y, items, pitem - items );
310 HeapFree( GetProcessHeap(), 0, items );
313 else /* rotated */
315 /* have to render character by character. */
316 double offset = 0.0;
317 int i;
319 for (i=0; i<count; i++)
321 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
322 - font->min_char_or_byte2;
323 int x_i = IROUND((double) (dc->DCOrgX + x) + offset *
324 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
325 int y_i = IROUND((double) (dc->DCOrgY + y) - offset *
326 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
328 X11DRV_cptable[pfo->fi->cptable].pDrawString(
329 pfo, display, physDev->drawable, physDev->gc,
330 x_i, y_i, &str2b[i], 1);
331 if (lpDx)
332 offset += XLSTODS(dc, lpDx[i]);
333 else
335 offset += (double) (font->per_char ?
336 font->per_char[char_metric_offset].attributes:
337 font->min_bounds.attributes)
338 * pfo->lpX11Trans->pixelsize / 1000.0;
339 offset += dc->charExtra;
340 if (str2b[i].byte2 == (char)dfBreakChar)
341 offset += dc->breakExtra;
345 HeapFree( GetProcessHeap(), 0, str2b );
347 /* Draw underline and strike-out if needed */
349 if (lfUnderline)
351 long linePos, lineWidth;
353 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
354 linePos = descent - 1;
355 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
356 lineWidth = 0;
357 else if (lineWidth == 1) lineWidth = 0;
358 TSXSetLineAttributes( display, physDev->gc, lineWidth,
359 LineSolid, CapRound, JoinBevel );
360 TSXDrawLine( display, physDev->drawable, physDev->gc,
361 dc->DCOrgX + x, dc->DCOrgY + y + linePos,
362 dc->DCOrgX + x + width, dc->DCOrgY + y + linePos );
364 if (lfStrikeOut)
366 long lineAscent, lineDescent;
367 if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
368 lineAscent = ascent / 2;
369 if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
370 lineDescent = -lineAscent * 2 / 3;
371 TSXSetLineAttributes( display, physDev->gc, lineAscent + lineDescent,
372 LineSolid, CapRound, JoinBevel );
373 TSXDrawLine( display, physDev->drawable, physDev->gc,
374 dc->DCOrgX + x, dc->DCOrgY + y - lineAscent,
375 dc->DCOrgX + x + width, dc->DCOrgY + y - lineAscent );
378 if (flags & ETO_CLIPPED)
379 RestoreVisRgn16( dc->hSelf );
381 goto END;
383 FAIL:
384 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
385 result = FALSE;
387 END:
388 if (dibUpdateFlag) X11DRV_UnlockDIBSection( dc, TRUE );
389 return result;
393 /***********************************************************************
394 * X11DRV_GetTextExtentPoint
396 BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
397 LPSIZE size )
399 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
400 fontObject* pfo = XFONT_GetFontObject( physDev->font );
402 TRACE("%s %d\n", debugstr_wn(str,count), count);
403 if( pfo ) {
404 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
405 if (!p) return FALSE;
406 if( !pfo->lpX11Trans ) {
407 int dir, ascent, descent;
408 int info_width;
409 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
410 count, &dir, &ascent, &descent, &info_width );
411 size->cx = abs((info_width + dc->breakRem + count *
412 dc->charExtra) * dc->wndExtX / dc->vportExtX);
413 size->cy = abs((pfo->fs->ascent + pfo->fs->descent) *
414 dc->wndExtY / dc->vportExtY);
415 } else {
416 INT i;
417 float x = 0.0, y = 0.0;
418 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
419 for(i = 0; i < count; i++) {
420 x += pfo->fs->per_char ?
421 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
422 pfo->fs->min_bounds.attributes;
424 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
425 TRACE("x = %f y = %f\n", x, y);
426 x *= pfo->lpX11Trans->pixelsize / 1000.0;
427 y *= pfo->lpX11Trans->pixelsize / 1000.0;
428 size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
429 dc->wndExtX / dc->vportExtX);
430 size->cy = fabs(y * dc->wndExtY / dc->vportExtY);
432 size->cx *= pfo->rescale;
433 size->cy *= pfo->rescale;
434 HeapFree( GetProcessHeap(), 0, p );
435 return TRUE;
437 return FALSE;