#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('flatten-annotations');

my $n_tests = 0;

# manual-appearances was created by hand-coding appearance streams
# with graphics that make it easy to test matrix calculations. The
# result of flattening the annotations was compared visually with
# okular. Some PDF viewers don't actually display the original version
# correctly. The pages are as follows:
# - page 1: normal
# - page 2: rotate 90 with /F 20 (NoRotate)
# - page 3: non-trivial matrix
# - page 4: non-trivial matrix, rotate
# - page 5: rotate 180 with /F 20
# - page 6: rotate 90, /F 20, non-trivial matrix
# - page 7: flags: top is print, middle is screen, bottom is hidden
# - page 8: rotate 270 with /F 20
# - page 9: normal -- available for additional testing
#
# form-filled-by-acrobat was filled in using the Acrobat Reader
# android app. One of its appearance streams is actually an image.
#
# need-appearances.pdf is based on field-types.pdf with manual edits
# to turn on NeedAppearances, change /V for several fields, and add
# the comment annotation from comment-annotation.pdf. The test output
# includes a flattened version of the comment annotation but not of
# the form fields. Changes:
# - field-types.pdf has /NeedAppearances true
# - text1: blank -> abc
# - r1: 1 -> 2
# - list1: blank -> five
# - combolist1: blank -> pi
# - drop1: blank -> elephant
# - combodrop1: blank -> delta

my @annotation_files = (
    'manual-appearances',
    'form-filled-by-acrobat',
    'comment-annotation',
    'comment-annotation-direct',
    'sample-form',
    'need-appearances',
    'need-appearances-more',
    'annotation-no-resources',
    );
$n_tests += 2 * scalar(@annotation_files);

foreach my $f (@annotation_files)
{
    my $exp_out = {$td->STRING => "", $td->EXIT_STATUS => 0};
    if (-f "$f-warn.out")
    {
        $exp_out = {$td->FILE => "$f-warn.out", $td->EXIT_STATUS => 3};
    }
    $td->runtest("flatten $f",
                 {$td->COMMAND =>
                      "qpdf --qdf --static-id --no-original-object-ids" .
                      " --flatten-annotations=all $f.pdf a.pdf"},
                 $exp_out,
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("check output",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "$f-out.pdf"});
}

$n_tests += 4;
foreach my $f (qw(screen print))
{
    $td->runtest("flatten for $f",
                 {$td->COMMAND =>
                      "qpdf --qdf --static-id --no-original-object-ids" .
                      " --flatten-annotations=$f manual-appearances.pdf a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("check output",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "manual-appearances-$f-out.pdf"});
}

cleanup();
$td->report($n_tests);
