样式系统 API¶
样式模块负责 CSS 属性的解析、注册和计算。
模块概览¶
| 子模块 | 职责 |
|---|---|
style.properties |
属性注册表 (PROPERTY_REGISTRY) |
style.parser |
值解析器和特殊值类型 |
style.computed |
ComputedStyle 计算样式对象 |
ComputedStyle¶
ComputedStyle 是每个节点的样式容器,负责:
- 存储显式设置的属性值
- 提供属性继承(从父节点)
- 计算最终使用值
from latticesvg import ComputedStyle
cs = ComputedStyle({"font-size": 24, "color": "red"})
print(cs.font_size) # 24
print(cs.get("color")) # "red"
自动生成的 API 文档¶
样式属性¶
properties ¶
CSS property registry — default values, inheritance flags, and type hints.
PropertyDef
dataclass
¶
Definition of a single CSS property.
default
instance-attribute
¶
Default (initial) value — raw CSS string or Python object.
inheritable
class-attribute
instance-attribute
¶
Whether this property is inherited from the parent element.
parser_hint
class-attribute
instance-attribute
¶
Hint for the parser: 'length', 'color', 'keyword', 'track-list', etc.
get_default ¶
Return the default value for a property, or None if unknown.
is_inheritable ¶
值解析器¶
parser ¶
CSS value parser — converts raw CSS strings to resolved Python values.
Handles units (px, %, em, fr), keywords, colors, and shorthand expansion.
AutoValue ¶
Singleton representing the auto keyword.
LineHeightMultiplier
dataclass
¶
A unitless line-height multiplier (e.g. 1.5).
CSS distinguishes line-height: 1.5 (multiplier, relative to
font-size) from line-height: 24px (absolute). This wrapper
preserves that distinction so downstream code does not need to
guess.
MinMaxValue
dataclass
¶
Represents a minmax(min, max) track sizing function.
GradientStop
dataclass
¶
A single color stop in a gradient.
LinearGradientValue
dataclass
¶
Parsed linear-gradient(...) value.
RadialGradientValue
dataclass
¶
RadialGradientValue(shape: str = 'ellipse', cx: float = 0.5, cy: float = 0.5, stops: Tuple[GradientStop, ...] = ())
Parsed radial-gradient(...) value.
BoxShadow
dataclass
¶
BoxShadow(offset_x: float = 0.0, offset_y: float = 0.0, blur_radius: float = 0.0, spread_radius: float = 0.0, color: str = 'rgba(0,0,0,1)', inset: bool = False)
A single box-shadow layer.
TransformFunction
dataclass
¶
A single CSS transform function (e.g. rotate(45)).
FilterFunction
dataclass
¶
A single CSS filter function (e.g. blur(5)).
ClipEllipse
dataclass
¶
Parsed ellipse(rx ry at cx cy).
ClipInset
dataclass
¶
Parsed inset(top right bottom left round r).
AreaMapping
dataclass
¶
Parsed result of grid-template-areas.
| ATTRIBUTE | DESCRIPTION |
|---|---|
areas |
Indices are 0-based.
TYPE:
|
num_rows |
Number of rows implied by the template.
TYPE:
|
num_cols |
Number of columns implied by the template.
TYPE:
|
parse_value ¶
parse_value(raw: Any, *, reference_length: Optional[float] = None, font_size: Optional[float] = None, root_font_size: Optional[float] = None) -> Any
Parse a single CSS value string into a resolved Python value.
| PARAMETER | DESCRIPTION |
|---|---|
raw
|
The raw value. Non-string types are returned as-is (numbers) or processed (lists).
TYPE:
|
reference_length
|
The reference length for resolving
TYPE:
|
font_size
|
Current computed font-size for resolving
TYPE:
|
root_font_size
|
Root element font-size for resolving
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Resolved value — float (px), FrValue, AutoValue, MinContent, MaxContent,
|
|
str (color / keyword), or list.
|
|
Source code in src/latticesvg/style/parser.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
expand_shorthand ¶
Expand CSS shorthand properties into their longhands.
Returns a dict of {longhand_name: raw_value} pairs.
If prop is not a shorthand, returns {prop: value}.
Source code in src/latticesvg/style/parser.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
parse_clip_path ¶
Parse a CSS clip-path value.
Supports circle(), ellipse(), polygon(), inset()
function syntax. Returns the corresponding dataclass instance,
or the string "none" for no clipping.
Source code in src/latticesvg/style/parser.py
parse_track_template ¶
Parse a grid-template-columns / grid-template-rows value.
Accepts a list of strings (["200px", "1fr"]) or a single
space-separated string ("200px 1fr").
Supports repeat(count, track_expr) and minmax(min, max)
function syntax.
Source code in src/latticesvg/style/parser.py
parse_gradient ¶
Parse a CSS gradient value.
Supports linear-gradient(...) and radial-gradient(...) with:
- Direction angle (45deg) or keyword (to right)
- Multiple color stops with optional percentage positions
- rgb() / rgba() color functions inside stops
Returns a :class:LinearGradientValue or :class:RadialGradientValue,
or "none" if the value cannot be parsed.
Source code in src/latticesvg/style/parser.py
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 | |
parse_grid_template_areas ¶
Parse a grid-template-areas value.
The CSS syntax is a series of quoted row strings, e.g.::
"header header header"
"sidebar main main"
"footer footer footer"
A single dot . represents an empty cell (unnamed).
The value may be supplied as: * a single string with quoted substrings (CSS syntax) * a list of strings, each representing one row
Returns None if the value is None or "none".
Raises ValueError on malformed input (non-rectangular areas, etc.).
Source code in src/latticesvg/style/parser.py
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 | |
parse_box_shadow ¶
Parse a CSS box-shadow value.
Returns a tuple of :class:BoxShadow instances, or the string
"none" for no shadow.
Source code in src/latticesvg/style/parser.py
parse_transform ¶
Parse a CSS transform value.
Returns a tuple of :class:TransformFunction instances, or the string
"none" for no transform.
Source code in src/latticesvg/style/parser.py
parse_filter ¶
Parse a CSS filter value.
Returns a tuple of :class:FilterFunction instances, or the string
"none" for no filter.
Source code in src/latticesvg/style/parser.py
计算样式¶
computed ¶
ComputedStyle — resolved style object with inheritance and shorthand expansion.
ComputedStyle ¶
Holds fully-resolved CSS property values for a single node.
Construction
style = ComputedStyle({"width": "200px", "padding": "10px"}, parent_style=None) style.width # 200.0 style.padding_top # 10.0
Attribute access maps underscores to hyphens so style.font_size
reads the font-size property.
Source code in src/latticesvg/style/computed.py
border_radii
property
¶
Return (top-left, top-right, bottom-right, bottom-left) radii.
get ¶
set ¶
Set a CSS property, with shorthand expansion and value parsing.
Supports both longhand and shorthand properties::
style.set("padding", "10px 20px") # expands to 4 longhands
style.set("border", "1px solid red") # expands to 12 longhands
style.set("font-size", "18px") # parsed to float 18.0
style.set("width", 200) # numeric values kept as-is
Source code in src/latticesvg/style/computed.py
resolve_percentages ¶
Resolve any remaining _Percentage values.
The original _Percentage instances are preserved internally so
that this method can be called again with different reference
dimensions (e.g. when layout() is invoked multiple times).