x11drv: Renamed the x11drv directory to winex11.drv.
[wine/multimedia.git] / dlls / winex11.drv / text.c
blob0ec9bbcc13d0f17830fa4dab30d69534449fb31c
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <math.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "x11font.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(text);
35 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
36 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
39 /***********************************************************************
40 * X11DRV_ExtTextOut
42 BOOL
43 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
44 const RECT *lprect, LPCWSTR wstr, UINT count,
45 const INT *lpDx )
47 unsigned int i;
48 fontObject* pfo;
49 XFontStruct* font;
50 BOOL rotated = FALSE;
51 XChar2b *str2b = NULL;
52 BOOL dibUpdateFlag = FALSE;
53 BOOL result = TRUE;
54 HRGN saved_region = 0;
56 if(physDev->has_gdi_font)
57 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx);
59 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
61 pfo = XFONT_GetFontObject( physDev->font );
62 font = pfo->fs;
64 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
65 rotated = TRUE;
67 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
68 physDev->hdc, (UINT16)(physDev->font), x, y,
69 debugstr_wn (wstr, count), count, flags, lpDx);
71 if (lprect != NULL) TRACE("\trect=(%ld,%ld - %ld,%ld)\n",
72 lprect->left, lprect->top,
73 lprect->right, lprect->bottom );
75 /* Draw the rectangle */
77 if (flags & ETO_OPAQUE)
79 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
80 dibUpdateFlag = TRUE;
81 wine_tsx11_lock();
82 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
83 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
84 physDev->org.x + lprect->left, physDev->org.y + lprect->top,
85 lprect->right - lprect->left, lprect->bottom - lprect->top );
86 wine_tsx11_unlock();
88 if (!count) goto END; /* Nothing more to do */
91 /* Set the clip region */
93 if (flags & ETO_CLIPPED)
95 HRGN clip_region;
97 clip_region = CreateRectRgnIndirect( lprect );
98 /* make a copy of the current device region */
99 saved_region = CreateRectRgn( 0, 0, 0, 0 );
100 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
101 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
102 DeleteObject( clip_region );
105 /* Draw the text background if necessary */
107 if (!dibUpdateFlag)
109 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
110 dibUpdateFlag = TRUE;
114 /* Draw the text (count > 0 verified) */
115 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
116 goto FAIL;
118 wine_tsx11_lock();
119 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
120 wine_tsx11_unlock();
121 if(!rotated)
123 if (!lpDx)
125 X11DRV_cptable[pfo->fi->cptable].pDrawString(
126 pfo, gdi_display, physDev->drawable, physDev->gc,
127 physDev->org.x + x, physDev->org.y + y, str2b, count );
129 else
131 XTextItem16 *items, *pitem;
133 pitem = items = HeapAlloc( GetProcessHeap(), 0,
134 count * sizeof(XTextItem16) );
135 if(items == NULL) goto FAIL;
137 for(i = 0; i < count; i++)
139 pitem->chars = str2b + i;
140 pitem->delta = lpDx[i];
141 pitem->nchars = 1;
142 pitem->font = None;
143 pitem++;
146 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
147 physDev->drawable, physDev->gc,
148 physDev->org.x + x, physDev->org.y + y, items, pitem - items );
149 HeapFree( GetProcessHeap(), 0, items );
152 else /* rotated */
154 /* have to render character by character. */
155 double offset = 0.0;
156 int i;
158 for (i=0; i<count; i++)
160 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
161 - font->min_char_or_byte2;
162 int x_i = IROUND((double) (physDev->org.x + x) + offset *
163 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
164 int y_i = IROUND((double) (physDev->org.y + y) - offset *
165 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
167 X11DRV_cptable[pfo->fi->cptable].pDrawString(
168 pfo, gdi_display, physDev->drawable, physDev->gc,
169 x_i, y_i, &str2b[i], 1);
170 if (lpDx)
172 offset += lpDx[i];
174 else
176 offset += (double) (font->per_char ?
177 font->per_char[char_metric_offset].attributes:
178 font->min_bounds.attributes)
179 * pfo->lpX11Trans->pixelsize / 1000.0;
183 HeapFree( GetProcessHeap(), 0, str2b );
185 if (flags & ETO_CLIPPED)
187 /* restore the device region */
188 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
189 DeleteObject( saved_region );
191 goto END;
193 FAIL:
194 HeapFree( GetProcessHeap(), 0, str2b );
195 result = FALSE;
197 END:
198 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
199 return result;
203 /***********************************************************************
204 * X11DRV_GetTextExtentPoint
206 BOOL X11DRV_GetTextExtentPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
207 LPSIZE size )
209 fontObject* pfo = XFONT_GetFontObject( physDev->font );
211 TRACE("%s %d\n", debugstr_wn(str,count), count);
212 if( pfo ) {
213 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
214 if (!p) return FALSE;
215 if( !pfo->lpX11Trans ) {
216 int dir, ascent, descent;
217 int info_width;
218 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
219 count, &dir, &ascent, &descent, &info_width );
221 size->cx = info_width;
222 size->cy = pfo->fs->ascent + pfo->fs->descent;
223 } else {
224 INT i;
225 float x = 0.0, y = 0.0;
226 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
227 for(i = 0; i < count; i++) {
228 x += pfo->fs->per_char ?
229 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
230 pfo->fs->min_bounds.attributes;
232 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
233 TRACE("x = %f y = %f\n", x, y);
234 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
235 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
237 size->cx *= pfo->rescale;
238 size->cy *= pfo->rescale;
239 HeapFree( GetProcessHeap(), 0, p );
240 return TRUE;
242 return FALSE;