/usr/share/perl5/Gscan2pdf/Scanner/Options.pm is in gscan2pdf 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | package Gscan2pdf::Scanner::Options;
use strict;
use warnings;
no if $] >= 5.018, warnings => 'experimental::smartmatch';
use Carp;
use Glib qw(TRUE FALSE); # To get TRUE and FALSE
use Image::Sane ':all'; # For enums
use Storable qw(dclone); # For cloning the options cache
use feature 'switch';
use Readonly;
Readonly my $MAX_VALUES => 255;
Readonly my $EMPTY_ARRAY => -1;
# Have to subclass Glib::Object to be able to name it as an object in
# Glib::ParamSpec->object in Gscan2pdf::Dialog::Scan
use Glib::Object::Subclass Glib::Object::;
our $VERSION = '2.1.0';
my $units = qr{(pel|bit|mm|dpi|%|us)}xsm;
my $EMPTY = q{};
my $list = ',...';
my $device;
sub new_from_data {
my ( $class, $options ) = @_;
my $self = $class->new();
if ( not defined $options ) { croak 'Error: no options supplied' }
if ( ref($options) eq 'ARRAY' ) {
$self->{array} = $options;
}
else {
$self->{array} = _parse_scanimage_output($options);
}
# add hash for easy retrieval
for my $i ( 0 .. $#{ $self->{array} } ) {
my $option;
if ( defined $self->{array}[$i] ) { $option = $self->{array}[$i] }
$option->{index} = $i;
if ( not defined $self->{array}[$i] ) { $self->{array}[$i] = $option }
if ( defined $self->{array}[$i]{name}
and $self->{array}[$i]{name} ne $EMPTY )
{
$self->{hash}{ $self->{array}[$i]{name} } = $self->{array}[$i];
}
}
# find source option
if ( defined $self->by_name('source') ) {
$self->{source} = $self->by_name('source');
}
else {
for my $option ( @{ $self->{array} } ) {
if ( defined $option->{name} and $option->{name} =~ /source/xsm ) {
$self->{source} = $option;
last;
}
}
}
$self->parse_geometry;
return $self;
}
sub by_index {
my ( $self, $i ) = @_;
return $self->{array}[$i];
}
sub by_name {
my ( $self, $name ) = @_;
return ( defined $name and defined $self->{hash}{$name} )
? $self->{hash}{$name}
: undef;
}
sub by_title {
my ( $self, $title ) = @_;
for ( @{ $self->{array} } ) {
return $_ if ( defined( $_->{title} ) and $_->{title} eq $title );
}
return;
}
sub num_options {
my ($self) = @_;
return $#{ $self->{array} } + 1;
}
sub delete_by_index {
my ( $self, $i ) = @_;
if ( defined $self->{array}[$i]{name} ) {
delete $self->{hash}{ $self->{array}[$i]{name} };
}
undef $self->{array}[$i];
return;
}
sub delete_by_name {
my ( $self, $name ) = @_;
undef $self->{array}[ $self->{hash}{$name}{index} ];
delete $self->{hash}{$name};
return;
}
sub device {
return $device;
}
# Parse out the geometry from libimage-sane-perl or scanimage option names
sub parse_geometry {
my ($self) = @_;
for ( ( SANE_NAME_PAGE_HEIGHT, 'pageheight' ) ) {
if ( defined $self->{hash}{$_} ) {
$self->{geometry}{h} = $self->{hash}{$_}{constraint}{max};
last;
}
}
for ( ( SANE_NAME_PAGE_WIDTH, 'pagewidth' ) ) {
if ( defined $self->{hash}{$_} ) {
$self->{geometry}{w} = $self->{hash}{$_}{constraint}{max};
last;
}
}
if ( defined $self->{hash}{ scalar SANE_NAME_SCAN_TL_X } ) {
$self->{geometry}{l} =
$self->{hash}{ scalar SANE_NAME_SCAN_TL_X }{constraint}{min};
if ( defined $self->{hash}{ scalar SANE_NAME_SCAN_BR_X } ) {
$self->{geometry}{x} =
$self->{hash}{ scalar SANE_NAME_SCAN_BR_X }{constraint}{max} -
$self->{geometry}{l};
}
}
elsif ( defined $self->{hash}{l} ) {
$self->{geometry}{l} = $self->{hash}{l}{constraint}{min};
if ( defined $self->{hash}{x}{constraint}{max} ) {
$self->{geometry}{x} = $self->{hash}{x}{constraint}{max};
}
}
if ( defined $self->{hash}{ scalar SANE_NAME_SCAN_TL_Y } ) {
$self->{geometry}{t} =
$self->{hash}{ scalar SANE_NAME_SCAN_TL_Y }{constraint}{min};
if ( defined $self->{hash}{ scalar SANE_NAME_SCAN_BR_Y } ) {
$self->{geometry}{y} =
$self->{hash}{ scalar SANE_NAME_SCAN_BR_Y }{constraint}{max} -
$self->{geometry}{t};
}
}
elsif ( defined $self->{hash}{t} ) {
$self->{geometry}{t} = $self->{hash}{t}{constraint}{min};
if ( defined $self->{hash}{y}{constraint}{max} ) {
$self->{geometry}{y} = $self->{hash}{y}{constraint}{max};
}
}
return;
}
sub supports_paper {
my ( $self, $paper, $tolerance ) = @_;
# Check the geometry against the paper size
if (
not( defined( $self->{geometry}{l} )
and defined( $self->{geometry}{x} )
and defined( $self->{geometry}{t} )
and defined( $self->{geometry}{y} )
and $self->{geometry}{l} <= $paper->{l} + $tolerance
and $self->{geometry}{t} <= $paper->{t} + $tolerance )
)
{
return 0;
}
if ( defined( $self->{geometry}{h} ) and defined( $self->{geometry}{w} ) ) {
if ( $self->{geometry}{h} + $tolerance >= $paper->{y} + $paper->{t}
and $self->{geometry}{w} + $tolerance >= $paper->{x} + $paper->{l} )
{
return 1;
}
else {
return 0;
}
}
elsif ( $self->{geometry}{x} + $self->{geometry}{l} + $tolerance >=
$paper->{x} + $paper->{l}
and $self->{geometry}{y} + $self->{geometry}{t} + $tolerance >=
$paper->{y} + $paper->{t} )
{
return 1;
}
return 0;
}
# returns TRUE if the current options support duplex, even if not currently
# selected. Alternatively expressed, return FALSE if the scanner is not capable
# of duplex scanner, or if the capability is inactive.
sub can_duplex {
my ($self) = @_;
for my $option ( @{ $self->{array} } ) {
if (
not( defined $option->{cap}
and ( $option->{cap} & SANE_CAP_INACTIVE ) )
)
{
if ( defined $option->{name} and $option->{name} =~ /duplex/xsmi ) {
return TRUE;
}
elsif ( defined $option->{constraint_type}
and $option->{constraint_type} == SANE_CONSTRAINT_STRING_LIST )
{
for my $item ( @{ $option->{constraint} } ) {
if ( $item =~ /duplex/xsmi ) {
return TRUE;
}
}
}
}
}
return FALSE;
}
# returns TRUE/FALSE if the value is within the tolerance of the given option or
# not, and undef for options with no value or for an invalid value
sub within_tolerance {
my ( $option, $value ) = @_;
given ( $option->{constraint_type} ) {
when (SANE_CONSTRAINT_RANGE) {
if ( defined $option->{constraint}{quant} ) {
return (
abs( $value - $option->{val} ) <=
$option->{constraint}{quant} / 2 );
}
}
when (SANE_CONSTRAINT_STRING_LIST) {
return ( $value eq $option->{val} );
}
when (SANE_CONSTRAINT_WORD_LIST) {
return ( $value == $option->{val} );
}
}
given ( $option->{type} ) {
when (SANE_TYPE_BOOL) {
return not( $value xor $option->{val} );
}
when (SANE_TYPE_STRING) {
return ( $value eq $option->{val} );
}
when ( $_ == SANE_TYPE_INT or $_ == SANE_TYPE_FIXED ) {
return ( $value == $option->{val} );
}
}
return;
}
# parse the scanimage/scanadf output into an array and a hash
sub _parse_scanimage_output {
my ($output) = @_;
# Remove everything above the options
if (
$output =~ qr{
Options[ ]specific[ ]to[ ]device[ ] # string
`(.+)':\n # device name
(.*) # options
}xsm
)
{
$device = $1;
$output = $2;
}
else {
return;
}
my @options;
while (1) {
my %option;
$option{unit} = SANE_UNIT_NONE;
$option{constraint_type} = SANE_CONSTRAINT_NONE;
my $values = qr{(?:(?:[ ]|[[]=[(])([^[].*?)(?:[)]\])?)?}xsm;
# parse group
if (
$output =~ qr{
\A[ ]{2} # two-character indent
([^\n]*) # the title
:\n # a colon at the end of the line
(.*) # the rest of the output
}xsm
)
{
$option{title} = $1;
$option{type} = SANE_TYPE_GROUP;
$option{cap} = 0;
$option{max_values} = 0;
$option{name} = $EMPTY;
$option{desc} = $EMPTY;
# Remove everything on the option line and above.
$output = $2;
}
# parse option
elsif (
$output =~ qr{
\A[ ]{4,} # four-character indent
-+ # at least one dash
([\w\-]+) # the option name
$values # optionally followed by the possible values
(?:[ ][[](.*?)[]])? # optionally a space, followed by the current value in square brackets
[ ]*\n # the rest of the line
(.*) # the rest of the output
}xsm
)
{
# scanimage & scanadf only display options
# if SANE_CAP_SOFT_DETECT is set
$option{cap} = SANE_CAP_SOFT_DETECT + SANE_CAP_SOFT_SELECT;
$option{name} = $1;
if ( defined $3 ) {
if ( $3 eq 'inactive' ) {
$option{cap} += SANE_CAP_INACTIVE;
}
else {
$option{val} = $3;
}
$option{max_values} = 1;
}
else {
$option{type} = SANE_TYPE_BUTTON;
$option{max_values} = 0;
}
# parse the constraint after the current value
# in order to be able to reset boolean values
parse_constraint( \%option, $2 );
# Remove everything on the option line and above.
$output = $4;
$option{title} = $option{name};
$option{title} =~ s/[-_]/ /xsmg; # dashes and underscores to spaces
$option{title} =~
s/\b(adf|cct|jpeg)\b/\U$1/xsmg; # upper case comment abbreviations
$option{title} =~
s/(^\w)/\U$1/xsmg; # capitalise at the beginning of the line
# Parse option description based on an 8-character indent.
my $desc = $EMPTY;
while (
$output =~ qr{
\A[ ]{8,} # 8-character indent
([^\n]*)\n # text
(.*) # rest of output
}xsm
)
{
if ( $desc eq $EMPTY ) {
$desc = $1;
}
else {
$desc = "$desc $1";
}
# Remove everything on the description line and above.
$output = $2;
}
$option{desc} = $desc;
given ( $option{name} ) {
when ('l') {
$option{name} = SANE_NAME_SCAN_TL_X;
$option{title} = 'Top-left x';
}
when ('t') {
$option{name} = SANE_NAME_SCAN_TL_Y;
$option{title} = 'Top-left y';
}
when ('x') {
$option{name} = SANE_NAME_SCAN_BR_X;
$option{title} = 'Bottom-right x';
$option{desc} = 'Bottom-right x position of scan area.';
}
when ('y') {
$option{name} = SANE_NAME_SCAN_BR_Y;
$option{title} = 'Bottom-right y';
$option{desc} = 'Bottom-right y position of scan area.';
}
}
}
else {
last;
}
push @options, \%option;
$option{index} = $#options + 1;
}
if (@options) { unshift @options, { index => 0 } }
return \@options;
}
# parse out range, step and units from the values string
sub parse_constraint {
my ( $option, $values ) = @_;
$option->{type} = SANE_TYPE_INT;
if ( defined( $option->{val} ) and $option->{val} =~ /[.]/xsm ) {
$option->{type} = SANE_TYPE_FIXED;
}
if (
defined $values
and $values =~ qr{
(-?\d+[.]?\d*) # min value, possibly negative or floating
[.]{2} # two dots
(\d+[.]?\d*) # max value, possible floating
$units? # optional unit
($list)? # multiple values
}xsm
)
{
$option->{constraint}{min} = $1;
$option->{constraint}{max} = $2;
$option->{constraint_type} = SANE_CONSTRAINT_RANGE;
if ( defined $3 ) { $option->{unit} = unit2enum($3) }
if ( defined $4 ) { $option->{max_values} = $MAX_VALUES }
if (
$values =~ qr{
[(] # opening round bracket
in[ ]steps[ ]of[ ] # text
(\d+[.]?\d*) # step
[)] # closing round bracket
}xsm
)
{
$option->{constraint}{quant} = $1;
}
if (
$option->{constraint}{min} =~ /[.]/xsm
or $option->{constraint}{max} =~ /[.]/xsm
or ( defined( $option->{constraint}{quant} )
and $option->{constraint}{quant} =~ /[.]/xsm )
)
{
$option->{type} = SANE_TYPE_FIXED;
}
}
elsif ( defined $values and $values =~ /^<(\w+)>($list)?$/xsm ) {
if ( $1 eq 'float' ) {
$option->{type} = SANE_TYPE_FIXED;
}
elsif ( $1 eq 'string' ) {
$option->{type} = SANE_TYPE_STRING;
}
if ( defined $2 ) { $option->{max_values} = $MAX_VALUES }
}
# if we haven't got a boolean, and there is no constraint, we have a button
elsif ( not defined $values ) {
$option->{type} = SANE_TYPE_BUTTON;
$option->{max_values} = 0;
}
else {
parse_list_constraint( $option, $values );
}
return;
}
sub parse_list_constraint {
my ( $option, $values ) = @_;
if ( $values =~ /(.*)$list/xsm ) {
$values = $1;
$option->{max_values} = $MAX_VALUES;
}
if ( $values =~ /(.*)$units$/xsm ) {
$values = $1;
$option->{unit} = unit2enum($2);
}
my @array = split /[|]+/xsm, $values;
if (@array) {
if ( $array[0] eq 'auto' ) {
$option->{cap} += SANE_CAP_AUTOMATIC;
shift @array;
}
if ( @array == 2 and $array[0] eq 'yes' and $array[1] eq 'no' ) {
$option->{type} = SANE_TYPE_BOOL;
if ( defined $option->{val} ) {
if ( $option->{val} eq 'yes' ) {
$option->{val} = SANE_TRUE;
}
else {
$option->{val} = SANE_FALSE;
}
}
}
else {
# Can't check before because 'auto' would mess things up
for (@array) {
if (/[[:alpha:]]/xsm) {
$option->{type} = SANE_TYPE_STRING;
}
elsif (/[.]/xsm) {
$option->{type} = SANE_TYPE_FIXED;
}
}
$option->{constraint} = [@array];
$option->{constraint_type} =
$option->{type} == SANE_TYPE_STRING
? SANE_CONSTRAINT_STRING_LIST
: SANE_CONSTRAINT_WORD_LIST;
}
}
return;
}
sub unit2enum {
my ($unit) = @_;
given ($unit) {
when ('pel') {
return SANE_UNIT_PIXEL;
}
when ('bit') {
return SANE_UNIT_BIT;
}
when ('mm') {
return SANE_UNIT_MM;
}
when ('dpi') {
return SANE_UNIT_DPI;
}
when (q{%}) {
return SANE_UNIT_PERCENT;
}
when ('us') {
return SANE_UNIT_MICROSECOND;
}
}
return;
}
1;
__END__
|