Ensure {{include}} output ends on its own line

Ensures fixup_pdf_template.py ends the output of any {{include}}
directive on its own line. This ensures any lines after an {{include}}
are treated separately from the inclusion itself.

For example, macOS Preview doesn't treat bug_1469.jp2 as a valid JP2
file, because it has an extraneous newline character at the end. This
character previously was necessary to separate the inclusion from the
following "endstream" token. With the fix, the extra character in the
JP2 file is no longer needed.

If including a file that allows modifying line endings, the final line
ending may be borrowed from the included file.

Bug: pdfium:2000
Change-Id: I23963ee8ebe92db659640f0878d2f8cc019b68d3
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/104690
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: K. Moon <kmoon@chromium.org>
diff --git a/testing/resources/bug_1469.jp2 b/testing/resources/bug_1469.jp2
index 892e8fa..b84a029 100644
--- a/testing/resources/bug_1469.jp2
+++ b/testing/resources/bug_1469.jp2
Binary files differ
diff --git a/testing/tools/fixup_pdf_template.py b/testing/tools/fixup_pdf_template.py
index 5f410f9..da2d608 100755
--- a/testing/tools/fixup_pdf_template.py
+++ b/testing/tools/fixup_pdf_template.py
@@ -166,6 +166,10 @@
     return
   visited_set.add(input_path)
   try:
+    _, file_extension = os.path.splitext(input_path)
+    override_line_endings = (file_extension in EXTENSION_OVERRIDE_LINE_ENDINGS)
+
+    end_of_file_line_ending = False
     with open(input_path, 'rb') as infile:
       for line in infile:
         match = re.match(b'\s*\{\{include\s+(.+)\}\}', line)
@@ -175,12 +179,18 @@
                   os.path.dirname(input_path),
                   match.group(1).decode('utf-8')), output_file, visited_set)
         else:
-          # Replace CRLF with LF line endings for .in files.
-          _, file_extension = os.path.splitext(input_path)
-          if (file_extension in EXTENSION_OVERRIDE_LINE_ENDINGS and
-              line.endswith(WINDOWS_LINE_ENDING)):
-            line = line.removesuffix(WINDOWS_LINE_ENDING) + UNIX_LINE_ENDING
+          if override_line_endings:
+            # Replace CRLF with LF line endings for .in files.
+            if line.endswith(WINDOWS_LINE_ENDING):
+              line = line.removesuffix(WINDOWS_LINE_ENDING) + UNIX_LINE_ENDING
+              end_of_file_line_ending = True
+            else:
+              end_of_file_line_ending = line.endswith(UNIX_LINE_ENDING)
           output_file.write(line)
+
+    # Ensure the include ends on its own line.
+    if not end_of_file_line_ending:
+      output_file.write(UNIX_LINE_ENDING)
   except IOError:
     print('failed to include %s' % input_path, file=sys.stderr)
     raise