Skip to content

Commit 510e5a2

Browse files
hajimehoshiclaude
andcommitted
basicwidget/internal/textstyle: remove the dedicated weight text style property
Ranged font weights will be modeled as the wght OpenType variation axis, which textstyle.Style already carries in its variations list and the font package already applies when resolving a face. A dedicated weight property would be a second representation of the same thing, so drop it from Style and Runs before implementing the feature. Tests covering weight now go through SetVariation with the wght tag. Add TODOs for the remaining face-selection work: choosing a concrete face from the font family based on the family and italic properties, and generalizing font.Attributes' ad-hoc Weight, Liga, and Tnum members into variations and features. Updates #131 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 9e2041a commit 510e5a2

6 files changed

Lines changed: 20 additions & 49 deletions

File tree

basicwidget/internal/font/font.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ var (
5151
// ligatures, tabular numerals, and language. A face is resolved from
5252
// Attributes together with a [Family]; the attributes alone do not identify a
5353
// font.
54+
//
55+
// TODO: Weight, Liga, and Tnum are ad-hoc special cases. Represent them as
56+
// general OpenType variations (wght) and features (liga, tnum) while keeping
57+
// Attributes comparable (#131).
5458
type Attributes struct {
5559
Size float64
5660
Weight text.Weight

basicwidget/internal/textstyle/export_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ func (s Style) WithFamily(family *font.Family) Style {
1919
return s
2020
}
2121

22-
func (s Style) WithWeight(weight text.Weight) Style {
23-
s.weight = opt(weight)
24-
return s
25-
}
26-
2722
func (s Style) WithItalic(italic bool) Style {
2823
s.italic = opt(italic)
2924
return s

basicwidget/internal/textstyle/runs.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ func (r *Runs) UnsetFamily(start, end int) {
5151
r.unset(start, end, styleMask{family: true})
5252
}
5353

54-
// SetWeight overrides the font weight in [start, end).
55-
func (r *Runs) SetWeight(start, end int, weight text.Weight) {
56-
r.apply(start, end, Style{weight: opt(weight)})
57-
}
58-
59-
// UnsetWeight removes the font weight override in [start, end).
60-
func (r *Runs) UnsetWeight(start, end int) {
61-
r.unset(start, end, styleMask{weight: true})
62-
}
63-
6454
// SetItalic overrides the italic face selection in [start, end).
6555
func (r *Runs) SetItalic(start, end int, italic bool) {
6656
r.apply(start, end, Style{italic: opt(italic)})

basicwidget/internal/textstyle/runs_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ func TestRunsSet(t *testing.T) {
7474
{
7575
name: "partial overlap merges properties",
7676
ops: func(runs *textstyle.Runs) {
77-
runs.SetWeight(0, 9, text.WeightBold)
77+
runs.SetVariation(0, 9, tagWght, float32(text.WeightBold))
7878
runs.SetUnderline(5, 16, true)
7979
},
8080
want: []textstyle.Run{
81-
{Start: 0, End: 5, Style: textstyle.Style{}.WithWeight(text.WeightBold)},
82-
{Start: 5, End: 9, Style: textstyle.Style{}.WithWeight(text.WeightBold).WithUnderline(true)},
81+
{Start: 0, End: 5, Style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightBold))},
82+
{Start: 5, End: 9, Style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightBold)).WithUnderline(true)},
8383
{Start: 9, End: 16, Style: textstyle.Style{}.WithUnderline(true)},
8484
},
8585
},
@@ -146,13 +146,13 @@ func TestRunsSet(t *testing.T) {
146146
{
147147
name: "whole-text set then a narrower one",
148148
ops: func(runs *textstyle.Runs) {
149-
runs.SetWeight(0, math.MaxInt, text.WeightBold)
149+
runs.SetVariation(0, math.MaxInt, tagWght, float32(text.WeightBold))
150150
runs.SetColor(2, 4, red)
151151
},
152152
want: []textstyle.Run{
153-
{Start: 0, End: 2, Style: textstyle.Style{}.WithWeight(text.WeightBold)},
154-
{Start: 2, End: 4, Style: textstyle.Style{}.WithWeight(text.WeightBold).WithColor(red)},
155-
{Start: 4, End: math.MaxInt, Style: textstyle.Style{}.WithWeight(text.WeightBold)},
153+
{Start: 0, End: 2, Style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightBold))},
154+
{Start: 2, End: 4, Style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightBold)).WithColor(red)},
155+
{Start: 4, End: math.MaxInt, Style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightBold))},
156156
},
157157
},
158158
{
@@ -194,12 +194,12 @@ func TestRunsSet(t *testing.T) {
194194
{
195195
name: "variations merge by tag",
196196
ops: func(runs *textstyle.Runs) {
197-
runs.SetVariation(0, 10, tagWght, 400)
198-
runs.SetVariation(5, 10, tagWght, 700)
197+
runs.SetVariation(0, 10, tagWght, float32(text.WeightNormal))
198+
runs.SetVariation(5, 10, tagWght, float32(text.WeightBold))
199199
},
200200
want: []textstyle.Run{
201-
{Start: 0, End: 5, Style: textstyle.Style{}.WithVariation(tagWght, 400)},
202-
{Start: 5, End: 10, Style: textstyle.Style{}.WithVariation(tagWght, 700)},
201+
{Start: 0, End: 5, Style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightNormal))},
202+
{Start: 5, End: 10, Style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightBold))},
203203
},
204204
},
205205
}
@@ -280,7 +280,7 @@ func TestRunsUnset(t *testing.T) {
280280
{
281281
name: "unset a variation",
282282
ops: func(runs *textstyle.Runs) {
283-
runs.SetVariation(0, 10, tagWght, 700)
283+
runs.SetVariation(0, 10, tagWght, float32(text.WeightBold))
284284
runs.UnsetVariation(0, 10, tagWght)
285285
},
286286
want: nil,

basicwidget/internal/textstyle/style.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ type Variation struct {
5959
// zero value overrides nothing. Styles are produced by [Runs]; an unset
6060
// property does not override anything.
6161
type Style struct {
62+
// TODO: family and italic are not consumed yet. Implement a face chooser
63+
// that selects a concrete face from the font family based on these
64+
// face-selection properties (#131).
6265
family optional[*font.Family]
63-
weight optional[text.Weight]
6466
italic optional[bool]
6567
scale optional[float64]
6668
color optional[color.Color]
@@ -80,11 +82,6 @@ func (s Style) Family() (*font.Family, bool) {
8082
return s.family.Value()
8183
}
8284

83-
// Weight returns the font weight override and whether it is set.
84-
func (s Style) Weight() (text.Weight, bool) {
85-
return s.weight.Value()
86-
}
87-
8885
// Italic returns the italic face selection override and whether it is set.
8986
func (s Style) Italic() (bool, bool) {
9087
return s.italic.Value()
@@ -135,7 +132,6 @@ func (s Style) Variations() []Variation {
135132
// IsZero reports whether the style overrides nothing.
136133
func (s Style) IsZero() bool {
137134
return !s.family.set &&
138-
!s.weight.set &&
139135
!s.italic.set &&
140136
!s.scale.set &&
141137
!s.color.set &&
@@ -150,7 +146,6 @@ func (s Style) IsZero() bool {
150146
// Equal reports whether two styles are identical.
151147
func (s Style) Equal(other Style) bool {
152148
return s.family == other.family &&
153-
s.weight == other.weight &&
154149
s.italic == other.italic &&
155150
s.scale == other.scale &&
156151
s.color == other.color &&
@@ -168,9 +163,6 @@ func (s Style) merge(other Style) Style {
168163
if other.family.set {
169164
s.family = other.family
170165
}
171-
if other.weight.set {
172-
s.weight = other.weight
173-
}
174166
if other.italic.set {
175167
s.italic = other.italic
176168
}
@@ -204,7 +196,6 @@ func (s Style) merge(other Style) Style {
204196
// styleMask selects style properties without carrying values.
205197
type styleMask struct {
206198
family bool
207-
weight bool
208199
italic bool
209200
scale bool
210201
color bool
@@ -219,7 +210,6 @@ type styleMask struct {
219210
// isZero reports whether the mask selects nothing.
220211
func (m styleMask) isZero() bool {
221212
return !m.family &&
222-
!m.weight &&
223213
!m.italic &&
224214
!m.scale &&
225215
!m.color &&
@@ -236,9 +226,6 @@ func (s Style) remove(mask styleMask) Style {
236226
if mask.family {
237227
s.family = optional[*font.Family]{}
238228
}
239-
if mask.weight {
240-
s.weight = optional[text.Weight]{}
241-
}
242229
if mask.italic {
243230
s.italic = optional[bool]{}
244231
}

basicwidget/internal/textstyle/style_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ func TestStyleIsZero(t *testing.T) {
2424
style: textstyle.Style{},
2525
want: true,
2626
},
27-
{
28-
name: "weight",
29-
style: textstyle.Style{}.WithWeight(text.WeightBold),
30-
want: false,
31-
},
3227
{
3328
name: "italic",
3429
style: textstyle.Style{}.WithItalic(true),
@@ -76,7 +71,7 @@ func TestStyleIsZero(t *testing.T) {
7671
},
7772
{
7873
name: "variation",
79-
style: textstyle.Style{}.WithVariation(tagWght, 700),
74+
style: textstyle.Style{}.WithVariation(tagWght, float32(text.WeightBold)),
8075
want: false,
8176
},
8277
}

0 commit comments

Comments
 (0)