| 1 | #!/usr/local/bin/perl | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use feature ':5.10'; | ||
| 5 | use File::Temp qw(tempfile); | ||
| 6 | use Time::HiRes qw(sleep); | ||
| 7 | |||
| 8 | use PDF::PDFUnit qw(:noinit); | ||
| 9 | PDF::PDFUnit->load_config(); | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | my $window_title = 'PDFUnit in Evaluation Mode'; | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | if (my $display = PDF::PDFUnit->val('outfox_display')) { | ||
| 19 | |||
| 20 | chomp(my $xvfb_pid = qx/ pidof Xvfb /); | ||
| 21 | |||
| 22 | unless ($xvfb_pid) { | ||
| 23 | say "Starting Xvfb X11 server on $display"; | ||
| 24 | system("Xvfb -fbdir /var/tmp $display &"); | ||
| 25 | } | ||
| 26 | |||
| 27 | $ENV{DISPLAY} = $display; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | while (1) { | ||
| 32 | sleep(0.5); | ||
| 33 | |||
| 34 | my $search_cmd = "xdotool search --name '$window_title'"; | ||
| 35 | |||
| 36 | $search_cmd .= " windowactivate" | ||
| 37 | unless PDF::PDFUnit->val('outfox_display'); | ||
| 38 | |||
| 39 | |||
| 40 | my $ret = system($search_cmd); | ||
| 41 | next unless ($ret >> 8) == 0; | ||
| 42 | |||
| 43 | say "Detected dialog window!"; | ||
| 44 | |||
| 45 | my (undef, $tmp_name) = tempfile( | ||
| 46 | "/tmp/pdfunit-outfox_XXXXXX", | ||
| 47 | OPEN => 0, | ||
| 48 | SUFFIX => '.tiff' | ||
| 49 | ); | ||
| 50 | |||
| 51 | system("xwd -name '$window_title' | convert - -resize 200% $tmp_name"); | ||
| 52 | |||
| 53 | my $ocr = qx/ tesseract -psm 6 $tmp_name stdout /; | ||
| 54 | |||
| 55 | unlink $tmp_name; | ||
| 56 | |||
| 57 | my ($expression) = $ocr =~ /result:\s+(\w+\s+[+-]\s+\w+)/i; | ||
| 58 | |||
| 59 | my $result = ""; | ||
| 60 | if (defined $expression) { | ||
| 61 | say "Detected expression: $expression"; | ||
| 62 | $result = eval $expression; | ||
| 63 | say "Sending response: $result\n"; | ||
| 64 | |||
| 65 | system("xdotool type $result"); | ||
| 66 | system("xdotool key KP_Enter"); | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | warn "Could not extract expression.\n"; | ||
| 70 | warn "Please cancel and restart your test run.\n"; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | =head1 NAME | ||
| 76 | |||
| 77 | pdfunit-outfox.pl - Send responses to the dialog box of the evaluation version | ||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | =head1 USAGE | ||
| 82 | |||
| 83 | pdfunit-outfox.pl | ||
| 84 | |||
| 85 | |||
| 86 | =head1 IMPORTANT | ||
| 87 | |||
| 88 | |||
| 89 | B<1. If you are using PDFUnit-Java for productive work, get yourself a license!> | ||
| 90 | |||
| 91 | B<2. This program only works on Linux! It will never be ported to Windows!> | ||
| 92 | |||
| 93 | B<3. See 1.!> | ||
| 94 | |||
| 95 | |||
| 96 | =head1 DESCRIPTION | ||
| 97 | |||
| 98 | B<pdfunit-outfox.pl> runs in an endless loop and waits for the dialog window | ||
| 99 | of the non-licened version of PDFUnit-Java to appear. | ||
| 100 | |||
| 101 | If this happens, it saves a temporary screenshot of the dialog window, | ||
| 102 | converts it to TIFF, | ||
| 103 | starts an optical character recognition, and extracts the | ||
| 104 | arithmetic expression. | ||
| 105 | |||
| 106 | Finally it calculates the response and sends it back to the dialog. | ||
| 107 | |||
| 108 | =head1 CONFIGURATION | ||
| 109 | |||
| 110 | Normally, no configuration is required. | ||
| 111 | |||
| 112 | If you don't have X11 running, or if you don't like the dialog to pop up, | ||
| 113 | you can "fake" an X Server with the following line in your pdfunit config: | ||
| 114 | |||
| 115 | outfox_display = :42 | ||
| 116 | |||
| 117 | (Choose any number different from existing $DISPLAY numbers.) | ||
| 118 | |||
| 119 | |||
| 120 | =head1 REQUIREMENTS | ||
| 121 | |||
| 122 | This program uses the following tools: | ||
| 123 | |||
| 124 | =over 4 | ||
| 125 | |||
| 126 | =item B<Xvfb> | ||
| 127 | |||
| 128 | =item B<xwd> | ||
| 129 | |||
| 130 | =item B<convert> | ||
| 131 | |||
| 132 | =item B<xdotool> | ||
| 133 | |||
| 134 | =item B<tesseract> | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | On a Debian 8 system, you get these installed via: | ||
| 139 | |||
| 140 | aptitude install xvfb x11-apps imagemagick xdotool tesseract-ocr | ||
| 141 | |||
| 142 | |||
| 143 | =head1 BUGS | ||
| 144 | |||
| 145 | Sometimes the OCR fails. That's the way it is. | ||
| 146 | |||
| 147 | See L</IMPORTANT> in this case. | ||
| 148 | |||
| 149 | |||
| 150 | =head1 AUTHOR | ||
| 151 | |||
| 152 | Axel Miesen <miesen@quadraginta-duo.de> | ||
| 153 | |||
| 154 | =head1 SEE ALSO | ||
| 155 | |||
| 156 | L<PDF::PDFUnit>, L<PDF::PDFUnit::Config> |