tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mac_register_font.py (2245B)


      1 #!/usr/bin/python
      2 # This Source Code Form is subject to the terms of the Mozilla Public
      3 # License, v. 2.0. If a copy of the MPL was not distributed with this
      4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 """
      7 mac_register_font.py
      8 
      9 Mac-specific utility command to register a font file with the OS.
     10 """
     11 
     12 import argparse
     13 import sys
     14 
     15 import Cocoa
     16 import CoreText
     17 
     18 
     19 def main():
     20    parser = argparse.ArgumentParser()
     21    parser.add_argument(
     22        "-v",
     23        "--verbose",
     24        action="store_true",
     25        help="print verbose registration failures",
     26        default=False,
     27    )
     28    parser.add_argument(
     29        "file", nargs="*", help="font file to register or unregister", default=[]
     30    )
     31    parser.add_argument(
     32        "-u",
     33        "--unregister",
     34        action="store_true",
     35        help="unregister the provided fonts",
     36        default=False,
     37    )
     38    parser.add_argument(
     39        "-p",
     40        "--persist-user",
     41        action="store_true",
     42        help="permanently register the font",
     43        default=False,
     44    )
     45 
     46    args = parser.parse_args()
     47 
     48    if args.persist_user:
     49        scope = CoreText.kCTFontManagerScopeUser
     50        scopeDesc = "user"
     51    else:
     52        scope = CoreText.kCTFontManagerScopeSession
     53        scopeDesc = "session"
     54 
     55    failureCount = 0
     56    for fontPath in args.file:
     57        fontURL = Cocoa.NSURL.fileURLWithPath_(fontPath)
     58        (result, error) = register_or_unregister_font(fontURL, args.unregister, scope)
     59        if result:
     60            print(
     61                "%sregistered font %s with %s scope"
     62                % (("un" if args.unregister else ""), fontPath, scopeDesc)
     63            )
     64        else:
     65            print(
     66                "Failed to %sregister font %s with %s scope"
     67                % (("un" if args.unregister else ""), fontPath, scopeDesc)
     68            )
     69            if args.verbose:
     70                print(error)
     71            failureCount += 1
     72 
     73    sys.exit(failureCount)
     74 
     75 
     76 def register_or_unregister_font(fontURL, unregister, scope):
     77    return (
     78        CoreText.CTFontManagerUnregisterFontsForURL(fontURL, scope, None)
     79        if unregister
     80        else CoreText.CTFontManagerRegisterFontsForURL(fontURL, scope, None)
     81    )
     82 
     83 
     84 if __name__ == "__main__":
     85    main()