Function to get all fonts on macOS
This commit is contained in:
parent
ddcd4daede
commit
58644e2b37
15
kitty/core_text.h
Normal file
15
kitty/core_text.h
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define UNUSED __attribute__ ((unused))
|
||||
|
||||
int init_CoreText(PyObject *);
|
||||
PyObject* coretext_all_fonts(PyObject UNUSED *self);
|
||||
|
||||
#define CORE_TEXT_FUNC_WRAPPERS \
|
||||
{"coretext_all_fonts", (PyCFunction)coretext_all_fonts, METH_NOARGS, ""},
|
||||
@ -11,6 +11,8 @@
|
||||
#include <math.h>
|
||||
#import <CoreGraphics/CGBitmapContext.h>
|
||||
#import <CoreText/CTFont.h>
|
||||
#include <Foundation/Foundation.h>
|
||||
#include <CoreText/CoreText.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSDictionary.h>
|
||||
|
||||
@ -28,11 +30,61 @@ static PyObject*
|
||||
convert_cfstring(CFStringRef src) {
|
||||
#define SZ 2048
|
||||
static char buf[SZ+2] = {0};
|
||||
const char *p = CFStringGetCStringPtr(src, kCFStringEncodingUTF8);
|
||||
if (p != NULL) return PyUnicode_FromString(buf);
|
||||
if(!CFStringGetCString(src, buf, SZ, kCFStringEncodingUTF8)) { PyErr_SetString(PyExc_ValueError, "Failed to convert CFString"); return NULL; }
|
||||
return PyUnicode_FromString(buf);
|
||||
#undef SZ
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
font_descriptor_to_python(CTFontDescriptorRef descriptor) {
|
||||
NSURL *url = (NSURL *) CTFontDescriptorCopyAttribute(descriptor, kCTFontURLAttribute);
|
||||
NSString *psName = (NSString *) CTFontDescriptorCopyAttribute(descriptor, kCTFontNameAttribute);
|
||||
NSString *family = (NSString *) CTFontDescriptorCopyAttribute(descriptor, kCTFontFamilyNameAttribute);
|
||||
NSString *style = (NSString *) CTFontDescriptorCopyAttribute(descriptor, kCTFontStyleNameAttribute);
|
||||
NSDictionary *traits = (NSDictionary *) CTFontDescriptorCopyAttribute(descriptor, kCTFontTraitsAttribute);
|
||||
unsigned int straits = [traits[(id)kCTFontSymbolicTrait] unsignedIntValue];
|
||||
NSNumber *weightVal = traits[(id)kCTFontWeightTrait];
|
||||
NSNumber *widthVal = traits[(id)kCTFontWidthTrait];
|
||||
|
||||
PyObject *ans = Py_BuildValue("{ssssssss sOsOsO sfsfsI}",
|
||||
"path", [[url path] UTF8String],
|
||||
"postscript_name", [psName UTF8String],
|
||||
"family", [family UTF8String],
|
||||
"style", [style UTF8String],
|
||||
|
||||
"bold", (straits & kCTFontBoldTrait) != 0 ? Py_True : Py_False,
|
||||
"italic", (straits & kCTFontItalicTrait) != 0 ? Py_True : Py_False,
|
||||
"monospace", (straits & kCTFontMonoSpaceTrait) != 0 ? Py_True : Py_False,
|
||||
|
||||
"weight", [weightVal floatValue],
|
||||
"width", [widthVal floatValue],
|
||||
"traits", straits
|
||||
);
|
||||
[url release];
|
||||
[psName release];
|
||||
[family release];
|
||||
[style release];
|
||||
[traits release];
|
||||
return ans;
|
||||
}
|
||||
|
||||
PyObject*
|
||||
coretext_all_fonts(PyObject UNUSED *_self) {
|
||||
static CTFontCollectionRef collection = NULL;
|
||||
if (collection == NULL) collection = CTFontCollectionCreateFromAvailableFonts(NULL);
|
||||
NSArray *matches = (NSArray *) CTFontCollectionCreateMatchingFontDescriptors(collection);
|
||||
PyObject *ans = PyTuple_New([matches count]), *temp;
|
||||
if (ans == NULL) return PyErr_NoMemory();
|
||||
for (unsigned int i = 0; i < [matches count]; i++) {
|
||||
temp = font_descriptor_to_python((CTFontDescriptorRef) matches[i]);
|
||||
if (temp == NULL) { Py_DECREF(ans); return NULL; }
|
||||
PyTuple_SET_ITEM(ans, i, temp); temp = NULL;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
static PyObject*
|
||||
new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
|
||||
|
||||
@ -41,6 +41,9 @@ redirect_std_streams(PyObject UNUSED *self, PyObject *args) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include "core_text.h"
|
||||
#endif
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
GL_METHODS
|
||||
@ -52,7 +55,9 @@ static PyMethodDef module_methods[] = {
|
||||
{"redirect_std_streams", (PyCFunction)redirect_std_streams, METH_VARARGS, ""},
|
||||
{"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""},
|
||||
{"change_wcwidth", (PyCFunction)change_wcwidth_wrap, METH_O, ""},
|
||||
#ifndef __APPLE__
|
||||
#ifdef __APPLE__
|
||||
CORE_TEXT_FUNC_WRAPPERS
|
||||
#else
|
||||
{"get_fontconfig_font", (PyCFunction)get_fontconfig_font, METH_VARARGS, ""},
|
||||
#endif
|
||||
GLFW_FUNC_WRAPPERS
|
||||
|
||||
@ -319,7 +319,6 @@ int init_ChangeTracker(PyObject *);
|
||||
int init_Screen(PyObject *);
|
||||
int init_Face(PyObject *);
|
||||
int init_Window(PyObject *);
|
||||
int init_CoreText(PyObject *);
|
||||
PyObject* create_256_color_table();
|
||||
PyObject* read_bytes_dump(PyObject UNUSED *, PyObject *);
|
||||
PyObject* read_bytes(PyObject UNUSED *, PyObject *);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user