Assign codepage for every X11 font suffix, and use it for converting
[wine.git] / graphics / x11drv / text.c
blobb0c3212f094afc5ae93ab884a8ede201330703e4
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 "dc.h"
18 #include "gdi.h"
19 #include "heap.h"
20 #include "x11font.h"
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(text);
25 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
26 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
29 /***********************************************************************
30 * unicode_to_char2b
32 * dup a Unicode string into a XChar2b array; must be HeapFree'd by the caller
34 static XChar2b *unicode_to_char2b( LPCWSTR wstr, UINT count, UINT codepage )
36 XChar2b *str2b;
37 UINT i, total_size = count * (sizeof(XChar2b) + (codepage ? sizeof(WCHAR) : 0));
39 if (!(str2b = HeapAlloc( GetProcessHeap(), 0, total_size ))) return NULL;
41 if (codepage != 0) /* a one byte font */
43 BYTE *str = (BYTE *)(str2b + count);
45 /* we have to convert from unicode to codepage first */
46 WideCharToMultiByte( codepage, 0, wstr, count, str, count, NULL, NULL );
47 for (i = 0; i < count; i++)
49 str2b[i].byte1 = 0;
50 str2b[i].byte2 = str[i];
53 else /* codepage 0 -> two byte font */
55 for (i = 0; i < count; i++)
57 str2b[i].byte1 = wstr[i] >> 8;
58 str2b[i].byte2 = wstr[i] & 0xff;
61 return str2b;
65 /***********************************************************************
66 * X11DRV_ExtTextOut
68 BOOL
69 X11DRV_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
70 const RECT *lprect, LPCWSTR wstr, UINT count,
71 const INT *lpDx )
73 int i;
74 fontObject* pfo;
75 INT width, ascent, descent, xwidth, ywidth;
76 XFontStruct* font;
77 RECT rect;
78 char dfBreakChar, lfUnderline, lfStrikeOut;
79 BOOL rotated = FALSE;
80 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
81 XChar2b *str2b = NULL;
82 BOOL dibUpdateFlag = FALSE;
83 BOOL result = TRUE;
85 if (!X11DRV_SetupGCForText( dc )) return TRUE;
87 pfo = XFONT_GetFontObject( physDev->font );
88 font = pfo->fs;
90 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
91 rotated = TRUE;
92 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
93 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
94 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
96 TRACE("hdc=%04x df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
97 dc->hSelf, (UINT16)(physDev->font), x, y,
98 debugstr_wn (wstr, count), count, flags, lpDx);
100 /* some strings sent here end in a newline for whatever reason. I have no
101 clue what the right treatment should be in general, but ignoring
102 terminating newlines seems ok. MW, April 1998. */
103 if (count > 0 && wstr[count - 1] == '\n') count--;
105 if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
106 lprect->left, lprect->top,
107 lprect->right, lprect->bottom );
108 /* Setup coordinates */
110 if (dc->w.textAlign & TA_UPDATECP)
112 x = dc->w.CursPosX;
113 y = dc->w.CursPosY;
116 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
118 if (!lprect) /* not always */
120 SIZE sz;
121 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
122 return FALSE;
123 if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
124 return FALSE;
125 rect.left = XLPTODP( dc, x );
126 rect.right = XLPTODP( dc, x+sz.cx );
127 rect.top = YLPTODP( dc, y );
128 rect.bottom = YLPTODP( dc, y+sz.cy );
130 else
132 rect.left = XLPTODP( dc, lprect->left );
133 rect.right = XLPTODP( dc, lprect->right );
134 rect.top = YLPTODP( dc, lprect->top );
135 rect.bottom = YLPTODP( dc, lprect->bottom );
137 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
138 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
141 x = XLPTODP( dc, x );
142 y = YLPTODP( dc, y );
144 TRACE("\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
145 x, y, rect.left, rect.top, rect.right, rect.bottom);
147 /* Draw the rectangle */
149 if (flags & ETO_OPAQUE)
151 X11DRV_DIB_UpdateDIBSection( dc, FALSE );
152 dibUpdateFlag = TRUE;
153 TSXSetForeground( display, physDev->gc, physDev->backgroundPixel );
154 TSXFillRectangle( display, physDev->drawable, physDev->gc,
155 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
156 rect.right-rect.left, rect.bottom-rect.top );
158 if (!count) goto END; /* Nothing more to do */
160 /* Compute text starting position */
162 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
164 int extra = dc->wndExtX / 2;
165 for (i = width = 0; i < count; i++) width += lpDx[i];
166 width = (width * dc->vportExtX + extra ) / dc->wndExtX;
168 else
170 SIZE sz;
171 if (!X11DRV_GetTextExtentPoint( dc, wstr, count, &sz ))
172 return FALSE;
173 width = XLSTODS(dc, sz.cx);
175 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
176 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
177 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
178 pfo->lpX11Trans->pixelsize : width;
179 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
180 pfo->lpX11Trans->pixelsize : 0;
182 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
184 case TA_LEFT:
185 if (dc->w.textAlign & TA_UPDATECP) {
186 dc->w.CursPosX = XDPTOLP( dc, x + xwidth );
187 dc->w.CursPosY = YDPTOLP( dc, y - ywidth );
189 break;
190 case TA_RIGHT:
191 x -= xwidth;
192 y += ywidth;
193 if (dc->w.textAlign & TA_UPDATECP) {
194 dc->w.CursPosX = XDPTOLP( dc, x );
195 dc->w.CursPosY = YDPTOLP( dc, y );
197 break;
198 case TA_CENTER:
199 x -= xwidth / 2;
200 y += ywidth / 2;
201 break;
204 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
206 case TA_TOP:
207 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
208 pfo->lpX11Trans->pixelsize : 0;
209 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
210 pfo->lpX11Trans->pixelsize : ascent;
211 break;
212 case TA_BOTTOM:
213 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
214 pfo->lpX11Trans->pixelsize : 0;
215 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
216 pfo->lpX11Trans->pixelsize : descent;
217 break;
218 case TA_BASELINE:
219 break;
222 /* Set the clip region */
224 if (flags & ETO_CLIPPED)
226 SaveVisRgn16( dc->hSelf );
227 CLIPPING_IntersectVisRect( dc, rect.left, rect.top, rect.right,
228 rect.bottom, FALSE );
231 /* Draw the text background if necessary */
233 if (!dibUpdateFlag)
235 X11DRV_DIB_UpdateDIBSection( dc, FALSE );
236 dibUpdateFlag = TRUE;
239 if (dc->w.backgroundMode != TRANSPARENT)
241 /* If rectangle is opaque and clipped, do nothing */
242 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
244 /* Only draw if rectangle is not opaque or if some */
245 /* text is outside the rectangle */
246 if (!(flags & ETO_OPAQUE) ||
247 (x < rect.left) ||
248 (x + width >= rect.right) ||
249 (y - ascent < rect.top) ||
250 (y + descent >= rect.bottom))
252 TSXSetForeground( display, physDev->gc,
253 physDev->backgroundPixel );
254 TSXFillRectangle( display, physDev->drawable, physDev->gc,
255 dc->w.DCOrgX + x,
256 dc->w.DCOrgY + y - ascent,
257 width,
258 ascent + descent );
263 /* Draw the text (count > 0 verified) */
264 if (!(str2b = unicode_to_char2b( wstr, count, pfo->fi->codepage ))) goto FAIL;
266 TSXSetForeground( display, physDev->gc, physDev->textPixel );
267 if(!rotated)
269 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
271 TSXDrawString16( display, physDev->drawable, physDev->gc,
272 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str2b, count );
274 else /* Now the fun begins... */
276 XTextItem16 *items, *pitem;
277 int delta;
279 /* allocate max items */
281 pitem = items = HeapAlloc( GetProcessHeap(), 0,
282 count * sizeof(XTextItem16) );
283 if(items == NULL) goto FAIL;
284 delta = i = 0;
285 if( lpDx ) /* explicit character widths */
287 int extra = dc->wndExtX / 2;
289 while (i < count)
291 /* initialize text item with accumulated delta */
293 pitem->chars = str2b + i;
294 pitem->delta = delta;
295 pitem->nchars = 0;
296 pitem->font = None;
297 delta = 0;
299 /* add characters to the same XTextItem until new delta
300 * becomes non-zero */
304 delta += (lpDx[i] * dc->vportExtX + extra) / dc->wndExtX
305 - TSXTextWidth16( font, str2b + i, 1);
306 pitem->nchars++;
307 } while ((++i < count) && !delta);
308 pitem++;
311 else /* charExtra or breakExtra */
313 while (i < count)
315 pitem->chars = str2b + i;
316 pitem->delta = delta;
317 pitem->nchars = 0;
318 pitem->font = None;
319 delta = 0;
323 delta += dc->w.charExtra;
324 if (str2b[i].byte2 == (char)dfBreakChar)
325 delta += dc->w.breakExtra;
326 pitem->nchars++;
327 } while ((++i < count) && !delta);
328 pitem++;
332 TSXDrawText16( display, physDev->drawable, physDev->gc,
333 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, pitem - items );
334 HeapFree( GetProcessHeap(), 0, items );
337 else /* rotated */
339 /* have to render character by character. */
340 double offset = 0.0;
341 int i;
343 for (i=0; i<count; i++)
345 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
346 - font->min_char_or_byte2;
347 int x_i = IROUND((double) (dc->w.DCOrgX + x) + offset *
348 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
349 int y_i = IROUND((double) (dc->w.DCOrgY + y) - offset *
350 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
352 TSXDrawString16( display, physDev->drawable, physDev->gc,
353 x_i, y_i, &str2b[i], 1);
354 if (lpDx)
355 offset += XLSTODS(dc, lpDx[i]);
356 else
358 offset += (double) (font->per_char ?
359 font->per_char[char_metric_offset].attributes:
360 font->min_bounds.attributes)
361 * pfo->lpX11Trans->pixelsize / 1000.0;
362 offset += dc->w.charExtra;
363 if (str2b[i].byte2 == (char)dfBreakChar)
364 offset += dc->w.breakExtra;
368 HeapFree( GetProcessHeap(), 0, str2b );
370 /* Draw underline and strike-out if needed */
372 if (lfUnderline)
374 long linePos, lineWidth;
376 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
377 linePos = descent - 1;
378 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
379 lineWidth = 0;
380 else if (lineWidth == 1) lineWidth = 0;
381 TSXSetLineAttributes( display, physDev->gc, lineWidth,
382 LineSolid, CapRound, JoinBevel );
383 TSXDrawLine( display, physDev->drawable, physDev->gc,
384 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
385 dc->w.DCOrgX + x + width, dc->w.DCOrgY + y + linePos );
387 if (lfStrikeOut)
389 long lineAscent, lineDescent;
390 if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
391 lineAscent = ascent / 2;
392 if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
393 lineDescent = -lineAscent * 2 / 3;
394 TSXSetLineAttributes( display, physDev->gc, lineAscent + lineDescent,
395 LineSolid, CapRound, JoinBevel );
396 TSXDrawLine( display, physDev->drawable, physDev->gc,
397 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
398 dc->w.DCOrgX + x + width, dc->w.DCOrgY + y - lineAscent );
401 if (flags & ETO_CLIPPED)
402 RestoreVisRgn16( dc->hSelf );
404 goto END;
406 FAIL:
407 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
408 result = FALSE;
410 END:
411 if (dibUpdateFlag) X11DRV_DIB_UpdateDIBSection( dc, TRUE );
412 return result;
416 /***********************************************************************
417 * X11DRV_GetTextExtentPoint
419 BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
420 LPSIZE size )
422 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
423 fontObject* pfo = XFONT_GetFontObject( physDev->font );
425 TRACE("%s %d\n", debugstr_wn(str,count), count);
426 if( pfo ) {
427 if( !pfo->lpX11Trans ) {
428 int dir, ascent, descent;
429 XCharStruct info;
430 XChar2b *p = unicode_to_char2b( str, count, pfo->fi->codepage );
431 if (!p) return FALSE;
432 TSXTextExtents16( pfo->fs, p, count, &dir, &ascent, &descent, &info );
433 size->cx = abs((info.width + dc->w.breakRem + count *
434 dc->w.charExtra) * dc->wndExtX / dc->vportExtX);
435 size->cy = abs((pfo->fs->ascent + pfo->fs->descent) *
436 dc->wndExtY / dc->vportExtY);
437 HeapFree( GetProcessHeap(), 0, p );
438 } else {
439 INT i;
440 float x = 0.0, y = 0.0;
441 for(i = 0; i < count; i++) {
442 x += pfo->fs->per_char ?
443 pfo->fs->per_char[str[i] - pfo->fs->min_char_or_byte2].attributes :
444 pfo->fs->min_bounds.attributes;
446 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
447 TRACE("x = %f y = %f\n", x, y);
448 x *= pfo->lpX11Trans->pixelsize / 1000.0;
449 y *= pfo->lpX11Trans->pixelsize / 1000.0;
450 size->cx = fabs((x + dc->w.breakRem + count * dc->w.charExtra) *
451 dc->wndExtX / dc->vportExtX);
452 size->cy = fabs(y * dc->wndExtY / dc->vportExtY);
454 size->cx *= pfo->rescale;
455 size->cy *= pfo->rescale;
456 return TRUE;
458 return FALSE;