svghmi/fonts.py
author Edouard Tisserant
Tue, 30 Mar 2021 14:54:43 +0200
branchsvghmi
changeset 3211 938b55abe946
parent 3203 debd5014ce21
child 3215 da2481f359b7
permissions -rw-r--r--
SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
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
3211
938b55abe946 SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
Edouard Tisserant
parents: 3203
diff changeset
    20
    uniquename = None
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
    21
    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
    22
    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
    23
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
    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
    25
    # 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
    26
    for name in font["name"].names:
3211
938b55abe946 SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
Edouard Tisserant
parents: 3203
diff changeset
    27
        if name.nameID in [1,16] and name.platformID==3 and name.langID==1033:
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
    28
            familyname = name.toUnicode()
3211
938b55abe946 SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
Edouard Tisserant
parents: 3203
diff changeset
    29
        if name.nameID==4 and name.platformID==3 and name.langID==1033:
938b55abe946 SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
Edouard Tisserant
parents: 3203
diff changeset
    30
            uniquename = name.toUnicode()
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
    31
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
    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
    33
        # 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
    34
        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
    35
        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
    36
    # 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
    37
    elif font.sfntVersion in ("\x00\x01\x00\x00", "true"):
3211
938b55abe946 SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
Edouard Tisserant
parents: 3203
diff changeset
    38
        formatname = "truetype"
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
    39
        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
    40
    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
    41
        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
    42
        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
    43
3211
938b55abe946 SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
Edouard Tisserant
parents: 3203
diff changeset
    44
    return familyname,uniquename,formatname,mimetype
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
    45
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
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
    47
    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
    48
        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
    49
    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
    50
        "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
    51
        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
    52
        ";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
    53
        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
    54
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
def GetCSSFontFaceFromFontFile(filename):
3211
938b55abe946 SVGHMI: Implemented "Add Font" and "Remove Font", add font embedding in CSS at build time, tested ok with some OTF for now.
Edouard Tisserant
parents: 3203
diff changeset
    56
    familyname, uniquename, formatname, mimetype = GetFontTypeAndFamilyName(filename)
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
    57
    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
    58
    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
    59
    """
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
    @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
    61
          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
    62
          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
    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
    """.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
    65
    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
    66
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
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
# 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
    69
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
    70
    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
    71
    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
    72
    print(GetCSSFontFaceFromFontFile("/usr/share/yelp/mathjax/fonts/HTML-CSS/TeX/woff/MathJax_SansSerif-Regular.woff"))