2 * X11 graphics driver text functions
4 * Copyright 1993,1994 Alexandre Julliard
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 /***********************************************************************
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
)
40 if (!(str2b
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(XChar2b
) )))
43 if (codepage
!= 0) /* multibyte font */
49 /* allocate the worst case count * 2 bytes */
50 if (!(str
= HeapAlloc( GetProcessHeap(), 0, count
* 2 )))
52 HeapFree( GetProcessHeap(), 0, str2b
);
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
++)
66 str2b
[i
].byte2
= str
[i
];
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];
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;
98 /***********************************************************************
102 X11DRV_ExtTextOut( DC
*dc
, INT x
, INT y
, UINT flags
,
103 const RECT
*lprect
, LPCWSTR wstr
, UINT count
,
108 INT width
, ascent
, descent
, xwidth
, ywidth
;
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
;
118 if (!X11DRV_SetupGCForText( dc
)) return TRUE
;
120 pfo
= XFONT_GetFontObject( physDev
->font
);
123 if (pfo
->lf
.lfEscapement
&& pfo
->lpX11Trans
)
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
)
149 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* there's a rectangle */
151 if (!lprect
) /* not always */
154 if (flags
& ETO_CLIPPED
) /* Can't clip with no rectangle */
156 if (!X11DRV_GetTextExtentPoint( dc
, wstr
, count
, &sz
))
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
);
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
;
204 if (!X11DRV_GetTextExtentPoint( dc
, wstr
, count
, &sz
))
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
) )
218 if (dc
->w
.textAlign
& TA_UPDATECP
) {
219 dc
->w
.CursPosX
= XDPTOLP( dc
, x
+ xwidth
);
220 dc
->w
.CursPosY
= YDPTOLP( dc
, y
- ywidth
);
226 if (dc
->w
.textAlign
& TA_UPDATECP
) {
227 dc
->w
.CursPosX
= XDPTOLP( dc
, x
);
228 dc
->w
.CursPosY
= YDPTOLP( dc
, y
);
237 switch( dc
->w
.textAlign
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
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
;
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
;
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 */
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
) ||
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
,
289 dc
->w
.DCOrgY
+ y
- ascent
,
296 /* Draw the text (count > 0 verified) */
297 if (!(str2b
= unicode_to_char2b( wstr
, count
, pfo
->fi
->codepage
, pfo
->fs
->default_char
)))
300 TSXSetForeground( display
, physDev
->gc
, physDev
->textPixel
);
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
;
313 /* allocate max items */
315 pitem
= items
= HeapAlloc( GetProcessHeap(), 0,
316 count
* sizeof(XTextItem16
) );
317 if(items
== NULL
) goto FAIL
;
319 if( lpDx
) /* explicit character widths */
321 int extra
= dc
->wndExtX
/ 2;
325 /* initialize text item with accumulated delta */
327 pitem
->chars
= str2b
+ i
;
328 pitem
->delta
= delta
;
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);
341 } while ((++i
< count
) && !delta
);
345 else /* charExtra or breakExtra */
349 pitem
->chars
= str2b
+ i
;
350 pitem
->delta
= delta
;
357 delta
+= dc
->w
.charExtra
;
358 if (str2b
[i
].byte2
== (char)dfBreakChar
)
359 delta
+= dc
->w
.breakExtra
;
361 } while ((++i
< count
) && !delta
);
366 TSXDrawText16( display
, physDev
->drawable
, physDev
->gc
,
367 dc
->w
.DCOrgX
+ x
, dc
->w
.DCOrgY
+ y
, items
, pitem
- items
);
368 HeapFree( GetProcessHeap(), 0, items
);
373 /* have to render character by character. */
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);
389 offset
+= XLSTODS(dc
, lpDx
[i
]);
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 */
408 long linePos
, lineWidth
;
410 if (!TSXGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
411 linePos
= descent
- 1;
412 if (!TSXGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
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
);
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
);
441 if(str2b
!= NULL
) HeapFree( GetProcessHeap(), 0, str2b
);
445 if (dibUpdateFlag
) X11DRV_DIB_UpdateDIBSection( dc
, TRUE
);
450 /***********************************************************************
451 * X11DRV_GetTextExtentPoint
453 BOOL
X11DRV_GetTextExtentPoint( DC
*dc
, LPCWSTR str
, INT count
,
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
);
461 if( !pfo
->lpX11Trans
) {
462 int dir
, ascent
, descent
;
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
);
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
;