1from ctypes import *
2from ctypes import util
3
4from .runtime import send_message, ObjCInstance
5from .cocoatypes import *
6
7######################################################################
8
9# CORE FOUNDATION
10lib = util.find_library('CoreFoundation')
11
12# Hack for compatibility with macOS > 11.0
13if lib is None:
14    lib = '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation'
15
16cf = cdll.LoadLibrary(lib)
17
18kCFStringEncodingUTF8 = 0x08000100
19
20CFAllocatorRef = c_void_p
21CFStringEncoding = c_uint32
22
23cf.CFStringCreateWithCString.restype = c_void_p
24cf.CFStringCreateWithCString.argtypes = [CFAllocatorRef, c_char_p, CFStringEncoding]
25
26cf.CFRelease.restype = c_void_p
27cf.CFRelease.argtypes = [c_void_p]
28
29cf.CFStringGetLength.restype = CFIndex
30cf.CFStringGetLength.argtypes = [c_void_p]
31
32cf.CFStringGetMaximumSizeForEncoding.restype = CFIndex
33cf.CFStringGetMaximumSizeForEncoding.argtypes = [CFIndex, CFStringEncoding]
34
35cf.CFStringGetCString.restype = c_bool
36cf.CFStringGetCString.argtypes = [c_void_p, c_char_p, CFIndex, CFStringEncoding]
37
38cf.CFStringGetTypeID.restype = CFTypeID
39cf.CFStringGetTypeID.argtypes = []
40
41cf.CFAttributedStringCreate.restype = c_void_p
42cf.CFAttributedStringCreate.argtypes = [CFAllocatorRef, c_void_p, c_void_p]
43
44# Core Foundation type to Python type conversion functions
45
46def CFSTR(string):
47    return ObjCInstance(c_void_p(cf.CFStringCreateWithCString(
48            None, string.encode('utf8'), kCFStringEncodingUTF8)))
49
50# Other possible names for this method:
51# at, ampersat, arobe, apenstaartje (little monkey tail), strudel,
52# klammeraffe (spider monkey), little_mouse, arroba, sobachka (doggie)
53# malpa (monkey), snabel (trunk), papaki (small duck), afna (monkey),
54# kukac (caterpillar).
55def get_NSString(string):
56    """Autoreleased version of CFSTR"""
57    return CFSTR(string).autorelease()
58
59def cfstring_to_string(cfstring):
60    length = cf.CFStringGetLength(cfstring)
61    size = cf.CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8)
62    buffer = c_buffer(size + 1)
63    result = cf.CFStringGetCString(cfstring, buffer, len(buffer), kCFStringEncodingUTF8)
64    if result:
65        return str(buffer.value, 'utf-8')
66
67cf.CFDataCreate.restype = c_void_p
68cf.CFDataCreate.argtypes = [c_void_p, c_void_p, CFIndex]
69
70cf.CFDataGetBytes.restype = None
71cf.CFDataGetBytes.argtypes = [c_void_p, CFRange, c_void_p]
72
73cf.CFDataGetLength.restype = CFIndex
74cf.CFDataGetLength.argtypes = [c_void_p]
75
76cf.CFDictionaryGetValue.restype = c_void_p
77cf.CFDictionaryGetValue.argtypes = [c_void_p, c_void_p]
78
79cf.CFDictionaryAddValue.restype = None
80cf.CFDictionaryAddValue.argtypes = [c_void_p, c_void_p, c_void_p]
81
82cf.CFDictionaryCreateMutable.restype = c_void_p
83cf.CFDictionaryCreateMutable.argtypes = [CFAllocatorRef, CFIndex, c_void_p, c_void_p]
84
85cf.CFNumberCreate.restype = c_void_p
86cf.CFNumberCreate.argtypes = [CFAllocatorRef, CFNumberType, c_void_p]
87
88cf.CFNumberGetType.restype = CFNumberType
89cf.CFNumberGetType.argtypes = [c_void_p]
90
91cf.CFNumberGetValue.restype = c_ubyte
92cf.CFNumberGetValue.argtypes = [c_void_p, CFNumberType, c_void_p]
93
94cf.CFNumberGetTypeID.restype = CFTypeID
95cf.CFNumberGetTypeID.argtypes = []
96
97cf.CFGetTypeID.restype = CFTypeID
98cf.CFGetTypeID.argtypes = [c_void_p]
99
100# CFNumber.h
101kCFNumberSInt8Type     = 1
102kCFNumberSInt16Type    = 2
103kCFNumberSInt32Type    = 3
104kCFNumberSInt64Type    = 4
105kCFNumberFloat32Type   = 5
106kCFNumberFloat64Type   = 6
107kCFNumberCharType      = 7
108kCFNumberShortType     = 8
109kCFNumberIntType       = 9
110kCFNumberLongType      = 10
111kCFNumberLongLongType  = 11
112kCFNumberFloatType     = 12
113kCFNumberDoubleType    = 13
114kCFNumberCFIndexType   = 14
115kCFNumberNSIntegerType = 15
116kCFNumberCGFloatType   = 16
117kCFNumberMaxType       = 16
118
119def cfnumber_to_number(cfnumber):
120    """Convert CFNumber to python int or float."""
121    numeric_type = cf.CFNumberGetType(cfnumber)
122    cfnum_to_ctype = {kCFNumberSInt8Type:c_int8, kCFNumberSInt16Type:c_int16,
123                      kCFNumberSInt32Type:c_int32, kCFNumberSInt64Type:c_int64,
124                      kCFNumberFloat32Type:c_float, kCFNumberFloat64Type:c_double,
125                      kCFNumberCharType:c_byte, kCFNumberShortType:c_short,
126                      kCFNumberIntType:c_int, kCFNumberLongType:c_long,
127                      kCFNumberLongLongType:c_longlong, kCFNumberFloatType:c_float,
128                      kCFNumberDoubleType:c_double, kCFNumberCFIndexType:CFIndex,
129                      kCFNumberCGFloatType:CGFloat}
130
131    if numeric_type in cfnum_to_ctype:
132        t = cfnum_to_ctype[numeric_type]
133        result = t()
134        if cf.CFNumberGetValue(cfnumber, numeric_type, byref(result)):
135            return result.value
136    else:
137        raise Exception('cfnumber_to_number: unhandled CFNumber type %d' % numeric_type)
138
139# Dictionary of cftypes matched to the method converting them to python values.
140known_cftypes = { cf.CFStringGetTypeID() : cfstring_to_string,
141                  cf.CFNumberGetTypeID() : cfnumber_to_number
142                  }
143
144def cftype_to_value(cftype):
145    """Convert a CFType into an equivalent python type.
146    The convertible CFTypes are taken from the known_cftypes
147    dictionary, which may be added to if another library implements
148    its own conversion methods."""
149    if not cftype:
150        return None
151    typeID = cf.CFGetTypeID(cftype)
152    if typeID in known_cftypes:
153        convert_function = known_cftypes[typeID]
154        return convert_function(cftype)
155    else:
156        return cftype
157
158cf.CFSetGetCount.restype = CFIndex
159cf.CFSetGetCount.argtypes = [c_void_p]
160
161cf.CFSetGetValues.restype = None
162# PyPy 1.7 is fine with 2nd arg as POINTER(c_void_p),
163# but CPython ctypes 1.1.0 complains, so just use c_void_p.
164cf.CFSetGetValues.argtypes = [c_void_p, c_void_p]
165
166def cfset_to_set(cfset):
167    """Convert CFSet to python set."""
168    count = cf.CFSetGetCount(cfset)
169    buffer = (c_void_p * count)()
170    cf.CFSetGetValues(cfset, byref(buffer))
171    return set([ cftype_to_value(c_void_p(buffer[i])) for i in range(count) ])
172
173cf.CFArrayGetCount.restype = CFIndex
174cf.CFArrayGetCount.argtypes = [c_void_p]
175
176cf.CFArrayGetValueAtIndex.restype = c_void_p
177cf.CFArrayGetValueAtIndex.argtypes = [c_void_p, CFIndex]
178
179def cfarray_to_list(cfarray):
180    """Convert CFArray to python list."""
181    count = cf.CFArrayGetCount(cfarray)
182    return [ cftype_to_value(c_void_p(cf.CFArrayGetValueAtIndex(cfarray, i)))
183             for i in range(count) ]
184
185
186kCFRunLoopDefaultMode = c_void_p.in_dll(cf, 'kCFRunLoopDefaultMode')
187
188cf.CFRunLoopGetCurrent.restype = c_void_p
189cf.CFRunLoopGetCurrent.argtypes = []
190
191cf.CFRunLoopGetMain.restype = c_void_p
192cf.CFRunLoopGetMain.argtypes = []
193
194######################################################################
195
196# APPLICATION KIT
197
198# Even though we don't use this directly, it must be loaded so that
199# we can find the NSApplication, NSWindow, and NSView classes.
200lib = util.find_library('AppKit')
201
202# Hack for compatibility with macOS > 11.0
203if lib is None:
204    lib = '/System/Library/Frameworks/AppKit.framework/AppKit'
205
206appkit = cdll.LoadLibrary(lib)
207
208NSDefaultRunLoopMode = c_void_p.in_dll(appkit, 'NSDefaultRunLoopMode')
209NSEventTrackingRunLoopMode = c_void_p.in_dll(appkit, 'NSEventTrackingRunLoopMode')
210NSApplicationDidHideNotification = c_void_p.in_dll(appkit, 'NSApplicationDidHideNotification')
211NSApplicationDidUnhideNotification = c_void_p.in_dll(appkit, 'NSApplicationDidUnhideNotification')
212
213# /System/Library/Frameworks/AppKit.framework/Headers/NSEvent.h
214NSAnyEventMask = 0xFFFFFFFF     # NSUIntegerMax
215
216NSKeyDown            = 10
217NSKeyUp              = 11
218NSFlagsChanged       = 12
219NSApplicationDefined = 15
220
221NSAlphaShiftKeyMask         = 1 << 16
222NSShiftKeyMask              = 1 << 17
223NSControlKeyMask            = 1 << 18
224NSAlternateKeyMask          = 1 << 19
225NSCommandKeyMask            = 1 << 20
226NSNumericPadKeyMask         = 1 << 21
227NSHelpKeyMask               = 1 << 22
228NSFunctionKeyMask           = 1 << 23
229
230NSInsertFunctionKey   = 0xF727
231NSDeleteFunctionKey   = 0xF728
232NSHomeFunctionKey     = 0xF729
233NSBeginFunctionKey    = 0xF72A
234NSEndFunctionKey      = 0xF72B
235NSPageUpFunctionKey   = 0xF72C
236NSPageDownFunctionKey = 0xF72D
237
238# /System/Library/Frameworks/AppKit.framework/Headers/NSWindow.h
239NSBorderlessWindowMask		= 0
240NSTitledWindowMask		= 1 << 0
241NSClosableWindowMask		= 1 << 1
242NSMiniaturizableWindowMask	= 1 << 2
243NSResizableWindowMask		= 1 << 3
244
245# /System/Library/Frameworks/AppKit.framework/Headers/NSPanel.h
246NSUtilityWindowMask		= 1 << 4
247
248# /System/Library/Frameworks/AppKit.framework/Headers/NSGraphics.h
249NSBackingStoreRetained	        = 0
250NSBackingStoreNonretained	= 1
251NSBackingStoreBuffered	        = 2
252
253# /System/Library/Frameworks/AppKit.framework/Headers/NSTrackingArea.h
254NSTrackingMouseEnteredAndExited  = 0x01
255NSTrackingMouseMoved             = 0x02
256NSTrackingCursorUpdate 		 = 0x04
257NSTrackingActiveInActiveApp 	 = 0x40
258
259# /System/Library/Frameworks/AppKit.framework/Headers/NSOpenGL.h
260NSOpenGLPFAAllRenderers       =   1   # choose from all available renderers
261NSOpenGLPFADoubleBuffer       =   5   # choose a double buffered pixel format
262NSOpenGLPFAStereo             =   6   # stereo buffering supported
263NSOpenGLPFAAuxBuffers         =   7   # number of aux buffers
264NSOpenGLPFAColorSize          =   8   # number of color buffer bits
265NSOpenGLPFAAlphaSize          =  11   # number of alpha component bits
266NSOpenGLPFADepthSize          =  12   # number of depth buffer bits
267NSOpenGLPFAStencilSize        =  13   # number of stencil buffer bits
268NSOpenGLPFAAccumSize          =  14   # number of accum buffer bits
269NSOpenGLPFAMinimumPolicy      =  51   # never choose smaller buffers than requested
270NSOpenGLPFAMaximumPolicy      =  52   # choose largest buffers of type requested
271NSOpenGLPFAOffScreen          =  53   # choose an off-screen capable renderer
272NSOpenGLPFAFullScreen         =  54   # choose a full-screen capable renderer
273NSOpenGLPFASampleBuffers      =  55   # number of multi sample buffers
274NSOpenGLPFASamples            =  56   # number of samples per multi sample buffer
275NSOpenGLPFAAuxDepthStencil    =  57   # each aux buffer has its own depth stencil
276NSOpenGLPFAColorFloat         =  58   # color buffers store floating point pixels
277NSOpenGLPFAMultisample        =  59   # choose multisampling
278NSOpenGLPFASupersample        =  60   # choose supersampling
279NSOpenGLPFASampleAlpha        =  61   # request alpha filtering
280NSOpenGLPFARendererID         =  70   # request renderer by ID
281NSOpenGLPFASingleRenderer     =  71   # choose a single renderer for all screens
282NSOpenGLPFANoRecovery         =  72   # disable all failure recovery systems
283NSOpenGLPFAAccelerated        =  73   # choose a hardware accelerated renderer
284NSOpenGLPFAClosestPolicy      =  74   # choose the closest color buffer to request
285NSOpenGLPFARobust             =  75   # renderer does not need failure recovery
286NSOpenGLPFABackingStore       =  76   # back buffer contents are valid after swap
287NSOpenGLPFAMPSafe             =  78   # renderer is multi-processor safe
288NSOpenGLPFAWindow             =  80   # can be used to render to an onscreen window
289NSOpenGLPFAMultiScreen        =  81   # single window can span multiple screens
290NSOpenGLPFACompliant          =  83   # renderer is opengl compliant
291NSOpenGLPFAScreenMask         =  84   # bit mask of supported physical screens
292NSOpenGLPFAPixelBuffer        =  90   # can be used to render to a pbuffer
293NSOpenGLPFARemotePixelBuffer  =  91   # can be used to render offline to a pbuffer
294NSOpenGLPFAAllowOfflineRenderers = 96 # allow use of offline renderers
295NSOpenGLPFAAcceleratedCompute =  97   # choose a hardware accelerated compute device
296NSOpenGLPFAOpenGLProfile      =  99   # specify an OpenGL Profile to use
297NSOpenGLPFAVirtualScreenCount = 128   # number of virtual screens in this format
298
299NSOpenGLProfileVersionLegacy  = 0x1000    # choose a Legacy/Pre-OpenGL 3.0 Implementation
300NSOpenGLProfileVersion3_2Core = 0x3200    # choose an OpenGL 3.2 Core Implementation
301NSOpenGLProfileVersion4_1Core = 0x4100    # choose an OpenGL 4.1 Core Implementation
302
303NSOpenGLCPSwapInterval        = 222
304
305
306# /System/Library/Frameworks/ApplicationServices.framework/Frameworks/...
307#     CoreGraphics.framework/Headers/CGImage.h
308kCGImageAlphaNone                   = 0
309kCGImageAlphaPremultipliedLast      = 1
310kCGImageAlphaPremultipliedFirst     = 2
311kCGImageAlphaLast                   = 3
312kCGImageAlphaFirst                  = 4
313kCGImageAlphaNoneSkipLast           = 5
314kCGImageAlphaNoneSkipFirst          = 6
315kCGImageAlphaOnly                   = 7
316
317kCGImageAlphaPremultipliedLast = 1
318
319kCGBitmapAlphaInfoMask              = 0x1F
320kCGBitmapFloatComponents            = 1 << 8
321
322kCGBitmapByteOrderMask              = 0x7000
323kCGBitmapByteOrderDefault           = 0 << 12
324kCGBitmapByteOrder16Little          = 1 << 12
325kCGBitmapByteOrder32Little          = 2 << 12
326kCGBitmapByteOrder16Big             = 3 << 12
327kCGBitmapByteOrder32Big             = 4 << 12
328
329# NSApplication.h
330NSApplicationPresentationDefault = 0
331NSApplicationPresentationHideDock = 1 << 1
332NSApplicationPresentationHideMenuBar = 1 << 3
333NSApplicationPresentationDisableProcessSwitching = 1 << 5
334NSApplicationPresentationDisableHideApplication = 1 << 8
335
336# NSRunningApplication.h
337NSApplicationActivationPolicyRegular = 0
338NSApplicationActivationPolicyAccessory = 1
339NSApplicationActivationPolicyProhibited = 2
340
341######################################################################
342
343# QUARTZ / COREGRAPHICS
344lib = util.find_library('Quartz')
345
346# Hack for compatibility with macOS > 11.0
347if lib is None:
348    lib = '/System/Library/Frameworks/Quartz.framework/Quartz'
349
350quartz = cdll.LoadLibrary(lib)
351
352CGDirectDisplayID = c_uint32     # CGDirectDisplay.h
353CGError = c_int32                # CGError.h
354CGBitmapInfo = c_uint32          # CGImage.h
355
356# /System/Library/Frameworks/ApplicationServices.framework/Frameworks/...
357#     ImageIO.framework/Headers/CGImageProperties.h
358kCGImagePropertyGIFDictionary = c_void_p.in_dll(quartz, 'kCGImagePropertyGIFDictionary')
359kCGImagePropertyGIFDelayTime = c_void_p.in_dll(quartz, 'kCGImagePropertyGIFDelayTime')
360
361# /System/Library/Frameworks/ApplicationServices.framework/Frameworks/...
362#     CoreGraphics.framework/Headers/CGColorSpace.h
363kCGRenderingIntentDefault = 0
364
365quartz.CGDisplayIDToOpenGLDisplayMask.restype = c_uint32
366quartz.CGDisplayIDToOpenGLDisplayMask.argtypes = [c_uint32]
367
368quartz.CGMainDisplayID.restype = CGDirectDisplayID
369quartz.CGMainDisplayID.argtypes = []
370
371quartz.CGShieldingWindowLevel.restype = c_int32
372quartz.CGShieldingWindowLevel.argtypes = []
373
374quartz.CGCursorIsVisible.restype = c_bool
375
376quartz.CGDisplayCopyAllDisplayModes.restype = c_void_p
377quartz.CGDisplayCopyAllDisplayModes.argtypes = [CGDirectDisplayID, c_void_p]
378
379quartz.CGDisplaySetDisplayMode.restype = CGError
380quartz.CGDisplaySetDisplayMode.argtypes = [CGDirectDisplayID, c_void_p, c_void_p]
381
382quartz.CGDisplayCapture.restype = CGError
383quartz.CGDisplayCapture.argtypes = [CGDirectDisplayID]
384
385quartz.CGDisplayRelease.restype = CGError
386quartz.CGDisplayRelease.argtypes = [CGDirectDisplayID]
387
388quartz.CGDisplayCopyDisplayMode.restype = c_void_p
389quartz.CGDisplayCopyDisplayMode.argtypes = [CGDirectDisplayID]
390
391quartz.CGDisplayModeGetRefreshRate.restype = c_double
392quartz.CGDisplayModeGetRefreshRate.argtypes = [c_void_p]
393
394quartz.CGDisplayModeRetain.restype = c_void_p
395quartz.CGDisplayModeRetain.argtypes = [c_void_p]
396
397quartz.CGDisplayModeRelease.restype = None
398quartz.CGDisplayModeRelease.argtypes = [c_void_p]
399
400quartz.CGDisplayModeGetWidth.restype = c_size_t
401quartz.CGDisplayModeGetWidth.argtypes = [c_void_p]
402
403quartz.CGDisplayModeGetHeight.restype = c_size_t
404quartz.CGDisplayModeGetHeight.argtypes = [c_void_p]
405
406quartz.CGDisplayModeCopyPixelEncoding.restype = c_void_p
407quartz.CGDisplayModeCopyPixelEncoding.argtypes = [c_void_p]
408
409quartz.CGGetActiveDisplayList.restype = CGError
410quartz.CGGetActiveDisplayList.argtypes = [c_uint32, POINTER(CGDirectDisplayID), POINTER(c_uint32)]
411
412quartz.CGDisplayBounds.restype = CGRect
413quartz.CGDisplayBounds.argtypes = [CGDirectDisplayID]
414
415quartz.CGImageSourceCreateWithData.restype = c_void_p
416quartz.CGImageSourceCreateWithData.argtypes = [c_void_p, c_void_p]
417
418quartz.CGImageSourceCreateImageAtIndex.restype = c_void_p
419quartz.CGImageSourceCreateImageAtIndex.argtypes = [c_void_p, c_size_t, c_void_p]
420
421quartz.CGImageSourceCopyPropertiesAtIndex.restype = c_void_p
422quartz.CGImageSourceCopyPropertiesAtIndex.argtypes = [c_void_p, c_size_t, c_void_p]
423
424quartz.CGImageGetDataProvider.restype = c_void_p
425quartz.CGImageGetDataProvider.argtypes = [c_void_p]
426
427quartz.CGDataProviderCopyData.restype = c_void_p
428quartz.CGDataProviderCopyData.argtypes = [c_void_p]
429
430quartz.CGDataProviderCreateWithCFData.restype = c_void_p
431quartz.CGDataProviderCreateWithCFData.argtypes = [c_void_p]
432
433quartz.CGImageCreate.restype = c_void_p
434quartz.CGImageCreate.argtypes = [c_size_t, c_size_t, c_size_t, c_size_t, c_size_t, c_void_p, c_uint32, c_void_p, c_void_p, c_bool, c_int]
435
436quartz.CGImageRelease.restype = None
437quartz.CGImageRelease.argtypes = [c_void_p]
438
439quartz.CGImageGetBytesPerRow.restype = c_size_t
440quartz.CGImageGetBytesPerRow.argtypes = [c_void_p]
441
442quartz.CGImageGetWidth.restype = c_size_t
443quartz.CGImageGetWidth.argtypes = [c_void_p]
444
445quartz.CGImageGetHeight.restype = c_size_t
446quartz.CGImageGetHeight.argtypes = [c_void_p]
447
448quartz.CGImageGetBitsPerPixel.restype = c_size_t
449quartz.CGImageGetBitsPerPixel.argtypes = [c_void_p]
450
451quartz.CGImageGetBitmapInfo.restype = CGBitmapInfo
452quartz.CGImageGetBitmapInfo.argtypes = [c_void_p]
453
454quartz.CGColorSpaceCreateDeviceRGB.restype = c_void_p
455quartz.CGColorSpaceCreateDeviceRGB.argtypes = []
456
457quartz.CGDataProviderRelease.restype = None
458quartz.CGDataProviderRelease.argtypes = [c_void_p]
459
460quartz.CGColorSpaceRelease.restype = None
461quartz.CGColorSpaceRelease.argtypes = [c_void_p]
462
463quartz.CGWarpMouseCursorPosition.restype = CGError
464quartz.CGWarpMouseCursorPosition.argtypes = [CGPoint]
465
466quartz.CGDisplayMoveCursorToPoint.restype = CGError
467quartz.CGDisplayMoveCursorToPoint.argtypes = [CGDirectDisplayID, CGPoint]
468
469quartz.CGAssociateMouseAndMouseCursorPosition.restype = CGError
470quartz.CGAssociateMouseAndMouseCursorPosition.argtypes = [c_bool]
471
472quartz.CGBitmapContextCreate.restype = c_void_p
473quartz.CGBitmapContextCreate.argtypes = [c_void_p, c_size_t, c_size_t, c_size_t, c_size_t, c_void_p, CGBitmapInfo]
474
475quartz.CGBitmapContextCreateImage.restype = c_void_p
476quartz.CGBitmapContextCreateImage.argtypes = [c_void_p]
477
478quartz.CGFontCreateWithDataProvider.restype = c_void_p
479quartz.CGFontCreateWithDataProvider.argtypes = [c_void_p]
480
481quartz.CGFontCreateWithFontName.restype = c_void_p
482quartz.CGFontCreateWithFontName.argtypes = [c_void_p]
483
484quartz.CGContextDrawImage.restype = None
485quartz.CGContextDrawImage.argtypes = [c_void_p, CGRect, c_void_p]
486
487quartz.CGContextRelease.restype = None
488quartz.CGContextRelease.argtypes = [c_void_p]
489
490quartz.CGContextSetTextPosition.restype = None
491quartz.CGContextSetTextPosition.argtypes = [c_void_p, CGFloat, CGFloat]
492
493quartz.CGContextSetShouldAntialias.restype = None
494quartz.CGContextSetShouldAntialias.argtypes = [c_void_p, c_bool]
495
496######################################################################
497
498# CORETEXT
499lib = util.find_library('CoreText')
500
501# Hack for compatibility with macOS > 11.0
502if lib is None:
503    lib = '/System/Library/Frameworks/CoreText.framework/CoreText'
504
505ct = cdll.LoadLibrary(lib)
506
507# Types
508CTFontOrientation = c_uint32      # CTFontDescriptor.h
509CTFontSymbolicTraits = c_uint32   # CTFontTraits.h
510
511# CoreText constants
512kCTFontAttributeName = c_void_p.in_dll(ct, 'kCTFontAttributeName')
513kCTFontFamilyNameAttribute = c_void_p.in_dll(ct, 'kCTFontFamilyNameAttribute')
514kCTFontSymbolicTrait = c_void_p.in_dll(ct, 'kCTFontSymbolicTrait')
515kCTFontWeightTrait = c_void_p.in_dll(ct, 'kCTFontWeightTrait')
516kCTFontTraitsAttribute = c_void_p.in_dll(ct, 'kCTFontTraitsAttribute')
517
518# constants from CTFontTraits.h
519kCTFontItalicTrait = (1 << 0)
520kCTFontBoldTrait   = (1 << 1)
521
522ct.CTLineCreateWithAttributedString.restype = c_void_p
523ct.CTLineCreateWithAttributedString.argtypes = [c_void_p]
524
525ct.CTLineDraw.restype = None
526ct.CTLineDraw.argtypes = [c_void_p, c_void_p]
527
528ct.CTFontGetBoundingRectsForGlyphs.restype = CGRect
529ct.CTFontGetBoundingRectsForGlyphs.argtypes = [c_void_p, CTFontOrientation, POINTER(CGGlyph), POINTER(CGRect), CFIndex]
530
531ct.CTFontGetAdvancesForGlyphs.restype = c_double
532ct.CTFontGetAdvancesForGlyphs.argtypes = [c_void_p, CTFontOrientation, POINTER(CGGlyph), POINTER(CGSize), CFIndex]
533
534ct.CTFontGetAscent.restype = CGFloat
535ct.CTFontGetAscent.argtypes = [c_void_p]
536
537ct.CTFontGetDescent.restype = CGFloat
538ct.CTFontGetDescent.argtypes = [c_void_p]
539
540ct.CTFontGetSymbolicTraits.restype = CTFontSymbolicTraits
541ct.CTFontGetSymbolicTraits.argtypes = [c_void_p]
542
543ct.CTFontGetGlyphsForCharacters.restype = c_bool
544ct.CTFontGetGlyphsForCharacters.argtypes = [c_void_p, POINTER(UniChar), POINTER(CGGlyph), CFIndex]
545
546ct.CTFontCreateWithGraphicsFont.restype = c_void_p
547ct.CTFontCreateWithGraphicsFont.argtypes = [c_void_p, CGFloat, c_void_p, c_void_p]
548
549ct.CTFontCopyFamilyName.restype = c_void_p
550ct.CTFontCopyFamilyName.argtypes = [c_void_p]
551
552ct.CTFontCopyFullName.restype = c_void_p
553ct.CTFontCopyFullName.argtypes = [c_void_p]
554
555ct.CTFontCreateWithFontDescriptor.restype = c_void_p
556ct.CTFontCreateWithFontDescriptor.argtypes = [c_void_p, CGFloat, c_void_p]
557
558ct.CTFontDescriptorCreateWithAttributes.restype = c_void_p
559ct.CTFontDescriptorCreateWithAttributes.argtypes = [c_void_p]
560
561######################################################################
562
563# FOUNDATION
564lib = util.find_library('Foundation')
565
566# Hack for compatibility with macOS > 11.0
567if lib is None:
568    lib = '/System/Library/Frameworks/Foundation.framework/Foundation'
569
570foundation = cdll.LoadLibrary(lib)
571
572foundation.NSMouseInRect.restype = c_bool
573foundation.NSMouseInRect.argtypes = [NSPoint, NSRect, c_bool]
574