Implemented BitBlt, StretchBlt and SetDIBitsToDevice EMF driver
[wine/gsoc_dplay.git] / graphics / x11drv / text.c
blob4b8aac9b49b4f061d37cbcd00bbaae5178378511
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 <stdarg.h>
28 #include <stdlib.h>
29 #include <math.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "wownt32.h"
35 #include "gdi.h"
36 #include "x11font.h"
37 #include "bitmap.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(text);
42 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
43 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
46 /***********************************************************************
47 * X11DRV_ExtTextOut
49 BOOL
50 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
51 const RECT *lprect, LPCWSTR wstr, UINT count,
52 const INT *lpDx )
54 int i;
55 fontObject* pfo;
56 INT width, ascent, descent, xwidth, ywidth;
57 XFontStruct* font;
58 RECT rect;
59 char dfBreakChar, lfUnderline, lfStrikeOut;
60 BOOL rotated = FALSE;
61 XChar2b *str2b = NULL;
62 BOOL dibUpdateFlag = FALSE;
63 BOOL result = TRUE;
64 POINT pt;
65 DC *dc = physDev->dc;
67 if(dc->gdiFont)
68 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count,
69 lpDx);
72 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
74 pfo = XFONT_GetFontObject( physDev->font );
75 font = pfo->fs;
77 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
78 rotated = TRUE;
79 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
80 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
81 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
83 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
84 dc->hSelf, (UINT16)(physDev->font), x, y,
85 debugstr_wn (wstr, count), count, flags, lpDx);
87 /* some strings sent here end in a newline for whatever reason. I have no
88 clue what the right treatment should be in general, but ignoring
89 terminating newlines seems ok. MW, April 1998. */
90 if (count > 0 && wstr[count - 1] == '\n') count--;
92 if (lprect != NULL) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
93 lprect->left, lprect->top,
94 lprect->right, lprect->bottom );
95 /* Setup coordinates */
97 if (dc->textAlign & TA_UPDATECP)
99 x = dc->CursPosX;
100 y = dc->CursPosY;
103 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
105 if (!lprect) /* not always */
107 SIZE sz;
108 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
109 return FALSE;
110 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
111 return FALSE;
112 rect.left = x;
113 rect.right = x + sz.cx;
114 rect.top = y;
115 rect.bottom = y + sz.cy;
117 else
119 rect = *lprect;
121 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
123 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
124 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
127 pt.x = x;
128 pt.y = y;
129 LPtoDP(physDev->hdc, &pt, 1);
130 x = pt.x;
131 y = pt.y;
133 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
134 x, y, rect.left, rect.top, rect.right, rect.bottom);
136 /* Draw the rectangle */
138 if (flags & ETO_OPAQUE)
140 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
141 dibUpdateFlag = TRUE;
142 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
143 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
144 physDev->org.x + rect.left, physDev->org.y + rect.top,
145 rect.right-rect.left, rect.bottom-rect.top );
147 if (!count) goto END; /* Nothing more to do */
149 /* Compute text starting position */
151 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
153 for (i = width = 0; i < count; i++) width += lpDx[i];
154 width = INTERNAL_XWSTODS(dc, width);
156 else
158 SIZE sz;
159 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
161 result = FALSE;
162 goto END;
164 width = INTERNAL_XWSTODS(dc, sz.cx);
166 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
167 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
168 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
169 pfo->lpX11Trans->pixelsize : width;
170 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
171 pfo->lpX11Trans->pixelsize : 0;
173 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
175 case TA_LEFT:
176 if (dc->textAlign & TA_UPDATECP) {
177 pt.x = x + xwidth;
178 pt.y = y - ywidth;
179 DPtoLP(physDev->hdc, &pt, 1);
180 dc->CursPosX = pt.x;
181 dc->CursPosY = pt.y;
183 break;
184 case TA_RIGHT:
185 x -= xwidth;
186 y += ywidth;
187 if (dc->textAlign & TA_UPDATECP) {
188 pt.x = x;
189 pt.y = y;
190 DPtoLP(physDev->hdc, &pt, 1);
191 dc->CursPosX = pt.x;
192 dc->CursPosY = pt.y;
194 break;
195 case TA_CENTER:
196 x -= xwidth / 2;
197 y += ywidth / 2;
198 break;
201 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
203 case TA_TOP:
204 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
205 pfo->lpX11Trans->pixelsize : 0;
206 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
207 pfo->lpX11Trans->pixelsize : ascent;
208 break;
209 case TA_BOTTOM:
210 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
211 pfo->lpX11Trans->pixelsize : 0;
212 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
213 pfo->lpX11Trans->pixelsize : descent;
214 break;
215 case TA_BASELINE:
216 break;
219 /* Set the clip region */
221 if (flags & ETO_CLIPPED)
223 SaveVisRgn16( HDC_16(dc->hSelf) );
224 IntersectVisRect16( HDC_16(dc->hSelf), lprect->left, lprect->top, lprect->right, lprect->bottom );
227 /* Draw the text background if necessary */
229 if (!dibUpdateFlag)
231 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
232 dibUpdateFlag = TRUE;
235 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
237 /* If rectangle is opaque and clipped, do nothing */
238 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
240 /* Only draw if rectangle is not opaque or if some */
241 /* text is outside the rectangle */
242 if (!(flags & ETO_OPAQUE) ||
243 (x < rect.left) ||
244 (x + width >= rect.right) ||
245 (y - ascent < rect.top) ||
246 (y + descent >= rect.bottom))
248 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
249 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
250 physDev->org.x + x, physDev->org.y + y - ascent,
251 width, ascent + descent );
256 /* Draw the text (count > 0 verified) */
257 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
258 goto FAIL;
260 TSXSetForeground( gdi_display, physDev->gc, physDev->textPixel );
261 if(!rotated)
263 if (!dc->charExtra && !dc->breakExtra && !lpDx)
265 X11DRV_cptable[pfo->fi->cptable].pDrawString(
266 pfo, gdi_display, physDev->drawable, physDev->gc,
267 physDev->org.x + x, physDev->org.y + y, str2b, count );
269 else /* Now the fun begins... */
271 XTextItem16 *items, *pitem;
272 int delta;
274 /* allocate max items */
276 pitem = items = HeapAlloc( GetProcessHeap(), 0,
277 count * sizeof(XTextItem16) );
278 if(items == NULL) goto FAIL;
279 delta = i = 0;
280 if( lpDx ) /* explicit character widths */
282 long ve_we;
283 unsigned short err = 0;
285 ve_we = (LONG)(dc->xformWorld2Vport.eM11 * 0x10000);
287 while (i < count)
289 /* initialize text item with accumulated delta */
291 long sum;
292 long fSum;
293 sum = 0;
294 pitem->chars = str2b + i;
295 pitem->delta = delta;
296 pitem->nchars = 0;
297 pitem->font = None;
298 delta = 0;
300 /* add characters to the same XTextItem
301 * until new delta becomes non-zero */
305 sum += lpDx[i];
306 fSum = sum*ve_we+err;
307 delta = (short)HIWORD(fSum)
308 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
309 pfo, pitem->chars, pitem->nchars+1);
310 pitem->nchars++;
311 } while ((++i < count) && !delta);
312 pitem++;
313 err = LOWORD(fSum);
316 else /* charExtra or breakExtra */
318 while (i < count)
320 pitem->chars = str2b + i;
321 pitem->delta = delta;
322 pitem->nchars = 0;
323 pitem->font = None;
324 delta = 0;
328 delta += dc->charExtra;
329 if (str2b[i].byte2 == (char)dfBreakChar)
330 delta += dc->breakExtra;
331 pitem->nchars++;
332 } while ((++i < count) && !delta);
333 pitem++;
337 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
338 physDev->drawable, physDev->gc,
339 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
340 HeapFree( GetProcessHeap(), 0, items );
343 else /* rotated */
345 /* have to render character by character. */
346 double offset = 0.0;
347 int i;
349 for (i=0; i<count; i++)
351 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
352 - font->min_char_or_byte2;
353 int x_i = IROUND((double) (physDev->org.x + x) + offset *
354 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
355 int y_i = IROUND((double) (physDev->org.y + y) - offset *
356 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
358 X11DRV_cptable[pfo->fi->cptable].pDrawString(
359 pfo, gdi_display, physDev->drawable, physDev->gc,
360 x_i, y_i, &str2b[i], 1);
361 if (lpDx)
363 offset += INTERNAL_XWSTODS(dc, lpDx[i]);
365 else
367 offset += (double) (font->per_char ?
368 font->per_char[char_metric_offset].attributes:
369 font->min_bounds.attributes)
370 * pfo->lpX11Trans->pixelsize / 1000.0;
371 offset += dc->charExtra;
372 if (str2b[i].byte2 == (char)dfBreakChar)
373 offset += dc->breakExtra;
377 HeapFree( GetProcessHeap(), 0, str2b );
379 /* Draw underline and strike-out if needed */
381 if (lfUnderline)
383 long linePos, lineWidth;
385 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
386 linePos = descent - 1;
387 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
388 lineWidth = 0;
389 else if (lineWidth == 1) lineWidth = 0;
390 TSXSetLineAttributes( gdi_display, physDev->gc, lineWidth,
391 LineSolid, CapRound, JoinBevel );
392 TSXDrawLine( gdi_display, physDev->drawable, physDev->gc,
393 physDev->org.x + x, physDev->org.y + y + linePos,
394 physDev->org.x + x + width, physDev->org.y + y + linePos );
396 if (lfStrikeOut)
398 long lineAscent, lineDescent;
399 if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
400 lineAscent = ascent / 2;
401 if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
402 lineDescent = -lineAscent * 2 / 3;
403 TSXSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
404 LineSolid, CapRound, JoinBevel );
405 TSXDrawLine( gdi_display, physDev->drawable, physDev->gc,
406 physDev->org.x + x, physDev->org.y + y - lineAscent,
407 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
410 if (flags & ETO_CLIPPED) RestoreVisRgn16( HDC_16(dc->hSelf) );
411 goto END;
413 FAIL:
414 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
415 result = FALSE;
417 END:
418 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
419 return result;
423 /***********************************************************************
424 * X11DRV_GetTextExtentPoint
426 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
427 LPSIZE size )
429 DC *dc = physDev->dc;
430 fontObject* pfo = XFONT_GetFontObject( physDev->font );
432 TRACE("%s %d\n", debugstr_wn(str,count), count);
433 if( pfo ) {
434 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
435 if (!p) return FALSE;
436 if( !pfo->lpX11Trans ) {
437 int dir, ascent, descent;
438 int info_width;
439 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
440 count, &dir, &ascent, &descent, &info_width );
442 size->cx = fabs((FLOAT)(info_width + dc->breakRem + count *
443 dc->charExtra) * dc->xformVport2World.eM11);
444 size->cy = fabs((FLOAT)(pfo->fs->ascent + pfo->fs->descent) *
445 dc->xformVport2World.eM22);
446 } else {
447 INT i;
448 float x = 0.0, y = 0.0;
449 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
450 for(i = 0; i < count; i++) {
451 x += pfo->fs->per_char ?
452 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
453 pfo->fs->min_bounds.attributes;
455 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
456 TRACE("x = %f y = %f\n", x, y);
457 x *= pfo->lpX11Trans->pixelsize / 1000.0;
458 y *= pfo->lpX11Trans->pixelsize / 1000.0;
459 size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
460 dc->xformVport2World.eM11);
461 size->cy = fabs(y * dc->xformVport2World.eM22);
463 size->cx *= pfo->rescale;
464 size->cy *= pfo->rescale;
465 HeapFree( GetProcessHeap(), 0, p );
466 return TRUE;
468 return FALSE;