Support non-standard trailers in fixup_pdf_template.py

An existing challenge is checking in PDFs with custom trailers with
an accompanying .in file. Add a {{trailersize}} token handle in the
script to maintain some automation in the trailer generation.

{{trailersize}} will still generate the generic trailer:
trailer <<
  /Root 1 0 R
  /Size N
>>

With {{trailersize}}, input files can have custom trailers like:
trailer <<
  /Info 2 0 R
  /Prev 3 0 R
  /Root 4 0 R
  {{trailersize}}
>>

At the same time, the developer will not have to worry about counting
the number of objects in the PDF.

Use {{trailersize}} in testing/resources/ files with non-standard
trailers.

Bug: pdfium:1495
Change-Id: If86d4ab2069c21d609f3241b5f375bb8d135924d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/67332
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
diff --git a/testing/resources/pixel/bug_557223.in b/testing/resources/pixel/bug_557223.in
index acb1ff2..c0c06b6 100644
--- a/testing/resources/pixel/bug_557223.in
+++ b/testing/resources/pixel/bug_557223.in
@@ -75,7 +75,7 @@
 trailer <<
     /Info 6 0 R
     /Root 1 0 R
-    /Size 7
+    {{trailersize}}
 >>
 {{startxref}}
 %%EOF
diff --git a/testing/resources/split_streams.in b/testing/resources/split_streams.in
index b134769..729c67a 100644
--- a/testing/resources/split_streams.in
+++ b/testing/resources/split_streams.in
@@ -117,7 +117,7 @@
 {{xref}}
 trailer <<
   /Root 1 0 R
-  /Size 15
+  {{trailersize}}
   /ID [<f341ae654a77acd5065a7645e596e6e6><bc37298a3f87f479229bce997ca791f7>]
 >>
 {{startxref}}
diff --git a/testing/resources/text_in_page_marked.in b/testing/resources/text_in_page_marked.in
index e338479..5978110 100644
--- a/testing/resources/text_in_page_marked.in
+++ b/testing/resources/text_in_page_marked.in
@@ -131,7 +131,7 @@
 {{xref}}
 trailer <<
   /Root 1 0 R
-  /Size 13
+  {{trailersize}}
   /ID [<f341ae654a77acd5065a7645e596e6e6><bc37298a3f87f479229bce997ca791f7>]
 >>
 {{startxref}}
diff --git a/testing/resources/text_in_page_marked_indirect.in b/testing/resources/text_in_page_marked_indirect.in
index 1f989f6..6004ad4 100644
--- a/testing/resources/text_in_page_marked_indirect.in
+++ b/testing/resources/text_in_page_marked_indirect.in
@@ -137,7 +137,7 @@
 {{xref}}
 trailer <<
   /Root 1 0 R
-  /Size 14
+  {{trailersize}}
   /ID [<f341ae654a77acd5065a7645e596e6e6><bc37298a3f87f479229bce997ca791f7>]
 >>
 {{startxref}}
diff --git a/testing/tools/fixup_pdf_template.py b/testing/tools/fixup_pdf_template.py
index ee47c4b..f723203 100755
--- a/testing/tools/fixup_pdf_template.py
+++ b/testing/tools/fixup_pdf_template.py
@@ -11,6 +11,7 @@
   {{header}} - expands to the header comment required for PDF files.
   {{xref}} - expands to a generated xref table, noting the offset.
   {{trailer}} - expands to a standard trailer with "1 0 R" as the /Root.
+  {{trailersize}} - expands to |/Size n|, to be used in non-standard trailers.
   {{startxref} - expands to a startxref directive followed by correct offset.
   {{object x y}} - expands to |x y obj| declaration, noting the offset.
   {{streamlen}} - expands to |/Length n|.
@@ -44,6 +45,9 @@
   TRAILER_TOKEN = '{{trailer}}'
   TRAILER_REPLACEMENT = 'trailer <<\n  /Root 1 0 R\n  /Size %d\n>>'
 
+  TRAILERSIZE_TOKEN = '{{trailersize}}'
+  TRAILERSIZE_REPLACEMENT = '/Size %d'
+
   STARTXREF_TOKEN = '{{startxref}}'
   STARTXREF_REPLACEMENT = 'startxref\n%d'
 
@@ -104,6 +108,9 @@
     if self.TRAILER_TOKEN in line:
       replacement = self.TRAILER_REPLACEMENT % (self.max_object_number + 1)
       line = line.replace(self.TRAILER_TOKEN, replacement)
+    if self.TRAILERSIZE_TOKEN in line:
+      replacement = self.TRAILERSIZE_REPLACEMENT % (self.max_object_number + 1)
+      line = line.replace(self.TRAILERSIZE_TOKEN, replacement)
     if self.STARTXREF_TOKEN in line:
       replacement = self.STARTXREF_REPLACEMENT % self.xref_offset
       line = line.replace(self.STARTXREF_TOKEN, replacement)