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