Skip to content

feat(layout): give a paragraph holding only a picture its line cache - #552

Merged
airmang merged 1 commit into
mainfrom
feat/hwp-save-line-caches
Oct 23, 2026
Merged

airmang merged 1 commit into
mainfrom
feat/hwp-save-line-caches

Conversation

@airmang

@airmang airmang commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Addresses #550 as an opt-in.

Background

A .hwp paragraph's PARA_LINE_SEG is the same line layout cache as hp:linesegarray in HWPX. python-hwpx writes new paragraphs without one and leaves the layout to Hancom Office, which lays out a paragraph without a cache when it opens the file. Hancom does take a cache that is present as written, so a cache can only be written when it is exactly the one Hancom would write — a wrong one moves or squeezes text.

Some other HWP viewers place a picture from its paragraph's PARA_LINE_SEG and skip it when there is none, which is what #550 reports.

Change

hwpx.layout.line_cache.write_line_caches(doc) (experimental, opt-in) now also writes the one line Hancom writes for a paragraph holding nothing but a plain picture set as a character:

  • vertsize = textheight = picture height + outer margins, or the paragraph's character size when larger
  • baseline = 85 % of that
  • spacing = the paragraph's percentage spacing over its characters, or over the line when the picture affects the line spacing

A picture with a caption, a shadow or effects, or in a paragraph not spaced by a percentage, is left for Hancom.

Calling it before saving to .hwp carries the caches into PARA_LINE_SEG:

from hwpx.layout.line_cache import write_line_caches
write_line_caches(doc)
doc.save_to_path("out.hwp")

Saving without calling it is unchanged.

Tests

  • line_cache_pictures_alone.hwpx: a Hancom-saved document of 120 picture paragraphs (character sizes, picture heights, outer margins, affectLSpacing) between text paragraphs; every cache written matches Hancom's.
  • A picture paragraph's cache reaches PARA_LINE_SEG in an .hwp save; a picture with a caption, a shadow or a fixed spacing gets none.

🤖 Generated with Claude Code

write_line_caches now writes the one line Hancom writes for a paragraph
holding nothing but a plain picture set as a character: as tall as the
picture and its outer margins, or as the paragraph's characters when they
are taller, its baseline at 85 % of that, spaced as the paragraph's
characters are unless the picture affects the line spacing. A picture with
a caption, a shadow or effects, or in a paragraph not spaced by a
percentage, is left for Hancom.

Saved as .hwp, the cache becomes the paragraph's PARA_LINE_SEG, which
viewers other than Hancom Office use to place the picture (#550). Saving
without calling write_line_caches is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@airmang
airmang force-pushed the feat/hwp-save-line-caches branch from 5c6d850 to 57cbb07 Compare October 23, 2026 13:15
@airmang
airmang merged commit 0f2e136 into main Oct 23, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant