|
| 1 | +# https://gist.github.com/lukaskollmer/0c01b7a27e512db480847fac2cc54103 |
| 2 | + |
| 3 | +# in association with script custom-editor-font |
| 4 | + |
| 5 | +# https://forum.omz-software.com/topic/3419/share-code-custom-editor-font |
| 6 | + |
| 7 | +""" |
| 8 | +Use custom fonts in the editor |
| 9 | +
|
| 10 | +Usage: https://forum.omz-software.com/topic/3419/share-code-custom-editor-font |
| 11 | +""" |
| 12 | + |
| 13 | +__author__ = "Lukas Kollmer<[email protected]>" |
| 14 | +__copyright__ = "Copyright (c) 2016 Lukas Kollmer<[email protected]>" |
| 15 | + |
| 16 | +from objc_util import * |
| 17 | +from ctypes import * |
| 18 | + |
| 19 | + |
| 20 | +CTFontManagerRegisterFontsForURL = c.CTFontManagerRegisterFontsForURL |
| 21 | +CTFontManagerRegisterFontsForURL.argtypes = [c_void_p, c_int, c_void_p] |
| 22 | +CTFontManagerRegisterFontsForURL.restype = c_bool |
| 23 | + |
| 24 | +CFURLCreateWithString = c.CFURLCreateWithString |
| 25 | +CFURLCreateWithString.argtypes = [c_void_p, c_void_p, c_void_p] |
| 26 | +CFURLCreateWithString.restype = c_void_p |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +def load_custom_font(file): |
| 31 | + #https://marco.org/2012/12/21/ios-dynamic-font-loading |
| 32 | + font_url = CFURLCreateWithString(None, ns(file), None) |
| 33 | + |
| 34 | + error = c_void_p(None) |
| 35 | + # This returns false, but still succeds |
| 36 | + CTFontManagerRegisterFontsForURL(ObjCInstance(font_url), 0, byref(error)) |
| 37 | + |
| 38 | +def set_editor_font(name, size=15): |
| 39 | + """ |
| 40 | + NOTE: This still requires a restart |
| 41 | + """ |
| 42 | + defaults = ObjCClass("NSUserDefaults").standardUserDefaults() |
| 43 | + defaults.setObject_forKey_(name, "EditorFontName") |
| 44 | + defaults.setObject_forKey_(size, "EditorFontSize") |
| 45 | + |
| 46 | + defaults.synchronize() |
| 47 | + |
| 48 | +def reset_custom_font(): |
| 49 | + defaults = ObjCClass("NSUserDefaults").standardUserDefaults() |
| 50 | + defaults.setObject_forKey_("Menlo-Regular", "EditorFontName") |
| 51 | + defaults.setObject_forKey_(15, "EditorFontSize") |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + import os |
| 56 | + font_path = "file://" + os.path.expanduser("~/Documents/Fonts/SFMono/SFMono-Regular.otf") |
| 57 | + |
| 58 | + load_custom_font(font_path) |
| 59 | + |
| 60 | + font_name = "Menlo-Regular" |
| 61 | + set_editor_font(font_name, 15) |
| 62 | + |
0 commit comments