Move caret when selecting a text area with the mouse.
[wine/multimedia.git] / graphics / x11drv / text.c
blob48c2e4d0de94f8cbb314196a289abd0b7fe4feeb
1 /*
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
21 #include "config.h"
23 #include <X11/Xatom.h>
25 #include "ts_xlib.h"
27 #include <stdlib.h>
28 #include <math.h>
30 #include "windef.h"
31 #include "winnls.h"
32 #include "gdi.h"
33 #include "x11font.h"
34 #include "bitmap.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(text);
39 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
40 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
43 /***********************************************************************
44 * X11DRV_ExtTextOut
46 BOOL
47 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
48 const RECT *lprect, LPCWSTR wstr, UINT count,
49 const INT *lpDx )
51 int i;
52 fontObject* pfo;
53 INT width, ascent, descent, xwidth, ywidth;
54 XFontStruct* font;
55 RECT rect;
56 char dfBreakChar, lfUnderline, lfStrikeOut;
57 BOOL rotated = FALSE;
58 XChar2b *str2b = NULL;
59 BOOL dibUpdateFlag = FALSE;
60 BOOL result = TRUE;
61 POINT pt;
62 DC *dc = physDev->dc;
64 if(dc->gdiFont)
65 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count,
66 lpDx);
69 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
71 pfo = XFONT_GetFontObject( physDev->font );
72 font = pfo->fs;
74 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
75 rotated = TRUE;
76 dfBreakChar = (char)pfo->fi->df.dfBreakChar;
77 lfUnderline = (pfo->fo_flags & FO_SYNTH_UNDERLINE) ? 1 : 0;
78 lfStrikeOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT) ? 1 : 0;
80 TRACE("hdc=%04x df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
81 dc->hSelf, (UINT16)(physDev->font), x, y,
82 debugstr_wn (wstr, count), count, flags, lpDx);
84 /* some strings sent here end in a newline for whatever reason. I have no
85 clue what the right treatment should be in general, but ignoring
86 terminating newlines seems ok. MW, April 1998. */
87 if (count > 0 && wstr[count - 1] == '\n') count--;
89 if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
90 lprect->left, lprect->top,
91 lprect->right, lprect->bottom );
92 /* Setup coordinates */
94 if (dc->textAlign & TA_UPDATECP)
96 x = dc->CursPosX;
97 y = dc->CursPosY;
100 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* there's a rectangle */
102 if (!lprect) /* not always */
104 SIZE sz;
105 if (flags & ETO_CLIPPED) /* Can't clip with no rectangle */
106 return FALSE;
107 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
108 return FALSE;
109 rect.left = x;
110 rect.right = x + sz.cx;
111 rect.top = y;
112 rect.bottom = y + sz.cy;
114 else
116 rect = *lprect;
118 LPtoDP(physDev->hdc, (POINT*)&rect, 2);
120 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
121 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
124 pt.x = x;
125 pt.y = y;
126 LPtoDP(physDev->hdc, &pt, 1);
127 x = pt.x;
128 y = pt.y;
130 TRACE("\treal coord: x=%i, y=%i, rect=(%d,%d - %d,%d)\n",
131 x, y, rect.left, rect.top, rect.right, rect.bottom);
133 /* Draw the rectangle */
135 if (flags & ETO_OPAQUE)
137 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
138 dibUpdateFlag = TRUE;
139 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
140 TSXFillRectangle( 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 );
144 if (!count) goto END; /* Nothing more to do */
146 /* Compute text starting position */
148 if (lpDx) /* have explicit character cell x offsets in logical coordinates */
150 for (i = width = 0; i < count; i++) width += lpDx[i];
151 width = INTERNAL_XWSTODS(dc, width);
153 else
155 SIZE sz;
156 if (!X11DRV_GetTextExtentPoint( physDev, wstr, count, &sz ))
158 result = FALSE;
159 goto END;
161 width = INTERNAL_XWSTODS(dc, sz.cx);
163 ascent = pfo->lpX11Trans ? pfo->lpX11Trans->ascent : font->ascent;
164 descent = pfo->lpX11Trans ? pfo->lpX11Trans->descent : font->descent;
165 xwidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->a /
166 pfo->lpX11Trans->pixelsize : width;
167 ywidth = pfo->lpX11Trans ? width * pfo->lpX11Trans->b /
168 pfo->lpX11Trans->pixelsize : 0;
170 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
172 case TA_LEFT:
173 if (dc->textAlign & TA_UPDATECP) {
174 pt.x = x + xwidth;
175 pt.y = y - ywidth;
176 DPtoLP(physDev->hdc, &pt, 1);
177 dc->CursPosX = pt.x;
178 dc->CursPosY = pt.y;
180 break;
181 case TA_RIGHT:
182 x -= xwidth;
183 y += ywidth;
184 if (dc->textAlign & TA_UPDATECP) {
185 pt.x = x;
186 pt.y = y;
187 DPtoLP(physDev->hdc, &pt, 1);
188 dc->CursPosX = pt.x;
189 dc->CursPosY = pt.y;
191 break;
192 case TA_CENTER:
193 x -= xwidth / 2;
194 y += ywidth / 2;
195 break;
198 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
200 case TA_TOP:
201 x -= pfo->lpX11Trans ? ascent * pfo->lpX11Trans->c /
202 pfo->lpX11Trans->pixelsize : 0;
203 y += pfo->lpX11Trans ? ascent * pfo->lpX11Trans->d /
204 pfo->lpX11Trans->pixelsize : ascent;
205 break;
206 case TA_BOTTOM:
207 x += pfo->lpX11Trans ? descent * pfo->lpX11Trans->c /
208 pfo->lpX11Trans->pixelsize : 0;
209 y -= pfo->lpX11Trans ? descent * pfo->lpX11Trans->d /
210 pfo->lpX11Trans->pixelsize : descent;
211 break;
212 case TA_BASELINE:
213 break;
216 /* Set the clip region */
218 if (flags & ETO_CLIPPED)
220 SaveVisRgn16( dc->hSelf );
221 IntersectVisRect16( dc->hSelf, lprect->left, lprect->top, lprect->right, lprect->bottom );
224 /* Draw the text background if necessary */
226 if (!dibUpdateFlag)
228 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
229 dibUpdateFlag = TRUE;
232 if (GetBkMode( physDev->hdc ) != TRANSPARENT)
234 /* If rectangle is opaque and clipped, do nothing */
235 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
237 /* Only draw if rectangle is not opaque or if some */
238 /* text is outside the rectangle */
239 if (!(flags & ETO_OPAQUE) ||
240 (x < rect.left) ||
241 (x + width >= rect.right) ||
242 (y - ascent < rect.top) ||
243 (y + descent >= rect.bottom))
245 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
246 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
247 physDev->org.x + x, physDev->org.y + y - ascent,
248 width, ascent + descent );
253 /* Draw the text (count > 0 verified) */
254 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
255 goto FAIL;
257 TSXSetForeground( gdi_display, physDev->gc, physDev->textPixel );
258 if(!rotated)
260 if (!dc->charExtra && !dc->breakExtra && !lpDx)
262 X11DRV_cptable[pfo->fi->cptable].pDrawString(
263 pfo, gdi_display, physDev->drawable, physDev->gc,
264 physDev->org.x + x, physDev->org.y + y, str2b, count );
266 else /* Now the fun begins... */
268 XTextItem16 *items, *pitem;
269 int delta;
271 /* allocate max items */
273 pitem = items = HeapAlloc( GetProcessHeap(), 0,
274 count * sizeof(XTextItem16) );
275 if(items == NULL) goto FAIL;
276 delta = i = 0;
277 if( lpDx ) /* explicit character widths */
279 long ve_we;
280 unsigned short err = 0;
282 ve_we = (LONG)(dc->xformWorld2Vport.eM11 * 0x10000);
284 while (i < count)
286 /* initialize text item with accumulated delta */
288 long sum;
289 long fSum;
290 sum = 0;
291 pitem->chars = str2b + i;
292 pitem->delta = delta;
293 pitem->nchars = 0;
294 pitem->font = None;
295 delta = 0;
297 /* add characters to the same XTextItem
298 * until new delta becomes non-zero */
302 sum += lpDx[i];
303 fSum = sum*ve_we+err;
304 delta = SHIWORD(fSum)
305 - X11DRV_cptable[pfo->fi->cptable].pTextWidth(
306 pfo, pitem->chars, pitem->nchars+1);
307 pitem->nchars++;
308 } while ((++i < count) && !delta);
309 pitem++;
310 err = LOWORD(fSum);
313 else /* charExtra or breakExtra */
315 while (i < count)
317 pitem->chars = str2b + i;
318 pitem->delta = delta;
319 pitem->nchars = 0;
320 pitem->font = None;
321 delta = 0;
325 delta += dc->charExtra;
326 if (str2b[i].byte2 == (char)dfBreakChar)
327 delta += dc->breakExtra;
328 pitem->nchars++;
329 } while ((++i < count) && !delta);
330 pitem++;
334 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
335 physDev->drawable, physDev->gc,
336 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
337 HeapFree( GetProcessHeap(), 0, items );
340 else /* rotated */
342 /* have to render character by character. */
343 double offset = 0.0;
344 int i;
346 for (i=0; i<count; i++)
348 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
349 - font->min_char_or_byte2;
350 int x_i = IROUND((double) (physDev->org.x + x) + offset *
351 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
352 int y_i = IROUND((double) (physDev->org.y + y) - offset *
353 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
355 X11DRV_cptable[pfo->fi->cptable].pDrawString(
356 pfo, gdi_display, physDev->drawable, physDev->gc,
357 x_i, y_i, &str2b[i], 1);
358 if (lpDx)
360 offset += INTERNAL_XWSTODS(dc, lpDx[i]);
362 else
364 offset += (double) (font->per_char ?
365 font->per_char[char_metric_offset].attributes:
366 font->min_bounds.attributes)
367 * pfo->lpX11Trans->pixelsize / 1000.0;
368 offset += dc->charExtra;
369 if (str2b[i].byte2 == (char)dfBreakChar)
370 offset += dc->breakExtra;
374 HeapFree( GetProcessHeap(), 0, str2b );
376 /* Draw underline and strike-out if needed */
378 if (lfUnderline)
380 long linePos, lineWidth;
382 if (!TSXGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
383 linePos = descent - 1;
384 if (!TSXGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
385 lineWidth = 0;
386 else if (lineWidth == 1) lineWidth = 0;
387 TSXSetLineAttributes( gdi_display, physDev->gc, lineWidth,
388 LineSolid, CapRound, JoinBevel );
389 TSXDrawLine( gdi_display, physDev->drawable, physDev->gc,
390 physDev->org.x + x, physDev->org.y + y + linePos,
391 physDev->org.x + x + width, physDev->org.y + y + linePos );
393 if (lfStrikeOut)
395 long lineAscent, lineDescent;
396 if (!TSXGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
397 lineAscent = ascent / 2;
398 if (!TSXGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
399 lineDescent = -lineAscent * 2 / 3;
400 TSXSetLineAttributes( gdi_display, physDev->gc, lineAscent + lineDescent,
401 LineSolid, CapRound, JoinBevel );
402 TSXDrawLine( gdi_display, physDev->drawable, physDev->gc,
403 physDev->org.x + x, physDev->org.y + y - lineAscent,
404 physDev->org.x + x + width, physDev->org.y + y - lineAscent );
407 if (flags & ETO_CLIPPED) RestoreVisRgn16( dc->hSelf );
408 goto END;
410 FAIL:
411 if(str2b != NULL) HeapFree( GetProcessHeap(), 0, str2b );
412 result = FALSE;
414 END:
415 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
416 return result;
420 /***********************************************************************
421 * X11DRV_GetTextExtentPoint
423 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
424 LPSIZE size )
426 DC *dc = physDev->dc;
427 fontObject* pfo = XFONT_GetFontObject( physDev->font );
429 TRACE("%s %d\n", debugstr_wn(str,count), count);
430 if( pfo ) {
431 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
432 if (!p) return FALSE;
433 if( !pfo->lpX11Trans ) {
434 int dir, ascent, descent;
435 int info_width;
436 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
437 count, &dir, &ascent, &descent, &info_width );
439 size->cx = fabs((FLOAT)(info_width + dc->breakRem + count *
440 dc->charExtra) * dc->xformVport2World.eM11);
441 size->cy = fabs((FLOAT)(pfo->fs->ascent + pfo->fs->descent) *
442 dc->xformVport2World.eM22);
443 } else {
444 INT i;
445 float x = 0.0, y = 0.0;
446 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
447 for(i = 0; i < count; i++) {
448 x += pfo->fs->per_char ?
449 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
450 pfo->fs->min_bounds.attributes;
452 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
453 TRACE("x = %f y = %f\n", x, y);
454 x *= pfo->lpX11Trans->pixelsize / 1000.0;
455 y *= pfo->lpX11Trans->pixelsize / 1000.0;
456 size->cx = fabs((x + dc->breakRem + count * dc->charExtra) *
457 dc->xformVport2World.eM11);
458 size->cy = fabs(y * dc->xformVport2World.eM22);
460 size->cx *= pfo->rescale;
461 size->cy *= pfo->rescale;
462 HeapFree( GetProcessHeap(), 0, p );
463 return TRUE;
465 return FALSE;