Skip to content

Commit d800e2f

Browse files
committed
Remove font decoration overrides and fix sample runner
1 parent 3a35985 commit d800e2f

6 files changed

Lines changed: 96 additions & 340 deletions

File tree

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.PHONY: binaries test samples ltml-samples ltml-samples-open ltml-image-sample-local ltml-image-sample-remote
22

3-
SAMPLES := $(sort $(wildcard samples/*/*.go))
43
BIN_DIR := bin
54
BINARY_PKGS := ./cmd/render-ltml ./cmd/serve-ltml ./ttdump
65
LTML_IMAGE_SAMPLE := ltml/samples/test_031_render_ltml_images.ltml
@@ -22,10 +21,7 @@ test:
2221
go test ./...
2322

2423
samples:
25-
@for sample in $(SAMPLES); do \
26-
echo "==> go run $$sample"; \
27-
go run $$sample || exit $$?; \
28-
done
24+
go run ./samples -all
2925

3026
ltml-samples:
3127
LTML_UA=true go test ./ltml -count=1 -run TestSamples -write-sample-pdfs

font/font.go

Lines changed: 4 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ package font
55

66
import (
77
"fmt"
8-
"strconv"
98

10-
"github.com/rowland/leadtype/colors"
119
"github.com/rowland/leadtype/options"
1210
"github.com/rowland/leadtype/shaping"
1311
)
@@ -33,58 +31,14 @@ type Font struct {
3331
// (e.g. Arabic). It is set automatically by New when the winning FontSource
3432
// implements ShaperSource.
3533
Shaper shaping.Shaper
36-
37-
strikeoutPosition int16
38-
strikeoutThickness int16
39-
strikeoutColor colors.Color
40-
strikeoutCapStyle string
41-
underlinePosition int16
42-
underlineThickness int16
43-
underlineColor colors.Color
44-
underlineCapStyle string
45-
46-
strikeoutPositionSet bool
47-
strikeoutThicknessSet bool
48-
strikeoutColorSet bool
49-
strikeoutCapStyleSet bool
50-
underlinePositionSet bool
51-
underlineThicknessSet bool
52-
underlineColorSet bool
53-
underlineCapStyleSet bool
5434
}
5535

5636
func New(family string, options options.Options, fontSources FontSources) (*Font, error) {
57-
strikeoutPosition, strikeoutPositionSet := decorationInt16Option(options, "strikeout_position")
58-
strikeoutThickness, strikeoutThicknessSet := decorationInt16Option(options, "strikeout_thickness")
59-
strikeoutColor, strikeoutColorSet := decorationColorOption(options, "strikeout_color")
60-
strikeoutCapStyle, strikeoutCapStyleSet := decorationLineCapStyleOption(options, "strikeout_cap")
61-
62-
underlinePosition, underlinePositionSet := decorationInt16Option(options, "underline_position")
63-
underlineThickness, underlineThicknessSet := decorationInt16Option(options, "underline_thickness")
64-
underlineColor, underlineColorSet := decorationColorOption(options, "underline_color")
65-
underlineCapStyle, underlineCapStyleSet := decorationLineCapStyleOption(options, "underline_cap")
66-
6737
font := &Font{
68-
family: family,
69-
Weight: options.StringDefault("weight", ""),
70-
style: options.StringDefault("style", ""),
71-
RelativeSize: options.FloatDefault("relative_size", 100) / 100.0,
72-
strikeoutPosition: strikeoutPosition,
73-
strikeoutThickness: strikeoutThickness,
74-
strikeoutColor: strikeoutColor,
75-
strikeoutCapStyle: strikeoutCapStyle,
76-
underlinePosition: underlinePosition,
77-
underlineThickness: underlineThickness,
78-
underlineColor: underlineColor,
79-
underlineCapStyle: underlineCapStyle,
80-
strikeoutPositionSet: strikeoutPositionSet,
81-
strikeoutThicknessSet: strikeoutThicknessSet,
82-
strikeoutColorSet: strikeoutColorSet,
83-
strikeoutCapStyleSet: strikeoutCapStyleSet,
84-
underlinePositionSet: underlinePositionSet,
85-
underlineThicknessSet: underlineThicknessSet,
86-
underlineColorSet: underlineColorSet,
87-
underlineCapStyleSet: underlineCapStyleSet,
38+
family: family,
39+
Weight: options.StringDefault("weight", ""),
40+
style: options.StringDefault("style", ""),
41+
RelativeSize: options.FloatDefault("relative_size", 100) / 100.0,
8842
}
8943
if Ranges, ok := options["ranges"]; ok {
9044
switch Ranges := Ranges.(type) {
@@ -194,22 +148,6 @@ func (font *Font) Matches(other *Font) bool {
194148
font.subType == other.subType &&
195149
font.RuneSet == other.RuneSet &&
196150
font.RelativeSize == other.RelativeSize &&
197-
font.strikeoutPosition == other.strikeoutPosition &&
198-
font.strikeoutThickness == other.strikeoutThickness &&
199-
font.strikeoutColor == other.strikeoutColor &&
200-
font.strikeoutCapStyle == other.strikeoutCapStyle &&
201-
font.underlinePosition == other.underlinePosition &&
202-
font.underlineThickness == other.underlineThickness &&
203-
font.underlineColor == other.underlineColor &&
204-
font.underlineCapStyle == other.underlineCapStyle &&
205-
font.strikeoutPositionSet == other.strikeoutPositionSet &&
206-
font.strikeoutThicknessSet == other.strikeoutThicknessSet &&
207-
font.strikeoutColorSet == other.strikeoutColorSet &&
208-
font.strikeoutCapStyleSet == other.strikeoutCapStyleSet &&
209-
font.underlinePositionSet == other.underlinePositionSet &&
210-
font.underlineThicknessSet == other.underlineThicknessSet &&
211-
font.underlineColorSet == other.underlineColorSet &&
212-
font.underlineCapStyleSet == other.underlineCapStyleSet &&
213151
stringSlicesEqual(font.Ranges, other.Ranges)
214152
}
215153

@@ -233,33 +171,13 @@ func (font *Font) SupportsArabic() bool {
233171
}
234172

235173
func (font *Font) StrikeoutPosition() int {
236-
if font.strikeoutPositionSet {
237-
return int(font.strikeoutPosition)
238-
}
239174
return font.metrics.StrikeoutPosition()
240175
}
241176

242177
func (font *Font) StrikeoutThickness() int {
243-
if font.strikeoutThicknessSet {
244-
return int(font.strikeoutThickness)
245-
}
246178
return font.metrics.StrikeoutThickness()
247179
}
248180

249-
func (font *Font) StrikeoutColor() (colors.Color, bool) {
250-
if font.strikeoutColorSet {
251-
return font.strikeoutColor, true
252-
}
253-
return 0, false
254-
}
255-
256-
func (font *Font) StrikeoutCapStyle() string {
257-
if font.strikeoutCapStyleSet {
258-
return font.strikeoutCapStyle
259-
}
260-
return ""
261-
}
262-
263181
func (font *Font) Style() string {
264182
return font.style
265183
}
@@ -269,33 +187,13 @@ func (font *Font) SubType() string {
269187
}
270188

271189
func (font *Font) UnderlinePosition() int {
272-
if font.underlinePositionSet {
273-
return int(font.underlinePosition)
274-
}
275190
return font.metrics.UnderlinePosition()
276191
}
277192

278193
func (font *Font) UnderlineThickness() int {
279-
if font.underlineThicknessSet {
280-
return int(font.underlineThickness)
281-
}
282194
return font.metrics.UnderlineThickness()
283195
}
284196

285-
func (font *Font) UnderlineColor() (colors.Color, bool) {
286-
if font.underlineColorSet {
287-
return font.underlineColor, true
288-
}
289-
return 0, false
290-
}
291-
292-
func (font *Font) UnderlineCapStyle() string {
293-
if font.underlineCapStyleSet {
294-
return font.underlineCapStyle
295-
}
296-
return ""
297-
}
298-
299197
func (font *Font) UnitsPerEm() int {
300198
return font.metrics.UnitsPerEm()
301199
}
@@ -362,83 +260,3 @@ func stringSlicesEqual(sl1, sl2 []string) bool {
362260
}
363261
return true
364262
}
365-
366-
func normalizeDecorationLineCapStyle(style string) string {
367-
switch style {
368-
case "butt_cap", "round_cap", "projecting_square_cap":
369-
return style
370-
default:
371-
return ""
372-
}
373-
}
374-
375-
func decorationColorOption(opts options.Options, key string) (colors.Color, bool) {
376-
value, ok := opts[key]
377-
if !ok {
378-
return 0, false
379-
}
380-
switch value := value.(type) {
381-
case colors.Color:
382-
return value, true
383-
case string:
384-
color, err := colors.NamedColor(value)
385-
if err != nil {
386-
return 0, false
387-
}
388-
return color, true
389-
case int:
390-
return colors.Color(value), true
391-
case int32:
392-
return colors.Color(value), true
393-
default:
394-
return 0, false
395-
}
396-
}
397-
398-
func decorationLineCapStyleOption(opts options.Options, key string) (string, bool) {
399-
value, ok := opts[key]
400-
if !ok {
401-
return "", false
402-
}
403-
style, ok := value.(string)
404-
if !ok {
405-
return "", false
406-
}
407-
style = normalizeDecorationLineCapStyle(style)
408-
if style == "" {
409-
return "", false
410-
}
411-
return style, true
412-
}
413-
414-
func decorationInt16Option(opts options.Options, key string) (int16, bool) {
415-
value, ok := opts[key]
416-
if !ok {
417-
return 0, false
418-
}
419-
switch value := value.(type) {
420-
case int:
421-
return int16InRange(value)
422-
case int16:
423-
return value, true
424-
case int32:
425-
return int16InRange(int(value))
426-
case float64:
427-
return int16InRange(int(value))
428-
case string:
429-
i, err := strconv.Atoi(value)
430-
if err != nil {
431-
return 0, false
432-
}
433-
return int16InRange(i)
434-
default:
435-
return 0, false
436-
}
437-
}
438-
439-
func int16InRange(value int) (int16, bool) {
440-
if value < -32768 || value > 32767 {
441-
return 0, false
442-
}
443-
return int16(value), true
444-
}

font/font_test.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"testing"
88

99
"github.com/rowland/leadtype/afm"
10-
"github.com/rowland/leadtype/colors"
11-
"github.com/rowland/leadtype/options"
1210
"github.com/rowland/leadtype/shaping"
1311
"github.com/rowland/leadtype/ttf"
1412
)
@@ -39,91 +37,6 @@ func TestFont_Matches(t *testing.T) {
3937
check(t, f1.Matches(f2), "Fonts should match.")
4038
}
4139

42-
func TestFont_Matches_DistinguishesDecorationCapOverrides(t *testing.T) {
43-
metrics, err := afm.LoadFont("data/fonts/Helvetica.afm")
44-
if err != nil {
45-
t.Skip(err)
46-
}
47-
f1 := &Font{metrics: metrics}
48-
f2 := &Font{metrics: metrics, underlineCapStyle: "round_cap", underlineCapStyleSet: true}
49-
f3 := &Font{metrics: metrics, underlineCapStyle: "round_cap", underlineCapStyleSet: true}
50-
51-
check(t, !f1.Matches(f2), "Fonts with different underline cap overrides should not match.")
52-
check(t, f2.Matches(f3), "Fonts with matching underline cap overrides should match.")
53-
check(t, f2.UnderlineCapStyle() == "round_cap", "UnderlineCapStyle should return explicit override.")
54-
}
55-
56-
func TestFont_Matches_DistinguishesDecorationColorOverrides(t *testing.T) {
57-
metrics, err := afm.LoadFont("data/fonts/Helvetica.afm")
58-
if err != nil {
59-
t.Skip(err)
60-
}
61-
f1 := &Font{metrics: metrics}
62-
f2 := &Font{metrics: metrics, underlineColor: colors.Green, underlineColorSet: true}
63-
f3 := &Font{metrics: metrics, underlineColor: colors.Green, underlineColorSet: true}
64-
65-
check(t, !f1.Matches(f2), "Fonts with different underline color overrides should not match.")
66-
check(t, f2.Matches(f3), "Fonts with matching underline color overrides should match.")
67-
color, ok := f2.UnderlineColor()
68-
check(t, ok, "UnderlineColor should report an explicit override.")
69-
check(t, color == colors.Green, "UnderlineColor should return explicit override.")
70-
}
71-
72-
func TestFont_Matches_DistinguishesDecorationMetricOverrides(t *testing.T) {
73-
metrics, err := afm.LoadFont("data/fonts/Helvetica.afm")
74-
if err != nil {
75-
t.Skip(err)
76-
}
77-
f1 := &Font{metrics: metrics}
78-
f2 := &Font{metrics: metrics, underlinePosition: -123, underlinePositionSet: true}
79-
f3 := &Font{metrics: metrics, underlinePosition: -123, underlinePositionSet: true}
80-
81-
check(t, !f1.Matches(f2), "Fonts with different underline position overrides should not match.")
82-
check(t, f2.Matches(f3), "Fonts with matching underline position overrides should match.")
83-
check(t, f2.UnderlinePosition() == -123, "UnderlinePosition should return explicit override.")
84-
}
85-
86-
func TestDecorationColorOption_IgnoresInvalidStrings(t *testing.T) {
87-
_, ok := decorationColorOption(options.Options{"underline_color": "not-a-color"}, "underline_color")
88-
check(t, !ok, "Invalid decoration colors should be ignored.")
89-
90-
color, ok := decorationColorOption(options.Options{"underline_color": "black"}, "underline_color")
91-
check(t, ok, "Valid named colors should be accepted.")
92-
check(t, color == colors.Black, "Valid named colors should round-trip.")
93-
}
94-
95-
func TestDecorationLineCapStyleOption_IgnoresInvalidValues(t *testing.T) {
96-
_, ok := decorationLineCapStyleOption(options.Options{"underline_cap": "not-a-cap"}, "underline_cap")
97-
check(t, !ok, "Invalid decoration cap styles should be ignored.")
98-
99-
_, ok = decorationLineCapStyleOption(options.Options{"underline_cap": 1}, "underline_cap")
100-
check(t, !ok, "Non-string decoration cap styles should be ignored.")
101-
}
102-
103-
func TestDecorationLineCapStyleOption_AcceptsValidValues(t *testing.T) {
104-
style, ok := decorationLineCapStyleOption(options.Options{"underline_cap": "round_cap"}, "underline_cap")
105-
check(t, ok, "Valid decoration cap styles should be accepted.")
106-
check(t, style == "round_cap", "Valid decoration cap styles should round-trip.")
107-
}
108-
109-
func TestDecorationInt16Option_IgnoresInvalidStrings(t *testing.T) {
110-
_, ok := decorationInt16Option(options.Options{"underline_position": "not-a-number"}, "underline_position")
111-
check(t, !ok, "Invalid decoration numbers should be ignored.")
112-
113-
_, ok = decorationInt16Option(options.Options{"underline_position": 40000}, "underline_position")
114-
check(t, !ok, "Out-of-range decoration numbers should be ignored.")
115-
}
116-
117-
func TestDecorationInt16Option_AcceptsValidValues(t *testing.T) {
118-
value, ok := decorationInt16Option(options.Options{"underline_position": "-123"}, "underline_position")
119-
check(t, ok, "Valid string numbers should be accepted.")
120-
check(t, value == -123, "Valid string numbers should round-trip.")
121-
122-
value, ok = decorationInt16Option(options.Options{"underline_thickness": 50.9}, "underline_thickness")
123-
check(t, ok, "Valid float values should be accepted.")
124-
check(t, value == 50, "Float decoration numbers should match existing truncation semantics.")
125-
}
126-
12740
func TestFont_SupportsArabic_TTFArabic(t *testing.T) {
12841
amiri, err := ttf.LoadFont("../shaping/testdata/Amiri-Regular.ttf")
12942
if err != nil {

pdf/page_writer.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,6 @@ func (pw *PageWriter) drawUnderline(
11281128
func pieceUnderlineStyle(p *rich_text.RichText) (position, thickness float64, lineColor colors.Color, hasLineColor bool, capStyle string, linePattern string, hasLinePattern bool) {
11291129
position = float64(p.UnderlinePosition)
11301130
thickness = float64(p.UnderlineThickness)
1131-
if p.Font != nil {
1132-
lineColor, hasLineColor = p.Font.UnderlineColor()
1133-
capStyle = p.Font.UnderlineCapStyle()
1134-
}
11351131
if p.Decoration != nil {
11361132
line := p.Decoration.Underline
11371133
if line.HasPosition {
@@ -1158,10 +1154,6 @@ func pieceUnderlineStyle(p *rich_text.RichText) (position, thickness float64, li
11581154
func pieceStrikeoutStyle(p *rich_text.RichText) (position, thickness float64, lineColor colors.Color, hasLineColor bool, capStyle string, linePattern string, hasLinePattern bool) {
11591155
position = float64(p.StrikeoutPosition)
11601156
thickness = float64(p.StrikeoutThickness)
1161-
if p.Font != nil {
1162-
lineColor, hasLineColor = p.Font.StrikeoutColor()
1163-
capStyle = p.Font.StrikeoutCapStyle()
1164-
}
11651157
if p.Decoration != nil {
11661158
line := p.Decoration.Strikeout
11671159
if line.HasPosition {

0 commit comments

Comments
 (0)