Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- update
Expand Down
10 changes: 5 additions & 5 deletions src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private sealed class ParagraphBuilder
private readonly int _heading;
private readonly HListRef? _list;
private bool _afterTable;
private List<HBlock>? _pictureCaptions;
private List<HBlock>? _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
Expand Down Expand Up @@ -166,16 +166,16 @@ public void Table(HTable table)
_afterTable = true;
}

/// <summary>Keep captions in reading order after the anchor paragraph, even when more text follows the picture.</summary>
public void AddPictureCaption(List<HBlock> blocks) => (_pictureCaptions ??= []).AddRange(blocks);
/// <summary>Keep captions in reading order after the anchor paragraph, even when more text follows the object.</summary>
public void AddObjectCaption(List<HBlock> 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 });
Expand Down
18 changes: 16 additions & 2 deletions src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -53,6 +52,21 @@ private void DrawingObject(XElement element, ParagraphBuilder builder, HCharForm
}
}

/// <summary>
/// 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.
/// </summary>
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);
}
}

/// <summary>The model objects for one drawing object; groups give one object per member.</summary>
private IEnumerable<HInline> Shapes(XElement element, Placement placement)
{
Expand Down
49 changes: 49 additions & 0 deletions tests/Filee.Engines.Tests/HwpxReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HInline> inlines)
{
var paragraph = new HParagraph();
Expand Down
Loading