summaryrefslogtreecommitdiffstats
path: root/security/sandbox/test/mac_register_font.py
blob: 549becf565afe0302dca4a57f78defe963543b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"""
mac_register_font.py

Mac-specific utility command to register a font file with the OS.
"""

import argparse
import sys

import Cocoa
import CoreText


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="print verbose registration failures",
        default=False,
    )
    parser.add_argument(
        "file", nargs="*", help="font file to register or unregister", default=[]
    )
    parser.add_argument(
        "-u",
        "--unregister",
        action="store_true",
        help="unregister the provided fonts",
        default=False,
    )
    parser.add_argument(
        "-p",
        "--persist-user",
        action="store_true",
        help="permanently register the font",
        default=False,
    )

    args = parser.parse_args()

    if args.persist_user:
        scope = CoreText.kCTFontManagerScopeUser
        scopeDesc = "user"
    else:
        scope = CoreText.kCTFontManagerScopeSession
        scopeDesc = "session"

    failureCount = 0
    for fontPath in args.file:
        fontURL = Cocoa.NSURL.fileURLWithPath_(fontPath)
        (result, error) = register_or_unregister_font(fontURL, args.unregister, scope)
        if result:
            print(
                "%sregistered font %s with %s scope"
                % (("un" if args.unregister else ""), fontPath, scopeDesc)
            )
        else:
            print(
                "Failed to %sregister font %s with %s scope"
                % (("un" if args.unregister else ""), fontPath, scopeDesc)
            )
            if args.verbose:
                print(error)
            failureCount += 1

    sys.exit(failureCount)


def register_or_unregister_font(fontURL, unregister, scope):
    return (
        CoreText.CTFontManagerUnregisterFontsForURL(fontURL, scope, None)
        if unregister
        else CoreText.CTFontManagerRegisterFontsForURL(fontURL, scope, None)
    )


if __name__ == "__main__":
    main()