145 lines
5.7 KiB
Ziran
145 lines
5.7 KiB
Ziran
#import "kryon/drawing_props"
|
|
#import "kryon/style"
|
|
#import "kryon/style_sheet"
|
|
#import "kryon/control_props"
|
|
#import "kss_parser"
|
|
#import "style_parse"
|
|
|
|
using KssStatus;
|
|
using KssTokenKind;
|
|
using MaterialKind;
|
|
|
|
InstallBodyTypeface :: (bytes: []u8) -> bool {
|
|
rules: StyleRules
|
|
rules.count = 1
|
|
rules.items[0].style.typeface = TextView(bytes[:])
|
|
return InstallStyleRules(rules)
|
|
}
|
|
|
|
StyleRulesAtCapacity :: (count: s32) -> StyleRules {
|
|
result: StyleRules
|
|
if count >= 0 && count <= StyleRuleCapacity { result.count = count }
|
|
return result
|
|
}
|
|
|
|
// A rule with this background; border, width, and radius when nonzero.
|
|
HasRule :: (rules: StyleRules, background: u32, border: u32,
|
|
border_width: float32, radius: float32) -> bool {
|
|
index: s32 = 0
|
|
while index < rules.count {
|
|
style: StyleData = rules.items[index].style
|
|
if style.background == background &&
|
|
(border == cast(u32)0 || style.border == border) &&
|
|
(border_width == 0.0 || style.border_width == border_width) &&
|
|
(radius == 0.0 || style.radius == radius) { return true }
|
|
index += 1
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Parses sheets with imports, color overrides, and full rule tables, then
|
|
// installs them into Kryon's style rules.
|
|
#program_export
|
|
Answer :: () -> s32 {
|
|
parsed: StyleRulesParse = BeginStyleRules(
|
|
"@pack demo; @import <base>; Progress[role=Track] { background: #112233; border: #99aabb; border-width: 2; radius: 5; }",
|
|
"demo.kss", KssDefaultEnvironment())
|
|
parsed = ParseStyleRules(parsed)
|
|
if parsed.parser.status != cast(s32)KssStatusNeedImport ||
|
|
parsed.rules.count != 0 { return 0 }
|
|
parsed = ProvideStyleRulesImport(parsed,
|
|
"Progress[role=Fill] { background: #556677; }", "base")
|
|
parsed = ParseStyleRules(parsed)
|
|
if parsed.parser.status != cast(s32)KssStatusDone ||
|
|
parsed.overflow || parsed.rules.count != 2 { return 0 }
|
|
if !InstallParsedStyleRules(parsed) { return 0 }
|
|
// The sheet's own rule and the imported one are both installed.
|
|
installed_pair: StyleRules = ActiveStyleRules()
|
|
if installed_pair.count != 2 ||
|
|
!HasRule(installed_pair, cast(u32)0x112233ff, cast(u32)0x99aabbff, 2.0, 5.0) ||
|
|
!HasRule(installed_pair, cast(u32)0x556677ff, cast(u32)0, 0.0, 0.0) {
|
|
return 0
|
|
}
|
|
if !KssCSSNeedsPixels("width") { return 0 }
|
|
parsed = BeginStyleRules(
|
|
"tokens { length { radius: 4; } material { face: Flat; } } Button { radius: radius; material: face; }",
|
|
"custom.kss", KssDefaultEnvironment())
|
|
radius: KssToken
|
|
radius.name = KssMakeName("radius")
|
|
radius.kind = cast(s32)KssTokenLength
|
|
radius.number = 12.0
|
|
parsed.parser = KssAddTokenOverride(parsed.parser, radius)
|
|
radius.number = 24.0
|
|
parsed.parser = KssAddTokenOverride(parsed.parser, radius)
|
|
face: KssToken
|
|
face.name = KssMakeName("face")
|
|
face.kind = cast(s32)KssTokenMaterial
|
|
face.material = cast(s32)MaterialGlass
|
|
parsed.parser = KssAddTokenOverride(parsed.parser, face)
|
|
parsed = ParseStyleRules(parsed)
|
|
if parsed.parser.status != cast(s32)KssStatusDone ||
|
|
parsed.parser.override_count != 2 || parsed.rules.count != 1 ||
|
|
parsed.rules.items[0].style.radius != 24.0 ||
|
|
cast(s32)parsed.rules.items[0].style.material != cast(s32)MaterialGlass {
|
|
return 0
|
|
}
|
|
parsed = BeginStyleRules(
|
|
"@pack colors; tokens { color { canvas: #112233; } }",
|
|
"colors.kss", KssDefaultEnvironment())
|
|
parsed.parser = KssAddColorOverride(parsed.parser, "canvas",
|
|
cast(u32)0xaabbccdd)
|
|
parsed = ParseStyleRules(parsed)
|
|
if !InstallParsedStyleRules(parsed) { return 0 }
|
|
color: Color = StyleTokenColor("canvas")
|
|
if color.r != 0xaa || color.g != 0xbb || color.b != 0xcc ||
|
|
color.a != 0xdd { return 0 }
|
|
color = StyleTokenColorOr("missing", Color.{1, 2, 3, 4})
|
|
if color.r != 1 || color.g != 2 || color.b != 3 || color.a != 4 {
|
|
return 0
|
|
}
|
|
parsed = BeginStyleRules(
|
|
"@pack replacement; tokens { color { text: #445566; } }",
|
|
"replacement.kss", KssDefaultEnvironment())
|
|
parsed = ParseStyleRules(parsed)
|
|
if !InstallParsedStyleRules(parsed) { return 0 }
|
|
color = StyleTokenColor("canvas")
|
|
if color.r != 0 || color.g != 0 || color.b != 0 || color.a != 255 {
|
|
return 0
|
|
}
|
|
full_rules: StyleRules = StyleRulesAtCapacity(StyleRuleCapacity)
|
|
parsed = BeginStyleRules("tokens { color { replacement: #998877; } }",
|
|
"invalid-snapshot.kss", KssDefaultEnvironment())
|
|
parsed = ParseStyleRules(parsed)
|
|
parsed.rules.count = StyleRuleCapacity + 1
|
|
if InstallParsedStyleRules(parsed) {
|
|
return 0
|
|
}
|
|
color = StyleTokenColor("text")
|
|
if color.r != 0x44 || color.g != 0x55 || color.b != 0x66 {
|
|
return 0
|
|
}
|
|
parsed = BeginStyleRules("Progress { background: #112233; }",
|
|
"full.kss", KssDefaultEnvironment())
|
|
parsed.rules = full_rules
|
|
parsed = StepStyleRules(parsed)
|
|
if !parsed.overflow || parsed.rules.count != StyleRuleCapacity { return 0 }
|
|
parsed = BeginStyleRules("@import <missing>;", "missing.kss",
|
|
KssDefaultEnvironment())
|
|
parsed = ParseStyleRules(parsed)
|
|
if parsed.parser.status != cast(s32)KssStatusNeedImport { return 0 }
|
|
parsed = FailStyleRulesImport(parsed)
|
|
if parsed.parser.status != cast(s32)KssStatusError { return 0 }
|
|
|
|
bytes: [4]u8 = .[98, 111, 100, 121]
|
|
if !InstallBodyTypeface(bytes[:]) { return 0 }
|
|
installed: StyleRules = ActiveStyleRules()
|
|
if installed.items[0].style.typeface != "body" { return 0 }
|
|
bytes[0] = cast(u8)120
|
|
if installed.items[0].style.typeface != "body" { return 0 }
|
|
owned_rules: StyleRules
|
|
owned_rules.count = StyleRuleCapacity + 1
|
|
if InstallStyleRules(owned_rules) ||
|
|
ActiveStyleRules().items[0].style.typeface != "body" { return 0 }
|
|
return 42
|
|
}
|