Skip to content

Commit

Permalink
fix(py): Correctly convert in, cm to rem (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Dec 10, 2024
2 parents 41796e3 + 562a940 commit f83166f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
6 changes: 6 additions & 0 deletions pkg-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
-->

## [UNRELEASED]

### Bug fixes

* Fixed a calculation to correctly convert `in` and `cm` to `rem` units for `brand.typography.base.size`. (#60)

## [0.1.0]

Initial release of `brand_yml`.
8 changes: 5 additions & 3 deletions pkg-py/src/brand_yml/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ def maybe_convert_font_size_to_rem(x: str) -> str:
"%": 100,
"px": 16,
"pt": 12,
"in": 96 / 16, # 96 px/inch
"cm": 96 / 16 * 2.54, # inch -> cm
"in": 16 / 96, # 96 px/inch
"cm": 16 / 96 * 2.54, # inch -> cm
"mm": 16 / 96 * 25.4, # cm -> mm
}

if unit in scale:
return f"{float(value) / scale[unit]}rem"
ret = f"{float(value) / scale[unit]:.4f}rem".replace(".0000", "")
ret = re.sub("[.]?0+rem", "rem", ret)
return ret

raise ValueError(
f"Could not convert font size {x_og!r} from {unit} units to a relative unit."
Expand Down
25 changes: 11 additions & 14 deletions pkg-py/src/brand_yml/typography.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Literal,
TypeVar,
Union,
cast,
overload,
)
from urllib.parse import urlencode, urljoin
Expand Down Expand Up @@ -102,17 +103,7 @@
100, 200, 300, 400, 500, 600, 700, 800, 900
]

font_weight_round_int = (
100,
200,
300,
400,
500,
600,
700,
800,
900,
)
font_weight_round_int = (100, 200, 300, 400, 500, 600, 700, 800, 900)

# https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#common_weight_name_mapping
font_weight_map: dict[str, BrandTypographyFontWeightRoundIntType] = {
Expand Down Expand Up @@ -591,7 +582,13 @@ class BrandTypographyGoogleFontsApi(BrandTypographyFontSource):
lambda x: x.to_serialized(),
return_type=Union[str, int, list[Union[int, str]]],
),
] = Field(default=list(font_weight_round_int), validate_default=True)
] = Field(
default=cast(
BrandTypographyGoogleFontsWeight,
list(font_weight_round_int),
),
validate_default=True,
)
"""
The desired front weights to be imported for the font family.
Expand Down Expand Up @@ -636,7 +633,7 @@ class BrandTypographyGoogleFontsApi(BrandTypographyFontSource):
version: PositiveInt = 2
"""Google Fonts API version. (Primarily for internal use.)"""

url: HttpUrl = Field("https://fonts.googleapis.com/", validate_default=True)
url: HttpUrl = Field(HttpUrl("https://fonts.googleapis.com/"))
"""URL of the Google Fonts-compatible API. (Primarily for internal use.)"""

def to_css(self) -> str:
Expand Down Expand Up @@ -769,7 +766,7 @@ class BrandTypographyFontBunny(BrandTypographyGoogleFontsApi):

source: Literal["bunny"] = Field("bunny", frozen=True) # type: ignore[reportIncompatibleVariableOverride]
version: PositiveInt = 1
url: HttpUrl = Field("https://fonts.bunny.net/", validate_default=True)
url: HttpUrl = Field(HttpUrl("https://fonts.bunny.net/"))


# Typography Options -----------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions pkg-py/tests/test_typography.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ def test_brand_typography_write_font_css():
("18px", "1.125rem"),
("50%", "0.5rem"),
("1.5em", "1.5rem"),
("1in", "6rem"),
("1.27cm", "3rem"),
("12.7mm", "3rem"),
],
)
def test_brand_typography_base_font_size_as_rem(original, rem):
Expand Down

0 comments on commit f83166f

Please sign in to comment.