/usr/share/perl5/Gscan2pdf/Canvas.pm is in gscan2pdf 1.3.9-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 | package Gscan2pdf::Canvas;
use strict;
use warnings;
use Goo::Canvas;
use Glib 1.220 qw(TRUE FALSE); # To get TRUE and FALSE
use Readonly;
Readonly my $_100_PERCENT => 100;
Readonly my $_360_DEGREES => 360;
my $SPACE = q{ };
my $EMPTY = q{};
BEGIN {
use Exporter ();
our ( $VERSION, @EXPORT_OK, %EXPORT_TAGS );
$VERSION = '1.3.9';
use base qw(Exporter Goo::Canvas);
%EXPORT_TAGS = (); # eg: TAG => [ qw!name1 name2! ],
# your exported package globals go here,
# as well as any optionally exported functions
@EXPORT_OK = qw();
}
sub new {
my ( $class, $page, $edit_callback ) = @_;
# Set up the canvas
my $self = Goo::Canvas->new;
my $root = $self->get_root_item;
if ( not defined $page->{w} ) {
# quotes required to prevent File::Temp object being clobbered
my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file("$page->{filename}");
$page->{w} = $pixbuf->get_width;
$page->{h} = $pixbuf->get_height;
}
$self->set_bounds( 0, 0, $page->{w}, $page->{h} );
# Attach the text to the canvas
for my $box ( @{ $page->boxes } ) {
boxed_text( $self->get_root_item, $box, $edit_callback );
}
$self->{page} = $page;
bless $self, $class;
return $self;
}
# Draw text on the canvas with a box around it
sub boxed_text {
my ( $root, $box, $edit_callback ) = @_;
my ( $x1, $y1, $x2, $y2 ) = @{ $box->{bbox} };
my $x_size = abs $x2 - $x1;
my $y_size = abs $y2 - $y1;
my $g = Goo::Canvas::Group->new($root);
$g->translate( $x1, $y1 );
# add box properties to group properties
map { $g->{$_} = $box->{$_}; } keys %{$box};
# draw the rect first to make sure the text goes on top
# and receives any mouse clicks
my $confidence =
defined $box->{confidence} ? $box->{confidence} : $_100_PERCENT;
$confidence = $confidence > 64 ## no critic (ProhibitMagicNumbers)
? 2 * int( ( $confidence - 65 ) / 12 ) ## no critic (ProhibitMagicNumbers)
+ 10 ## no critic (ProhibitMagicNumbers)
: 0;
my $color = defined $box->{confidence}
? sprintf '#%xfff%xfff%xfff',
0xf - int( $confidence / 10 ), ## no critic (ProhibitMagicNumbers)
$confidence, $confidence
: '#7fff7fff7fff';
my $rect = Goo::Canvas::Rect->new( $g, 0, 0, $x_size, $y_size,
'stroke-color' => $color );
if ( $box->{text} ) {
# # show text baseline (currently of no use)
# if ($box->{baseline} and $box->{baseline} >= $y1) {
# $rect = Goo::Canvas::Rect->new( $g,
# 0, $box->{baseline} - $y1, $x_size, 1,
# 'stroke-color' => 'yellow');
# }
my $text;
if ( $box->{type} eq 'page' ) {
# only basic info on text, so simply create it
$text = Goo::Canvas::Text->new( $g, $box->{text}, 0, 0, $x_size,
'nw', 'height' => $y_size );
}
else {
# create text and then scale, shift & rotate it into the bounding box
my $angle =
defined( $box->{textangle} )
? ( -$box->{textangle} % $_360_DEGREES )
: 0;
$text = Goo::Canvas::Text->new(
$g,
$box->{text},
$x_size / 2,
$y_size / 2,
-1, 'center', ## no critic (ProhibitMagicNumbers)
'font' => 'Sans '
. (
$angle
? $x_size
: $y_size
)
);
my $bounds = $text->get_bounds;
my $scale =
( $angle ? $y_size : $x_size ) / ( $bounds->x2 - $bounds->x1 );
$text->set_simple_transform( 0, 0, $scale, $angle );
$bounds = $text->get_bounds;
my $x_offset = ( $x1 + $x2 - $bounds->x1 - $bounds->x2 ) / 2;
my $y_offset = ( $y1 + $y2 - $bounds->y1 - $bounds->y2 ) / 2;
$text->set_simple_transform( $x_offset, $y_offset, $scale, $angle );
}
# clicking text box produces a dialog to edit the text
if ($edit_callback) {
$text->signal_connect( 'button-press-event' => $edit_callback );
}
}
if ( $box->{contents} ) {
for my $box ( @{ $box->{contents} } ) {
boxed_text( $g, $box, $edit_callback );
}
}
# $rect->signal_connect(
# 'button-press-event' => sub {
# my ( $widget, $target, $ev ) = @_;
# print "rect button-press-event\n";
# # return TRUE;
# }
# );
# $g->signal_connect(
# 'button-press-event' => sub {
# my ( $widget, $target, $ev ) = @_;
# print "group $widget button-press-event\n";
# my $n = $widget->get_n_children;
# for ( my $i = 0 ; $i < $n ; $i++ ) {
# my $item = $widget->get_child($i);
# if ( $item->isa('Goo::Canvas::Text') ) {
# print "contains $item\n", $item->get('text'), "\n";
# last;
# }
# }
# # return TRUE;
# }
# );
return;
}
# Set the text in the given widget
sub set_box_text {
my ( $self, $widget, $text ) = @_;
# per above: group = text's parent, group's 1st child = rect
my $g = $widget->get_property('parent');
my $rect = $g->get_child(0);
if ( length $text ) {
$widget->set( text => $text );
$g->{text} = $text;
$g->{confidence} = $_100_PERCENT;
# color for 100% confidence
$rect->set_property( 'stroke-color' => '#efffefffefff' );
# re-adjust text size & position
if ( $g->{type} ne 'page' ) {
my ( $x1, $y1, $x2, $y2 ) = @{ $g->{bbox} };
my $x_size = abs $x2 - $x1;
my $y_size = abs $y2 - $y1;
$widget->set_simple_transform( 0, 0, 1, 0 );
my $angle =
defined( $g->{textangle} )
? ( -$g->{textangle} % $_360_DEGREES )
: 0;
my $bounds = $widget->get_bounds;
my $scale =
( $angle ? $y_size : $x_size ) / ( $bounds->x2 - $bounds->x1 );
$widget->set_simple_transform( 0, 0, $scale, $angle );
$bounds = $widget->get_bounds;
my $x_offset = ( $x1 + $x2 - $bounds->x1 - $bounds->x2 ) / 2;
my $y_offset = ( $y1 + $y2 - $bounds->y1 - $bounds->y2 ) / 2;
$widget->set_simple_transform( $x_offset,
$y_offset, $scale, $angle );
}
}
else {
delete $g->{text};
$g->remove_child(0);
$g->remove_child(1);
}
$self->canvas2hocr();
return;
}
# Convert the canvas into hocr
sub canvas2hocr {
my ($self) = @_;
my ( $x, $y, $w, $h ) = $self->get_bounds;
my $root = $self->get_root_item;
my $string = _group2hocr($root);
$self->{page}{hocr} = <<"EOS";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>OCR Output</title>
</head>
<body>
$string
</body>
</html>
EOS
return;
}
sub _group2hocr {
my ($parent) = @_;
my $string = $EMPTY;
for my $i ( 0 .. $parent->get_n_children - 1 ) {
my $group = $parent->get_child($i);
if ( ref($group) eq 'Goo::Canvas::Group' ) {
# try to preserve as much information as possible
if ( $group->{bbox} and $group->{type} ) {
my $block_type = (
$group->{type} eq 'page'
or $group->{type} eq 'column'
) ? 'div' : 'span';
my $type =
$group->{type} eq 'column' ? 'carea' : $group->{type};
$string .= "<$block_type class='ocr_$type'";
if ( $group->{id} ) { $string .= " id='$group->{id}'" }
my $title = " title='bbox " . join $SPACE, @{ $group->{bbox} };
if ( $group->{text} ) {
if ( $group->{textangle} ) {
$title .= '; textangle ' . $group->{textangle};
}
if ( $group->{baseline} ) {
$title .= '; baseline ' . $group->{baseline};
}
if ( $group->{confidence} ) {
$title .= '; x_wconf ' . $group->{confidence};
}
$title .= "'>$group->{text}";
}
else {
$title .= "'>";
}
$string .= $title . _group2hocr($group) . "</$block_type>";
}
}
}
return $string;
}
1;
__END__
|