Do not assume that double byte font == unicode font.
[wine.git] / graphics / x11drv / text.c
blobf45488a554ef201acfd3e1ffac82c701f9121e4e
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 "dc.h"
19 #include "gdi.h"
20 #include "heap.h"
21 #include "x11font.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 * unicode_to_char2b
33 * dup a Unicode string into a XChar2b array; must be HeapFree'd by the caller
35 static XChar2b *unicode_to_char2b( LPCWSTR wstr, UINT count, UINT codepage, UINT def_char )
37 XChar2b *str2b;
38 UINT i;
40 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XChar2b) )))
41 return NULL;
43 if (codepage != 0) /* multibyte font */
45 BYTE *str;
46 char ch = def_char;
47 CPINFO cpinfo;
49 /* allocate the worst case count * 2 bytes */
50 if (!(str = HeapAlloc( GetProcessHeap(), 0, count * 2 )))
52 HeapFree( GetProcessHeap(), 0, str2b );
53 return NULL;
56 /* we have to convert from unicode to codepage first */
57 WideCharToMultiByte( codepage, 0, wstr, count, str, count, &ch, NULL );
59 GetCPInfo( codepage, &cpinfo );
61 if (cpinfo.MaxCharSize == 1)
63 for (i = 0; i < count; i++)
65 str2b[i].byte1 = 0;
66 str2b[i].byte2 = str[i];
69 else
71 XChar2b *str2b_dst = str2b;
72 for (i = 0; i < count; i++, str2b_dst++)
74 str2b_dst->byte2 = str[i];
75 if (IsDBCSLeadByteEx( codepage, str[i] ))
77 str2b_dst->byte1 = str[i + 1];
78 i++;
80 else
81 str2b_dst->byte1 = 0;
84 HeapFree( GetProcessHeap(), 0, str );
86 else /* codepage 0 -> unicode font */
88 for (i = 0; i < count; i++)
90 str2b[i].byte1 = wstr[i] >> 8;
91 str2b[i].byte2 = wstr[i] & 0xff;
94 return str2b;
98 /***********************************************************************
99 * X11DRV_ExtTextOut
101 BOOL
102 X11DRV_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
103 const RECT *lprect, LPCWSTR wstr, UINT count,
104 const INT *lpDx )
106 int i;
107 fontObject* pfo;
108 INT width, ascent, descent, xwidth, ywidth;
109 XFontStruct* font;
110 RECT rect;
111 char dfBreakChar, lfUnderline, lfStrikeOut;
112 BOOL rotated = FALSE;
113 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
114 XChar2b *str2b = NULL;
115 BOOL dibUpdateFlag = FALSE;
116 BOOL result = TRUE;
118 if (!X11DRV_SetupGCForText( dc )) return TRUE;
120 pfo = XFONT_GetFontObject( physDev->font );
121 font = pfo->fs;
123 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
124 rotated = TRUE;
125 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
126 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
127 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
129 TRACE("hdc=%04x df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
130 dc->hSelf, (UINT16)(physDev->font), x, y,
131 debugstr_wn (wstr, count), count, flags, lpDx);
133 /* some strings sent here end in a newline for whatever reason. I have no
134 clue what the right treatment should be in general, but ignoring
135 terminating newlines seems ok. MW, April 1998. */
136 if (count > 0 && wstr[count - 1] == '\n') count--;
138 if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
139 lprect->left, lprect->top,
140 lprect->right, lprect->bottom );
141 /* Setup coordinates */
143 if (dc->w.textAlign & TA_UPDATECP)
145 x = dc->w.CursPosX;
146 y = dc->w.CursPosY;
149 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
151 if (!lprect) /* not always */
153 SIZE sz;
154 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
155 return FALSE;
156 if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
157 return FALSE;
158 rect.left = XLPTODP( dc, x );
159 rect.right = XLPTODP( dc, x+sz.cx );
160 rect.top = YLPTODP( dc, y );
161 rect.bottom = YLPTODP( dc, y+sz.cy );
163 else
165 rect.left = XLPTODP( dc, lprect->left );
166 rect.right = XLPTODP( dc, lprect->right );
167 rect.top = YLPTODP( dc, lprect->top );
168 rect.bottom = YLPTODP( dc, lprect->bottom );
170 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
171 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
174 x = XLPTODP( dc, x );
175 y = YLPTODP( dc, y );
177 TRACE("\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
178 x, y, rect.left, rect.top, rect.right, rect.bottom);
180 /* Draw the rectangle */
182 if (flags & ETO_OPAQUE)
184 X11DRV_DIB_UpdateDIBSection( dc, FALSE );
185 dibUpdateFlag = TRUE;
186 TSXSetForeground( display, physDev->gc, physDev->backgroundPixel );
187 TSXFillRectangle( display, physDev->drawable, physDev->gc,
188 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
189 rect.right-rect.left, rect.bottom-rect.top );
191 if (!count) goto END; /* Nothing more to do */
193 /* Compute text starting position */
195 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
197 int extra = dc->wndExtX / 2;
198 for (i = width = 0; i < count; i++) width += lpDx[i];
199 width = (width * dc->vportExtX + extra ) / dc->wndExtX;
201 else
203 SIZE sz;
204 if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
205 return FALSE;
206 width = XLSTODS(dc, sz.cx);
208 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
209 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
210 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
211 pfo->lpX11Trans->pixelsize : width;
212 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
213 pfo->lpX11Trans->pixelsize : 0;
215 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
217 case TA_LEFT:
218 if (dc->w.textAlign & TA_UPDATECP) {
219 dc->w.CursPosX = XDPTOLP( dc, x + xwidth );
220 dc->w.CursPosY = YDPTOLP( dc, y - ywidth );
222 break;
223 case TA_RIGHT:
224 x -= xwidth;
225 y += ywidth;
226 if (dc->w.textAlign & TA_UPDATECP) {
227 dc->w.CursPosX = XDPTOLP( dc, x );
228 dc->w.CursPosY = YDPTOLP( dc, y );
230 break;
231 case TA_CENTER:
232 x -= xwidth / 2;
233 y += ywidth / 2;
234 break;
237 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
239 case TA_TOP:
240 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
241 pfo->lpX11Trans->pixelsize : 0;
242 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
243 pfo->lpX11Trans->pixelsize : ascent;
244 break;
245 case TA_BOTTOM:
246 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
247 pfo->lpX11Trans->pixelsize : 0;
248 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
249 pfo->lpX11Trans->pixelsize : descent;
250 break;
251 case TA_BASELINE:
252 break;
255 /* Set the clip region */
257 if (flags & ETO_CLIPPED)
259 SaveVisRgn16( dc->hSelf );
260 CLIPPING_IntersectVisRect( dc, rect.left, rect.top, rect.right,
261 rect.bottom, FALSE );
264 /* Draw the text background if necessary */
266 if (!dibUpdateFlag)
268 X11DRV_DIB_UpdateDIBSection( dc, FALSE );
269 dibUpdateFlag = TRUE;
272 if (dc->w.backgroundMode != TRANSPARENT)
274 /* If rectangle is opaque and clipped, do nothing */
275 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
277 /* Only draw if rectangle is not opaque or if some */
278 /* text is outside the rectangle */
279 if (!(flags & ETO_OPAQUE) ||
280 (x < rect.left) ||
281 (x + width >= rect.right) ||
282 (y - ascent < rect.top) ||
283 (y + descent >= rect.bottom))
285 TSXSetForeground( display, physDev->gc,
286 physDev->backgroundPixel );
287 TSXFillRectangle( display, physDev->drawable, physDev->gc,
288 dc->w.DCOrgX + x,
289 dc->w.DCOrgY + y - ascent,
290 width,
291 ascent + descent );
296 /* Draw the text (count > 0 verified) */
297 if (!(str2b = unicode_to_char2b( wstr, count, pfo->fi->codepage, pfo->fs->default_char )))
298 goto FAIL;
300 TSXSetForeground( display, physDev->gc, physDev->textPixel );
301 if(!rotated)
303 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
305 TSXDrawString16( display, physDev->drawable, physDev->gc,
306 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str2b, count );
308 else /* Now the fun begins... */
310 XTextItem16 *items, *pitem;
311 int delta;
313 /* allocate max items */
315 pitem = items = HeapAlloc( GetProcessHeap(), 0,
316 count * sizeof(XTextItem16) );
317 if(items == NULL) goto FAIL;
318 delta = i = 0;
319 if( lpDx ) /* explicit character widths */
321 int extra = dc->wndExtX / 2;
323 while (i < count)
325 /* initialize text item with accumulated delta */
327 pitem->chars = str2b + i;
328 pitem->delta = delta;
329 pitem->nchars = 0;
330 pitem->font = None;
331 delta = 0;
333 /* add characters to the same XTextItem until new delta
334 * becomes non-zero */
338 delta += (lpDx[i] * dc->vportExtX + extra) / dc->wndExtX
339 - TSXTextWidth16( font, str2b + i, 1);
340 pitem->nchars++;
341 } while ((++i < count) && !delta);
342 pitem++;
345 else /* charExtra or breakExtra */
347 while (i < count)
349 pitem->chars = str2b + i;
350 pitem->delta = delta;
351 pitem->nchars = 0;
352 pitem->font = None;
353 delta = 0;
357 delta += dc->w.charExtra;
358 if (str2b[i].byte2 == (char)dfBreakChar)
359 delta += dc->w.breakExtra;
360 pitem->nchars++;
361 } while ((++i < count) && !delta);
362 pitem++;
366 TSXDrawText16( display, physDev->drawable, physDev->gc,
367 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
368 HeapFree( GetProcessHeap(), 0, items );
371 else /* rotated */
373 /* have to render character by character. */
374 double offset = 0.0;
375 int i;
377 for (i=0; i<count; i++)
379 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
380 - font->min_char_or_byte2;
381 int x_i = IROUND((double) (dc->w.DCOrgX + x) + offset *
382 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
383 int y_i = IROUND((double) (dc->w.DCOrgY + y) - offset *
384 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
386 TSXDrawString16( display, physDev->drawable, physDev->gc,
387 x_i, y_i, &str2b[i], 1);
388 if (lpDx)
389 offset += XLSTODS(dc, lpDx[i]);
390 else
392 offset += (double) (font->per_char ?
393 font->per_char[char_metric_offset].attributes:
394 font->min_bounds.attributes)
395 * pfo->lpX11Trans->pixelsize / 1000.0;
396 offset += dc->w.charExtra;
397 if (str2b[i].byte2 == (char)dfBreakChar)
398 offset += dc->w.breakExtra;
402 HeapFree( GetProcessHeap(), 0, str2b );
404 /* Draw underline and strike-out if needed */
406 if (lfUnderline)
408 long linePos, lineWidth;
410 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
411 linePos = descent - 1;
412 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
413 lineWidth = 0;
414 else if (lineWidth == 1) lineWidth = 0;
415 TSXSetLineAttributes( display, physDev->gc, lineWidth,
416 LineSolid, CapRound, JoinBevel );
417 TSXDrawLine( display, physDev->drawable, physDev->gc,
418 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
419 dc->w.DCOrgX + x + width, dc->w.DCOrgY + y + linePos );
421 if (lfStrikeOut)
423 long lineAscent, lineDescent;
424 if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
425 lineAscent = ascent / 2;
426 if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
427 lineDescent = -lineAscent * 2 / 3;
428 TSXSetLineAttributes( display, physDev->gc, lineAscent + lineDescent,
429 LineSolid, CapRound, JoinBevel );
430 TSXDrawLine( display, physDev->drawable, physDev->gc,
431 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
432 dc->w.DCOrgX + x + width, dc->w.DCOrgY + y - lineAscent );
435 if (flags & ETO_CLIPPED)
436 RestoreVisRgn16( dc->hSelf );
438 goto END;
440 FAIL:
441 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
442 result = FALSE;
444 END:
445 if (dibUpdateFlag) X11DRV_DIB_UpdateDIBSection( dc, TRUE );
446 return result;
450 /***********************************************************************
451 * X11DRV_GetTextExtentPoint
453 BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
454 LPSIZE size )
456 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
457 fontObject* pfo = XFONT_GetFontObject( physDev->font );
459 TRACE("%s %d\n", debugstr_wn(str,count), count);
460 if( pfo ) {
461 if( !pfo->lpX11Trans ) {
462 int dir, ascent, descent;
463 XCharStruct info;
464 XChar2b *p = unicode_to_char2b( str, count, pfo->fi->codepage, pfo->fs->default_char );
465 if (!p) return FALSE;
466 TSXTextExtents16( pfo->fs, p, count, &dir, &ascent, &descent, &info );
467 size->cx = abs((info.width + dc->w.breakRem + count *
468 dc->w.charExtra) * dc->wndExtX / dc->vportExtX);
469 size->cy = abs((pfo->fs->ascent + pfo->fs->descent) *
470 dc->wndExtY / dc->vportExtY);
471 HeapFree( GetProcessHeap(), 0, p );
472 } else {
473 INT i;
474 float x = 0.0, y = 0.0;
475 for(i = 0; i < count; i++) {
476 x += pfo->fs->per_char ?
477 pfo->fs->per_char[str[i] - pfo->fs->min_char_or_byte2].attributes :
478 pfo->fs->min_bounds.attributes;
480 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
481 TRACE("x = %f y = %f\n", x, y);
482 x *= pfo->lpX11Trans->pixelsize / 1000.0;
483 y *= pfo->lpX11Trans->pixelsize / 1000.0;
484 size->cx = fabs((x + dc->w.breakRem + count * dc->w.charExtra) *
485 dc->wndExtX / dc->vportExtX);
486 size->cy = fabs(y * dc->wndExtY / dc->vportExtY);
488 size->cx *= pfo->rescale;
489 size->cy *= pfo->rescale;
490 return TRUE;
492 return FALSE;