forked from KanjiVG/kanjivg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-all-strokes.pl
executable file
·155 lines (128 loc) · 3.59 KB
/
check-all-strokes.pl
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
#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use XML::Parser;
use FindBin;
use Image::SVG::Path 'extract_path_info';
use utf8;
my $dir = "$FindBin::Bin/kanjivg";
# The grep only allows the "normal" files from the complete list of
# files.
my @files = grep /\/[0-9a-f]+\.svg$/, <$dir/*.svg>;
my %stroke_types;
my %global;
my %angles;
# List of errors which are known to come from bad information about
# stroke types.
my @known_bad_elements = qw/冬 羽 尽 辛 手 羊 冫 半/;
my %known_bad_elements = map {$_ => 1} @known_bad_elements;
#print keys %known_bad_elements;
$global{known_bad_elements} = \%known_bad_elements;
my $parser = XML::Parser->new (
Handlers => {
Start => sub { &{handle_start} (\%global, @_) },
},
);
# This doesn't let us use current_line.
#$global{parser} = $parser;
for my $file (@files) {
#for my $file (qw!kanjivg/087bd.svg!) {
$global{file} = $file;
$global{bad_element} = undef;
$parser->parsefile ($file);
}
#for my $t (sort keys %stroke_types) {
# print "$t\n";
#}
my %average;
for my $t (sort keys %angles) {
if ($t eq 'None') {
next;
}
my $total_angle = 0;
my $n = 0;
for my $se (@{$angles{$t}}) {
my ($start, $end) = @$se;
my $angle = atan2 ($end->[1] - $start->[1], $end->[0] - $start->[0]);
$total_angle += $angle;
$n++;
}
$average{$t} = $total_angle / $n;
# The following line prints out the "type" field and the average angle
# in radians.
# print "$t $average{$t}\n";
}
my $limit = 1.0;
for my $t (sort keys %angles) {
if ($t eq 'None') {
next;
}
for my $se (@{$angles{$t}}) {
my ($start, $end, $location) = @$se;
my $angle = atan2 ($end->[1] - $start->[1], $end->[0] - $start->[0]);
if ($angle - $average{$t} > $limit) {
print $location, "more than $limit radian from average.\n"
}
}
}
exit;
sub handle_start
{
my ($global_ref, $parser, $element, %attr) = @_;
if ($global_ref->{bad_element}) {
return;
}
# Use the expat parser so we can use current_line.
$global_ref->{parser} = $parser;
if ($element eq 'path') {
gather_path_info ($global_ref, \%attr);
}
elsif ($element eq 'g') {
if ($attr{id} =~ /^([0-9a-f]+)$/) {
$global_ref->{kanji_id} = $attr{id};
}
my $el = $attr{"kanjivg:element"};
# print "element $el\n";
if (defined $el) {
if ($global_ref->{known_bad_elements}->{$el}) {
# print "Known bad element $el in $global_ref->{file}.\n";
$global_ref->{bad_element} = 1;
}
}
}
}
# Get the location for warning messages.
sub location
{
my ($global) = @_;
my $l = '';
$l .= $global->{file};
$l .= ":";
$l .= $global->{parser}->current_line ();
$l .= ": ";
return $l;
}
sub gather_path_info
{
my ($global_ref, $attr_ref) = @_;
my $type = $attr_ref->{'kanjivg:type'};
if (! $type) {
warn location ($global_ref), "no type.\n";
return;
}
$type =~ s/([^[:ascii:]])/"{" . sprintf ("%X", ord $1) . "}"/ge;
$stroke_types{$type}++;
my $d = $attr_ref->{d};
if (! $d) {
warn location ($global_ref), "no path.\n";
return;
}
my @info = extract_path_info ($d, {absolute => 1, no_shortcuts => 1});
my $start = $info[0]->{point};
my $end = $info[-1]->{end};
if (! $start || ! $end) {
warn location ($global_ref), "parse failed for '$d': no start/end";
return;
}
push @{$angles{$type}}, [$start, $end, location ($global_ref)];
}