Removed trailing whitespace.
[wine.git] / graphics / x11drv / text.c
blob325eac0ababea6b80d39994beef62b69e5889115
1 /*
2 * X11 graphics driver text functions
4 * Copyright 1993,1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <X11/Xatom.h>
25 #include "ts_xlib.h"
27 #include <stdlib.h>
28 #include <math.h>
30 #include "windef.h"
31 #include "winnls.h"
32 #include "gdi.h"
33 #include "x11font.h"
34 #include "bitmap.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(text);
39 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
40 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
43 /***********************************************************************
44 * X11DRV_ExtTextOut
46 BOOL
47 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
48 const RECT *lprect, LPCWSTR wstr, UINT count,
49 const INT *lpDx )
51 int i;
52 fontObject* pfo;
53 INT width, ascent, descent, xwidth, ywidth;
54 XFontStruct* font;
55 RECT rect;
56 char dfBreakChar, lfUnderline, lfStrikeOut;
57 BOOL rotated = FALSE;
58 XChar2b *str2b = NULL;
59 BOOL dibUpdateFlag = FALSE;
60 BOOL result = TRUE;
61 DC *dc = physDev->dc;
63 if(dc->gdiFont)
64 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count,
65 lpDx);
68 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
70 pfo = XFONT_GetFontObject( physDev->font );
71 font = pfo->fs;
73 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
74 rotated = TRUE;
75 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
76 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
77 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
79 TRACE("hdc=%04x df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
80 dc->hSelf, (UINT16)(physDev->font), x, y,
81 debugstr_wn (wstr, count), count, flags, lpDx);
83 /* some strings sent here end in a newline for whatever reason. I have no
84 clue what the right treatment should be in general, but ignoring
85 terminating newlines seems ok. MW, April 1998. */
86 if (count > 0 && wstr[count - 1] == '\n') count--;
88 if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
89 lprect->left, lprect->top,
90 lprect->right, lprect->bottom );
91 /* Setup coordinates */
93 if (dc->textAlign & TA_UPDATECP)
95 x = dc->CursPosX;
96 y = dc->CursPosY;
99 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
101 if (!lprect) /* not always */
103 SIZE sz;
104 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
105 return FALSE;
106 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
107 return FALSE;
108 rect.left = INTERNAL_XWPTODP( dc, x, y );
109 rect.right = INTERNAL_XWPTODP( dc, x+sz.cx, y+sz.cy );
110 rect.top = INTERNAL_YWPTODP( dc, x, y );
111 rect.bottom = INTERNAL_YWPTODP( dc, x+sz.cx, y+sz.cy );
113 else
115 rect.left = INTERNAL_XWPTODP( dc, lprect->left, lprect->top );
116 rect.right = INTERNAL_XWPTODP( dc, lprect->right, lprect->bottom );
117 rect.top = INTERNAL_YWPTODP( dc, lprect->left, lprect->top );
118 rect.bottom = INTERNAL_YWPTODP( dc, lprect->right, lprect->bottom );
120 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
121 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
124 x = INTERNAL_XWPTODP( dc, x, y );
125 y = INTERNAL_YWPTODP( dc, x, y );
127 TRACE("\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
128 x, y, rect.left, rect.top, rect.right, rect.bottom);
130 /* Draw the rectangle */
132 if (flags & ETO_OPAQUE)
134 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
135 dibUpdateFlag = TRUE;
136 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
137 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
138 dc->DCOrgX + rect.left, dc->DCOrgY + rect.top,
139 rect.right-rect.left, rect.bottom-rect.top );
141 if (!count) goto END; /* Nothing more to do */
143 /* Compute text starting position */
145 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
147 for (i = width = 0; i < count; i++) width += lpDx[i];
148 width = INTERNAL_XWSTODS(dc, width);
150 else
152 SIZE sz;
153 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
155 result = FALSE;
156 goto END;
158 width = INTERNAL_XWSTODS(dc, sz.cx);
160 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
161 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
162 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
163 pfo->lpX11Trans->pixelsize : width;
164 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
165 pfo->lpX11Trans->pixelsize : 0;
167 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
169 case TA_LEFT:
170 if (dc->textAlign & TA_UPDATECP) {
171 dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
172 dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
174 break;
175 case TA_RIGHT:
176 x -= xwidth;
177 y += ywidth;
178 if (dc->textAlign & TA_UPDATECP) {
179 dc->CursPosX = INTERNAL_XDPTOWP( dc, x, y );
180 dc->CursPosY = INTERNAL_YDPTOWP( dc, x, y );
182 break;
183 case TA_CENTER:
184 x -= xwidth / 2;
185 y += ywidth / 2;
186 break;
189 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
191 case TA_TOP:
192 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
193 pfo->lpX11Trans->pixelsize : 0;
194 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
195 pfo->lpX11Trans->pixelsize : ascent;
196 break;
197 case TA_BOTTOM:
198 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
199 pfo->lpX11Trans->pixelsize : 0;
200 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
201 pfo->lpX11Trans->pixelsize : descent;
202 break;
203 case TA_BASELINE:
204 break;
207 /* Set the clip region */
209 if (flags & ETO_CLIPPED)
211 SaveVisRgn16( dc->hSelf );
212 IntersectVisRect16( dc->hSelf, lprect->left, lprect->top, lprect->right, lprect->bottom );
215 /* Draw the text background if necessary */
217 if (!dibUpdateFlag)
219 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
220 dibUpdateFlag = TRUE;
223 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
225 /* If rectangle is opaque and clipped, do nothing */
226 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
228 /* Only draw if rectangle is not opaque or if some */
229 /* text is outside the rectangle */
230 if (!(flags & ETO_OPAQUE) ||
231 (x < rect.left) ||
232 (x + width >= rect.right) ||
233 (y - ascent < rect.top) ||
234 (y + descent >= rect.bottom))
236 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
237 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
238 dc->DCOrgX + x,
239 dc->DCOrgY + y - ascent,
240 width,
241 ascent + descent );
246 /* Draw the text (count > 0 verified) */
247 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
248 goto FAIL;
250 TSXSetForeground( gdi_display, physDev->gc, physDev->textPixel );
251 if(!rotated)
253 if (!dc->charExtra && !dc->breakExtra && !lpDx)
255 X11DRV_cptable[pfo->fi->cptable].pDrawString(
256 pfo, gdi_display, physDev->drawable, physDev->gc,
257 dc->DCOrgX + x, dc->DCOrgY + y, str2b, count );
259 else /* Now the fun begins... */
261 XTextItem16 *items, *pitem;
262 int delta;
264 /* allocate max items */
266 pitem = items = HeapAlloc( GetProcessHeap(), 0,
267 count * sizeof(XTextItem16) );
268 if(items == NULL) goto FAIL;
269 delta = i = 0;
270 if( lpDx ) /* explicit character widths */
272 long ve_we;
273 unsigned short err = 0;
275 ve_we = (LONG)(dc->xformWorld2Vport.eM11 * 0x10000);
277 while (i < count)
279 /* initialize text item with accumulated delta */
281 long sum;
282 long fSum;
283 sum = 0;
284 pitem->chars = str2b + i;
285 pitem->delta = delta;
286 pitem->nchars = 0;
287 pitem->font = None;
288 delta = 0;
290 /* add characters to the same XTextItem
291 * until new delta becomes non-zero */
295 sum += lpDx[i];
296 fSum = sum*ve_we+err;
297 delta = SHIWORD(fSum)
298 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
299 pfo, pitem->chars, pitem->nchars+1);
300 pitem->nchars++;
301 } while ((++i < count) && !delta);
302 pitem++;
303 err = LOWORD(fSum);
306 else /* charExtra or breakExtra */
308 while (i < count)
310 pitem->chars = str2b + i;
311 pitem->delta = delta;
312 pitem->nchars = 0;
313 pitem->font = None;
314 delta = 0;
318 delta += dc->charExtra;
319 if (str2b[i].byte2 == (char)dfBreakChar)
320 delta += dc->breakExtra;
321 pitem->nchars++;
322 } while ((++i < count) && !delta);
323 pitem++;
327 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
328 physDev->drawable, physDev->gc,
329 dc->DCOrgX + x, dc->DCOrgY + y, items, pitem - items );
330 HeapFree( GetProcessHeap(), 0, items );
333 else /* rotated */
335 /* have to render character by character. */
336 double offset = 0.0;
337 int i;
339 for (i=0; i<count; i++)
341 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
342 - font->min_char_or_byte2;
343 int x_i = IROUND((double) (dc->DCOrgX + x) + offset *
344 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
345 int y_i = IROUND((double) (dc->DCOrgY + y) - offset *
346 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
348 X11DRV_cptable[pfo->fi->cptable].pDrawString(
349 pfo, gdi_display, physDev->drawable, physDev->gc,
350 x_i, y_i, &str2b[i], 1);
351 if (lpDx)
353 offset += INTERNAL_XWSTODS(dc, lpDx[i]);
355 else
357 offset += (double) (font->per_char ?
358 font->per_char[char_metric_offset].attributes:
359 font->min_bounds.attributes)
360 * pfo->lpX11Trans->pixelsize / 1000.0;
361 offset += dc->charExtra;
362 if (str2b[i].byte2 == (char)dfBreakChar)
363 offset += dc->breakExtra;
367 HeapFree( GetProcessHeap(), 0, str2b );
369 /* Draw underline and strike-out if needed */
371 if (lfUnderline)
373 long linePos, lineWidth;
375 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
376 linePos = descent - 1;
377 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
378 lineWidth = 0;
379 else if (lineWidth == 1) lineWidth = 0;
380 TSXSetLineAttributes( gdi_display, physDev->gc, lineWidth,
381 LineSolid, CapRound, JoinBevel );
382 TSXDrawLine( gdi_display, physDev->drawable, physDev->gc,
383 dc->DCOrgX + x, dc->DCOrgY + y + linePos,
384 dc->DCOrgX + x + width, dc->DCOrgY + y + linePos );
386 if (lfStrikeOut)
388 long lineAscent, lineDescent;
389 if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
390 lineAscent = ascent / 2;
391 if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
392 lineDescent = -lineAscent * 2 / 3;
393 TSXSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
394 LineSolid, CapRound, JoinBevel );
395 TSXDrawLine( gdi_display, physDev->drawable, physDev->gc,
396 dc->DCOrgX + x, dc->DCOrgY + y - lineAscent,
397 dc->DCOrgX + x + width, dc->DCOrgY + y - lineAscent );
400 if (flags & ETO_CLIPPED) RestoreVisRgn16( dc->hSelf );
401 goto END;
403 FAIL:
404 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
405 result = FALSE;
407 END:
408 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
409 return result;
413 /***********************************************************************
414 * X11DRV_GetTextExtentPoint
416 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
417 LPSIZE size )
419 DC *dc = physDev->dc;
420 fontObject* pfo = XFONT_GetFontObject( physDev->font );
422 TRACE("%s %d\n", debugstr_wn(str,count), count);
423 if( pfo ) {
424 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
425 if (!p) return FALSE;
426 if( !pfo->lpX11Trans ) {
427 int dir, ascent, descent;
428 int info_width;
429 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
430 count, &dir, &ascent, &descent, &info_width );
432 size->cx = fabs((FLOAT)(info_width + dc->breakRem + count *
433 dc->charExtra) * dc->xformVport2World.eM11);
434 size->cy = fabs((FLOAT)(pfo->fs->ascent + pfo->fs->descent) *
435 dc->xformVport2World.eM22);
436 } else {
437 INT i;
438 float x = 0.0, y = 0.0;
439 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
440 for(i = 0; i < count; i++) {
441 x += pfo->fs->per_char ?
442 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
443 pfo->fs->min_bounds.attributes;
445 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
446 TRACE("x = %f y = %f\n", x, y);
447 x *= pfo->lpX11Trans->pixelsize / 1000.0;
448 y *= pfo->lpX11Trans->pixelsize / 1000.0;
449 size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
450 dc->xformVport2World.eM11);
451 size->cy = fabs(y * dc->xformVport2World.eM22);
453 size->cx *= pfo->rescale;
454 size->cy *= pfo->rescale;
455 HeapFree( GetProcessHeap(), 0, p );
456 return TRUE;
458 return FALSE;