File size: 3,363 Bytes
0ad74ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from __future__ import annotations

import json
import textwrap
from collections.abc import Iterable


class FontEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Font):
            return {
                "__gradio_font__": True,
                "name": obj.name,
                "class": "google"
                if isinstance(obj, GoogleFont)
                else "local"
                if isinstance(obj, LocalFont)
                else "font",
                "weights": obj.weights
                if isinstance(obj, (GoogleFont, LocalFont))
                else None,
            }
        # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)


def as_font(dct):
    if "__gradio_font__" in dct:
        name = dct["name"]
        if dct["class"] == "google":
            return (
                GoogleFont(name, weights=dct["weights"])
                if "weights" in dct
                else GoogleFont(name)
            )
        if dct["class"] == "local":
            return (
                LocalFont(name, weights=dct["weights"])
                if "weights" in dct
                else LocalFont(name)
            )
        return Font(name)
    return dct


class Font:
    def __init__(self, name: str):
        self.name = name

    def __str__(self) -> str:
        return (
            self.name
            if self.name in ["sans-serif", "serif", "monospace", "cursive", "fantasy"]
            else f"'{self.name}'"
        )

    def stylesheet(self) -> dict:
        return {"url": None, "css": None}

    def __eq__(self, other: Font) -> bool:
        return self.name == other.name and self.stylesheet() == other.stylesheet()

    def __repr__(self) -> str:
        klass = type(self)
        class_repr = klass.__module__ + "." + klass.__qualname__
        attrs = ", ".join([k + "=" + repr(v) for k, v in self.__dict__.items()])
        return f"<{class_repr} ({attrs})>"


class GoogleFont(Font):
    def __init__(self, name: str, weights: Iterable[int] = (400, 600)):
        self.name = name
        self.weights = weights

    def stylesheet(self) -> dict:
        url = f'https://fonts.googleapis.com/css2?family={self.name.replace(" ", "+")}:wght@{";".join(str(weight) for weight in self.weights)}&display=swap'
        return {"url": url, "css": None}


class LocalFont(Font):
    def __init__(self, name: str, weights: Iterable[int] = (400, 700)):
        super().__init__(name)
        self.weights = weights

    def stylesheet(self) -> dict:
        css_template = textwrap.dedent("""
            @font-face {{
                font-family: '{name}';
                src: url('static/fonts/{file_name}/{file_name}-{weight}.woff2') format('woff2');
                font-weight: {weight};
                font-style: normal;
            }}
            """)
        css_rules = []
        for weight in self.weights:
            weight_name = (
                "Regular" if weight == 400 else "Bold" if weight == 700 else str(weight)
            )
            css_rules.append(
                css_template.format(
                    name=self.name,
                    file_name=self.name.replace(" ", ""),
                    weight=weight_name,
                )
            )
        return {"url": None, "css": "\n".join(css_rules)}