windowscodecs: Protect from invalid input some metadata handler methods.
[wine.git] / dlls / gdiplus / stringformat.c
blobcf6ea1f392ab3c85ab35731a5e01c3ed24728e18
1 /*
3 * Copyright (C) 2007 Google (Evan Stade)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winnls.h"
27 #include "objbase.h"
29 #include "gdiplus.h"
30 #include "gdiplus_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
35 GpStatus WINGDIPAPI GdipCreateStringFormat(INT attr, LANGID lang,
36 GpStringFormat **format)
38 TRACE("(%i, %x, %p)\n", attr, lang, format);
40 if(!format)
41 return InvalidParameter;
43 *format = GdipAlloc(sizeof(GpStringFormat));
44 if(!*format) return OutOfMemory;
46 (*format)->attr = attr;
47 (*format)->lang = lang;
48 (*format)->digitlang = LANG_NEUTRAL;
49 (*format)->trimming = StringTrimmingCharacter;
50 (*format)->digitsub = StringDigitSubstituteUser;
51 (*format)->character_ranges = NULL;
52 (*format)->range_count = 0;
53 /* tabstops */
54 (*format)->tabcount = 0;
55 (*format)->firsttab = 0.0;
56 (*format)->tabs = NULL;
58 TRACE("<-- %p\n", *format);
60 return Ok;
63 GpStatus WINGDIPAPI GdipDeleteStringFormat(GpStringFormat *format)
65 if(!format)
66 return InvalidParameter;
68 GdipFree(format->character_ranges);
69 GdipFree(format->tabs);
70 GdipFree(format);
72 return Ok;
75 GpStatus WINGDIPAPI GdipStringFormatGetGenericDefault(GpStringFormat **format)
77 GpStatus stat;
79 if (!format)
80 return InvalidParameter;
82 stat = GdipCreateStringFormat(0, LANG_NEUTRAL, format);
83 if(stat != Ok)
84 return stat;
86 (*format)->align = StringAlignmentNear;
87 (*format)->vertalign = StringAlignmentNear;
89 return Ok;
92 GpStatus WINGDIPAPI GdipGetStringFormatAlign(GpStringFormat *format,
93 StringAlignment *align)
95 if(!format || !align)
96 return InvalidParameter;
98 *align = format->align;
100 return Ok;
103 GpStatus WINGDIPAPI GdipGetStringFormatDigitSubstitution(GDIPCONST GpStringFormat *format,
104 LANGID *language, StringDigitSubstitute *substitute)
106 if(!format)
107 return InvalidParameter;
109 if(language) *language = format->digitlang;
110 if(substitute) *substitute = format->digitsub;
112 return Ok;
115 GpStatus WINGDIPAPI GdipGetStringFormatFlags(GDIPCONST GpStringFormat* format,
116 INT* flags)
118 if (!(format && flags))
119 return InvalidParameter;
121 *flags = format->attr;
123 return Ok;
126 GpStatus WINGDIPAPI GdipGetStringFormatHotkeyPrefix(GDIPCONST GpStringFormat
127 *format, INT *hkpx)
129 if(!format || !hkpx)
130 return InvalidParameter;
132 *hkpx = (INT)format->hkprefix;
134 return Ok;
137 GpStatus WINGDIPAPI GdipGetStringFormatLineAlign(GpStringFormat *format,
138 StringAlignment *align)
140 if(!format || !align)
141 return InvalidParameter;
143 *align = format->vertalign;
145 return Ok;
148 GpStatus WINGDIPAPI GdipGetStringFormatMeasurableCharacterRangeCount(
149 GDIPCONST GpStringFormat *format, INT *count)
151 if (!(format && count))
152 return InvalidParameter;
154 TRACE("%p %p\n", format, count);
156 *count = format->range_count;
158 return Ok;
161 GpStatus WINGDIPAPI GdipGetStringFormatTabStopCount(GDIPCONST GpStringFormat *format,
162 INT *count)
164 if(!format || !count)
165 return InvalidParameter;
167 *count = format->tabcount;
169 return Ok;
172 GpStatus WINGDIPAPI GdipGetStringFormatTabStops(GDIPCONST GpStringFormat *format, INT count,
173 REAL *firsttab, REAL *tabs)
175 if(!format || !firsttab || !tabs)
176 return InvalidParameter;
178 /* native simply crashes on count < 0 */
179 if(count != 0)
180 memcpy(tabs, format->tabs, sizeof(REAL)*count);
182 *firsttab = format->firsttab;
184 return Ok;
187 GpStatus WINGDIPAPI GdipGetStringFormatTrimming(GpStringFormat *format,
188 StringTrimming *trimming)
190 if(!format || !trimming)
191 return InvalidParameter;
193 *trimming = format->trimming;
195 return Ok;
198 GpStatus WINGDIPAPI GdipSetStringFormatAlign(GpStringFormat *format,
199 StringAlignment align)
201 TRACE("(%p, %i)\n", format, align);
203 if(!format)
204 return InvalidParameter;
206 format->align = align;
208 return Ok;
211 /*FIXME: digit substitution actually not implemented, get/set only */
212 GpStatus WINGDIPAPI GdipSetStringFormatDigitSubstitution(GpStringFormat *format,
213 LANGID language, StringDigitSubstitute substitute)
215 TRACE("(%p, %x, %i)\n", format, language, substitute);
217 if(!format)
218 return InvalidParameter;
220 format->digitlang = language;
221 format->digitsub = substitute;
223 return Ok;
226 GpStatus WINGDIPAPI GdipSetStringFormatHotkeyPrefix(GpStringFormat *format,
227 INT hkpx)
229 TRACE("(%p, %i)\n", format, hkpx);
231 if(!format || hkpx < 0 || hkpx > 2)
232 return InvalidParameter;
234 format->hkprefix = (HotkeyPrefix) hkpx;
236 return Ok;
239 GpStatus WINGDIPAPI GdipSetStringFormatLineAlign(GpStringFormat *format,
240 StringAlignment align)
242 TRACE("(%p, %i)\n", format, align);
244 if(!format)
245 return InvalidParameter;
247 format->vertalign = align;
249 return Ok;
252 GpStatus WINGDIPAPI GdipSetStringFormatMeasurableCharacterRanges(
253 GpStringFormat *format, INT rangeCount, GDIPCONST CharacterRange *ranges)
255 CharacterRange *new_ranges;
257 if (!(format && ranges))
258 return InvalidParameter;
260 TRACE("%p, %d, %p\n", format, rangeCount, ranges);
262 new_ranges = GdipAlloc(rangeCount * sizeof(CharacterRange));
263 if (!new_ranges)
264 return OutOfMemory;
266 GdipFree(format->character_ranges);
267 format->character_ranges = new_ranges;
268 memcpy(format->character_ranges, ranges, sizeof(CharacterRange) * rangeCount);
269 format->range_count = rangeCount;
271 return Ok;
274 GpStatus WINGDIPAPI GdipSetStringFormatTabStops(GpStringFormat *format, REAL firsttab,
275 INT count, GDIPCONST REAL *tabs)
277 TRACE("(%p, %0.2f, %i, %p)\n", format, firsttab, count, tabs);
279 if(!format || !tabs)
280 return InvalidParameter;
282 if(count > 0){
283 if(firsttab < 0.0) return NotImplemented;
284 /* first time allocation */
285 if(format->tabcount == 0){
286 format->tabs = GdipAlloc(sizeof(REAL)*count);
287 if(!format->tabs)
288 return OutOfMemory;
290 /* reallocation */
291 if((format->tabcount < count) && (format->tabcount > 0)){
292 REAL *ptr;
293 ptr = HeapReAlloc(GetProcessHeap(), 0, format->tabs, sizeof(REAL)*count);
294 if(!ptr)
295 return OutOfMemory;
296 format->tabs = ptr;
298 format->firsttab = firsttab;
299 format->tabcount = count;
300 memcpy(format->tabs, tabs, sizeof(REAL)*count);
303 return Ok;
306 GpStatus WINGDIPAPI GdipSetStringFormatTrimming(GpStringFormat *format,
307 StringTrimming trimming)
309 TRACE("(%p, %i)\n", format, trimming);
311 if(!format)
312 return InvalidParameter;
314 format->trimming = trimming;
316 return Ok;
319 GpStatus WINGDIPAPI GdipSetStringFormatFlags(GpStringFormat *format, INT flags)
321 TRACE("(%p, %x)\n", format, flags);
323 if(!format)
324 return InvalidParameter;
326 format->attr = flags;
328 return Ok;
331 GpStatus WINGDIPAPI GdipCloneStringFormat(GDIPCONST GpStringFormat *format, GpStringFormat **newFormat)
333 if(!format || !newFormat)
334 return InvalidParameter;
336 *newFormat = GdipAlloc(sizeof(GpStringFormat));
337 if(!*newFormat) return OutOfMemory;
339 **newFormat = *format;
341 if(format->tabcount > 0){
342 (*newFormat)->tabs = GdipAlloc(sizeof(REAL) * format->tabcount);
343 if(!(*newFormat)->tabs){
344 GdipFree(*newFormat);
345 return OutOfMemory;
347 memcpy((*newFormat)->tabs, format->tabs, sizeof(REAL) * format->tabcount);
349 else
350 (*newFormat)->tabs = NULL;
352 if(format->range_count > 0){
353 (*newFormat)->character_ranges = GdipAlloc(sizeof(CharacterRange) * format->range_count);
354 if(!(*newFormat)->character_ranges){
355 GdipFree((*newFormat)->tabs);
356 GdipFree(*newFormat);
357 return OutOfMemory;
359 memcpy((*newFormat)->character_ranges, format->character_ranges,
360 sizeof(CharacterRange) * format->range_count);
362 else
363 (*newFormat)->character_ranges = NULL;
365 TRACE("%p %p\n",format,newFormat);
367 return Ok;
370 GpStatus WINGDIPAPI GdipStringFormatGetGenericTypographic(GpStringFormat **format)
372 GpStatus stat;
374 if(!format)
375 return InvalidParameter;
377 stat = GdipCreateStringFormat(StringFormatFlagsNoFitBlackBox |
378 StringFormatFlagsLineLimit |
379 StringFormatFlagsNoClip, LANG_NEUTRAL, format);
380 if(stat != Ok)
381 return stat;
383 (*format)->digitlang = LANG_NEUTRAL;
384 (*format)->digitsub = StringDigitSubstituteUser;
385 (*format)->trimming = StringTrimmingNone;
386 (*format)->hkprefix = HotkeyPrefixNone;
387 (*format)->align = StringAlignmentNear;
388 (*format)->vertalign = StringAlignmentNear;
390 return Ok;