15 """ |
15 """ |
16 Getting font family, format and MIME type |
16 Getting font family, format and MIME type |
17 """ |
17 """ |
18 |
18 |
19 familyname = None |
19 familyname = None |
|
20 uniquename = None |
20 formatname = None |
21 formatname = None |
21 mimetype = None |
22 mimetype = None |
22 |
23 |
23 font = ttLib.TTFont(filename) |
24 font = ttLib.TTFont(filename) |
24 # https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html |
25 # https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html |
25 for name in font["name"].names: |
26 for name in font["name"].names: |
26 if name.nameID==1 and name.platformID in [0,3]: |
27 if name.nameID in [1,16] and name.platformID==3 and name.langID==1033: |
27 familyname = name.toUnicode() |
28 familyname = name.toUnicode() |
|
29 if name.nameID==4 and name.platformID==3 and name.langID==1033: |
|
30 uniquename = name.toUnicode() |
28 |
31 |
29 if font.flavor : |
32 if font.flavor : |
30 # woff and woff2 |
33 # woff and woff2 |
31 formatname = font.flavor |
34 formatname = font.flavor |
32 mimetype = "font/" + formatname |
35 mimetype = "font/" + formatname |
33 # conditions on sfntVersion was deduced from fontTools.ttLib.sfnt |
36 # conditions on sfntVersion was deduced from fontTools.ttLib.sfnt |
34 elif font.sfntVersion in ("\x00\x01\x00\x00", "true"): |
37 elif font.sfntVersion in ("\x00\x01\x00\x00", "true"): |
35 formatname = "truetype" |
38 formatname = "truetype" |
36 mimetype = "font/ttf" |
39 mimetype = "font/ttf" |
37 elif font.sfntVersion == "OTTO": |
40 elif font.sfntVersion == "OTTO": |
38 formatname = "opentype" |
41 formatname = "opentype" |
39 mimetype = "font/otf" |
42 mimetype = "font/otf" |
40 |
43 |
41 return familyname,formatname,mimetype |
44 return familyname,uniquename,formatname,mimetype |
42 |
45 |
43 def DataURIFromFile(filename, mimetype): |
46 def DataURIFromFile(filename, mimetype): |
44 with open(filename, "rb") as fp: |
47 with open(filename, "rb") as fp: |
45 data = fp.read() |
48 data = fp.read() |
46 return "".join([ |
49 return "".join([ |
48 mimetype, |
51 mimetype, |
49 ";base64,", |
52 ";base64,", |
50 b64encode(data).strip()]) |
53 b64encode(data).strip()]) |
51 |
54 |
52 def GetCSSFontFaceFromFontFile(filename): |
55 def GetCSSFontFaceFromFontFile(filename): |
53 familyname, formatname, mimetype = GetFontTypeAndFamilyName(filename) |
56 familyname, uniquename, formatname, mimetype = GetFontTypeAndFamilyName(filename) |
54 data_uri = DataURIFromFile(filename, mimetype) |
57 data_uri = DataURIFromFile(filename, mimetype) |
55 css_font_face = \ |
58 css_font_face = \ |
56 """ |
59 """ |
57 @font-face {{ |
60 @font-face {{ |
58 font-family: "{}"; |
61 font-family: "{}"; |