msvcp90: Fix buffer size in basic_string_char_grow.
[wine/wine-gecko.git] / dlls / winex11.drv / text.c
blobbce0af91ab75aeb89880933d0cc3a2a888460e95
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 IROUND(x) (int)((x)>0? (x)+0.5 : (x) - 0.5)
37 /***********************************************************************
38 * X11DRV_ExtTextOut
40 BOOL X11DRV_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags,
41 const RECT *lprect, LPCWSTR wstr, UINT count, const INT *lpDx )
43 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
44 RGNDATA *saved_region = NULL;
45 unsigned int i;
46 fontObject* pfo;
47 XFontStruct* font;
48 BOOL rotated = FALSE;
49 XChar2b *str2b = NULL;
50 BOOL dibUpdateFlag = FALSE;
51 BOOL result = TRUE;
53 if (!X11DRV_SetupGCForText( physDev )) return TRUE;
55 pfo = XFONT_GetFontObject( physDev->font );
56 font = pfo->fs;
58 if (pfo->lf.lfEscapement && pfo->lpX11Trans)
59 rotated = TRUE;
61 TRACE("hdc=%p df=%04x %d,%d rc %s %s, %d flags=%d lpDx=%p\n",
62 dev->hdc, (UINT16)physDev->font, x, y, wine_dbgstr_rect(lprect),
63 debugstr_wn (wstr, count), count, flags, lpDx);
65 /* Draw the rectangle */
67 if (flags & ETO_OPAQUE)
69 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod );
70 dibUpdateFlag = TRUE;
71 wine_tsx11_lock();
72 XSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
73 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
74 physDev->dc_rect.left + lprect->left, physDev->dc_rect.top + lprect->top,
75 lprect->right - lprect->left, lprect->bottom - lprect->top );
76 wine_tsx11_unlock();
78 if (!count) goto END; /* Nothing more to do */
81 /* Set the clip region */
83 if (flags & ETO_CLIPPED)
85 HRGN clip_region = CreateRectRgnIndirect( lprect );
86 saved_region = add_extra_clipping_region( physDev, clip_region );
87 DeleteObject( clip_region );
90 /* Draw the text background if necessary */
92 if (!dibUpdateFlag)
94 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod );
95 dibUpdateFlag = TRUE;
99 /* Draw the text (count > 0 verified) */
100 if (!(str2b = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, wstr, count )))
102 result = FALSE;
103 goto END;
106 wine_tsx11_lock();
107 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
108 wine_tsx11_unlock();
109 if(!rotated)
111 if (!lpDx)
113 X11DRV_cptable[pfo->fi->cptable].pDrawString(
114 pfo, gdi_display, physDev->drawable, physDev->gc,
115 physDev->dc_rect.left + x, physDev->dc_rect.top + y, str2b, count );
117 else
119 XTextItem16 *items;
121 items = HeapAlloc( GetProcessHeap(), 0, count * sizeof(XTextItem16) );
122 if(items == NULL)
124 result = FALSE;
125 goto END;
127 items[0].chars = str2b;
128 items[0].delta = 0;
129 items[0].nchars = 1;
130 items[0].font = None;
131 for(i = 1; i < count; i++)
133 items[i].chars = str2b + i;
134 items[i].delta = (flags & ETO_PDY) ? lpDx[(i - 1) * 2] : lpDx[i - 1];
135 items[i].delta -= X11DRV_cptable[pfo->fi->cptable].pTextWidth( pfo, str2b + i - 1, 1 );
136 items[i].nchars = 1;
137 items[i].font = None;
139 X11DRV_cptable[pfo->fi->cptable].pDrawText( pfo, gdi_display,
140 physDev->drawable, physDev->gc,
141 physDev->dc_rect.left + x, physDev->dc_rect.top + y, items, count );
142 HeapFree( GetProcessHeap(), 0, items );
145 else /* rotated */
147 /* have to render character by character. */
148 double offset = 0.0;
149 UINT i;
151 for (i=0; i<count; i++)
153 int char_metric_offset = str2b[i].byte2 + (str2b[i].byte1 << 8)
154 - font->min_char_or_byte2;
155 int x_i = IROUND((double) (physDev->dc_rect.left + x) + offset *
156 pfo->lpX11Trans->a / pfo->lpX11Trans->pixelsize );
157 int y_i = IROUND((double) (physDev->dc_rect.top + y) - offset *
158 pfo->lpX11Trans->b / pfo->lpX11Trans->pixelsize );
160 X11DRV_cptable[pfo->fi->cptable].pDrawString(
161 pfo, gdi_display, physDev->drawable, physDev->gc,
162 x_i, y_i, &str2b[i], 1);
163 if (lpDx)
165 offset += (flags & ETO_PDY) ? lpDx[i * 2] : lpDx[i];
167 else
169 offset += (double) (font->per_char ?
170 font->per_char[char_metric_offset].attributes:
171 font->min_bounds.attributes)
172 * pfo->lpX11Trans->pixelsize / 1000.0;
177 END:
178 HeapFree( GetProcessHeap(), 0, str2b );
179 if (dibUpdateFlag) X11DRV_UnlockDIBSection( physDev, TRUE );
180 restore_clipping_region( physDev, saved_region );
181 return result;
185 /***********************************************************************
186 * X11DRV_GetTextExtentExPoint
188 BOOL X11DRV_GetTextExtentExPoint( PHYSDEV dev, LPCWSTR str, INT count,
189 INT maxExt, LPINT lpnFit, LPINT alpDx, LPSIZE size )
191 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
192 fontObject* pfo = XFONT_GetFontObject( physDev->font );
194 TRACE("%s %d\n", debugstr_wn(str,count), count);
195 if( pfo ) {
196 XChar2b *p = X11DRV_cptable[pfo->fi->cptable].punicode_to_char2b( pfo, str, count );
197 if (!p) return FALSE;
198 if( !pfo->lpX11Trans ) {
199 int dir, ascent, descent;
200 int info_width;
201 X11DRV_cptable[pfo->fi->cptable].pTextExtents( pfo, p,
202 count, &dir, &ascent, &descent, &info_width,
203 maxExt, lpnFit, alpDx );
205 size->cx = info_width;
206 size->cy = pfo->fs->ascent + pfo->fs->descent;
207 } else {
208 INT i;
209 INT nfit = 0;
210 float x = 0.0, y = 0.0;
211 float scaled_x = 0.0, pixsize = pfo->lpX11Trans->pixelsize;
212 /* FIXME: Deal with *_char_or_byte2 != 0 situations */
213 for(i = 0; i < count; i++) {
214 x += pfo->fs->per_char ?
215 pfo->fs->per_char[p[i].byte2 - pfo->fs->min_char_or_byte2].attributes :
216 pfo->fs->min_bounds.attributes;
217 scaled_x = x * pixsize / 1000.0;
218 if (alpDx)
219 alpDx[i] = scaled_x;
220 if (scaled_x <= maxExt)
221 ++nfit;
223 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
224 TRACE("x = %f y = %f\n", x, y);
225 size->cx = x * pfo->lpX11Trans->pixelsize / 1000.0;
226 size->cy = y * pfo->lpX11Trans->pixelsize / 1000.0;
227 if (lpnFit)
228 *lpnFit = nfit;
230 size->cx *= pfo->rescale;
231 size->cy *= pfo->rescale;
232 HeapFree( GetProcessHeap(), 0, p );
233 return TRUE;
235 return FALSE;