From 69c38a0d23c39432ff19e1e20f1e398a14d8c648 Mon Sep 17 00:00:00 2001
From: Paul Gardiner <paul@glidos.net>
Date: Tue, 12 Jun 2012 15:49:11 +0100
Subject: Forms: handle carriage control in multiline text widget

---
 apps/win_res.rc |  2 +-
 pdf/pdf_form.c  | 32 +++++++++++++++++++++++++++++---
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/apps/win_res.rc b/apps/win_res.rc
index e57a3bb7..5311ed2a 100644
--- a/apps/win_res.rc
+++ b/apps/win_res.rc
@@ -54,7 +54,7 @@ IDD_DLOGTEXT DIALOG 50, 50, 204, 85
 CAPTION " MuPDF: fill out form"
 FONT 8, "MS Shell Dlg"
 BEGIN
-    EDITTEXT        3,8,7,183,50,4
+    EDITTEXT        3,8,7,183,50,0x1004
     DEFPUSHBUTTON   "Okay",1,89,64,50,14
     PUSHBUTTON      "Cancel",2,147,64,50,14
 END
diff --git a/pdf/pdf_form.c b/pdf/pdf_form.c
index abee963d..66ce4ed3 100644
--- a/pdf/pdf_form.c
+++ b/pdf/pdf_form.c
@@ -558,7 +558,15 @@ static int text_splitter_layout(fz_context *ctx, text_splitter *splitter)
 	text = splitter->text + splitter->text_start;
 	room = splitter->unscaled_width - splitter->x;
 
-	if (text[0] == ' ')
+	if (strchr("\r\n", text[0]))
+	{
+		/* Consume return chars and report end of line */
+		splitter->text_end += strspn(text, "\r\n");
+		splitter->text_start = splitter->text_end;
+		splitter->done = (splitter->text[splitter->text_end] == '\0');
+		return 0;
+	}
+	else if (text[0] == ' ')
 	{
 		/* Treat each space as a word */
 		len = 1;
@@ -566,7 +574,7 @@ static int text_splitter_layout(fz_context *ctx, text_splitter *splitter)
 	else
 	{
 		len = 0;
-		while (text[len] != '\0' && text[len] != ' ')
+		while (text[len] != '\0' && !strchr(" \r\n", text[len]))
 			len ++;
 	}
 
@@ -629,6 +637,24 @@ static void text_splitter_move(text_splitter *splitter, float newy, float *relx,
 	splitter->y_orig = newy;
 }
 
+static void text_splitter_retry(text_splitter *splitter)
+{
+	if (splitter->retry)
+	{
+		/* Already tried expanding lines. Overflow must
+		 * be caused by carriage control */
+		splitter->max_lines ++;
+		splitter->retry = 0;
+		splitter->unscaled_width = splitter->width * splitter->max_lines * splitter->fontsize
+			/ splitter->height;
+		splitter->scale = splitter->width / splitter->unscaled_width;
+	}
+	else
+	{
+		splitter->retry = 1;
+	}
+}
+
 static void fzbuf_print_text_start(fz_context *ctx, fz_buffer *fzbuf, fz_rect *clip, font_info *font, fz_matrix *tm)
 {
 	fz_buffer_printf(ctx, fzbuf, fmt_Tx_BMC);
@@ -749,7 +775,7 @@ fz_buffer *create_text_appearance(pdf_document *doc, fz_rect *bbox, fz_matrix *o
 				}
 
 				if (!splitter.done)
-					splitter.retry = 1;
+					text_splitter_retry(&splitter);
 			}
 
 			fzbuf = fz_new_buffer(ctx, 0);
-- 
cgit v1.2.3