Skip to content

Commit

Permalink
feat(typography): Easy font setting, default is google font
Browse files Browse the repository at this point in the history
Treats `base: Open Sans` and
`base.family: Open Sans` as "get from Google Fonts"

For #21
  • Loading branch information
gadenbuie committed Sep 20, 2024
1 parent 65f0384 commit 812b415
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
9 changes: 9 additions & 0 deletions examples/brand-typography-minimal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
meta:
name: examples/brand-typography-simple.yml
typography:
fonts:
- family: Open Sans
source: file
base: Open Sans
headings: Roboto Slab
monospace: Fira Code
55 changes: 54 additions & 1 deletion pkg-py/src/brand_yaml/typography.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class BrandTypographyFontFiles(BaseModel):

source: Literal["file"] = "file"
family: str
files: list[BrandTypographyFontFilesPath]
files: list[BrandTypographyFontFilesPath] = Field(default_factory=list)


class BrandTypographyFontFilesPath(BaseModel):
Expand Down Expand Up @@ -413,6 +413,59 @@ class BrandTypography(BrandBase):
)
link: BrandTypographyLink | None = None

@model_validator(mode="before")
@classmethod
def simple_google_fonts(cls, data: Any):
if not isinstance(data, dict):
return data

defined_families = set()
file_families = set()

if (
"fonts" in data
and isinstance(data["fonts"], list)
and len(data["fonts"]) > 0
):
for font in data["fonts"]:
defined_families.add(font["family"])
if font["source"] == "file":
file_families.add(font["family"])
else:
data["fonts"] = []

for field in (
"base",
"headings",
"monospace",
"monospace_inline",
"monospace_block",
):
if field not in data:
continue

if not isinstance(data[field], (str, dict)):
continue

if isinstance(data[field], str):
data[field] = {"family": data[field]}

if "family" not in data[field]:
continue

if data[field]["family"] in defined_families:
continue

data["fonts"].append(
{
"family": data[field]["family"],
"source": "google",
}
)
defined_families.add(data[field]["family"])

return data

@model_validator(mode="after")
def forward_monospace_values(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"base": {
"color": "#1b1818"
},
"fonts": [],
"headings": {
"color": "#87CEEB"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"meta": {
"name": {
"full": "examples/brand-typography-simple.yml"
}
},
"typography": {
"base": {
"family": "Open Sans"
},
"fonts": [
{
"family": "Open Sans",
"source": "file"
},
{
"family": "Roboto Slab",
"source": "google"
},
{
"family": "Fira Code",
"source": "google"
}
],
"headings": {
"family": "Roboto Slab"
},
"monospace": {
"family": "Fira Code"
},
"monospace-block": {
"family": "Fira Code"
},
"monospace-inline": {
"family": "Fira Code"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
"line-height": 1.25,
"size": "1rem"
},
"fonts": [
{
"family": "Open Sans",
"source": "google"
},
{
"family": "Roboto Slab",
"source": "google"
},
{
"family": "Fira Code",
"source": "google"
}
],
"headings": {
"color": "primary",
"family": "Roboto Slab",
Expand Down
30 changes: 29 additions & 1 deletion pkg-py/tests/test_typography.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,16 @@ def test_brand_typography_ex_simple(snapshot_json):
brand = read_brand_yaml(path_examples("brand-typography-simple.yml"))

assert isinstance(brand.typography, BrandTypography)
assert brand.typography.fonts == []

assert isinstance(brand.typography.fonts, list)
assert len(brand.typography.fonts) == 3
assert [f.family for f in brand.typography.fonts] == [
"Open Sans",
"Roboto Slab",
"Fira Code",
]
assert [f.source for f in brand.typography.fonts] == ["google"] * 3

assert brand.typography.link is None
assert isinstance(brand.typography.base, BrandTypographyBase)
assert isinstance(brand.typography.headings, BrandTypographyHeadings)
Expand Down Expand Up @@ -355,3 +364,22 @@ def test_brand_typography_ex_color(snapshot_json):
assert t.link.color == color.palette["red"]

assert snapshot_json == pydantic_data_from_json(brand)


def test_brand_typography_ex_minimal(snapshot_json):
brand = read_brand_yaml(path_examples("brand-typography-minimal.yml"))

assert isinstance(brand.typography, BrandTypography)

assert isinstance(brand.typography.fonts, list)
assert len(brand.typography.fonts) == 3
assert brand.typography.fonts[0].source == "file"
assert brand.typography.fonts[0].files == []

assert isinstance(brand.typography.fonts[1], BrandTypographyFontGoogle)
assert brand.typography.fonts[1].family == "Roboto Slab"

assert isinstance(brand.typography.fonts[2], BrandTypographyFontGoogle)
assert brand.typography.fonts[2].family == "Fira Code"

assert snapshot_json == pydantic_data_from_json(brand)

0 comments on commit 812b415

Please sign in to comment.