Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / x11drv / text.c
blobbcb37e8c65e51a66b7c835f02e4b56564052a001
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 <stdarg.h>
24 #include <stdlib.h>
25 #include <math.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "x11font.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(text);
35 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
36 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
39 /***********************************************************************
40 * X11DRV_ExtTextOut
42 BOOL
43 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
44 const RECT *lprect, LPCWSTR wstr, UINT count,
45 const INT *lpDx, INT breakExtra )
47 unsigned int i;
48 fontObject* pfo;
49 INT width, ascent, descent, xwidth, ywidth;
50 XFontStruct* font;
51 RECT rect;
52 char dfBreakChar, lfUnderline, lfStrikeOut;
53 BOOL rotated = FALSE;
54 XChar2b *str2b = NULL;
55 BOOL dibUpdateFlag = FALSE;
56 BOOL result = TRUE;
57 HRGN saved_region = 0;
58 POINT pt;
59 UINT align;
60 INT charExtra;
62 if(physDev->has_gdi_font)
63 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx, breakExtra);
65 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
67 align = GetTextAlign( physDev->hdc );
68 charExtra = GetTextCharacterExtra( physDev->hdc );
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=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
80 physDev->hdc, (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=(%ld,%ld - %ld,%ld)\n",
89 lprect->left, lprect->top,
90 lprect->right, lprect->bottom );
91 /* Setup coordinates */
93 if (align & TA_UPDATECP)
95 GetCurrentPositionEx( physDev->hdc, &pt );
96 x = pt.x;
97 y = pt.y;
100 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
102 if (!lprect) /* not always */
104 SIZE sz;
105 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
106 return FALSE;
107 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
108 return FALSE;
109 rect.left = x;
110 rect.right = x + sz.cx;
111 rect.top = y;
112 rect.bottom = y + sz.cy;
114 else
116 rect = *lprect;
118 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
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 pt.x = x;
125 pt.y = y;
126 LPtoDP(physDev->hdc, &pt, 1);
127 x = pt.x;
128 y = pt.y;
130 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
131 x, y, rect.left, rect.top, rect.right, rect.bottom);
133 /* Draw the rectangle */
135 if (flags & ETO_OPAQUE)
137 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
138 dibUpdateFlag = TRUE;
139 wine_tsx11_lock();
140 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
141 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
142 physDev->org.x + rect.left, physDev->org.y + rect.top,
143 rect.right-rect.left, rect.bottom-rect.top );
144 wine_tsx11_unlock();
146 if (!count) goto END; /* Nothing more to do */
148 /* Compute text starting position */
150 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
152 for (i = width = 0; i < count; i++) width += lpDx[i];
153 width = X11DRV_XWStoDS(physDev, width);
155 else
157 SIZE sz;
158 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
160 result = FALSE;
161 goto END;
163 width = X11DRV_XWStoDS(physDev, sz.cx);
165 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
166 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
167 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
168 pfo->lpX11Trans->pixelsize : width;
169 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
170 pfo->lpX11Trans->pixelsize : 0;
172 switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) )
174 case TA_LEFT:
175 if (align & TA_UPDATECP) {
176 pt.x = x + xwidth;
177 pt.y = y - ywidth;
178 DPtoLP(physDev->hdc, &pt, 1);
179 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
181 break;
182 case TA_RIGHT:
183 x -= xwidth;
184 y += ywidth;
185 if (align & TA_UPDATECP) {
186 pt.x = x;
187 pt.y = y;
188 DPtoLP(physDev->hdc, &pt, 1);
189 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
191 break;
192 case TA_CENTER:
193 x -= xwidth / 2;
194 y += ywidth / 2;
195 break;
198 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
200 case TA_TOP:
201 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
202 pfo->lpX11Trans->pixelsize : 0;
203 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
204 pfo->lpX11Trans->pixelsize : ascent;
205 break;
206 case TA_BOTTOM:
207 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
208 pfo->lpX11Trans->pixelsize : 0;
209 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
210 pfo->lpX11Trans->pixelsize : descent;
211 break;
212 case TA_BASELINE:
213 break;
216 /* Set the clip region */
218 if (flags & ETO_CLIPPED)
220 HRGN clip_region;
221 RECT clip_rect = *lprect;
223 LPtoDP( physDev->hdc, (POINT *)&clip_rect, 2 );
224 clip_region = CreateRectRgnIndirect( &clip_rect );
225 /* make a copy of the current device region */
226 saved_region = CreateRectRgn( 0, 0, 0, 0 );
227 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
228 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
229 DeleteObject( clip_region );
232 /* Draw the text background if necessary */
234 if (!dibUpdateFlag)
236 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
237 dibUpdateFlag = TRUE;
240 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
242 /* If rectangle is opaque and clipped, do nothing */
243 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
245 /* Only draw if rectangle is not opaque or if some */
246 /* text is outside the rectangle */
247 if (!(flags & ETO_OPAQUE) ||
248 (x < rect.left) ||
249 (x + width >= rect.right) ||
250 (y - ascent < rect.top) ||
251 (y + descent >= rect.bottom))
253 wine_tsx11_lock();
254 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
255 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
256 physDev->org.x + x, physDev->org.y + y - ascent,
257 width, ascent + descent );
258 wine_tsx11_unlock();
263 /* Draw the text (count > 0 verified) */
264 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
265 goto FAIL;
267 wine_tsx11_lock();
268 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
269 wine_tsx11_unlock();
270 if(!rotated)
272 if (!charExtra && !breakExtra && !lpDx)
274 X11DRV_cptable[pfo->fi->cptable].pDrawString(
275 pfo, gdi_display, physDev->drawable, physDev->gc,
276 physDev->org.x + x, physDev->org.y + y, str2b, count );
278 else /* Now the fun begins... */
280 XTextItem16 *items, *pitem;
281 int delta;
283 /* allocate max items */
285 pitem = items = HeapAlloc( GetProcessHeap(), 0,
286 count * sizeof(XTextItem16) );
287 if(items == NULL) goto FAIL;
288 delta = i = 0;
289 if( lpDx ) /* explicit character widths */
291 long ve_we;
292 unsigned short err = 0;
294 ve_we = X11DRV_XWStoDS( physDev, 0x10000 );
296 while (i < count)
298 /* initialize text item with accumulated delta */
300 long sum;
301 long fSum;
302 sum = 0;
303 pitem->chars = str2b + i;
304 pitem->delta = delta;
305 pitem->nchars = 0;
306 pitem->font = None;
307 delta = 0;
309 /* add characters to the same XTextItem
310 * until new delta becomes non-zero */
314 sum += lpDx[i];
315 fSum = sum*ve_we+err;
316 delta = (short)HIWORD(fSum)
317 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
318 pfo, pitem->chars, pitem->nchars+1);
319 pitem->nchars++;
320 } while ((++i < count) && !delta);
321 pitem++;
322 err = LOWORD(fSum);
325 else /* charExtra or breakExtra */
327 while (i < count)
329 pitem->chars = str2b + i;
330 pitem->delta = delta;
331 pitem->nchars = 0;
332 pitem->font = None;
333 delta = 0;
337 delta += charExtra;
338 if (str2b[i].byte2 == (char)dfBreakChar)
339 delta += breakExtra;
340 pitem->nchars++;
341 } while ((++i < count) && !delta);
342 pitem++;
346 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
347 physDev->drawable, physDev->gc,
348 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
349 HeapFree( GetProcessHeap(), 0, items );
352 else /* rotated */
354 /* have to render character by character. */
355 double offset = 0.0;
356 int i;
358 for (i=0; i<count; i++)
360 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
361 - font->min_char_or_byte2;
362 int x_i = IROUND((double) (physDev->org.x + x) + offset *
363 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
364 int y_i = IROUND((double) (physDev->org.y + y) - offset *
365 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
367 X11DRV_cptable[pfo->fi->cptable].pDrawString(
368 pfo, gdi_display, physDev->drawable, physDev->gc,
369 x_i, y_i, &str2b[i], 1);
370 if (lpDx)
372 offset += X11DRV_XWStoDS(physDev, lpDx[i]);
374 else
376 offset += (double) (font->per_char ?
377 font->per_char[char_metric_offset].attributes:
378 font->min_bounds.attributes)
379 * pfo->lpX11Trans->pixelsize / 1000.0;
380 offset += charExtra;
381 if (str2b[i].byte2 == (char)dfBreakChar)
382 offset += breakExtra;
386 HeapFree( GetProcessHeap(), 0, str2b );
388 /* Draw underline and strike-out if needed */
390 wine_tsx11_lock();
391 if (lfUnderline)
393 long linePos, lineWidth;
395 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
396 linePos = descent - 1;
397 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
398 lineWidth = 0;
399 else if (lineWidth == 1) lineWidth = 0;
400 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
401 LineSolid, CapRound, JoinBevel );
402 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
403 physDev->org.x + x, physDev->org.y + y + linePos,
404 physDev->org.x + x + width, physDev->org.y + y + linePos );
406 if (lfStrikeOut)
408 long lineAscent, lineDescent;
409 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
410 lineAscent = ascent / 2;
411 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
412 lineDescent = -lineAscent * 2 / 3;
413 XSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
414 LineSolid, CapRound, JoinBevel );
415 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
416 physDev->org.x + x, physDev->org.y + y - lineAscent,
417 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
419 wine_tsx11_unlock();
421 if (flags & ETO_CLIPPED)
423 /* restore the device region */
424 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
425 DeleteObject( saved_region );
427 goto END;
429 FAIL:
430 HeapFree( GetProcessHeap(), 0, str2b );
431 result = FALSE;
433 END:
434 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
435 return result;
439 /***********************************************************************
440 * X11DRV_GetTextExtentPoint
442 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
443 LPSIZE size )
445 fontObject* pfo = XFONT_GetFontObject( physDev->font );
447 TRACE("%s %d\n", debugstr_wn(str,count), count);
448 if( pfo ) {
449 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
450 if (!p) return FALSE;
451 if( !pfo->lpX11Trans ) {
452 int dir, ascent, descent;
453 int info_width;
454 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
455 count, &dir, &ascent, &descent, &info_width );
457 size->cx = info_width;
458 size->cy = pfo->fs->ascent + pfo->fs->descent;
459 } else {
460 INT i;
461 float x = 0.0, y = 0.0;
462 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
463 for(i = 0; i < count; i++) {
464 x += pfo->fs->per_char ?
465 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
466 pfo->fs->min_bounds.attributes;
468 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
469 TRACE("x = %f y = %f\n", x, y);
470 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
471 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
473 size->cx *= pfo->rescale;
474 size->cy *= pfo->rescale;
475 HeapFree( GetProcessHeap(), 0, p );
476 return TRUE;
478 return FALSE;