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
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(text
);
37 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
38 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
41 /***********************************************************************
45 X11DRV_ExtTextOut( X11DRV_PDEVICE
*physDev
, INT x
, INT y
, UINT flags
,
46 const RECT
*lprect
, LPCWSTR wstr
, UINT count
,
47 const INT
*lpDx
, INT breakExtra
)
51 INT width
, ascent
, descent
, xwidth
, ywidth
;
54 char dfBreakChar
, lfUnderline
, lfStrikeOut
;
56 XChar2b
*str2b
= NULL
;
57 BOOL dibUpdateFlag
= FALSE
;
59 HRGN saved_region
= 0;
61 UINT align
= GetTextAlign( physDev
->hdc
);
62 INT charExtra
= GetTextCharacterExtra( physDev
->hdc
);
64 if(physDev
->dc
->gdiFont
)
65 return X11DRV_XRender_ExtTextOut(physDev
, x
, y
, flags
, lprect
, wstr
, count
, lpDx
, breakExtra
);
67 if (!X11DRV_SetupGCForText( physDev
)) return TRUE
;
69 pfo
= XFONT_GetFontObject( physDev
->font
);
72 if (pfo
->lf
.lfEscapement
&& pfo
->lpX11Trans
)
74 dfBreakChar
= (char)pfo
->fi
->df
.dfBreakChar
;
75 lfUnderline
= (pfo
->fo_flags
& FO_SYNTH_UNDERLINE
) ? 1 : 0;
76 lfStrikeOut
= (pfo
->fo_flags
& FO_SYNTH_STRIKEOUT
) ? 1 : 0;
78 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
79 physDev
->hdc
, (UINT16
)(physDev
->font
), x
, y
,
80 debugstr_wn (wstr
, count
), count
, flags
, lpDx
);
82 /* some strings sent here end in a newline for whatever reason. I have no
83 clue what the right treatment should be in general, but ignoring
84 terminating newlines seems ok. MW, April 1998. */
85 if (count
> 0 && wstr
[count
- 1] == '\n') count
--;
87 if (lprect
!= NULL
) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
88 lprect
->left
, lprect
->top
,
89 lprect
->right
, lprect
->bottom
);
90 /* Setup coordinates */
92 if (align
& TA_UPDATECP
)
94 GetCurrentPositionEx( physDev
->hdc
, &pt
);
99 if (flags
& (ETO_OPAQUE
| ETO_CLIPPED
)) /* there's a rectangle */
101 if (!lprect
) /* not always */
104 if (flags
& ETO_CLIPPED
) /* Can't clip with no rectangle */
106 if (!X11DRV_GetTextExtentPoint( physDev
, wstr
, count
, &sz
))
109 rect
.right
= x
+ sz
.cx
;
111 rect
.bottom
= y
+ sz
.cy
;
117 LPtoDP(physDev
->hdc
, (POINT
*)&rect
, 2);
119 if (rect
.right
< rect
.left
) SWAP_INT( rect
.left
, rect
.right
);
120 if (rect
.bottom
< rect
.top
) SWAP_INT( rect
.top
, rect
.bottom
);
125 LPtoDP(physDev
->hdc
, &pt
, 1);
129 TRACE("\treal coord: x=%i, y=%i, rect=(%ld,%ld - %ld,%ld)\n",
130 x
, y
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
132 /* Draw the rectangle */
134 if (flags
& ETO_OPAQUE
)
136 X11DRV_LockDIBSection( physDev
, DIB_Status_GdiMod
, FALSE
);
137 dibUpdateFlag
= TRUE
;
139 XSetForeground( gdi_display
, physDev
->gc
, physDev
->backgroundPixel
);
140 XFillRectangle( gdi_display
, physDev
->drawable
, physDev
->gc
,
141 physDev
->org
.x
+ rect
.left
, physDev
->org
.y
+ rect
.top
,
142 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
);
145 if (!count
) goto END
; /* Nothing more to do */
147 /* Compute text starting position */
149 if (lpDx
) /* have explicit character cell x offsets in logical coordinates */
151 for (i
= width
= 0; i
< count
; i
++) width
+= lpDx
[i
];
152 width
= X11DRV_XWStoDS(physDev
, width
);
157 if (!X11DRV_GetTextExtentPoint( physDev
, wstr
, count
, &sz
))
162 width
= X11DRV_XWStoDS(physDev
, sz
.cx
);
164 ascent
= pfo
->lpX11Trans
? pfo
->lpX11Trans
->ascent
: font
->ascent
;
165 descent
= pfo
->lpX11Trans
? pfo
->lpX11Trans
->descent
: font
->descent
;
166 xwidth
= pfo
->lpX11Trans
? width
* pfo
->lpX11Trans
->a
/
167 pfo
->lpX11Trans
->pixelsize
: width
;
168 ywidth
= pfo
->lpX11Trans
? width
* pfo
->lpX11Trans
->b
/
169 pfo
->lpX11Trans
->pixelsize
: 0;
171 switch( align
& (TA_LEFT
| TA_RIGHT
| TA_CENTER
) )
174 if (align
& TA_UPDATECP
) {
177 DPtoLP(physDev
->hdc
, &pt
, 1);
178 MoveToEx(physDev
->hdc
, pt
.x
, pt
.y
, NULL
);
184 if (align
& TA_UPDATECP
) {
187 DPtoLP(physDev
->hdc
, &pt
, 1);
188 MoveToEx(physDev
->hdc
, pt
.x
, pt
.y
, NULL
);
197 switch( align
& (TA_TOP
| TA_BOTTOM
| TA_BASELINE
) )
200 x
-= pfo
->lpX11Trans
? ascent
* pfo
->lpX11Trans
->c
/
201 pfo
->lpX11Trans
->pixelsize
: 0;
202 y
+= pfo
->lpX11Trans
? ascent
* pfo
->lpX11Trans
->d
/
203 pfo
->lpX11Trans
->pixelsize
: ascent
;
206 x
+= pfo
->lpX11Trans
? descent
* pfo
->lpX11Trans
->c
/
207 pfo
->lpX11Trans
->pixelsize
: 0;
208 y
-= pfo
->lpX11Trans
? descent
* pfo
->lpX11Trans
->d
/
209 pfo
->lpX11Trans
->pixelsize
: descent
;
215 /* Set the clip region */
217 if (flags
& ETO_CLIPPED
)
220 RECT clip_rect
= *lprect
;
222 LPtoDP( physDev
->hdc
, (POINT
*)&clip_rect
, 2 );
223 clip_region
= CreateRectRgnIndirect( &clip_rect
);
224 /* make a copy of the current device region */
225 saved_region
= CreateRectRgn( 0, 0, 0, 0 );
226 CombineRgn( saved_region
, physDev
->region
, 0, RGN_COPY
);
227 X11DRV_SetDeviceClipping( physDev
, saved_region
, clip_region
);
228 DeleteObject( clip_region
);
231 /* Draw the text background if necessary */
235 X11DRV_LockDIBSection( physDev
, DIB_Status_GdiMod
, FALSE
);
236 dibUpdateFlag
= TRUE
;
239 if (GetBkMode( physDev
->hdc
) != 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
) ||
248 (x
+ width
>= rect
.right
) ||
249 (y
- ascent
< rect
.top
) ||
250 (y
+ descent
>= rect
.bottom
))
253 XSetForeground( gdi_display
, physDev
->gc
, physDev
->backgroundPixel
);
254 XFillRectangle( gdi_display
, physDev
->drawable
, physDev
->gc
,
255 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
- ascent
,
256 width
, ascent
+ descent
);
262 /* Draw the text (count > 0 verified) */
263 if (!(str2b
= X11DRV_cptable
[pfo
->fi
->cptable
].punicode_to_char2b( pfo
, wstr
, count
)))
267 XSetForeground( gdi_display
, physDev
->gc
, physDev
->textPixel
);
271 if (!charExtra
&& !breakExtra
&& !lpDx
)
273 X11DRV_cptable
[pfo
->fi
->cptable
].pDrawString(
274 pfo
, gdi_display
, physDev
->drawable
, physDev
->gc
,
275 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
, str2b
, count
);
277 else /* Now the fun begins... */
279 XTextItem16
*items
, *pitem
;
282 /* allocate max items */
284 pitem
= items
= HeapAlloc( GetProcessHeap(), 0,
285 count
* sizeof(XTextItem16
) );
286 if(items
== NULL
) goto FAIL
;
288 if( lpDx
) /* explicit character widths */
291 unsigned short err
= 0;
293 ve_we
= (LONG
)(physDev
->dc
->xformWorld2Vport
.eM11
* 0x10000);
297 /* initialize text item with accumulated delta */
302 pitem
->chars
= str2b
+ i
;
303 pitem
->delta
= delta
;
308 /* add characters to the same XTextItem
309 * until new delta becomes non-zero */
314 fSum
= sum
*ve_we
+err
;
315 delta
= (short)HIWORD(fSum
)
316 - X11DRV_cptable
[pfo
->fi
->cptable
].pTextWidth(
317 pfo
, pitem
->chars
, pitem
->nchars
+1);
319 } while ((++i
< count
) && !delta
);
324 else /* charExtra or breakExtra */
328 pitem
->chars
= str2b
+ i
;
329 pitem
->delta
= delta
;
337 if (str2b
[i
].byte2
== (char)dfBreakChar
)
340 } while ((++i
< count
) && !delta
);
345 X11DRV_cptable
[pfo
->fi
->cptable
].pDrawText( pfo
, gdi_display
,
346 physDev
->drawable
, physDev
->gc
,
347 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
, items
, pitem
- items
);
348 HeapFree( GetProcessHeap(), 0, items
);
353 /* have to render character by character. */
357 for (i
=0; i
<count
; i
++)
359 int char_metric_offset
= str2b
[i
].byte2
+ (str2b
[i
].byte1
<< 8)
360 - font
->min_char_or_byte2
;
361 int x_i
= IROUND((double) (physDev
->org
.x
+ x
) + offset
*
362 pfo
->lpX11Trans
->a
/ pfo
->lpX11Trans
->pixelsize
);
363 int y_i
= IROUND((double) (physDev
->org
.y
+ y
) - offset
*
364 pfo
->lpX11Trans
->b
/ pfo
->lpX11Trans
->pixelsize
);
366 X11DRV_cptable
[pfo
->fi
->cptable
].pDrawString(
367 pfo
, gdi_display
, physDev
->drawable
, physDev
->gc
,
368 x_i
, y_i
, &str2b
[i
], 1);
371 offset
+= X11DRV_XWStoDS(physDev
, lpDx
[i
]);
375 offset
+= (double) (font
->per_char
?
376 font
->per_char
[char_metric_offset
].attributes
:
377 font
->min_bounds
.attributes
)
378 * pfo
->lpX11Trans
->pixelsize
/ 1000.0;
380 if (str2b
[i
].byte2
== (char)dfBreakChar
)
381 offset
+= breakExtra
;
385 HeapFree( GetProcessHeap(), 0, str2b
);
387 /* Draw underline and strike-out if needed */
392 long linePos
, lineWidth
;
394 if (!XGetFontProperty( font
, XA_UNDERLINE_POSITION
, &linePos
))
395 linePos
= descent
- 1;
396 if (!XGetFontProperty( font
, XA_UNDERLINE_THICKNESS
, &lineWidth
))
398 else if (lineWidth
== 1) lineWidth
= 0;
399 XSetLineAttributes( gdi_display
, physDev
->gc
, lineWidth
,
400 LineSolid
, CapRound
, JoinBevel
);
401 XDrawLine( gdi_display
, physDev
->drawable
, physDev
->gc
,
402 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
+ linePos
,
403 physDev
->org
.x
+ x
+ width
, physDev
->org
.y
+ y
+ linePos
);
407 long lineAscent
, lineDescent
;
408 if (!XGetFontProperty( font
, XA_STRIKEOUT_ASCENT
, &lineAscent
))
409 lineAscent
= ascent
/ 2;
410 if (!XGetFontProperty( font
, XA_STRIKEOUT_DESCENT
, &lineDescent
))
411 lineDescent
= -lineAscent
* 2 / 3;
412 XSetLineAttributes( gdi_display
, physDev
->gc
, lineAscent
+ lineDescent
,
413 LineSolid
, CapRound
, JoinBevel
);
414 XDrawLine( gdi_display
, physDev
->drawable
, physDev
->gc
,
415 physDev
->org
.x
+ x
, physDev
->org
.y
+ y
- lineAscent
,
416 physDev
->org
.x
+ x
+ width
, physDev
->org
.y
+ y
- lineAscent
);
420 if (flags
& ETO_CLIPPED
)
422 /* restore the device region */
423 X11DRV_SetDeviceClipping( physDev
, saved_region
, 0 );
424 DeleteObject( saved_region
);
429 if(str2b
!= NULL
) HeapFree( GetProcessHeap(), 0, str2b
);
433 if (dibUpdateFlag
) X11DRV_UnlockDIBSection( physDev
, TRUE
);
438 /***********************************************************************
439 * X11DRV_GetTextExtentPoint
441 BOOL
X11DRV_GetTextExtentPoint( X11DRV_PDEVICE
*physDev
, LPCWSTR str
, INT count
,
444 fontObject
* pfo
= XFONT_GetFontObject( physDev
->font
);
446 TRACE("%s %d\n", debugstr_wn(str
,count
), count
);
448 XChar2b
*p
= X11DRV_cptable
[pfo
->fi
->cptable
].punicode_to_char2b( pfo
, str
, count
);
449 if (!p
) return FALSE
;
450 if( !pfo
->lpX11Trans
) {
451 int dir
, ascent
, descent
;
453 X11DRV_cptable
[pfo
->fi
->cptable
].pTextExtents( pfo
, p
,
454 count
, &dir
, &ascent
, &descent
, &info_width
);
456 size
->cx
= info_width
;
457 size
->cy
= pfo
->fs
->ascent
+ pfo
->fs
->descent
;
460 float x
= 0.0, y
= 0.0;
461 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
462 for(i
= 0; i
< count
; i
++) {
463 x
+= pfo
->fs
->per_char
?
464 pfo
->fs
->per_char
[p
[i
].byte2
- pfo
->fs
->min_char_or_byte2
].attributes
:
465 pfo
->fs
->min_bounds
.attributes
;
467 y
= pfo
->lpX11Trans
->RAW_ASCENT
+ pfo
->lpX11Trans
->RAW_DESCENT
;
468 TRACE("x = %f y = %f\n", x
, y
);
469 size
->cx
= x
* pfo
->lpX11Trans
->pixelsize
/ 1000.0;
470 size
->cy
= y
* pfo
->lpX11Trans
->pixelsize
/ 1000.0;
472 size
->cx
*= pfo
->rescale
;
473 size
->cy
*= pfo
->rescale
;
474 HeapFree( GetProcessHeap(), 0, p
);