File: AddPrivateFont.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (110 lines) | stat: -rw-r--r-- 3,476 bytes parent folder | download | duplicates (2)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python

import os
import sys

try:
    gFileDir = os.path.dirname(os.path.abspath(__file__))
except:
    gFileDir = os.path.dirname(os.path.abspath(sys.argv[0]))
gDataDir = os.path.join(gFileDir, 'data')

import wx


FILENAME = 'SourceCodePro-Regular.ttf'

SAMPLETEXT = """
SourceCodePro-Regular.ttf

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

abcdefghijklmnopqrstyvwxyz
ABCDEFGHIJKLMNOPQRSTYVWXYZ
1234567890.:,:\'"([{<\\|/>}])
`~!@#$%^&*_+-=?

Pangrams
The Quick Brown Fox Jumps Over The Lazy Dog.
Grumpy wizards make toxic brew for the evil queen and jack!
"""

#----------------------------------------------------------------------

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, -1)

        # Show how to add a private font to the application at runtime that
        # doesn't have to be installed on the user's operating system.
        filename = os.path.join(gDataDir, 'SourceCodePro-Regular.ttf')
        wx.Font.AddPrivateFont(filename)

        text1 = "The font used in the text below was dynamically loaded from\n{}.".format(filename)
        st1 = wx.StaticText(self, -1, text1, (15, 15))

        st2 = wx.StaticText(self, -1, SAMPLETEXT, (15, 42))
        f = wx.Font(pointSize=12,
                    family=wx.FONTFAMILY_DEFAULT,
                    style=wx.FONTSTYLE_NORMAL,
                    weight=wx.FONTWEIGHT_NORMAL,
                    underline=False,
                    faceName="Source Code Pro",
                    encoding=wx.FONTENCODING_DEFAULT)
        st2.SetFont(f)


        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(st1, 0, wx.ALL, 10)
        sizer.Add(wx.StaticLine(self, style=wx.HORIZONTAL), 0, wx.EXPAND | wx.ALL, 10)
        sizer.Add(st2, 0, wx.ALL, 10)
        self.SetSizer(sizer)
        self.Layout()


#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win

#----------------------------------------------------------------------



overview = """
This method can be used to allow this application to use the font from the
given file even if it is not globally installed on the system.

Under OS X this method actually doesn't do anything other than check for the
existence of the file in the "Fonts" subdirectory of the application bundle
"Resources" directory. You are responsible for actually making the font file
available in this directory and setting ATSApplicationFontsPath to Fonts value
in your Info.plist file. See also wx.StandardPaths.GetResourcesDir .

Under MSW this method must be called before any wx.GraphicsContext objects
have been created, otherwise the private font won't be usable from them.

Under Unix this method requires Pango 1.38 or later and will return False and
log an error message explaining the problem if this requirement is not
satisfied either at compile- or run-time.

Currently this method is implemented for all major platforms (subject to
having Pango 1.38 or later when running configure under Unix) and
USE_PRIVATE_FONTS is always set to 0 under the other platforms, making
this function unavailable at compile-time.

Parameters: filename (string) -
Return type: bool
Returns: True if the font was added and can now be used.

New in version wxWidgets 3.1.1.

"""


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])