]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/strconv_cf.cpp
Use unsigned char for XBM bitmaps data.
[wxWidgets.git] / src / osx / core / strconv_cf.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/corefoundation/strconv.cpp
3 // Purpose: Unicode conversion classes
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2007-07-06
7 // RCS-ID: $Id$
8 // Copyright: (c) 2007 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/string.h"
17 #endif
18
19 #include "wx/strconv.h"
20 #include "wx/fontmap.h"
21
22 #ifdef __DARWIN__
23
24 #include "wx/osx/core/private/strconv_cf.h"
25 #include "wx/osx/core/cfref.h"
26
27
28 // ============================================================================
29 // CoreFoundation conversion classes
30 // ============================================================================
31
32 /* Provide factory functions for unit tests. Not in any header. Do not
33 * assume ABI compatibility even within a given wxWidgets release.
34 */
35
36 #if wxUSE_FONTMAP
37 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf( const char* name)
38 {
39 wxMBConv_cf *result = new wxMBConv_cf(name);
40 if(!result->IsOk())
41 {
42 delete result;
43 return NULL;
44 }
45 else
46 return result;
47 }
48 #endif // wxUSE_FONTMAP
49
50 WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_cf(wxFontEncoding encoding)
51 {
52 wxMBConv_cf *result = new wxMBConv_cf(encoding);
53 if(!result->IsOk())
54 {
55 delete result;
56 return NULL;
57 }
58 else
59 return result;
60 }
61
62 // Provide a constant for the wchat_t encoding used by the host platform.
63 #ifdef WORDS_BIGENDIAN
64 static const CFStringEncoding wxCFStringEncodingWcharT = kCFStringEncodingUTF32BE;
65 #else
66 static const CFStringEncoding wxCFStringEncodingWcharT = kCFStringEncodingUTF32LE;
67 #endif
68
69 size_t wxMBConv_cf::ToWChar(wchar_t * dst, size_t dstSize, const char * src, size_t srcSize) const
70 {
71 wxCHECK(src, wxCONV_FAILED);
72
73 /* NOTE: This is wrong if the source encoding has an element size
74 * other than char (e.g. it's kCFStringEncodingUnicode)
75 * If the user specifies it, it's presumably right though.
76 * Right now we don't support UTF-16 in anyway since wx can do a better job.
77 */
78 if(srcSize == wxNO_LEN)
79 srcSize = strlen(src) + 1;
80
81 // First create the temporary CFString
82 wxCFRef<CFStringRef> theString( CFStringCreateWithBytes (
83 NULL, //the allocator
84 (const UInt8*)src,
85 srcSize,
86 m_encoding,
87 false //no BOM/external representation
88 ));
89
90 if ( theString == NULL )
91 return wxCONV_FAILED;
92
93 /* NOTE: The string content includes the NULL element if the source string did
94 * That means we have to do nothing special because the destination will have
95 * the NULL element iff the source did and the NULL element will be included
96 * in the count iff it was included in the source count.
97 */
98
99
100 /* If we're compiling against Tiger headers we can support direct conversion
101 * to UTF32. If we are then run against a pre-Tiger system, the encoding
102 * won't be available so we'll defer to the string->UTF-16->UTF-32 conversion.
103 */
104 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT))
105 {
106 CFRange fullStringRange = CFRangeMake(0, CFStringGetLength(theString));
107 CFIndex usedBufLen;
108
109 CFIndex charsConverted = CFStringGetBytes(
110 theString,
111 fullStringRange,
112 wxCFStringEncodingWcharT,
113 0,
114 false,
115 // if dstSize is 0 then pass NULL to get required length in usedBufLen
116 dstSize != 0?(UInt8*)dst:NULL,
117 dstSize * sizeof(wchar_t),
118 &usedBufLen);
119
120 if(charsConverted < CFStringGetLength(theString))
121 return wxCONV_FAILED;
122
123 /* usedBufLen is the number of bytes written, so we divide by
124 * sizeof(wchar_t) to get the number of elements written.
125 */
126 wxASSERT( (usedBufLen % sizeof(wchar_t)) == 0 );
127
128 // CFStringGetBytes does exactly the right thing when buffer
129 // pointer is NULL and returns the number of bytes required
130 return usedBufLen / sizeof(wchar_t);
131 }
132 else
133 {
134 // NOTE: Includes NULL iff source did
135 /* NOTE: This is an approximation. The eventual UTF-32 will
136 * possibly have less elements but certainly not more.
137 */
138 size_t returnSize = CFStringGetLength(theString);
139
140 if (dstSize == 0 || dst == NULL)
141 {
142 return returnSize;
143 }
144
145 // Convert the entire string.. too hard to figure out how many UTF-16 we'd need
146 // for an undersized UTF-32 destination buffer.
147 CFRange fullStringRange = CFRangeMake(0, CFStringGetLength(theString));
148 UniChar *szUniCharBuffer = new UniChar[fullStringRange.length];
149
150 CFStringGetCharacters(theString, fullStringRange, szUniCharBuffer);
151
152 wxMBConvUTF16 converter;
153 returnSize = converter.ToWChar( dst, dstSize, (const char*)szUniCharBuffer, fullStringRange.length );
154 delete [] szUniCharBuffer;
155
156 return returnSize;
157 }
158 // NOTREACHED
159 }
160
161 size_t wxMBConv_cf::FromWChar(char *dst, size_t dstSize, const wchar_t *src, size_t srcSize) const
162 {
163 wxCHECK(src, wxCONV_FAILED);
164
165 if(srcSize == wxNO_LEN)
166 srcSize = wxStrlen(src) + 1;
167
168 // Temporary CFString
169 wxCFRef<CFStringRef> theString;
170
171 /* If we're compiling against Tiger headers we can support direct conversion
172 * from UTF32. If we are then run against a pre-Tiger system, the encoding
173 * won't be available so we'll defer to the UTF-32->UTF-16->string conversion.
174 */
175 if(CFStringIsEncodingAvailable(wxCFStringEncodingWcharT))
176 {
177 theString = wxCFRef<CFStringRef>(CFStringCreateWithBytes(
178 kCFAllocatorDefault,
179 (UInt8*)src,
180 srcSize * sizeof(wchar_t),
181 wxCFStringEncodingWcharT,
182 false));
183 }
184 else
185 {
186 wxMBConvUTF16 converter;
187 size_t cbUniBuffer = converter.FromWChar( NULL, 0, src, srcSize );
188 wxASSERT(cbUniBuffer % sizeof(UniChar));
189
190 // Will be free'd by kCFAllocatorMalloc when CFString is released
191 UniChar *tmpUniBuffer = (UniChar*)malloc(cbUniBuffer);
192
193 cbUniBuffer = converter.FromWChar( (char*) tmpUniBuffer, cbUniBuffer, src, srcSize );
194 wxASSERT(cbUniBuffer % sizeof(UniChar));
195
196 theString = wxCFRef<CFStringRef>(CFStringCreateWithCharactersNoCopy(
197 kCFAllocatorDefault,
198 tmpUniBuffer,
199 cbUniBuffer / sizeof(UniChar),
200 kCFAllocatorMalloc
201 ));
202
203 }
204
205 wxCHECK(theString != NULL, wxCONV_FAILED);
206
207 CFIndex usedBufLen;
208
209 CFIndex charsConverted = CFStringGetBytes(
210 theString,
211 CFRangeMake(0, CFStringGetLength(theString)),
212 m_encoding,
213 0, // FAIL on unconvertible characters
214 false, // not an external representation
215 (UInt8*)dst,
216 dstSize,
217 &usedBufLen
218 );
219
220 // when dst is non-NULL, we check usedBufLen against dstSize as
221 // CFStringGetBytes sometimes treats dst as being NULL when dstSize==0
222 if( (charsConverted < CFStringGetLength(theString)) ||
223 (dst && (size_t) usedBufLen > dstSize) )
224 return wxCONV_FAILED;
225
226 return usedBufLen;
227 }
228
229 #endif // __DARWIN__
230
231