fetch up and clean up pyglet

This commit is contained in:
shenjack 2023-03-10 21:04:54 +08:00
parent cb719876d6
commit 0ace5dd225
3 changed files with 33 additions and 9 deletions

View File

@ -2,12 +2,13 @@
import math import math
import warnings 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 from pyglet.font import base
import pyglet.image 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 cf = cocoapy.cf
ct = cocoapy.ct ct = cocoapy.ct
@ -183,6 +184,7 @@ class QuartzFont(base.Font):
traits |= cocoapy.kCTFontItalicTrait traits |= cocoapy.kCTFontItalicTrait
name = str(name) name = str(name)
self.traits = traits
# First see if we can find an appropriate font from our table of loaded fonts. # 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) cgFont = self._lookup_font_with_family_and_traits(name, traits)
if cgFont: 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. # Create a font descriptor for given name and traits and use it to create font.
descriptor = self._create_font_descriptor(name, traits) descriptor = self._create_font_descriptor(name, traits)
self.ctFont = c_void_p(ct.CTFontCreateWithFontDescriptor(descriptor, size, None)) self.ctFont = c_void_p(ct.CTFontCreateWithFontDescriptor(descriptor, size, None))
cf.CFRelease(descriptor) cf.CFRelease(descriptor)
assert self.ctFont, "Couldn't load font: " + name 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.ascent = int(math.ceil(ct.CTFontGetAscent(self.ctFont)))
self.descent = -int(math.ceil(ct.CTFontGetDescent(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 @property
def name(self): def name(self):
return self._family_name return self._family_name

View File

@ -570,6 +570,11 @@ ct.CTFontCreateWithFontDescriptor.argtypes = [c_void_p, CGFloat, c_void_p]
ct.CTFontDescriptorCreateWithAttributes.restype = c_void_p ct.CTFontDescriptorCreateWithAttributes.restype = c_void_p
ct.CTFontDescriptorCreateWithAttributes.argtypes = [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 # FOUNDATION

View File

@ -415,6 +415,15 @@ objc.sel_isEqual.argtypes = [c_void_p, c_void_p]
objc.sel_registerName.restype = c_void_p objc.sel_registerName.restype = c_void_p
objc.sel_registerName.argtypes = [c_char_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) # void *objc_autoreleasePoolPush(void)
@ -766,6 +775,7 @@ class ObjCMethod:
# print('no argtypes encoding for %s (%s)' % (self.name, self.argument_types)) # print('no argtypes encoding for %s (%s)' % (self.name, self.argument_types))
self.argtypes = None self.argtypes = None
# Get types for the return type. # Get types for the return type.
try: try:
if self.return_type == b'@': if self.return_type == b'@':
self.restype = ObjCInstance self.restype = ObjCInstance
@ -842,8 +852,6 @@ class ObjCMethod:
result = ObjCInstance(result) result = ObjCInstance(result)
elif self.restype == ObjCClass: elif self.restype == ObjCClass:
result = ObjCClass(result) result = ObjCClass(result)
# if result != None:
# print("result2", self, result, self.restype)
return result return result
except ArgumentError as error: except ArgumentError as error:
# Add more useful info to argument error exceptions, then reraise. # Add more useful info to argument error exceptions, then reraise.
@ -1294,8 +1302,6 @@ class ObjCSubclass:
result = result.ptr.value result = result.ptr.value
elif isinstance(result, ObjCInstance): elif isinstance(result, ObjCInstance):
result = result.ptr.value result = result.ptr.value
print("objc_class_method", py_cls)
return result return result
name = f.__name__.replace('_', ':') 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) objc_ptr = get_instance_variable(objc_obs, 'observed_object', c_void_p)
if objc_ptr: if objc_ptr:
objc.objc_setAssociatedObject(objc_ptr, objc_obs, None, OBJC_ASSOCIATION_ASSIGN) 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) 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. 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. 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()): for cobjc_ptr in list(ObjCInstance._cached_objects.keys()):
cobjc_i = ObjCInstance._cached_objects[cobjc_ptr] cobjc_i = ObjCInstance._cached_objects[cobjc_ptr]
if cobjc_i.retained is False and cobjc_i.pool == pool_id: if cobjc_i.retained is False and cobjc_i.pool == pool_id: