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 public class PdfBuilder
{ {
private const char RecipientTextMarker = '\u001f';
private readonly PdfExportSettings _settings; private readonly PdfExportSettings _settings;
private readonly XFont _boldFont; private readonly XFont _boldFont;
private readonly XFont _regularFont; private readonly XFont _regularFont;
@@ -140,9 +142,9 @@ public class PdfBuilder
else else
senderLineID += "\n</font6>"; senderLineID += "\n</font6>";
if (!string.IsNullOrEmpty(senderLineID)) if (!string.IsNullOrEmpty(senderLineID))
addresses_german.Add(senderLineID + (addr ?? "")); addresses_german.Add(senderLineID + RecipientTextMarker + (addr ?? ""));
else else
addresses_german.Add(addr); addresses_german.Add(RecipientTextMarker + (addr ?? ""));
} }
// international addresses // international addresses
@@ -163,9 +165,9 @@ public class PdfBuilder
else else
senderLineID += "\n</font6>"; senderLineID += "\n</font6>";
if (!string.IsNullOrEmpty(senderLineID)) if (!string.IsNullOrEmpty(senderLineID))
addresses_inter.Add(senderLineID + (addr ?? "")); addresses_inter.Add(senderLineID + RecipientTextMarker + (addr ?? ""));
else else
addresses_inter.Add(addr); addresses_inter.Add(RecipientTextMarker + (addr ?? ""));
} }
if (addresses_german.Count == 0 && addresses_inter.Count == 0) if (addresses_german.Count == 0 && addresses_inter.Count == 0)
@@ -359,12 +361,14 @@ public class PdfBuilder
public string Text { get; } public string Text { get; }
public TextStyle Style { get; } public TextStyle Style { get; }
public bool LineBreak { 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; Text = text;
Style = style; Style = style;
LineBreak = lineBreak; LineBreak = lineBreak;
WrapAtWords = wrapAtWords;
} }
} }
@@ -418,19 +422,28 @@ public class PdfBuilder
var i = 0; var i = 0;
var style = TextStyle.Regular; var style = TextStyle.Regular;
var wrapAtWords = false;
var buffer = new System.Text.StringBuilder(); var buffer = new System.Text.StringBuilder();
void FlushBuffer() void FlushBuffer()
{ {
if (buffer.Length > 0) if (buffer.Length > 0)
{ {
tokens.Add(new TextToken(buffer.ToString(), style)); tokens.Add(new TextToken(buffer.ToString(), style, wrapAtWords: wrapAtWords));
buffer.Clear(); buffer.Clear();
} }
} }
while (i < text.Length) while (i < text.Length)
{ {
if (text[i] == RecipientTextMarker)
{
FlushBuffer();
wrapAtWords = true;
i++;
continue;
}
if (text[i] == '\r') if (text[i] == '\r')
{ {
i++; i++;
@@ -547,6 +560,39 @@ public class PdfBuilder
available = maxWidth; 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); var fit = FitTextToWidth(gfx, remaining, font, available);
if (string.IsNullOrEmpty(fit)) if (string.IsNullOrEmpty(fit))
{ {