msvcrt: New implementation of fcvt.
[wine/multimedia.git] / dlls / winex11.drv / text.c
bloba8032cf582372028f8cade2d762990d4e06ce9be
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 "x11font.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(text);
34 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
35 #define IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
38 /***********************************************************************
39 * X11DRV_ExtTextOut
41 BOOL
42 X11DRV_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
43 const RECT *lprect, LPCWSTR wstr, UINT count,
44 const INT *lpDx )
46 unsigned int i;
47 fontObject* pfo;
48 XFontStruct* font;
49 BOOL rotated = FALSE;
50 XChar2b *str2b = NULL;
51 BOOL dibUpdateFlag = FALSE;
52 BOOL result = TRUE;
53 HRGN saved_region = 0;
55 if(physDev->has_gdi_font)
56 return X11DRV_XRender_ExtTextOut(physDev, x, y, flags, lprect, wstr, count, lpDx);
58 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
60 pfo = XFONT_GetFontObject( physDev->font );
61 font = pfo->fs;
63 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
64 rotated = TRUE;
66 TRACE("hdc=%p df=%04x %d,%d %s, %d flags=%d lpDx=%p\n",
67 physDev->hdc, (UINT16)(physDev->font), x, y,
68 debugstr_wn (wstr, count), count, flags, lpDx);
70 if (lprect != NULL) TRACE("\trect=(%d,%d - %d,%d)\n",
71 lprect->left, lprect->top,
72 lprect->right, lprect->bottom );
74 /* Draw the rectangle */
76 if (flags & ETO_OPAQUE)
78 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
79 dibUpdateFlag = TRUE;
80 wine_tsx11_lock();
81 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
82 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
83 physDev->dc_rect.left + lprect->left, physDev->dc_rect.top + lprect->top,
84 lprect->right - lprect->left, lprect->bottom - lprect->top );
85 wine_tsx11_unlock();
87 if (!count) goto END; /* Nothing more to do */
90 /* Set the clip region */
92 if (flags & ETO_CLIPPED)
94 HRGN clip_region;
96 clip_region = CreateRectRgnIndirect( lprect );
97 /* make a copy of the current device region */
98 saved_region = CreateRectRgn( 0, 0, 0, 0 );
99 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
100 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
101 DeleteObject( clip_region );
104 /* Draw the text background if necessary */
106 if (!dibUpdateFlag)
108 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
109 dibUpdateFlag = TRUE;
113 /* Draw the text (count > 0 verified) */
114 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
115 goto FAIL;
117 wine_tsx11_lock();
118 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
119 wine_tsx11_unlock();
120 if(!rotated)
122 if (!lpDx)
124 X11DRV_cptable[pfo->fi->cptable].pDrawString(
125 pfo, gdi_display, physDev->drawable, physDev->gc,
126 physDev->dc_rect.left + x, physDev->dc_rect.top + y, str2b, count );
128 else
130 XTextItem16 *items, *pitem;
132 pitem = items = HeapAlloc( GetProcessHeap(), 0,
133 count * sizeof(XTextItem16) );
134 if(items == NULL) goto FAIL;
136 for(i = 0; i < count; i++)
138 pitem->chars = str2b + i;
139 pitem->delta = lpDx[i];
140 pitem->nchars = 1;
141 pitem->font = None;
142 pitem++;
145 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
146 physDev->drawable, physDev->gc,
147 physDev->dc_rect.left + x, physDev->dc_rect.top + y, items, pitem - items );
148 HeapFree( GetProcessHeap(), 0, items );
151 else /* rotated */
153 /* have to render character by character. */
154 double offset = 0.0;
155 int i;
157 for (i=0; i<count; i++)
159 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
160 - font->min_char_or_byte2;
161 int x_i = IROUND((double) (physDev->dc_rect.left + x) + offset *
162 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
163 int y_i = IROUND((double) (physDev->dc_rect.top + y) - offset *
164 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
166 X11DRV_cptable[pfo->fi->cptable].pDrawString(
167 pfo, gdi_display, physDev->drawable, physDev->gc,
168 x_i, y_i, &str2b[i], 1);
169 if (lpDx)
171 offset += lpDx[i];
173 else
175 offset += (double) (font->per_char ?
176 font->per_char[char_metric_offset].attributes:
177 font->min_bounds.attributes)
178 * pfo->lpX11Trans->pixelsize / 1000.0;
182 HeapFree( GetProcessHeap(), 0, str2b );
184 if (flags & ETO_CLIPPED)
186 /* restore the device region */
187 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
188 DeleteObject( saved_region );
190 goto END;
192 FAIL:
193 HeapFree( GetProcessHeap(), 0, str2b );
194 result = FALSE;
196 END:
197 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
198 return result;
202 /***********************************************************************
203 * X11DRV_GetTextExtentExPoint
205 BOOL X11DRV_GetTextExtentExPoint( X11DRV_PDEVICE *physDev, LPCWSTR str, INT count,
206 INT maxExt, LPINT lpnFit, LPINT alpDx, LPSIZE size )
208 fontObject* pfo = XFONT_GetFontObject( physDev->font );
210 TRACE("%s %d\n", debugstr_wn(str,count), count);
211 if( pfo ) {
212 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
213 if (!p) return FALSE;
214 if( !pfo->lpX11Trans ) {
215 int dir, ascent, descent;
216 int info_width;
217 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
218 count, &dir, &ascent, &descent, &info_width,
219 maxExt, lpnFit, alpDx );
221 size->cx = info_width;
222 size->cy = pfo->fs->ascent + pfo->fs->descent;
223 } else {
224 INT i;
225 INT nfit = 0;
226 float x = 0.0, y = 0.0;
227 float scaled_x = 0.0, pixsize = pfo->lpX11Trans->pixelsize;
228 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
229 for(i = 0; i < count; i++) {
230 x += pfo->fs->per_char ?
231 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
232 pfo->fs->min_bounds.attributes;
233 scaled_x = x * pixsize / 1000.0;
234 if (alpDx)
235 alpDx[i] = scaled_x;
236 if (scaled_x <= maxExt)
237 ++nfit;
239 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
240 TRACE("x = %f y = %f\n", x, y);
241 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
242 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
243 if (lpnFit)
244 *lpnFit = nfit;
246 size->cx *= pfo->rescale;
247 size->cy *= pfo->rescale;
248 HeapFree( GetProcessHeap(), 0, p );
249 return TRUE;
251 return FALSE;