File: using_fonts.py

package info (click to toggle)
svgwrite 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,304 kB
  • sloc: python: 12,524; makefile: 116; sh: 5
file content (73 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download | duplicates (2)
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
import svgwrite


def write_html_loader(name, title):
    open('{name}.html'.format(name=name), 'wt', encoding='utf-8').write("""<!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>title</title>
    </head>
    <body>
        <image src="{name}.svg"/>
    </body>
    </html>
    """.format(name=name, title=title))


def font_by_stylsheet(name):
    dwg = svgwrite.Drawing(name+'.svg', (800, 200), debug=True)
    # load web font by CSS @import
    dwg.embed_stylesheet("""
    @import url(http://fonts.googleapis.com/css?family=Indie+Flower);
    .flower14 {
        font-family: "Indie Flower";
        font-size: 14;
    }
    """)
    paragraph = dwg.add(dwg.g(class_="flower14", ))
    paragraph.add(dwg.text("Font 'Indie Flower' referenced by embedded CSS stylesheet.", insert=(10, 40)))
    dwg.save(pretty=True)
    # This seems easy, BUT this only works if the SVG file is opened stand alone,
    # embedded in a website, this approach does not work!
    write_html_loader(name, title="Test font for SVG by CSS reference inside of HTML")


def font_embedded(name):
    dwg = svgwrite.Drawing(name+'.svg', (800, 200), debug=True)
    # font data has to be downloaded to the local file system
    dwg.embed_font(name="Indie Flower", filename='fonts/IndieFlower.ttf')
    dwg.embed_stylesheet("""
    .flower14 {
        font-family: "Indie Flower";
        font-size: 14;
    }
    """)
    # This should work stand alone and embedded in a website!
    paragraph = dwg.add(dwg.g(class_="flower14", ))
    paragraph.add(dwg.text("Font 'Indie Flower' embedded from local file system.", insert=(10, 40)))
    dwg.save(pretty=True)
    write_html_loader(name, title="Test SVG with embedded font inside of HTML")


def web_font_embedded(name):
    dwg = svgwrite.Drawing(name+'.svg', (800, 200), debug=True)
    # font data downloaded from google fonts
    dwg.embed_google_web_font(name="Indie Flower", uri='http://fonts.googleapis.com/css?family=Indie+Flower')
    dwg.embed_stylesheet("""
    .flower14 {
        font-family: "Indie Flower";
        font-size: 14;
    }
    """)
    # This should work stand alone and embedded in a website!
    paragraph = dwg.add(dwg.g(class_="flower14", ))
    paragraph.add(dwg.text("Font 'Indie Flower' embedded from Google fonts.", insert=(10, 40)))
    dwg.save(pretty=True)
    write_html_loader(name, title="Test SVG with embedded font inside of HTML")


if __name__ == '__main__':
    font_by_stylsheet("font_by_CSS_reference")
    font_embedded("font_embedded")
    web_font_embedded("font_embedded_from_google")