Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / quartz / utils.cxx
blobb07e68f746428bf0428c543dfdbd234cbb1f60d0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <iostream>
23 #include <iomanip>
25 #include <rtl/alloc.h>
26 #include <rtl/ustrbuf.hxx>
28 #include <quartz/utils.h>
30 OUString GetOUString( CFStringRef rStr )
32 if( rStr == nullptr )
34 return OUString();
37 CFIndex nLength = CFStringGetLength( rStr );
38 if( nLength == 0 )
40 return OUString();
43 const UniChar* pConstStr = CFStringGetCharactersPtr( rStr );
44 if( pConstStr )
46 return OUString( reinterpret_cast<sal_Unicode const *>(pConstStr), nLength );
49 std::unique_ptr<UniChar[]> pStr(new UniChar[nLength]);
50 CFRange aRange = { 0, nLength };
51 CFStringGetCharacters( rStr, aRange, pStr.get() );
53 OUString aRet( reinterpret_cast<sal_Unicode *>(pStr.get()), nLength );
54 return aRet;
57 OUString GetOUString( const NSString* pStr )
59 if( ! pStr )
61 return OUString();
64 int nLen = [pStr length];
65 if( nLen == 0 )
67 return OUString();
70 OUStringBuffer aBuf( nLen+1 );
71 aBuf.setLength( nLen );
72 [pStr getCharacters:
73 reinterpret_cast<unichar *>(const_cast<sal_Unicode*>(aBuf.getStr()))];
75 return aBuf.makeStringAndClear();
78 CFStringRef CreateCFString( const OUString& rStr )
80 return CFStringCreateWithCharacters(kCFAllocatorDefault, reinterpret_cast<UniChar const *>(rStr.getStr()), rStr.getLength() );
83 NSString* CreateNSString( const OUString& rStr )
85 return [[NSString alloc] initWithCharacters: reinterpret_cast<unichar const *>(rStr.getStr()) length: rStr.getLength()];
88 OUString NSStringArrayToOUString(NSArray* array)
90 OUString result = "[";
91 OUString sep;
92 for (NSUInteger i = 0; i < [array count]; i++)
94 result = result + sep + OUString::fromUtf8([[array objectAtIndex:i] UTF8String]);
95 sep = ",";
97 result = result + "]";
98 return result;
101 OUString NSDictionaryKeysToOUString(NSDictionary* dict)
103 OUString result = "{";
104 OUString sep;
105 for (NSString *key in dict)
107 result = result + sep + OUString::fromUtf8([key UTF8String]);
108 sep = ",";
110 result = result + "}";
111 return result;
114 std::ostream &operator <<(std::ostream& s, const CGRect &rRect)
116 #ifndef SAL_LOG_INFO
117 (void) rRect;
118 #else
119 if (CGRectIsNull(rRect))
121 s << "NULL";
123 else
125 s << rRect.size << "@" << rRect.origin;
127 #endif
128 return s;
131 std::ostream &operator <<(std::ostream& s, const CGPoint &rPoint)
133 #ifndef SAL_LOG_INFO
134 (void) rPoint;
135 #else
136 s << "(" << rPoint.x << "," << rPoint.y << ")";
137 #endif
138 return s;
141 std::ostream &operator <<(std::ostream& s, const CGSize &rSize)
143 #ifndef SAL_LOG_INFO
144 (void) rSize;
145 #else
146 s << rSize.width << "x" << rSize.height;
147 #endif
148 return s;
151 std::ostream &operator <<(std::ostream& s, CGColorRef pColor)
153 #ifndef SAL_LOG_INFO
154 (void) pColor;
155 #else
156 CFStringRef colorString = CFCopyDescription(pColor);
157 if (colorString)
159 s << GetOUString(colorString);
160 CFRelease(colorString);
162 else
164 s << "NULL";
166 #endif
167 return s;
170 std::ostream &operator <<(std::ostream& s, const CGAffineTransform &aXform)
172 #ifndef SAL_LOG_INFO
173 (void) aXform;
174 #else
175 if (CGAffineTransformIsIdentity(aXform))
177 s << "IDENT";
179 else
181 s << "[" << aXform.a << "," << aXform.b << "," << aXform.c << "," << aXform.d << "," << aXform.tx << "," << aXform.ty << "]";
183 #endif
184 return s;
187 std::ostream &operator <<(std::ostream& s, CGColorSpaceRef cs)
189 #ifndef SAL_LOG_INFO
190 (void) cs;
191 #else
192 if (cs == nullptr)
194 s << "null";
195 return s;
198 CGColorSpaceModel model = CGColorSpaceGetModel(cs);
199 switch (model)
201 case kCGColorSpaceModelUnknown:
202 s << "Unknown";
203 break;
204 case kCGColorSpaceModelMonochrome:
205 s << "Monochrome";
206 break;
207 case kCGColorSpaceModelRGB:
208 s << "RGB";
209 if (CGColorSpaceIsWideGamutRGB(cs))
210 s << " (wide gamut)";
211 break;
212 case kCGColorSpaceModelCMYK:
213 s << "CMYK";
214 break;
215 case kCGColorSpaceModelLab:
216 s << "Lab";
217 break;
218 case kCGColorSpaceModelDeviceN:
219 s << "DeviceN";
220 break;
221 case kCGColorSpaceModelIndexed:
222 s << "Indexed (" << CGColorSpaceGetColorTableCount(cs) << ")";
223 break;
224 case kCGColorSpaceModelPattern:
225 s << "Pattern";
226 break;
227 case kCGColorSpaceModelXYZ:
228 s << "XYZ";
229 break;
230 default:
231 s << "?(" << model << ")";
232 break;
235 CFStringRef name = CGColorSpaceCopyName(cs);
236 if (name != nullptr)
237 s << " (" << [static_cast<NSString *>(name) UTF8String] << ")";
238 #endif
239 return s;
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */