From 222781c81452cb93d50a232572419e3520ef475a Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Wed, 1 Mar 2023 18:20:56 +0000
Subject: [PATCH] bring the two loops in act_txtobj into alignment

The function act_txtobj() performs two passes over the sequence of
text operators. The first pass tracks position and length of the
string. Then an array is allocated of the indicated length and a
second pass fills it with the actual characters.

The two passes must be close carbon copies of each other or a mismatch
between the predetermined length and the actual number of characters
produced might occur and cause the assertion "txtlen == idx" to fail.

This code structure is obviously bad, a text book example of why code
duplication should be avoided. Nevertheless, before rewriting it
entirely, this patch at least corrects an immediate bug.

Fixes #44.
---
 pdf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pdf.c b/pdf.c
index daca61d..fb8876b 100644
--- a/pdf.c
+++ b/pdf.c
@@ -2048,6 +2048,7 @@ act_txtobj(const HParseResult *p, void *u)
 
 		// text positioning and showing operators
 		case TP_TD:
+			node->ts.line_spacing = txte->pos.ty;
 		case TP_Td:
 			if ( (*px == 0.0) && (*py == 0.0) ) { // initialize
 				*px = txte->pos.tx;
@@ -2084,7 +2085,7 @@ act_txtobj(const HParseResult *p, void *u)
 			memcpy(&tstr[idx], txte->tstr.text, txte->tstr.nchars);
 			idx += txte->tstr.nchars;
 			if (node->ts.font != NULL)
-				*px += txte->tarray.flattened.nchars * node->ts.font->fref.fontsize; // TODO:: handle character width from font description
+				*px += txte->tstr.nchars * node->ts.font->fref.fontsize; // TODO:: handle character width from font description
 			break;
 
 
-- 
GitLab