From 0ace5dd225a6ab9375cf6e2e1bfa7041785b857d Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 10 Mar 2023 21:04:54 +0800 Subject: [PATCH] fetch up and clean up pyglet --- libs/pyglet/font/quartz.py | 20 +++++++++++++++++--- libs/pyglet/libs/darwin/cocoapy/cocoalibs.py | 5 +++++ libs/pyglet/libs/darwin/cocoapy/runtime.py | 17 +++++++++++------ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/libs/pyglet/font/quartz.py b/libs/pyglet/font/quartz.py index 26b9d96..c0a60e4 100644 --- a/libs/pyglet/font/quartz.py +++ b/libs/pyglet/font/quartz.py @@ -2,12 +2,13 @@ import math import warnings -from ctypes import c_void_p, c_int32, byref, c_byte +from ctypes import c_void_p, c_int32, byref, c_byte, c_buffer from pyglet.font import base import pyglet.image -from pyglet.libs.darwin import cocoapy +from pyglet.libs.darwin import cocoapy, kCTFontURLAttribute, kCFStringEncodingUTF8 +from pyglet.window.cocoa.pyglet_view import NSURL cf = cocoapy.cf ct = cocoapy.ct @@ -183,6 +184,7 @@ class QuartzFont(base.Font): traits |= cocoapy.kCTFontItalicTrait name = str(name) + self.traits = traits # First see if we can find an appropriate font from our table of loaded fonts. cgFont = self._lookup_font_with_family_and_traits(name, traits) if cgFont: @@ -192,7 +194,6 @@ class QuartzFont(base.Font): # Create a font descriptor for given name and traits and use it to create font. descriptor = self._create_font_descriptor(name, traits) self.ctFont = c_void_p(ct.CTFontCreateWithFontDescriptor(descriptor, size, None)) - cf.CFRelease(descriptor) assert self.ctFont, "Couldn't load font: " + name @@ -203,6 +204,19 @@ class QuartzFont(base.Font): self.ascent = int(math.ceil(ct.CTFontGetAscent(self.ctFont))) self.descent = -int(math.ceil(ct.CTFontGetDescent(self.ctFont))) + @property + def filename(self): + descriptor = self._create_font_descriptor(self.name, self.traits) + ref = c_void_p(ct.CTFontDescriptorCopyAttribute(descriptor, kCTFontURLAttribute)) + if ref: + url = cocoapy.ObjCInstance(ref, cache=False) # NSURL + filepath = url.fileSystemRepresentation().decode() + cf.CFRelease(ref) + return filepath + + cf.CFRelease(descriptor) + return 'Unknown' + @property def name(self): return self._family_name diff --git a/libs/pyglet/libs/darwin/cocoapy/cocoalibs.py b/libs/pyglet/libs/darwin/cocoapy/cocoalibs.py index b79c695..f9ef2fc 100644 --- a/libs/pyglet/libs/darwin/cocoapy/cocoalibs.py +++ b/libs/pyglet/libs/darwin/cocoapy/cocoalibs.py @@ -570,6 +570,11 @@ ct.CTFontCreateWithFontDescriptor.argtypes = [c_void_p, CGFloat, c_void_p] ct.CTFontDescriptorCreateWithAttributes.restype = c_void_p ct.CTFontDescriptorCreateWithAttributes.argtypes = [c_void_p] +ct.CTFontDescriptorCopyAttribute.restype = c_void_p +ct.CTFontDescriptorCopyAttribute.argtypes = [c_void_p, CFStringRef] + +kCTFontURLAttribute = c_void_p.in_dll(ct, 'kCTFontURLAttribute') + ###################################################################### # FOUNDATION diff --git a/libs/pyglet/libs/darwin/cocoapy/runtime.py b/libs/pyglet/libs/darwin/cocoapy/runtime.py index ade2f72..88dd6ff 100644 --- a/libs/pyglet/libs/darwin/cocoapy/runtime.py +++ b/libs/pyglet/libs/darwin/cocoapy/runtime.py @@ -415,6 +415,15 @@ objc.sel_isEqual.argtypes = [c_void_p, c_void_p] objc.sel_registerName.restype = c_void_p objc.sel_registerName.argtypes = [c_char_p] +###################################################################### +# void *objc_autoreleasePoolPush(void) +objc.objc_autoreleasePoolPush.restype = c_void_p +objc.objc_autoreleasePoolPush.argtypes = [] + +# void objc_autoreleasePoolPop(void *pool) +objc.objc_autoreleasePoolPop.restype = None +objc.objc_autoreleasePoolPop.argtypes = [c_void_p] + ###################################################################### # void *objc_autoreleasePoolPush(void) @@ -766,6 +775,7 @@ class ObjCMethod: # print('no argtypes encoding for %s (%s)' % (self.name, self.argument_types)) self.argtypes = None # Get types for the return type. + try: if self.return_type == b'@': self.restype = ObjCInstance @@ -842,8 +852,6 @@ class ObjCMethod: result = ObjCInstance(result) elif self.restype == ObjCClass: result = ObjCClass(result) - # if result != None: - # print("result2", self, result, self.restype) return result except ArgumentError as error: # Add more useful info to argument error exceptions, then reraise. @@ -1294,8 +1302,6 @@ class ObjCSubclass: result = result.ptr.value elif isinstance(result, ObjCInstance): result = result.ptr.value - - print("objc_class_method", py_cls) return result name = f.__name__.replace('_', ':') @@ -1352,7 +1358,7 @@ def _obj_observer_dealloc(objc_obs, selector_name): objc_ptr = get_instance_variable(objc_obs, 'observed_object', c_void_p) if objc_ptr: objc.objc_setAssociatedObject(objc_ptr, objc_obs, None, OBJC_ASSOCIATION_ASSIGN) - objc_i = ObjCInstance._cached_objects.pop(objc_ptr, None) + ObjCInstance._cached_objects.pop(objc_ptr, None) send_super(objc_obs, selector_name) @@ -1368,7 +1374,6 @@ def _clear_arp_objects(pool_id): 2) Some objects such as ObjCSubclass's must be retained. 3) When a pool is drained and dealloc'd, clear all ObjCInstances in that pool that are not retained. """ - #if objc_i.objc_class.name == b"NSAutoreleasePool": for cobjc_ptr in list(ObjCInstance._cached_objects.keys()): cobjc_i = ObjCInstance._cached_objects[cobjc_ptr] if cobjc_i.retained is False and cobjc_i.pool == pool_id: