11from __future__ import annotations
22
33import os
4- import re
54import warnings
65from pathlib import Path
76from typing import Any , Optional , Union
@@ -81,7 +80,7 @@ def warn_or_raise_unmapped_variable(unmapped: str):
8180typography_map : dict [str , dict [str , list [str ]]] = {
8281 "base" : {
8382 "family" : ["font-family-base" ],
84- "size" : ["font-size-base" ],
83+ "size" : ["font-size-base" ], # TODO: consider using $font-size-root instead
8584 "line_height" : ["line-height-base" ],
8685 "weight" : ["font-weight-base" ],
8786 },
@@ -372,6 +371,7 @@ def _prepare_typography_vars(brand: Brand) -> dict[str, str]:
372371 brand_typography = brand .typography .model_dump (
373372 exclude = {"fonts" },
374373 exclude_none = True ,
374+ context = {"typography_base_size_unit" : "rem" },
375375 )
376376
377377 for field , prop in brand_typography .items ():
@@ -381,9 +381,6 @@ def _prepare_typography_vars(brand: Brand) -> dict[str, str]:
381381
382382 for prop_key , prop_value in prop .items ():
383383 if prop_key in typography_map [field ]:
384- if field == "base" and prop_key == "size" :
385- prop_value = maybe_convert_font_size_to_rem (prop_value )
386-
387384 typo_sass_vars = typography_map [field ][prop_key ]
388385 for typo_sass_var in typo_sass_vars :
389386 mapped [typo_sass_var ] = prop_value
@@ -560,53 +557,3 @@ def _html_dependencies(self) -> list[HTMLDependency]:
560557 return theme_deps
561558
562559 return [fonts_dep , * theme_deps ]
563-
564-
565- def maybe_convert_font_size_to_rem (x : str ) -> str :
566- """
567- Convert a font size to rem
568-
569- Bootstrap expects base font size to be in `rem`. This function converts `em`, `%`,
570- `px`, `pt` to `rem`:
571-
572- 1. `em` is directly replace with `rem`.
573- 2. `1%` is `0.01rem`, e.g. `90%` becomes `0.9rem`.
574- 3. `16px` is `1rem`, e.g. `18px` becomes `1.125rem`.
575- 4. `12pt` is `1rem`.
576- 5. `0.1666in` is `1rem`.
577- 6. `4.234cm` is `1rem`.
578- 7. `42.3mm` is `1rem`.
579- """
580- x_og = f"{ x } "
581-
582- value , unit = split_css_value_and_unit (x )
583-
584- if unit == "rem" :
585- return x
586-
587- if unit == "em" :
588- return f"{ value } rem"
589-
590- scale = {
591- "%" : 100 ,
592- "px" : 16 ,
593- "pt" : 12 ,
594- "in" : 96 / 16 , # 96 px/inch
595- "cm" : 96 / 16 * 2.54 , # inch -> cm
596- "mm" : 16 / 96 * 25.4 , # cm -> mm
597- }
598-
599- if unit in scale :
600- return f"{ float (value ) / scale [unit ]} rem"
601-
602- raise ValueError (
603- f"Shiny does not support brand.yml font sizes in { unit } units ({ x_og !r} )"
604- )
605-
606-
607- def split_css_value_and_unit (x : str ) -> tuple [str , str ]:
608- match = re .match (r"^(-?\d*\.?\d+)([a-zA-Z%]*)$" , x )
609- if not match :
610- raise ValueError (f"Invalid CSS value format: { x } " )
611- value , unit = match .groups ()
612- return value , unit
0 commit comments