summaryrefslogtreecommitdiff
path: root/third_party/freetype-py/examples/ftdump.py
blob: 13662aa471933d9989dab12e759893ae32a4df15 (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
#  FreeType high-level python API - Copyright 2011 Nicolas P. Rougier
#  Distributed under the terms of the new BSD license.
# -----------------------------------------------------------------------------
from __future__ import print_function
from __future__ import division
import sys
from freetype import *

verbose     = 0
debug       = 0
name_tables = 0

def usage( execname ):
    print( )
    print( "ftdump: simple font dumper -- part of the FreeType project" )
    print( "----------------------------------------------------------" )
    print( "Usage: %s [options] fontname", execname )
    print( )
    print( "  -n        print SFNT name tables" )
    print( "  -v        be verbose" )
    print( )
    sys.exit()


def Print_Name( face ):
    print( "font name entries" );
    print( "   family:     %s" % face.family_name )
    print( "   style:      %s" % face.style_name )
    ps_name = face.postscript_name or "UNAVAILABLE"
    print( "   postscript: %s" % ps_name )


def Print_Type( face ):

    print( "font type entries" )

    #   module = &face->driver->root;
    #   printf( "   FreeType driver: %s\n", module->clazz->module_name );

    #  Is it better to dump all sfnt tag names?
    print( "   sfnt wrapped:    ",end="")
    if face.is_sfnt: print( "yes")
    else:            print( "no")

    # is scalable ?
    print( "   type:            ", end="")
    if face.is_scalable:
        print( "scalable, ", end="")
        if face.has_multiple_masters:
            print( "multiple_masters, ", end="")
    if face.has_fixed_sizes:
        print( "fixed size",end="")
    print()

    #  Direction
    print( "   direction:       ", end="" )
    if face.has_horizontal:
        print( "horizontal, ", end="")
    if face.has_vertical:
        print( "vertical", end="")
    print( )

    # Fixed width
    print( "   fixed width:     ", end="")
    if face.is_fixed_width: print( "yes")
    else:                   print( "no")

    # Glyph names
    print( "   glyph names:     ", end="")
    if face.has_glyph_names: print( "yes")
    else:                   print( "no")

    if face.is_scalable:
        print( "   EM size:         %d" % face.units_per_EM )
        print( "   global BBox:     (%ld,%ld):(%ld,%ld)" %
               (face.bbox.xMin, face.bbox.yMin,
                face.bbox.xMax, face.bbox.yMax ))
        print( "   ascent:          %d" % face.ascender )
        print( "   descent:         %d" % face.descender )
        print( "   text height:     %d" % face.height )


def get_platform_id( platform_id ):
    if platform_id == TT_PLATFORM_APPLE_UNICODE:
        return "Apple (Unicode)"
    elif platform_id == TT_PLATFORM_MACINTOSH:
        return "Macintosh"
    elif platform_id == TT_PLATFORM_ISO:
        return "ISO (deprecated)"
    elif platform_id == TT_PLATFORM_MICROSOFT:
        return "Microsoft"
    elif platform_id == TT_PLATFORM_CUSTOM:
        return "custom"
    elif platform_id == TT_PLATFORM_ADOBE:
        return "Adobe"
    else:
      return "UNKNOWN"

def get_name_id( name_id ):
    if name_id == TT_NAME_ID_COPYRIGHT:
        return "copyright"
    elif name_id == TT_NAME_ID_FONT_FAMILY:
        return "font family"
    elif name_id == TT_NAME_ID_FONT_SUBFAMILY:
        return "font subfamily"
    elif name_id == TT_NAME_ID_UNIQUE_ID:
        return "unique ID"
    elif name_id == TT_NAME_ID_FULL_NAME:
        return "full name"
    elif name_id == TT_NAME_ID_VERSION_STRING:
        return "version string"
    elif name_id == TT_NAME_ID_PS_NAME:
        return "PostScript name"
    elif name_id == TT_NAME_ID_TRADEMARK:
        return "trademark"

    # the following values are from the OpenType spec 
    elif name_id == TT_NAME_ID_MANUFACTURER:
        return "manufacturer"
    elif name_id == TT_NAME_ID_DESIGNER:
        return "designer"
    elif name_id == TT_NAME_ID_DESCRIPTION:
        return "description"
    elif name_id == TT_NAME_ID_VENDOR_URL:
        return "vendor URL"
    elif name_id == TT_NAME_ID_DESIGNER_URL:
        return "designer URL"
    elif name_id == TT_NAME_ID_LICENSE:
        return "license"
    elif name_id == TT_NAME_ID_LICENSE_URL:
        return "license URL"
    # number 15 is reserved
    elif name_id == TT_NAME_ID_PREFERRED_FAMILY:
        return "preferred family"
    elif name_id == TT_NAME_ID_PREFERRED_SUBFAMILY:
        return "preferred subfamily"
    elif name_id == TT_NAME_ID_MAC_FULL_NAME:
        return "Mac full name"

    # The following code is new as of 2000-01-21
    elif name_id == TT_NAME_ID_SAMPLE_TEXT:
      return "sample text"

    # This is new in OpenType 1.3
    elif name_id == TT_NAME_ID_CID_FINDFONT_NAME:
        return "CID 'findfont' name"
    else:
        return "UNKNOWN";


def Print_Sfnt_Names( face ):
    print( "font string entries" );

    for i in range(face.sfnt_name_count):

        name = face.get_sfnt_name(i)
        print( "   %-15s [%s]" % ( get_name_id( name.name_id ),
                                   get_platform_id( name.platform_id )),end="")

        if name.platform_id == TT_PLATFORM_APPLE_UNICODE:
            if name.encoding_id in [TT_APPLE_ID_DEFAULT,
                                    TT_APPLE_ID_UNICODE_1_1,
                                    TT_APPLE_ID_ISO_10646,
                                    TT_APPLE_ID_UNICODE_2_0]:
                print(name.string.decode('utf-16be', 'ignore'))
            else:
                print( "{unsupported encoding %d}" % name.encoding_id )

        elif name.platform_id == TT_PLATFORM_MACINTOSH:
            if name.language_id != TT_MAC_LANGID_ENGLISH:
                print( " (language=%d)" % name.language_id )
            print ( " : " )
            if name.encoding_id == TT_MAC_ID_ROMAN:
                # FIXME: convert from MacRoman to ASCII/ISO8895-1/whatever
                # (MacRoman is mostly like ISO8895-1 but there are differences)
                print(name.string)
            else:
                print( "{unsupported encoding %d}" % name.encoding_id )

        elif name.platform_id == TT_PLATFORM_ISO:
            if name.encoding_id in [ TT_ISO_ID_7BIT_ASCII,
                                     TT_ISO_ID_8859_1]:
                print(name.string)
            print ( " : " )
            if name.encoding_id == TT_ISO_ID_10646:
                print(name.string.decode('utf-16be', 'ignore'))
            else:
                print( "{unsupported encoding %d}" % name.encoding_id )

        elif name.platform_id == TT_PLATFORM_MICROSOFT:
            if name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES:
                print( " (language=0x%04x)" % name.language_id );
            print( " : " )
            if name.encoding_id in [TT_MS_ID_SYMBOL_CS,
                                    TT_MS_ID_UNICODE_CS]:
                print(name.string.decode('utf-16be', 'ignore'))
            else:
                print( "{unsupported encoding %d}" % name.encoding_id )
        else:
           print( "{unsupported platform}" )

        print( )


def Print_Fixed( face ):

    # num_fixed_size
    print( "fixed size\n" )

    # available size
    for i,bsize in enumerate(face.available_sizes):
        print( "   %3d: height %d, width %d\n",
               i, bsize.height, bsize.width )
        print( "        size %.3f, x_ppem %.3f, y_ppem %.3f\n",
               bsize.size / 64.0,
               bsize.x_ppem / 64.0, bsize.y_ppem / 64.0 )


def Print_Charmaps( face ):
    global verbose
    active = -1
    if face.charmap:
        active = face.charmap.index

    # CharMaps
    print( "charmaps" )
    for i,charmap in enumerate(face.charmaps):
        print( "   %d: platform %d, encoding %d, language %d" %
               (i, charmap.platform_id, charmap.encoding_id,
               int(charmap.cmap_language_id)), end="" )
        if i == active:
            print( " (active)", end="" )
        print ( )
        if verbose:
            face.set_charmap( charmap )
            charcode, gindex = face.get_first_char()
            while ( gindex ):
                print( "      0x%04lx => %d" % (charcode, gindex) )
                charcode, gindex = face.get_next_char( charcode, gindex )



# -----------------------------------------------------------------------------
if __name__ == '__main__':
    import getopt
    execname = sys.argv[0]

    if len(sys.argv) < 2:
        usage( execname )
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], ':nv')
    except getopt.GetoptError, err:
        usage( execname )

    verbose = False
    name_tables = False

    for o, a in opts:
        if o == "-v":   verbose = True
        elif o == "-n": name_tables = True
        else:           usage( execname )


    face = Face(args[0])
    num_faces = face.num_faces

    if num_faces > 1:
        print( "There are %d faces in this file." % num_faces)
    else:
        print( "There is 1 face in this file.")

    for i in range(num_faces):
        face = Face(args[0], i)

        print( "\n----- Face number: %d -----\n" % i )
        Print_Name( face )
        print( "" )
        Print_Type( face )
        print( "   glyph count:     %d" % face.num_glyphs )

        if name_tables and face.is_sfnt:
            print( )
            Print_Sfnt_Names( face )
            
        if face.num_fixed_sizes:
            print(  )
            Print_Fixed( face )

        if face.num_charmaps:
            print(  )
            Print_Charmaps( face )