forked from MatthewBlanchard/MFEKStroke
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_along_path.rs
More file actions
345 lines (317 loc) · 14.5 KB
/
Copy pathpattern_along_path.rs
File metadata and controls
345 lines (317 loc) · 14.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::fs;
use float_cmp::ApproxEq as _;
use float_cmp::F64Margin;
use MFEKmath::pattern_along_glif;
use glifparser::glif::contour_operations::pap::{PatternCopies, PatternStretch, PatternSubdivide};
use MFEKmath::vec2;
use MFEKmath::vector::Vector;
use MFEKmath::EvalScale;
use MFEKmath::Piecewise;
use MFEKmath::PatternSettings;
use clap::{App, AppSettings, Arg};
pub fn clap_app() -> clap::App<'static> {
App::new("PAP")
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::AllowNegativeNumbers)
.alias("patterned")
.alias("pap")
.alias("PaP")
.about("Maps a pattern glyph along a path glyph.")
.version("0.2.1")
.author("Matthew Blanchard <matthewrblanchard@gmail.com>; Fredrick R. Brennan <copypasteⒶkittens.ph>; MFEK Authors")
.arg(Arg::new("pattern")
.long("pattern")
.short('p')
.takes_value(true)
//.allow_invalid_utf8(true)
.required_unless_present_any(&["dot-pattern", "dash-pattern"])
.conflicts_with_all(&["dot-pattern", "dash-pattern"])
.help("The path to the input pattern file. You may also provide either --dot-pattern or --dash-pattern to use built-in patterns."))
.arg(Arg::new("dash-pattern")
.long("dash-pattern")
.short('=')
.conflicts_with("dot-pattern")
.help("Use a simple dash pattern"))
.arg(Arg::new("warp")
.long("warp")
.short('w')
.help("Warp the pattern to fit the path."))
.arg(Arg::new("dot-pattern")
.long("dot-pattern")
.short('.')
.conflicts_with("dash-pattern")
.help("Use a simple dot pattern"))
.arg(Arg::new("path")
.long("path")
.short('P')
.takes_value(true)
//.allow_invalid_utf8(true)
.help("The path to the input path file.")
.required(true))
.arg(Arg::new("output")
.long("output")
.alias("out")
.short('o')
.takes_value(true)
//.allow_invalid_utf8(true)
.help("The path where the output will be saved. If omitted, or `-`, stdout.\n\n\n"))
.arg(Arg::new("contour")
.long("contour")
.short('c')
.takes_value(true)
.validator(super::arg_validator_isize)
.default_value("-1")
.help("<isize> if this is a positive number we stroke only that specific contour in the outline by index."))
.arg(Arg::new("mode")
.short('m')
.long("mode")
.takes_value(true)
.default_value("single")
.possible_values(&["single", "repeated"])
.help("Repeat mode."))
.arg(Arg::new("subdivide")
.short('s')
.long("subdivide")
.takes_value(true)
.default_value("0")
.hide_default_value(true)
.validator(super::arg_validator_usize)
.help("<usize> how many times to subdivide the patterns at their midpoint. [default: 0]\n\n\n"))
.arg(Arg::new("subdivide_angle")
.short('°')
.long("subdivide-angle")
.takes_value(true)
.default_value("1")
.hide_default_value(true)
.validator(super::arg_validator_positive_f64)
.help("<f64> how many degrees of change in direction to subdivide the patterns at. [default: 0]\n\n\n"))
.arg(Arg::new("sx")
.long("sx")
.short('X')
.takes_value(true)
.default_value("1")
.validator(super::arg_validator_positive_f64)
.help("<f64> how much we scale our input pattern on the x-axis."))
.arg(Arg::new("sy")
.long("sy")
.short('Y')
.takes_value(true)
.default_value("1")
.validator(super::arg_validator_positive_f64)
.help("<f64> how much we scale our input pattern on the y-axis."))
.arg(Arg::new("split_at_discontinuity")
.short('|')
.long("split-at-discontinuity")
.help("Handle discontinuities by splitting the path.")
)
.arg(Arg::new("normal-offset")
.long("noffset")
.short('n')
.takes_value(true)
.default_value("0")
.validator(super::arg_validator_f64)
.help("<f64> how much to offset the pattern along the normal of the path."))
.arg(Arg::new("tangent-offset")
.long("toffset")
.short('t')
.takes_value(true)
.default_value("0")
.hide_default_value(true)
.validator(super::arg_validator_f64)
.help("<f64> how much to offset the pattern along the tangent of the path. [default: 0]\n\n\n"))
.arg(Arg::new("spacing")
.long("spacing")
.short('W')
.takes_value(true)
.default_value("0")
.validator(super::arg_validator_positive_or_zero_f64)
.help("<f64> how much padding to trail each copy with."))
.arg(Arg::new("stretch")
.long("stretch")
.short('!')
.takes_value(true)
.possible_values(&["spacing"])
.help("<stretch> false if not given, true if given, spacing mode if value of spacing given"))
.arg(Arg::new("simplify")
.short('S')
.long("simplify")
.help("<boolean> if we should run the result through Skia's (buggy) simplify routine."))
.arg(Arg::new("remove_overlapping")
.long("remove-overlapping")
.short('O')
.conflicts_with_all(&["erase_overlapping_stroke_width", "erase_overlapping", "erase_overlapping_area_percent"])
.help("Remove patterns that would overlap."))
.arg(Arg::new("erase_overlapping")
.long("erase-overlapping")
.short('Z')
.help("Erase the area underneath patterns that would overlap."))
.arg(Arg::new("erase_overlapping_stroke_width")
.long("erase-overlapping-stroke")
.short('z')
.takes_value(true)
.default_value("5")
.hide_default_value(true)
.validator(super::arg_validator_f64)
.help("<float> how much we should expand the pattern when erasing overlapping patterns."))
.arg(Arg::new("erase_overlapping_area_percent")
.short('%')
.long("erase-overlapping-area-percent")
.takes_value(true)
.default_value("5")
.hide_default_value(true)
.validator(super::arg_validator_f64)
.help("<float> how much we should expand the pattern when erasing overlapping patterns."))
.arg(Arg::new("one-pass")
.long("one-pass")
.short('Q')
.help("<boolean> whether we should not reflow the path after culling during overdraw (faster but worse)."))
.arg(Arg::new("no-center-pattern")
.long("no-center-pattern")
.short('C')
.conflicts_with_all(&["sx", "sy"])
.help("<boolean> supply if you wish to center the pattern"))
.arg(Arg::new("reverse")
.long("reverse")
.short('r')
.help("<boolean> true will reverse the path."))
.arg(Arg::new("reverse-culling")
.long("reverse-culling")
.short('R')
.help("<boolean> true will reverse the order we check for overlaps during overlap culling.\n\n\n"))
}
pub fn pap_cli(matches: &clap::ArgMatches) {
let path_string = matches.value_of("path").unwrap(); // required options shouldn't panic
let pattern_string = matches.value_of("pattern");
let output_string = matches.value_of("output");
// TODO: Handle errors properly!
let path: glifparser::Glif<()> = glifparser::read(&fs::read_to_string(path_string).expect("Failed to read path file!"))
.expect("glifparser couldn't parse input path glif. Invalid glif?");
let pattern: glifparser::Glif<()> = match pattern_string {
None => {
if matches.is_present("dot-pattern") {
let mut dot = glifparser::read(include_str!("../assets/dot.glif")).unwrap();
let piece_pattern = Piecewise::from(dot.outline.as_ref().unwrap());
let normalized_pattern = piece_pattern.scale(vec2!(1. / 20., 1. / 20.));
dot.outline = Some(normalized_pattern.to_outline());
dot
} else if matches.is_present("dash-pattern") {
let mut dash = glifparser::read(include_str!("../assets/dash.glif")).unwrap();
let piece_pattern = Piecewise::from(dash.outline.as_ref().unwrap());
let normalized_pattern = piece_pattern.scale(vec2!(1. / 20., 1. / 20.));
dash.outline = Some(normalized_pattern.to_outline());
dash
} else {
unreachable!()
}
}
Some(pattern) => glifparser::read(&fs::read_to_string(pattern).expect("Failed to read pattern file!"))
.expect("glifparser couldn't parse input pattern glif. Invalid glif?"),
};
let mut settings = PatternSettings {
copies: PatternCopies::Single,
subdivide: PatternSubdivide::Off,
is_vertical: false,
normal_offset: 0.,
tangent_offset: 0.,
center_pattern: true,
pattern_scale: Vector { x: 1., y: 1. },
spacing: 0.,
stretch: PatternStretch::Spacing,
simplify: false,
cull_overlap: glifparser::glif::contour_operations::pap::PatternCulling::Off,
two_pass_culling: false,
reverse_path: false,
reverse_culling: false,
split_path: false,
warp_pattern: false,
};
if let Some(copies) = matches.value_of("mode") {
match copies {
"single" => settings.copies = PatternCopies::Single,
"repeated" => settings.copies = PatternCopies::Repeated,
_ => eprintln!("Invalid mode argument. Falling back to default. (Single)"),
}
}
if let Some(sx_string) = matches.value_of("sx") {
settings.pattern_scale.x = sx_string.parse::<f64>().unwrap();
}
if let Some(sy_string) = matches.value_of("sy") {
settings.pattern_scale.y = sy_string.parse::<f64>().unwrap();
}
if let Some(sub_string) = matches.value_of("subdivide") {
let n = sub_string.parse::<usize>().unwrap();
settings.subdivide = match n {
0 => PatternSubdivide::Off,
_ => PatternSubdivide::Simple(n),
};
}
if let Some(sub_angle_string) = matches.value_of("subdivide_angle") {
let n = sub_angle_string.parse::<f64>().unwrap();
if 0.0f64.approx_eq(n, F64Margin { ulps: 2, epsilon: 0.01 }) {
settings.subdivide = PatternSubdivide::Off;
} else {
settings.subdivide = PatternSubdivide::Angle(n);
}
}
if let Some(spacing_string) = matches.value_of("spacing") {
settings.spacing = spacing_string.parse::<f64>().unwrap();
}
if let Some(normal_string) = matches.value_of("normal-offset") {
settings.normal_offset = normal_string.parse::<f64>().unwrap();
}
if let Some(tangent_string) = matches.value_of("tangent-offset") {
settings.tangent_offset = tangent_string.parse::<f64>().unwrap();
}
settings.warp_pattern = matches.is_present("warp");
settings.center_pattern = !matches.is_present("no-center-pattern");
settings.simplify = matches.is_present("simplify");
if matches.value_of("remove_overlapping").is_some() {
settings.cull_overlap = glifparser::glif::contour_operations::pap::PatternCulling::RemoveOverlapping;
}
if matches.is_present("erase_overlapping") {
settings.cull_overlap = glifparser::glif::contour_operations::pap::PatternCulling::EraseOverlapping(
matches
.value_of("erase_overlapping_stroke_width")
.unwrap_or("5")
.parse::<f64>()
.unwrap(),
matches
.value_of("erase_overlapping_area_percent")
.unwrap_or("25")
.parse::<f64>()
.unwrap(),
);
}
settings.split_path = matches.is_present("split_at_discontinuity");
// We know the string must be "spacing" as that's the only .possible_value to clap::Arg
if let Some(s) = matches.value_of("stretch") {
debug_assert_eq!(s, "spacing");
settings.stretch = PatternStretch::Spacing;
} else if matches.is_present("stretch") {
settings.stretch = PatternStretch::On;
} else {
settings.stretch = PatternStretch::Off;
}
let mut target_contour = None;
if let Some(contour) = matches.value_of("contour") {
let idx = contour.parse::<isize>().unwrap();
match (idx, path.outline.as_ref().map(|o| o.len() as isize >= idx), idx == -1) {
(n, Some(false), false) => target_contour = Some(n as usize),
(_, _, true) => {} // -1 ⇒ do nothing, target_contour already None
_ => eprintln!("Invalid contour argument. Falling back to default. (-1)"),
}
}
settings.two_pass_culling = !matches.is_present("one-pass");
settings.reverse_path = matches.is_present("reverse");
settings.reverse_culling = matches.is_present("reverse-culling");
let output = pattern_along_glif(&path, &pattern, &settings, target_contour);
let glifstring = glifparser::write(&output).expect("glifparser failed to understand output of PaP?"); // TODO: Proper error handling.
if let Some(output_file) = output_string {
if output_file != "-" {
// common stand-in for stdout on *nix
fs::write(output_file, &glifstring).expect("Unable to write output file");
return;
}
}
print!("{}", glifstring);
}