Skip to content

Commit 9f70d94

Browse files
authored
Merge pull request #35 from 5anniversary/font_weight_raw
Fix incorrect font-weight mapping based on Mozilla documentation
2 parents a6cd4d6 + 4a3494f commit 9f70d94

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Sources/ZMarkupParser/HTML/Processor/HTMLTagAttributeToMarkupStyleVisitor.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,47 @@ struct HTMLTagStyleAttributeToMarkupStyleVisitor: HTMLTagStyleAttributeVisitor {
5959
return MarkupStyle(font: MarkupStyleFont(weight: .style(.regular)))
6060
}
6161
case .rawValue(let value):
62-
return MarkupStyle(font: MarkupStyleFont(weight: .rawValue(value)))
62+
/*
63+
https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#common_weight_name_mapping
64+
Value Common weight name
65+
100 Thin (Hairline)
66+
200 Extra Light (Ultra Light)
67+
300 Light
68+
400 Normal (Regular)
69+
500 Medium
70+
600 Semi Bold (Demi Bold)
71+
700 Bold
72+
800 Extra Bold (Ultra Bold)
73+
900 Black (Heavy)
74+
950 Extra Black (Ultra Black)
75+
*/
76+
switch value {
77+
case 100:
78+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.thin)))
79+
case 200:
80+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.ultraLight)))
81+
case 300:
82+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.light)))
83+
case 400:
84+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.regular)))
85+
case 500:
86+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.medium)))
87+
case 600:
88+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.semibold)))
89+
case 700:
90+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.bold)))
91+
case 800:
92+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.heavy)))
93+
case 900:
94+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.black)))
95+
case 950:
96+
return MarkupStyle(font: MarkupStyleFont(weight: .style(.black)))
97+
default:
98+
return MarkupStyle(font: MarkupStyleFont(weight: .rawValue(value)))
99+
}
63100
}
64101
}
65-
102+
66103
func visit(_ styleAttribute: FontFamilyHTMLTagStyleAttribute) -> MarkupStyle? {
67104
// e.g. "Times New Roman", Times, serif
68105
// use first match font

0 commit comments

Comments
 (0)