svghmi/fonts.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 19 Oct 2022 20:53:01 +0200
branchwxPython4
changeset 3635 5107a2712b17
parent 3215 da2481f359b7
child 3933 6750083ae878
permissions -rw-r--r--
Update README.md
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
3215
da2481f359b7 SVGHMI: fixed embedded fonts family name not matching when using windows version of inkscape
Edouard Tisserant
parents: 3211
diff changeset
    10
import sys
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
    11
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
    12
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
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
    14
3215
da2481f359b7 SVGHMI: fixed embedded fonts family name not matching when using windows version of inkscape
Edouard Tisserant
parents: 3211
diff changeset
    15
# Inkscape seems to refer to font with different family name depending on platform.
da2481f359b7 SVGHMI: fixed embedded fonts family name not matching when using windows version of inkscape
Edouard Tisserant
parents: 3211
diff changeset
    16
# this are heuristics that would need extensive testing to be sure.
da2481f359b7 SVGHMI: fixed embedded fonts family name not matching when using windows version of inkscape
Edouard Tisserant
parents: 3211
diff changeset
    17
FamilyNameIDs = [1] if sys.platform.startswith('win') else [1, 16]
da2481f359b7 SVGHMI: fixed embedded fonts family name not matching when using windows version of inkscape
Edouard Tisserant
parents: 3211
diff changeset
    18
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
    19
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
    20
    """
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
    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
    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
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
    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
    25
    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
    26
    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
    27
    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
    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
    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
    30
    # 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
    31
    for name in font["name"].names:
3215
da2481f359b7 SVGHMI: fixed embedded fonts family name not matching when using windows version of inkscape
Edouard Tisserant
parents: 3211
diff changeset
    32
        #print(name.nameID, name.platformID, name.langID, name.toUnicode())
da2481f359b7 SVGHMI: fixed embedded fonts family name not matching when using windows version of inkscape
Edouard Tisserant
parents: 3211
diff changeset
    33
        if name.nameID in FamilyNameIDs 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
    34
            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
    35
        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
    36
            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
    37
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
    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
    39
        # 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
    40
        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
    41
        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
    42
    # 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
    43
    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
    44
        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
    45
        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
    46
    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
    47
        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
    48
        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
    49
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
    50
    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
    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 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
    53
    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
    54
        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
    55
    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
    56
        "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
    57
        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
        ";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
    59
        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
    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
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
    62
    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
    63
    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
    64
    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
    65
    """
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
    @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
    67
          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
    68
          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
    69
    }}
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
    """.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
    71
    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
    72
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
    73
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
    74
# 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
    75
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
    76
    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
    77
    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
    78
    print(GetCSSFontFaceFromFontFile("/usr/share/yelp/mathjax/fonts/HTML-CSS/TeX/woff/MathJax_SansSerif-Regular.woff"))