From 1972e4753ad6d4f659d47b5182c32ae6e051a090 Mon Sep 17 00:00:00 2001 From: Rion Hwang <111563519+RionHwang0@users.noreply.github.com> Date: Sun, 6 Dec 2026 14:01:08 +0900 Subject: [PATCH 1/2] fix(hwpx): keep the captions of text boxes, shapes and group members MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HwpxReader kept a caption only on hp:pic, so the caption of a text box or other shape (hp:rect, hp:ellipse, ...) and of every member of a group (hp:container) was dropped on HWPX → DOCX, HTML, EPUB and the other routes that read HWPX with Filee's own reader. Every drawing object's caption now follows its anchor paragraph, as a picture's already does. --- src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs | 10 ++-- .../Hwp/Hwpx/HwpxReader.Objects.cs | 18 ++++++- tests/Filee.Engines.Tests/HwpxReaderTests.cs | 49 +++++++++++++++++++ 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs index 95849da4c..698e91b0f 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs @@ -77,7 +77,7 @@ private sealed class ParagraphBuilder private readonly int _heading; private readonly HListRef? _list; private bool _afterTable; - private List? _pictureCaptions; + private List? _objectCaptions; // Text joined to the HText at _joinedAt in _joinedSink, kept in a StringBuilder until something else is // added there: joining strings one piece at a time costs the square of the run's length (a run of a @@ -166,16 +166,16 @@ public void Table(HTable table) _afterTable = true; } - /// Keep captions in reading order after the anchor paragraph, even when more text follows the picture. - public void AddPictureCaption(List blocks) => (_pictureCaptions ??= []).AddRange(blocks); + /// Keep captions in reading order after the anchor paragraph, even when more text follows the object. + public void AddObjectCaption(List blocks) => (_objectCaptions ??= []).AddRange(blocks); public void Finish() { FinishJoin(); if (!_afterTable || HasContent) Emit(); - if (_pictureCaptions is not null) - _output.AddRange(_pictureCaptions); + if (_objectCaptions is not null) + _output.AddRange(_objectCaptions); } private bool HasContent => Current.Inlines.Any(i => i is not HLink { Content.Count: 0 }); diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs index 1b816abe3..4aa66e039 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs @@ -22,8 +22,7 @@ private void DrawingObject(XElement element, ParagraphBuilder builder, HCharForm { foreach (var inline in Shapes(element, PlacementOf(element))) builder.Add(inline); - if (name == "pic" && Child(Child(element, "caption"), "subList") is { } caption) - builder.AddPictureCaption(ReadSubList(caption)); + AddCaptions(element, builder); return; } switch (name) @@ -53,6 +52,21 @@ private void DrawingObject(XElement element, ParagraphBuilder builder, HCharForm } } + /// + /// The captions of a drawing object, after its anchor paragraph: 한글 lets every object carry one (a picture, a + /// text box, a shape), and in a group each member keeps its own. + /// + private void AddCaptions(XElement element, ParagraphBuilder builder) + { + if (Child(Child(element, "caption"), "subList") is { } caption) + builder.AddObjectCaption(ReadSubList(caption)); + if (element.Name.LocalName == "container") + { + foreach (var member in Elements(element).Where(m => ShapeNames.Contains(m.Name.LocalName))) + AddCaptions(member, builder); + } + } + /// The model objects for one drawing object; groups give one object per member. private IEnumerable Shapes(XElement element, Placement placement) { diff --git a/tests/Filee.Engines.Tests/HwpxReaderTests.cs b/tests/Filee.Engines.Tests/HwpxReaderTests.cs index 6c4dc5afb..6d5be4c31 100644 --- a/tests/Filee.Engines.Tests/HwpxReaderTests.cs +++ b/tests/Filee.Engines.Tests/HwpxReaderTests.cs @@ -539,6 +539,55 @@ public async Task A_picture_caption_follows_its_complete_anchor_paragraph_in_doc Assert.Equal(["그림 설명", "설명 둘째 줄", "다음 문단"], anchor.ElementsAfterSelf(w + "p").Take(3).Select(Words)); } + [Theory] + [InlineData(false)] + [InlineData(true)] + public void A_text_box_caption_follows_its_anchor_paragraph_also_in_a_group(bool grouped) + { + var dir = fx.NewFolder(); + var box = new HTextBox { Width = 8000, Height = 3000 }; + box.Blocks.Add(ParagraphOf([new HText("상자 안", new HCharFormat())])); + var section = new HSection(); + section.Blocks.Add(ParagraphOf([new HText("상자 앞 ", new HCharFormat()), box, new HText(" 상자 뒤", new HCharFormat())])); + section.Blocks.Add(ParagraphOf([new HText("다음 문단", new HCharFormat())])); + var document = new HDocument(); + document.Sections.Add(section); + var hwpx = Path.Combine(dir, "box.hwpx"); + HwpxWriter.Write(document, hwpx); + + // 한글 stores a caption inside the object it belongs to, here the text box (hp:rect), or a member of a group. + using (var zip = ZipFile.Open(hwpx, ZipArchiveMode.Update)) + { + var entry = zip.GetEntry("Contents/section0.xml")!; + XDocument xml; + using (var stream = entry.Open()) + xml = XDocument.Load(stream, LoadOptions.PreserveWhitespace); + var hp = HwpxAssert.Hp; + var rect = xml.Descendants(hp + "rect").Single(); + rect.Add(new XElement(hp + "caption", new XAttribute("side", "BOTTOM"), new XAttribute("fullSz", "0"), + new XAttribute("width", "8000"), new XAttribute("gap", "850"), new XAttribute("lastWidth", "8000"), + new XElement(hp + "subList", new XAttribute("vertAlign", "TOP"), + new XElement(hp + "p", new XAttribute("paraPrIDRef", "0"), new XAttribute("styleIDRef", "0"), + new XElement(hp + "run", new XAttribute("charPrIDRef", "0"), new XElement(hp + "t", "상자 설명")))))); + if (grouped) + { + // A group (hp:container) placed where the box was, with the box as its only member. + var group = new XElement(hp + "container", rect.Attributes().Where(a => a.Name.LocalName is "id" or "zOrder" or "textWrap" or "textFlow"), + new XElement(rect.Element(hp + "sz")!), new XElement(rect.Element(hp + "pos")!), new XElement(rect)); + rect.ReplaceWith(group); + } + entry.Delete(); + using var output = zip.CreateEntry("Contents/section0.xml").Open(); + xml.Save(output); + } + + var read = HwpxReader.Read(hwpx, Path.Combine(dir, "media")); + var blocks = read.Sections.Single().Blocks; + var anchor = blocks.FindIndex(b => Text([b]).Contains("상자 앞", StringComparison.Ordinal)); + Assert.True(anchor >= 0, HDocumentAssert.Dump(read, layout: false)); + Assert.Equal(["상자 설명", "다음 문단"], blocks.Skip(anchor + 1).Where(b => Text([b]).Length > 0).Take(2).Select(b => Text([b]))); + } + private static HParagraph ParagraphOf(IEnumerable inlines) { var paragraph = new HParagraph(); From 00cc8e29e54f6ff22cabddb28ec965d4c651a268 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Sun, 6 Dec 2026 18:13:20 +0900 Subject: [PATCH 2/2] docs(changelog): captions of text boxes and grouped objects kept from HWPX (#2107) Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 434f63b4d..3ca65c61f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -676,6 +676,10 @@ zh: 修复了 HTML 和 Word 转 PDF、PDF/A 表单值、PDF 页面编辑的书 - **Tables inside table cells kept in TXT and Markdown** (by @RionHwang0, #2106). HTML to TXT, and e-books to TXT or Markdown, dropped a table nested in a cell with all its text. Its text now stays in that cell, on one line. +- **Captions of text boxes and grouped objects kept from HWPX** (by @RionHwang0, #2107). Converting HWPX to DOCX, + HTML, EPUB and the other formats dropped the caption of a text box, a shape or an object in a group. It now comes + after the paragraph the object sits in, like a picture's caption. + ## 2.5.0