fixup_pdf_template: Don't count final newline in {{streamlen}}

Expansion of `{{streamlen}}` used to include the newline in front of
`endstream`.

macOS's Preview.app cannot load JPEG2000 files if they have an
additional `\n` byte at the end of the data, so don't count this
final newline.

With this, macOS's Preview.app now displays the PDF files generated
by fixup_pdf_template.py much better for
testing/resources/pixel/jpxdecode*.in

This requires adding newlines in a couple of tests that depend on
stream contents actually ending in a newline:

* bug_1236805: Apparently the type3 charprocs parser doesn't treat
               EOF as a token separator (?)
* bug_1395648: Added in https://pdfium-review.googlesource.com/102930
               as a regression test with a missing `/Filter` line.
               In the previous image in the text, the `ASCIIHexDecode`
               decode filter stripped newlines, but here it doesn't and
               the test needs the newline.

Bug: 394805680
Change-Id: I91610fe7d85080e5d4e0b3c52e2d5d10ff4b985d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/128570
Reviewed-by: Nico Weber <thakis@google.com>
Commit-Queue: Nico Weber <thakis@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/testing/resources/pixel/bug_1236805.in b/testing/resources/pixel/bug_1236805.in
index 564f048..ea2fc48 100644
--- a/testing/resources/pixel/bug_1236805.in
+++ b/testing/resources/pixel/bug_1236805.in
@@ -78,6 +78,7 @@
 ID
 EI
 Q
+
 endstream
 endobj
 {{xref}}
diff --git a/testing/resources/pixel/bug_1395648.in b/testing/resources/pixel/bug_1395648.in
index 6c38fb1..828e84c 100644
--- a/testing/resources/pixel/bug_1395648.in
+++ b/testing/resources/pixel/bug_1395648.in
@@ -59,6 +59,7 @@
 >>
 stream
 789cabaa1a05a3808e4061148c027a028351300ae809246c128a027e0020065c9c90d
+
 endstream
 endobj
 {{xref}}
diff --git a/testing/tools/fixup_pdf_template.py b/testing/tools/fixup_pdf_template.py
index 1d38117..06ed3c1 100755
--- a/testing/tools/fixup_pdf_template.py
+++ b/testing/tools/fixup_pdf_template.py
@@ -109,6 +109,9 @@
     if self.streamlen_state == StreamLenState.FIND_ENDSTREAM:
       if line.rstrip() == b'endstream':
         self.streamlen_state = StreamLenState.START
+        # Don't count final newline.
+        # insert_includes() already stripped windows line endings.
+        self.streamlens[-1] -= 1
       else:
         self.streamlens[-1] += len(line)