-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
242 lines (213 loc) · 5.94 KB
/
config.go
File metadata and controls
242 lines (213 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
package freezelib
import (
"fmt"
"strconv"
"strings"
)
// Config represents the configuration for generating code screenshots
type Config struct {
// Window settings
Background string `json:"background"`
Margin []float64 `json:"margin"`
Padding []float64 `json:"padding"`
Window bool `json:"window"`
Width float64 `json:"width"`
Height float64 `json:"height"`
// Language and theme
Language string `json:"language"`
Theme string `json:"theme"`
Wrap int `json:"wrap"`
// Decoration
Border Border `json:"border"`
Shadow Shadow `json:"shadow"`
// Font
Font Font `json:"font"`
// Line settings
LineHeight float64 `json:"line_height"`
Lines []int `json:"lines"`
ShowLineNumbers bool `json:"show_line_numbers"`
}
// Shadow configuration for drop shadow effects
type Shadow struct {
Blur float64 `json:"blur"`
X float64 `json:"x"`
Y float64 `json:"y"`
}
// Border configuration for window borders
type Border struct {
Radius float64 `json:"radius"`
Width float64 `json:"width"`
Color string `json:"color"`
}
// Font configuration
type Font struct {
Family string `json:"family"`
File string `json:"file"`
Size float64 `json:"size"`
Ligatures bool `json:"ligatures"`
}
// DefaultConfig returns a default configuration
func DefaultConfig() *Config {
return &Config{
Background: "#171717",
Margin: []float64{0},
Padding: []float64{20},
Window: false,
Width: 0,
Height: 0,
Language: "",
Theme: "charm",
Wrap: 0,
Border: Border{Radius: 0, Width: 0, Color: "#515151"},
Shadow: Shadow{Blur: 0, X: 0, Y: 0},
Font: Font{Family: "JetBrains Mono", Size: 14, Ligatures: true},
LineHeight: 1.2,
Lines: []int{},
ShowLineNumbers: false,
}
}
// SetPadding sets padding for all sides or specific sides
// Accepts 1, 2, or 4 values like CSS padding
func (c *Config) SetPadding(values ...float64) *Config {
c.Padding = values
return c
}
// SetMargin sets margin for all sides or specific sides
// Accepts 1, 2, or 4 values like CSS margin
func (c *Config) SetMargin(values ...float64) *Config {
c.Margin = values
return c
}
// SetFont sets font family and size
func (c *Config) SetFont(family string, size float64) *Config {
c.Font.Family = family
c.Font.Size = size
return c
}
// SetTheme sets the syntax highlighting theme
func (c *Config) SetTheme(theme string) *Config {
c.Theme = theme
return c
}
// SetLanguage sets the programming language for syntax highlighting
func (c *Config) SetLanguage(language string) *Config {
c.Language = language
return c
}
// SetBackground sets the background color
func (c *Config) SetBackground(color string) *Config {
c.Background = color
return c
}
// SetWindow enables or disables window controls
func (c *Config) SetWindow(enabled bool) *Config {
c.Window = enabled
return c
}
// SetLineNumbers enables or disables line numbers
func (c *Config) SetLineNumbers(enabled bool) *Config {
c.ShowLineNumbers = enabled
return c
}
// SetShadow sets shadow properties
func (c *Config) SetShadow(blur, x, y float64) *Config {
c.Shadow = Shadow{Blur: blur, X: x, Y: y}
return c
}
// SetBorder sets border properties
func (c *Config) SetBorder(width, radius float64, color string) *Config {
c.Border = Border{Width: width, Radius: radius, Color: color}
return c
}
// SetDimensions sets the output dimensions
func (c *Config) SetDimensions(width, height float64) *Config {
c.Width = width
c.Height = height
return c
}
// SetLines sets the line range to capture (1-indexed)
func (c *Config) SetLines(start, end int) *Config {
if start > 0 && end > 0 && start <= end {
c.Lines = []int{start - 1, end - 1} // Convert to 0-indexed
}
return c
}
// expandPadding expands padding values according to CSS rules
func (c *Config) expandPadding(scale float64) []float64 {
p := c.Padding
switch len(p) {
case 1:
return []float64{p[0] * scale, p[0] * scale, p[0] * scale, p[0] * scale}
case 2:
return []float64{p[0] * scale, p[1] * scale, p[0] * scale, p[1] * scale}
case 4:
return []float64{p[0] * scale, p[1] * scale, p[2] * scale, p[3] * scale}
default:
return []float64{0, 0, 0, 0}
}
}
// expandMargin expands margin values according to CSS rules
func (c *Config) expandMargin(scale float64) []float64 {
m := c.Margin
switch len(m) {
case 1:
return []float64{m[0] * scale, m[0] * scale, m[0] * scale, m[0] * scale}
case 2:
return []float64{m[0] * scale, m[1] * scale, m[0] * scale, m[1] * scale}
case 4:
return []float64{m[0] * scale, m[1] * scale, m[2] * scale, m[3] * scale}
default:
return []float64{0, 0, 0, 0}
}
}
// Clone creates a deep copy of the configuration
func (c *Config) Clone() *Config {
clone := *c
clone.Margin = make([]float64, len(c.Margin))
copy(clone.Margin, c.Margin)
clone.Padding = make([]float64, len(c.Padding))
copy(clone.Padding, c.Padding)
clone.Lines = make([]int, len(c.Lines))
copy(clone.Lines, c.Lines)
return &clone
}
// Validate checks if the configuration is valid
func (c *Config) Validate() error {
if c.Font.Size <= 0 {
return fmt.Errorf("font size must be positive")
}
if c.LineHeight <= 0 {
return fmt.Errorf("line height must be positive")
}
if len(c.Lines) == 2 && c.Lines[0] > c.Lines[1] {
return fmt.Errorf("start line must be less than or equal to end line")
}
return nil
}
// parseColor validates and normalizes color values
func parseColor(color string) string {
color = strings.TrimSpace(color)
if color == "" {
return "#000000"
}
if !strings.HasPrefix(color, "#") {
color = "#" + color
}
return color
}
// dimensionToInt converts dimension strings to integers
func dimensionToInt(dimension string) int {
dimension = strings.TrimSuffix(dimension, "px")
val, err := strconv.Atoi(dimension)
if err != nil {
return 0
}
return val
}
// side constants for padding/margin indexing
const (
top = 0
right = 1
bottom = 2
left = 3
)