svghmi/fonts.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 29 Mar 2021 07:11:45 +0200
branchsvghmi
changeset 3203 debd5014ce21
child 3211 938b55abe946
permissions -rw-r--r--
SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
3203
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
# This file is part of Beremiz
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
# Copyright (C) 2021: Edouard TISSERANT
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
#
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
# See COPYING file for copyrights details.
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
from __future__ import print_function
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
from base64 import b64encode
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
from fontTools import ttLib
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
def GetFontTypeAndFamilyName(filename):
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
    """
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
    Getting font family, format and MIME type
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
    """
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
    familyname = None
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
    formatname = None
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
    mimetype = None
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
    font = ttLib.TTFont(filename)
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
    # https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
    for name in font["name"].names:
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
        if name.nameID==1 and name.platformID in [0,3]:
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
            familyname = name.toUnicode()
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
    if font.flavor :
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
        # woff and woff2
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
        formatname = font.flavor
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
        mimetype = "font/" + formatname
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
    # conditions on sfntVersion was deduced from fontTools.ttLib.sfnt
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
    elif font.sfntVersion in ("\x00\x01\x00\x00", "true"):
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
        formatname = "truetype" 
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
        mimetype = "font/ttf"
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
    elif font.sfntVersion == "OTTO":
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
        formatname = "opentype"
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
        mimetype = "font/otf"
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
    return familyname,formatname,mimetype
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
def DataURIFromFile(filename, mimetype):
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
    with open(filename, "rb") as fp:
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
        data = fp.read()
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
    return "".join([
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
        "data:",
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
        mimetype,
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
        ";base64,",
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
        b64encode(data).strip()])
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
def GetCSSFontFaceFromFontFile(filename):
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
    familyname, formatname, mimetype = GetFontTypeAndFamilyName(filename)
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
    data_uri = DataURIFromFile(filename, mimetype)
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
    css_font_face = \
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
    """
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
    @font-face {{
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
          font-family: "{}";
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
          src: url("{}") format("{}")
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
    }}
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
    """.format(familyname, data_uri, formatname)
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
    return css_font_face
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
# tests
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
if __name__ == '__main__':
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
    print(GetCSSFontFaceFromFontFile("/usr/share/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf"))
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
    print(GetCSSFontFaceFromFontFile("/usr/share/fonts/opentype/urw-base35/NimbusSans-Regular.otf"))
debd5014ce21 SVGHMI: Added fonts.py with functions to convert ttf, otf and woff fonts into data_uri based CSS font-face
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
    print(GetCSSFontFaceFromFontFile("/usr/share/yelp/mathjax/fonts/HTML-CSS/TeX/woff/MathJax_SansSerif-Regular.woff"))