diff --git a/Elwig/Documents/BusinessDocument.cs b/Elwig/Documents/BusinessDocument.cs index 295fb31..b09108a 100644 --- a/Elwig/Documents/BusinessDocument.cs +++ b/Elwig/Documents/BusinessDocument.cs @@ -137,11 +137,15 @@ namespace Elwig.Documents { return new KernedParagraph(10) .AddAll(Utils.GenerateFooter("\n", " \u00b7 ") .Item(c.NameFull).NextLine() - .Item(c.Address).Item($"{c.Plz} {c.Ort}").Item("Österreich").Item("Tel.", c.PhoneNr).Item("Fax", c.FaxNr).NextLine() - .Item(c.EmailAddress != null ? link1 : null) - .Item(c.Website != null ? link2 : null) - .Item("Betriebs-Nr.", c.LfbisNr).Item("Bio-KSt.", c.OrganicAuthorityCode).NextLine() + .Item(c.Address).Item($"{c.Plz} {c.Ort}").Item("Österreich").NextGroup() + .Item("Tel.", c.PhoneNr).Item("Fax", c.FaxNr).NextGroup() + .Item(c.EmailAddress != null ? link1 : null).NextLine() + .Item(c.Website != null ? link2 : null).NextGroup() + .Item("Betriebs-Nr.", c.LfbisNr).NextGroup() + .Item("Bio-KSt.", c.OrganicAuthorityCode).NextGroup() + .Item("OOC-ID", c.OrganicOperatorId).NextLine() .Item("UID", c.UstIdNr).Item("BIC", c.Bic).Item("IBAN", c.Iban) + .RebalanceLines() .ToLeafElements()); } diff --git a/Elwig/Helpers/Utils.cs b/Elwig/Helpers/Utils.cs index 6c736c3..629ad6e 100644 --- a/Elwig/Helpers/Utils.cs +++ b/Elwig/Helpers/Utils.cs @@ -323,7 +323,7 @@ namespace Elwig.Helpers { } public class Footer { - private readonly List> Items = [[]]; + private readonly List>> Items = [[[]]]; private readonly string LineBreak; private readonly string Separator; @@ -334,7 +334,7 @@ namespace Elwig.Helpers { public Footer Item(object? text) { if (text == null) return this; - Items[^1].Add(text); + Items[^1][^1].Add(text); return this; } @@ -342,13 +342,38 @@ namespace Elwig.Helpers { return text == null ? this : Item($"{name}: {text}"); } + public Footer NextGroup() { + Items[^1].Add([]); + return this; + } + public Footer NextLine() { - Items.Add([]); + Items.Add([[]]); + return this; + } + + public Footer RebalanceLines() { + for (int i = 0; i < Items.Count - 1; i++) { + var groups = Items[i]; + var nextGroups = Items[i + 1]; + if (groups.Count <= 1) continue; + var g = groups[^1]; + var gl = g.Sum(i => (i is Text txt ? txt.GetText().Length : i.ToString()?.Length ?? 0) + Separator.Length); + var l1a = groups.SelectMany(g => g).Sum(i => (i is Text txt ? txt.GetText().Length : i.ToString()?.Length ?? 0) + Separator.Length) - Separator.Length; + var l2a = nextGroups.SelectMany(g => g).Sum(i => (i is Text txt ? txt.GetText().Length : i.ToString()?.Length ?? 0) + Separator.Length) - Separator.Length; + var l1b = l1a - gl; + var l2b = l2a + gl; + if (Math.Abs(l1b - l2b) <= Math.Abs(l1a - l2a)) { + Items[i].Remove(g); + Items[i + 1].Insert(0, g); + i--; + } + } return this; } public override string ToString() { - return string.Join(LineBreak, Items.Select(l => string.Join(Separator, l.ToString()))); + return string.Join(LineBreak, Items.Select(l => string.Join(Separator, l.SelectMany(g => g).ToString()))); } public IList ToLeafElements() { @@ -357,10 +382,12 @@ namespace Elwig.Helpers { foreach (var line in Items) { if (!first1) l.Add(new Text(LineBreak)); var first2 = true; - foreach (var item in line) { - if (!first2) l.Add(new Text(Separator)); - l.Add(item as ILeafElement ?? new Text(item.ToString() ?? "")); - first2 = false; + foreach (var group in line) { + foreach (var item in group) { + if (!first2) l.Add(new Text(Separator)); + l.Add(item as ILeafElement ?? new Text(item.ToString() ?? "")); + first2 = false; + } } first1 = false; } @@ -675,7 +702,7 @@ namespace Elwig.Helpers { p[2], int.Parse(p[3]), p[4], - p[5].Split(',').Select(a => a.Replace(" | ", "\n")).ToArray(), + [.. p[5].Split(',').Select(a => a.Replace(" | ", "\n"))], p[6], p[7].Split(',') ));