Pass the gdiFont object to the SelectFont driver entry point so that
[wine.git] / dlls / x11drv / text.c
blob8832a513f02477a18df00eb520d05694dcf580c0
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 "wownt32.h"
31 #include "x11font.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(text);
36 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
37 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
40 /***********************************************************************
41 * X11DRV_ExtTextOut
43 BOOL
44 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
45 const RECT *lprect, LPCWSTR wstr, UINT count,
46 const INT *lpDx, INT breakExtra )
48 int i;
49 fontObject* pfo;
50 INT width, ascent, descent, xwidth, ywidth;
51 XFontStruct* font;
52 RECT rect;
53 char dfBreakChar, lfUnderline, lfStrikeOut;
54 BOOL rotated = FALSE;
55 XChar2b *str2b = NULL;
56 BOOL dibUpdateFlag = FALSE;
57 BOOL result = TRUE;
58 HRGN saved_region = 0;
59 POINT pt;
60 UINT align = GetTextAlign( physDev->hdc );
61 INT charExtra = GetTextCharacterExtra( physDev->hdc );
63 if(physDev->has_gdi_font)
64 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx, breakExtra);
66 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
68 pfo = XFONT_GetFontObject( physDev->font );
69 font = pfo->fs;
71 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
72 rotated = TRUE;
73 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
74 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
75 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
77 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
78 physDev->hdc, (UINT16)(physDev->font), x, y,
79 debugstr_wn (wstr, count), count, flags, lpDx);
81 /* some strings sent here end in a newline for whatever reason. I have no
82 clue what the right treatment should be in general, but ignoring
83 terminating newlines seems ok. MW, April 1998. */
84 if (count > 0 && wstr[count - 1] == '\n') count--;
86 if (lprect != NULL) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
87 lprect->left, lprect->top,
88 lprect->right, lprect->bottom );
89 /* Setup coordinates */
91 if (align & TA_UPDATECP)
93 GetCurrentPositionEx( physDev->hdc, &pt );
94 x = pt.x;
95 y = pt.y;
98 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
100 if (!lprect) /* not always */
102 SIZE sz;
103 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
104 return FALSE;
105 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
106 return FALSE;
107 rect.left = x;
108 rect.right = x + sz.cx;
109 rect.top = y;
110 rect.bottom = y + sz.cy;
112 else
114 rect = *lprect;
116 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
118 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
119 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
122 pt.x = x;
123 pt.y = y;
124 LPtoDP(physDev->hdc, &pt, 1);
125 x = pt.x;
126 y = pt.y;
128 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
129 x, y, rect.left, rect.top, rect.right, rect.bottom);
131 /* Draw the rectangle */
133 if (flags & ETO_OPAQUE)
135 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
136 dibUpdateFlag = TRUE;
137 wine_tsx11_lock();
138 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
139 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
140 physDev->org.x + rect.left, physDev->org.y + rect.top,
141 rect.right-rect.left, rect.bottom-rect.top );
142 wine_tsx11_unlock();
144 if (!count) goto END; /* Nothing more to do */
146 /* Compute text starting position */
148 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
150 for (i = width = 0; i < count; i++) width += lpDx[i];
151 width = X11DRV_XWStoDS(physDev, width);
153 else
155 SIZE sz;
156 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
158 result = FALSE;
159 goto END;
161 width = X11DRV_XWStoDS(physDev, sz.cx);
163 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
164 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
165 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
166 pfo->lpX11Trans->pixelsize : width;
167 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
168 pfo->lpX11Trans->pixelsize : 0;
170 switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) )
172 case TA_LEFT:
173 if (align & TA_UPDATECP) {
174 pt.x = x + xwidth;
175 pt.y = y - ywidth;
176 DPtoLP(physDev->hdc, &pt, 1);
177 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
179 break;
180 case TA_RIGHT:
181 x -= xwidth;
182 y += ywidth;
183 if (align & TA_UPDATECP) {
184 pt.x = x;
185 pt.y = y;
186 DPtoLP(physDev->hdc, &pt, 1);
187 MoveToEx(physDev->hdc, pt.x, pt.y, NULL);
189 break;
190 case TA_CENTER:
191 x -= xwidth / 2;
192 y += ywidth / 2;
193 break;
196 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
198 case TA_TOP:
199 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
200 pfo->lpX11Trans->pixelsize : 0;
201 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
202 pfo->lpX11Trans->pixelsize : ascent;
203 break;
204 case TA_BOTTOM:
205 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
206 pfo->lpX11Trans->pixelsize : 0;
207 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
208 pfo->lpX11Trans->pixelsize : descent;
209 break;
210 case TA_BASELINE:
211 break;
214 /* Set the clip region */
216 if (flags & ETO_CLIPPED)
218 HRGN clip_region;
219 RECT clip_rect = *lprect;
221 LPtoDP( physDev->hdc, (POINT *)&clip_rect, 2 );
222 clip_region = CreateRectRgnIndirect( &clip_rect );
223 /* make a copy of the current device region */
224 saved_region = CreateRectRgn( 0, 0, 0, 0 );
225 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
226 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
227 DeleteObject( clip_region );
230 /* Draw the text background if necessary */
232 if (!dibUpdateFlag)
234 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
235 dibUpdateFlag = TRUE;
238 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
240 /* If rectangle is opaque and clipped, do nothing */
241 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
243 /* Only draw if rectangle is not opaque or if some */
244 /* text is outside the rectangle */
245 if (!(flags & ETO_OPAQUE) ||
246 (x < rect.left) ||
247 (x + width >= rect.right) ||
248 (y - ascent < rect.top) ||
249 (y + descent >= rect.bottom))
251 wine_tsx11_lock();
252 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
253 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
254 physDev->org.x + x, physDev->org.y + y - ascent,
255 width, ascent + descent );
256 wine_tsx11_unlock();
261 /* Draw the text (count > 0 verified) */
262 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
263 goto FAIL;
265 wine_tsx11_lock();
266 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
267 wine_tsx11_unlock();
268 if(!rotated)
270 if (!charExtra && !breakExtra && !lpDx)
272 X11DRV_cptable[pfo->fi->cptable].pDrawString(
273 pfo, gdi_display, physDev->drawable, physDev->gc,
274 physDev->org.x + x, physDev->org.y + y, str2b, count );
276 else /* Now the fun begins... */
278 XTextItem16 *items, *pitem;
279 int delta;
281 /* allocate max items */
283 pitem = items = HeapAlloc( GetProcessHeap(), 0,
284 count * sizeof(XTextItem16) );
285 if(items == NULL) goto FAIL;
286 delta = i = 0;
287 if( lpDx ) /* explicit character widths */
289 long ve_we;
290 unsigned short err = 0;
292 ve_we = X11DRV_XWStoDS( physDev, 0x10000 );
294 while (i < count)
296 /* initialize text item with accumulated delta */
298 long sum;
299 long fSum;
300 sum = 0;
301 pitem->chars = str2b + i;
302 pitem->delta = delta;
303 pitem->nchars = 0;
304 pitem->font = None;
305 delta = 0;
307 /* add characters to the same XTextItem
308 * until new delta becomes non-zero */
312 sum += lpDx[i];
313 fSum = sum*ve_we+err;
314 delta = (short)HIWORD(fSum)
315 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
316 pfo, pitem->chars, pitem->nchars+1);
317 pitem->nchars++;
318 } while ((++i < count) && !delta);
319 pitem++;
320 err = LOWORD(fSum);
323 else /* charExtra or breakExtra */
325 while (i < count)
327 pitem->chars = str2b + i;
328 pitem->delta = delta;
329 pitem->nchars = 0;
330 pitem->font = None;
331 delta = 0;
335 delta += charExtra;
336 if (str2b[i].byte2 == (char)dfBreakChar)
337 delta += breakExtra;
338 pitem->nchars++;
339 } while ((++i < count) && !delta);
340 pitem++;
344 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
345 physDev->drawable, physDev->gc,
346 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
347 HeapFree( GetProcessHeap(), 0, items );
350 else /* rotated */
352 /* have to render character by character. */
353 double offset = 0.0;
354 int i;
356 for (i=0; i<count; i++)
358 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
359 - font->min_char_or_byte2;
360 int x_i = IROUND((double) (physDev->org.x + x) + offset *
361 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
362 int y_i = IROUND((double) (physDev->org.y + y) - offset *
363 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
365 X11DRV_cptable[pfo->fi->cptable].pDrawString(
366 pfo, gdi_display, physDev->drawable, physDev->gc,
367 x_i, y_i, &str2b[i], 1);
368 if (lpDx)
370 offset += X11DRV_XWStoDS(physDev, lpDx[i]);
372 else
374 offset += (double) (font->per_char ?
375 font->per_char[char_metric_offset].attributes:
376 font->min_bounds.attributes)
377 * pfo->lpX11Trans->pixelsize / 1000.0;
378 offset += charExtra;
379 if (str2b[i].byte2 == (char)dfBreakChar)
380 offset += breakExtra;
384 HeapFree( GetProcessHeap(), 0, str2b );
386 /* Draw underline and strike-out if needed */
388 wine_tsx11_lock();
389 if (lfUnderline)
391 long linePos, lineWidth;
393 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
394 linePos = descent - 1;
395 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
396 lineWidth = 0;
397 else if (lineWidth == 1) lineWidth = 0;
398 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
399 LineSolid, CapRound, JoinBevel );
400 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
401 physDev->org.x + x, physDev->org.y + y + linePos,
402 physDev->org.x + x + width, physDev->org.y + y + linePos );
404 if (lfStrikeOut)
406 long lineAscent, lineDescent;
407 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
408 lineAscent = ascent / 2;
409 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
410 lineDescent = -lineAscent * 2 / 3;
411 XSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
412 LineSolid, CapRound, JoinBevel );
413 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
414 physDev->org.x + x, physDev->org.y + y - lineAscent,
415 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
417 wine_tsx11_unlock();
419 if (flags & ETO_CLIPPED)
421 /* restore the device region */
422 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
423 DeleteObject( saved_region );
425 goto END;
427 FAIL:
428 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
429 result = FALSE;
431 END:
432 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
433 return result;
437 /***********************************************************************
438 * X11DRV_GetTextExtentPoint
440 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
441 LPSIZE size )
443 fontObject* pfo = XFONT_GetFontObject( physDev->font );
445 TRACE("%s %d\n", debugstr_wn(str,count), count);
446 if( pfo ) {
447 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
448 if (!p) return FALSE;
449 if( !pfo->lpX11Trans ) {
450 int dir, ascent, descent;
451 int info_width;
452 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
453 count, &dir, &ascent, &descent, &info_width );
455 size->cx = info_width;
456 size->cy = pfo->fs->ascent + pfo->fs->descent;
457 } else {
458 INT i;
459 float x = 0.0, y = 0.0;
460 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
461 for(i = 0; i < count; i++) {
462 x += pfo->fs->per_char ?
463 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
464 pfo->fs->min_bounds.attributes;
466 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
467 TRACE("x = %f y = %f\n", x, y);
468 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
469 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
471 size->cx *= pfo->rescale;
472 size->cy *= pfo->rescale;
473 HeapFree( GetProcessHeap(), 0, p );
474 return TRUE;
476 return FALSE;