-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAMC-db.pl.in
More file actions
187 lines (146 loc) · 5.67 KB
/
AMC-db.pl.in
File metadata and controls
187 lines (146 loc) · 5.67 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
#! @/PERLPATH/@
#
# Copyright (C) 2026 Alexis Bienvenüe <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice. If not, see
# <http://www.gnu.org/licenses/>.
use warnings;
use 5.012;
use File::Spec::Functions
qw/splitpath catpath splitdir catdir catfile rel2abs tmpdir/;
use File::Temp qw/ tempfile tempdir /;
use File::Copy;
use AMC::Path;
use AMC::Basic;
use AMC::Data;
my $project_dir = '';
my $data_dir = '';
my $scan = '';
my $dpi = 200;
my $downscale = 2;
my $scan_type = 'jpg';
my $image_type = 'png';
my $dest_file = '';
my $css_url = '';
GetProjectOptions(
":data:dir|data=s" => \$data_dir,
":project_dir|project=s" => \$project_dir,
"scan!" => \$scan,
"dpi=s" => \$dpi,
"dest-file=s" => \$dest_file,
"css-url=s" => \$css_url,
);
die "Needs data directory" if ( !$data_dir );
my $action = shift(@ARGV);
my $data = AMC::Data->new($data_dir);
sub reverse_transform {
my ($t) = @_;
my $delta = $t->{a} * $t->{d} - $t->{b} * $t->{c};
my $rev = {};
$rev->{a} = $t->{d} / $delta;
$rev->{b} = -$t->{b} / $delta;
$rev->{c} = -$t->{c} / $delta;
$rev->{d} = $t->{a} / $delta;
$rev->{e} = ( $t->{b} * $t->{f} - $t->{e} * $t->{d} ) / $delta;
$rev->{f} = ( $t->{e} * $t->{c} - $t->{a} * $t->{f} ) / $delta;
return ($rev);
}
sub transform_matrix {
my ($t) = @_;
return "matrix(" . join( " ", map { $t->{$_} } (qw/a c b d e f/) ) . ")";
}
if($action eq "upgrade") {
for my $module (qw/association capture layout report scoring/) {
$data->require_module($module);
}
} elsif($action eq "clear") {
$data->clear_data($project_dir, $ARGV[0]);
} elsif($action eq "layout-image") {
my @spc = (1, 1, 0);
if( $ARGV[0] =~ /^([0-9]+)\/([0-9]+)(?::([0-9]+))?$/ ) {
@spc = ($1, $2, $3);
} else {
die "Unparsable page id: $ARGV[0]";
}
$spc[2] = '0' if(! $spc[2]);
my @ep = @spc[ 0, 1 ];
my $layout = $data->module("layout");
my $capture = $data->module("capture");
$data->begin_read_transaction("DBIO");
my $info = $layout->page_info(@ep);
my $page = $info->{subjectpage};
my $mark_radius = $info->{markdiameter} / 2;
my $dest_image = $dest_file;
my $width = int($info->{width});
my $height = int($info->{height});
my $transform = "";
my $scan_file =
proj2abs( { '%PROJET' => $project_dir }, $capture->get_scan_page(@spc) );
my $scan_page = $capture->get_page(@spc);
my @boxes = $layout->page_boxes(@ep);
my @marks = $layout->page_marks(@ep);
my $namefield = $layout->page_zone(@ep, '__n');
$data->end_transaction("DBIO");
if ( $scan && -f $scan_file ) {
$dest_image .= "." . $scan_type;
system_debug(
cmd => [
"convert", $scan_file, $dest_image ]
);
$transform = transform_matrix(reverse_transform($scan_page));
} else {
my $temp_loc = tmpdir();
my $tempdir = tempdir(
DIRECTORY => $temp_loc,
CLEANUP => ( !get_debug() )
);
$dest_image .= "." . $image_type;
system_debug(
cmd => [
"gs", "-q",
"-dNOPAUSE", "-dBATCH",
"-sDEVICE=pnggray", "-dDownScaleFactor=$downscale",
"-r" . ( $dpi * $downscale ), "-dPDFFitPage",
"-dFirstPage=$page", "-dLastPage=$page",
"-dUseCropBox", "-sOutputFile=" . $dest_image,
"$project_dir/DOC-sujet.pdf"
]
);
}
print("VAR: image=$dest_image\n");
my $fn = $dest_image;
$fn =~ s/.*\///;
open(SVG, ">", "$dest_file.svg");
print SVG "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 $width $height\">\n";
print SVG '<link xmlns="http://www.w3.org/1999/xhtml" rel="stylesheet" href="'.$css_url.'" type="text/css" />'. "\n" if($css_url);
print SVG "<image x=\"0\" y=\"0\" width=\"$width\" height=\"$height\" href=\"$fn\"";
if($transform) {
print SVG " transform=\"$transform\"";
}
print SVG " />\n";
for my $box (@boxes) {
print SVG '<rect x="' . $box->{xmin} .'" y="'. $box->{ymin}.'" width="'.($box->{xmax}-$box->{xmin}).'" height="'.($box->{ymax}-$box->{ymin}).'" class="box'.$box->{role}.'" id="box'.$box->{role}.'_'.$box->{question}.'_'.$box->{answer}.'" onclick="clickBox('.$box->{question}.','.$box->{answer}.')"/>' ."\n";
}
for my $m (@marks) {
print SVG '<circle r="'.$mark_radius.'" cx="'.$m->{x}.'" cy="'.$m->{y}.'" class="corner-mark" />' ."\n";
}
if($namefield) {
print SVG '<rect x="' . $namefield->{xmin} .'" y="'. $namefield->{ymin}.'" width="'.($namefield->{xmax}-$namefield->{xmin}).'" height="'.($namefield->{ymax}-$namefield->{ymin}).'" class="namefield" id="namefield" />' ."\n";
}
print SVG "</svg>\n";
close(SVG);
print("VAR: svg=$dest_file.svg\n");
}