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
23 #include <X11/Xatom.h>
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(text
);
42 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
43 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
46 /***********************************************************************
50 X11DRV_ExtTextOut( X11DRV_PDEVICE
*physDev
, INT x
, INT y
, UINT flags
,
51 const RECT
*lprect
, LPCWSTR wstr
, UINT count
,
56 INT width
, ascent
, descent
, xwidth
, ywidth
;
59 char dfBreakChar
, lfUnderline
, lfStrikeOut
;
61 XChar2b
*str2b
= NULL
;
62 BOOL dibUpdateFlag
= FALSE
;
68 return X11DRV_XRender_ExtTextOut(physDev
, x
, y
, flags
, lprect
, wstr
, count
,
72 if (!X11DRV_SetupGCForText( physDev
)) return TRUE
;
74 pfo
= XFONT_GetFontObject( physDev
->font
);
77 if (pfo
->lf
.lfEscapement
&& pfo
->lpX11Trans
)
79 dfBreakChar
= (char)pfo
->fi
->df
.dfBreakChar
;
80 lfUnderline
= (pfo
->fo_flags
& FO_SYNTH_UNDERLINE
) ? 1 : 0;
81 lfStrikeOut
= (pfo
->fo_flags
& FO_SYNTH_STRIKEOUT
) ? 1 : 0;
83 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
84 dc
->hSelf
, (UINT16
)(physDev
->font
), x
, y
,
85 debugstr_wn (wstr
, count
), count
, flags
, lpDx
);
87 /* some strings sent here end in a newline for whatever reason. I have no
88 clue what the right treatment should be in general, but ignoring
89 terminating newlines seems ok. MW, April 1998. */
90 if (count
> 0 && wstr
[count
- 1] == '\n') count
--;
92 if (lprect
!= NULL
) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
93 lprect
->left
, lprect
->top
,
94 lprect
->right
, lprect
->bottom
);
95 /* Setup coordinates */
97 if (dc
->textAlign
& TA_UPDATECP
)
103 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* there's a rectangle */
105 if (!lprect
) /* not always */
108 if (flags
& ETO_CLIPPED
) /* Can't clip with no rectangle */
110 if (!X11DRV_GetTextExtentPoint( physDev
, wstr
, count
, &sz
))
113 rect
.right
= x
+ sz
.cx
;
115 rect
.bottom
= y
+ sz
.cy
;
121 LPtoDP(physDev
->hdc
, (POINT
*)&rect
, 2);
123 if (rect
.right
< rect
.left
) SWAP_INT( rect
.left
, rect
.right
);
124 if (rect
.bottom
< rect
.top
) SWAP_INT( rect
.top
, rect
.bottom
);
129 LPtoDP(physDev
->hdc
, &pt
, 1);
133 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
134 x
, y
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
136 /* Draw the rectangle */
138 if (flags
& ETO_OPAQUE
)
140 X11DRV_LockDIBSection( physDev
, DIB_Status_GdiMod
, FALSE
);
141 dibUpdateFlag
= TRUE
;
142 TSXSetForeground( gdi_display
, physDev
->gc
, physDev
->backgroundPixel
);
143 TSXFillRectangle( gdi_display
, physDev
->drawable
, physDev
->gc
,
144 physDev
->org
.x
+ rect
.left
, physDev
->org
.y
+ rect
.top
,
145 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
147 if (!count
) goto END
; /* Nothing more to do */
149 /* Compute text starting position */
151 if (lpDx
) /* have explicit character cell x offsets in logical coordinates */
153 for (i
= width
= 0; i
< count
; i
++) width
+= lpDx
[i
];
154 width
= INTERNAL_XWSTODS(dc
, width
);
159 if (!X11DRV_GetTextExtentPoint( physDev
, wstr
, count
, &sz
))
164 width
= INTERNAL_XWSTODS(dc
, sz
.cx
);
166 ascent
= pfo
->lpX11Trans
? pfo
->lpX11Trans
->ascent
: font
->ascent
;
167 descent
= pfo
->lpX11Trans
? pfo
->lpX11Trans
->descent
: font
->descent
;
168 xwidth
= pfo
->lpX11Trans
? width
* pfo
->lpX11Trans
->a
/
169 pfo
->lpX11Trans
->pixelsize
: width
;
170 ywidth
= pfo
->lpX11Trans
? width
* pfo
->lpX11Trans
->b
/
171 pfo
->lpX11Trans
->pixelsize
: 0;
173 switch( dc
->textAlign
& (TA_LEFT
| TA_RIGHT
| TA_CENTER
) )
176 if (dc
->textAlign
& TA_UPDATECP
) {
179 DPtoLP(physDev
->hdc
, &pt
, 1);
187 if (dc
->textAlign
& TA_UPDATECP
) {
190 DPtoLP(physDev
->hdc
, &pt
, 1);
201 switch( dc
->textAlign
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
204 x
-= pfo
->lpX11Trans
? ascent
* pfo
->lpX11Trans
->c
/
205 pfo
->lpX11Trans
->pixelsize
: 0;
206 y
+= pfo
->lpX11Trans
? ascent
* pfo
->lpX11Trans
->d
/
207 pfo
->lpX11Trans
->pixelsize
: ascent
;
210 x
+= pfo
->lpX11Trans
? descent
* pfo
->lpX11Trans
->c
/
211 pfo
->lpX11Trans
->pixelsize
: 0;
212 y
-= pfo
->lpX11Trans
? descent
* pfo
->lpX11Trans
->d
/
213 pfo
->lpX11Trans
->pixelsize
: descent
;
219 /* Set the clip region */
221 if (flags
& ETO_CLIPPED
)
223 SaveVisRgn16( HDC_16(dc
->hSelf
) );
224 IntersectVisRect16( HDC_16(dc
->hSelf
), lprect
->left
, lprect
->top
, lprect
->right
, lprect
->bottom
);
227 /* Draw the text background if necessary */
231 X11DRV_LockDIBSection( physDev
, DIB_Status_GdiMod
, FALSE
);
232 dibUpdateFlag
= TRUE
;
235 if (GetBkMode( physDev
->hdc
) != TRANSPARENT
)
237 /* If rectangle is opaque and clipped, do nothing */
238 if (!(flags
& ETO_CLIPPED
) || !(flags
& ETO_OPAQUE
))
240 /* Only draw if rectangle is not opaque or if some */
241 /* text is outside the rectangle */
242 if (!(flags
& ETO_OPAQUE
) ||
244 (x
+ width
>= rect
.right
) ||
245 (y
- ascent
< rect
.top
) ||
246 (y
+ descent
>= rect
.bottom
))
248 TSXSetForeground( gdi_display
, physDev
->gc
, physDev
->backgroundPixel
);
249 TSXFillRectangle( gdi_display
, physDev
->drawable
, physDev
->gc
,
250 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
- ascent
,
251 width
, ascent
+ descent
);
256 /* Draw the text (count > 0 verified) */
257 if (!(str2b
= X11DRV_cptable
[pfo
->fi
->cptable
].punicode_to_char2b( pfo
, wstr
, count
)))
260 TSXSetForeground( gdi_display
, physDev
->gc
, physDev
->textPixel
);
263 if (!dc
->charExtra
&& !dc
->breakExtra
&& !lpDx
)
265 X11DRV_cptable
[pfo
->fi
->cptable
].pDrawString(
266 pfo
, gdi_display
, physDev
->drawable
, physDev
->gc
,
267 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
, str2b
, count
);
269 else /* Now the fun begins... */
271 XTextItem16
*items
, *pitem
;
274 /* allocate max items */
276 pitem
= items
= HeapAlloc( GetProcessHeap(), 0,
277 count
* sizeof(XTextItem16
) );
278 if(items
== NULL
) goto FAIL
;
280 if( lpDx
) /* explicit character widths */
283 unsigned short err
= 0;
285 ve_we
= (LONG
)(dc
->xformWorld2Vport
.eM11
* 0x10000);
289 /* initialize text item with accumulated delta */
294 pitem
->chars
= str2b
+ i
;
295 pitem
->delta
= delta
;
300 /* add characters to the same XTextItem
301 * until new delta becomes non-zero */
306 fSum
= sum
*ve_we
+err
;
307 delta
= (short)HIWORD(fSum
)
308 - X11DRV_cptable
[pfo
->fi
->cptable
].pTextWidth(
309 pfo
, pitem
->chars
, pitem
->nchars
+1);
311 } while ((++i
< count
) && !delta
);
316 else /* charExtra or breakExtra */
320 pitem
->chars
= str2b
+ i
;
321 pitem
->delta
= delta
;
328 delta
+= dc
->charExtra
;
329 if (str2b
[i
].byte2
== (char)dfBreakChar
)
330 delta
+= dc
->breakExtra
;
332 } while ((++i
< count
) && !delta
);
337 X11DRV_cptable
[pfo
->fi
->cptable
].pDrawText( pfo
, gdi_display
,
338 physDev
->drawable
, physDev
->gc
,
339 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
, items
, pitem
- items
);
340 HeapFree( GetProcessHeap(), 0, items
);
345 /* have to render character by character. */
349 for (i
=0; i
<count
; i
++)
351 int char_metric_offset
= str2b
[i
].byte2
+ (str2b
[i
].byte1
<< 8)
352 - font
->min_char_or_byte2
;
353 int x_i
= IROUND((double) (physDev
->org
.x
+ x
) + offset
*
354 pfo
->lpX11Trans
->a
/ pfo
->lpX11Trans
->pixelsize
);
355 int y_i
= IROUND((double) (physDev
->org
.y
+ y
) - offset
*
356 pfo
->lpX11Trans
->b
/ pfo
->lpX11Trans
->pixelsize
);
358 X11DRV_cptable
[pfo
->fi
->cptable
].pDrawString(
359 pfo
, gdi_display
, physDev
->drawable
, physDev
->gc
,
360 x_i
, y_i
, &str2b
[i
], 1);
363 offset
+= INTERNAL_XWSTODS(dc
, lpDx
[i
]);
367 offset
+= (double) (font
->per_char
?
368 font
->per_char
[char_metric_offset
].attributes
:
369 font
->min_bounds
.attributes
)
370 * pfo
->lpX11Trans
->pixelsize
/ 1000.0;
371 offset
+= dc
->charExtra
;
372 if (str2b
[i
].byte2
== (char)dfBreakChar
)
373 offset
+= dc
->breakExtra
;
377 HeapFree( GetProcessHeap(), 0, str2b
);
379 /* Draw underline and strike-out if needed */
383 long linePos
, lineWidth
;
385 if (!TSXGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
386 linePos
= descent
- 1;
387 if (!TSXGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
389 else if (lineWidth
== 1) lineWidth
= 0;
390 TSXSetLineAttributes( gdi_display
, physDev
->gc
, lineWidth
,
391 LineSolid
, CapRound
, JoinBevel
);
392 TSXDrawLine( gdi_display
, physDev
->drawable
, physDev
->gc
,
393 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
+ linePos
,
394 physDev
->org
.x
+ x
+ width
, physDev
->org
.y
+ y
+ linePos
);
398 long lineAscent
, lineDescent
;
399 if (!TSXGetFontProperty( font
, XA_STRIKEOUT_ASCENT
, &lineAscent
))
400 lineAscent
= ascent
/ 2;
401 if (!TSXGetFontProperty( font
, XA_STRIKEOUT_DESCENT
, &lineDescent
))
402 lineDescent
= -lineAscent
* 2 / 3;
403 TSXSetLineAttributes( gdi_display
, physDev
->gc
, lineAscent
+ lineDescent
,
404 LineSolid
, CapRound
, JoinBevel
);
405 TSXDrawLine( gdi_display
, physDev
->drawable
, physDev
->gc
,
406 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
- lineAscent
,
407 physDev
->org
.x
+ x
+ width
, physDev
->org
.y
+ y
- lineAscent
);
410 if (flags
& ETO_CLIPPED
) RestoreVisRgn16( HDC_16(dc
->hSelf
) );
414 if(str2b
!= NULL
) HeapFree( GetProcessHeap(), 0, str2b
);
418 if (dibUpdateFlag
) X11DRV_UnlockDIBSection( physDev
, TRUE
);
423 /***********************************************************************
424 * X11DRV_GetTextExtentPoint
426 BOOL
X11DRV_GetTextExtentPoint( X11DRV_PDEVICE
*physDev
, LPCWSTR str
, INT count
,
429 DC
*dc
= physDev
->dc
;
430 fontObject
* pfo
= XFONT_GetFontObject( physDev
->font
);
432 TRACE("%s %d\n", debugstr_wn(str
,count
), count
);
434 XChar2b
*p
= X11DRV_cptable
[pfo
->fi
->cptable
].punicode_to_char2b( pfo
, str
, count
);
435 if (!p
) return FALSE
;
436 if( !pfo
->lpX11Trans
) {
437 int dir
, ascent
, descent
;
439 X11DRV_cptable
[pfo
->fi
->cptable
].pTextExtents( pfo
, p
,
440 count
, &dir
, &ascent
, &descent
, &info_width
);
442 size
->cx
= fabs((FLOAT
)(info_width
+ dc
->breakRem
+ count
*
443 dc
->charExtra
) * dc
->xformVport2World
.eM11
);
444 size
->cy
= fabs((FLOAT
)(pfo
->fs
->ascent
+ pfo
->fs
->descent
) *
445 dc
->xformVport2World
.eM22
);
448 float x
= 0.0, y
= 0.0;
449 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
450 for(i
= 0; i
< count
; i
++) {
451 x
+= pfo
->fs
->per_char
?
452 pfo
->fs
->per_char
[p
[i
].byte2
- pfo
->fs
->min_char_or_byte2
].attributes
:
453 pfo
->fs
->min_bounds
.attributes
;
455 y
= pfo
->lpX11Trans
->RAW_ASCENT
+ pfo
->lpX11Trans
->RAW_DESCENT
;
456 TRACE("x = %f y = %f\n", x
, y
);
457 x
*= pfo
->lpX11Trans
->pixelsize
/ 1000.0;
458 y
*= pfo
->lpX11Trans
->pixelsize
/ 1000.0;
459 size
->cx
= fabs((x
+ dc
->breakRem
+ count
* dc
->charExtra
) *
460 dc
->xformVport2World
.eM11
);
461 size
->cy
= fabs(y
* dc
->xformVport2World
.eM22
);
463 size
->cx
*= pfo
->rescale
;
464 size
->cy
*= pfo
->rescale
;
465 HeapFree( GetProcessHeap(), 0, p
);