fix: innverword-linebreaks (partially tested)

This commit is contained in:
2026-11-09 12:16:31 +01:00
parent 89a9a919e3
commit de97421914
+52 -6
View File
@@ -11,6 +11,8 @@ namespace Logof_Client;
public class PdfBuilder
{
private const char RecipientTextMarker = '\u001f';
private readonly PdfExportSettings _settings;
private readonly XFont _boldFont;
private readonly XFont _regularFont;
@@ -140,9 +142,9 @@ public class PdfBuilder
else
senderLineID += "\n</font6>";
if (!string.IsNullOrEmpty(senderLineID))
addresses_german.Add(senderLineID + (addr ?? ""));
addresses_german.Add(senderLineID + RecipientTextMarker + (addr ?? ""));
else
addresses_german.Add(addr);
addresses_german.Add(RecipientTextMarker + (addr ?? ""));
}
// international addresses
@@ -163,9 +165,9 @@ public class PdfBuilder
else
senderLineID += "\n</font6>";
if (!string.IsNullOrEmpty(senderLineID))
addresses_inter.Add(senderLineID + (addr ?? ""));
addresses_inter.Add(senderLineID + RecipientTextMarker + (addr ?? ""));
else
addresses_inter.Add(addr);
addresses_inter.Add(RecipientTextMarker + (addr ?? ""));
}
if (addresses_german.Count == 0 && addresses_inter.Count == 0)
@@ -359,12 +361,14 @@ public class PdfBuilder
public string Text { get; }
public TextStyle Style { get; }
public bool LineBreak { get; }
public bool WrapAtWords { get; }
public TextToken(string text, TextStyle style, bool lineBreak = false)
public TextToken(string text, TextStyle style, bool lineBreak = false, bool wrapAtWords = false)
{
Text = text;
Style = style;
LineBreak = lineBreak;
WrapAtWords = wrapAtWords;
}
}
@@ -418,19 +422,28 @@ public class PdfBuilder
var i = 0;
var style = TextStyle.Regular;
var wrapAtWords = false;
var buffer = new System.Text.StringBuilder();
void FlushBuffer()
{
if (buffer.Length > 0)
{
tokens.Add(new TextToken(buffer.ToString(), style));
tokens.Add(new TextToken(buffer.ToString(), style, wrapAtWords: wrapAtWords));
buffer.Clear();
}
}
while (i < text.Length)
{
if (text[i] == RecipientTextMarker)
{
FlushBuffer();
wrapAtWords = true;
i++;
continue;
}
if (text[i] == '\r')
{
i++;
@@ -547,6 +560,39 @@ public class PdfBuilder
available = maxWidth;
}
if (token.WrapAtWords)
{
var wordParts = System.Text.RegularExpressions.Regex.Split(remaining, @"(\s+)");
foreach (var wordPart in wordParts)
{
if (wordPart.Length == 0)
continue;
var wordWidth = gfx.MeasureString(wordPart, font).Width;
if (string.IsNullOrWhiteSpace(wordPart))
{
if (currentLine.Count == 0)
continue;
if (currentWidth + wordWidth > maxWidth)
{
PushLine();
continue;
}
}
else if (currentLine.Count > 0 && currentWidth + wordWidth > maxWidth)
{
PushLine();
}
currentLine.Add(new TextRun { Text = wordPart, Font = font });
currentWidth += wordWidth;
}
remaining = "";
continue;
}
var fit = FitTextToWidth(gfx, remaining, font, available);
if (string.IsNullOrEmpty(fit))
{