Files
fotoplaner/venv/lib/python3.12/site-packages/docling/backend/iwork/archives.py
T
anders 16c54c6653 Tilføjet venv med docling og afhængigheder
Testfiler opdelt i PNG og previews
2026-10-27 12:20:23 +01:00

1341 lines
46 KiB
Python

# SPDX-FileCopyrightText: The Docling Contributors
# SPDX-License-Identifier: MIT
"""Readers for the archives an iWork 2013+ document is built from.
Pages, Numbers and Keynote share their engines, and share the archives those
engines write: ``TSWP`` holds text, ``TST`` tables, ``TSD`` drawables and
``TSK`` annotations. A paragraph in a Pages document and a paragraph on a
Keynote slide are the same ``TSWP.StorageArchive``, laid out the same way, so
they are read once here and the per-app modules are left with nothing but their
own document walk.
Only the message and field numbers are format knowledge; the container layer
lives in :mod:`docling.backend.iwork.iwa`. Apple has never published the
schemas, so every number below was established against real documents.
"""
import logging
import struct
import zipfile
from collections.abc import Callable, Iterator
from decimal import Decimal
from typing import NamedTuple, TypeVar
from docling_core.types.doc import (
Formatting,
TableCell,
TableData,
)
from docling.backend.iwork.cells import (
CellValues,
iwa_cell_text,
)
from docling.backend.iwork.content import (
SCRIPTS,
Block,
Comment,
Geometry,
ListStyle,
Paragraph,
Picture,
StorageRuns,
authored,
build_formatting,
label_for_style,
list_label_at,
runs_for,
split_paragraphs,
value_at,
)
from docling.backend.iwork.iwa import (
IWAObject,
iter_objects,
read_fields,
read_reference,
)
from docling.exceptions import DocumentLoadError
_log = logging.getLogger(__name__)
_T = TypeVar("_T")
class Placement(NamedTuple):
"""Where one stored cell sits in a table, and where to find its bytes."""
row: int
col: int
storage: bytes
start: int
MAX_REFERENCE_DEPTH = 4
"""How far to descend when collecting references from a message."""
REFERENCE_MAX_BYTES = 6
"""Longest a ``TSP.Reference`` can be; anything larger is a nested message."""
TSWP_CHARACTER_STYLE = 2021
"""Message type of ``TSWP.CharacterStyleArchive``."""
STORAGE_CHARACTER_STYLE_FIELD = 8
"""Field of ``TSWP.StorageArchive`` holding the character style run table."""
STYLE_PROPERTIES_FIELD = 11
"""Field of a character style holding its property map."""
STYLE_SCRIPT_FIELD = 10
"""Field of a character style holding its superscript or subscript setting."""
CHARACTER_PROPERTY_LABELS = {
1: "bold",
2: "italic",
11: "underline",
12: "strikethrough",
}
"""Property fields of a character style, as they map onto ``Formatting``.
Established by correlating style *names* with their properties across three real
Apple documents: "Emphasis"/"Bold" set field 1, "Italic" field 2, "Underline"
and "Link" field 11, and "Strikethrough" field 12. Fields carrying anything else
— colours, fonts, capitalisation — have no equivalent here and are ignored.
"""
TSWP_SHAPE_INFO = 2011
"""Message type of ``TSWP.ShapeInfoArchive``, a shape that holds text."""
SHAPE_STORAGE_FIELD = 2
"""Field of ``TSWP.ShapeInfoArchive`` referencing the storage holding its text."""
GEOMETRY_POSITION_FIELD = 1
GEOMETRY_SIZE_FIELD = 2
"""Fields of ``TSD.GeometryArchive``, each a pair of 32-bit floats.
Every drawable reaches its geometry by the same route: a chain of ``super``
messages, each of them field 1 of the one above, ending at the
``TSD.DrawableArchive`` whose own field 1 this is. How long that chain is
depends on what the drawable is — an image sits one level above it, a Keynote
placeholder four — so :func:`drawable_geometry` walks down rather than counting.
"""
POINT_X_FIELD = 1
POINT_Y_FIELD = 2
"""Fields of a ``TSP.Point``, which ``TSP.Size`` spells the same way."""
MAX_DRAWABLE_DEPTH = 6
"""How far to descend through a drawable's supers looking for its geometry."""
TSWP_STORAGE_ARCHIVE = 2001
"""Message type of ``TSWP.StorageArchive``, which holds a run of text.
TSWP is Apple's shared text engine, so the same archive appears in Numbers and
Keynote documents.
"""
TSWP_PARAGRAPH_STYLE = 2022
"""Message type of ``TSWP.ParagraphStyleArchive``, named by its ``TSS`` super."""
STORAGE_TEXT_FIELD = 3
"""Field of ``TSWP.StorageArchive`` holding the text itself."""
STYLE_SUPER_FIELD = 1
"""Field of a ``TSWP`` style archive holding its ``TSS.StyleArchive`` super."""
STYLE_PARENT_FIELD = 3
"""Field of ``TSS.StyleArchive`` referencing the style it inherits from.
A style that sets a property leaves the rest to its parent, so a chain of
these is how a theme hands anything down. It is the 2013 spelling of the
``sf:parent-ident`` an iWork '09 style carries.
"""
MAX_STYLE_INHERITANCE = 8
"""How far to follow a style's parent before giving up.
A real chain is two or three long. The bound is what keeps a document whose
styles inherit from each other in a circle from looping forever.
"""
STYLE_NAME_FIELD = 1
"""Field of ``TSS.StyleArchive`` holding the style's human-facing name."""
TST_TABLE_MODEL = 6001
"""Message type of ``TST.TableModelArchive``, the root of one table."""
TST_TABULAR_INFO = 6000
"""Message type of ``TST.TableInfoArchive``, the drawable a table sits in."""
TABULAR_INFO_MODEL_FIELD = 2
"""Field of ``TST.TableInfoArchive`` referencing its ``TST.TableModelArchive``."""
TSD_IMAGE = 3005
"""Message type of ``TSD.ImageArchive``, one placed image."""
TSD_GROUP = 3008
"""Message type of ``TSD.GroupArchive``, several drawables grouped together."""
GROUP_CHILDREN_FIELD = 2
"""Field of ``TSD.GroupArchive`` referencing the drawables it holds."""
IMAGE_DATA_FIELDS = (15, 13, 11, 12)
"""Fields of ``TSD.ImageArchive`` that may carry the image's bytes.
Pages keeps several renditions of a placed image and does not always write all
of them, so they are tried in descending order of fidelity: the adjusted image
first, then the original, then the placed data, then the thumbnail.
"""
TSWP_DRAWABLE_ATTACHMENT = 2003
"""Message type of ``TSWP.DrawableAttachmentArchive``.
This is what a U+FFFC in the text resolves to: a drawable — an image, a table,
a text box — anchored at that character.
"""
ATTACHMENT_DRAWABLE_FIELD = 1
"""Field of ``TSWP.DrawableAttachmentArchive`` referencing the anchored drawable."""
STORAGE_ATTACHMENT_FIELD = 9
"""Field of ``TSWP.StorageArchive`` holding the attachment run table."""
STORAGE_COMMENT_FIELD = 23
"""Field of ``TSWP.StorageArchive`` holding the comment run table.
Its entries mark the stretch of text a comment is attached to, which is how
Pages 5 records a comment: as a highlight over the words being commented on
rather than as a character in the text.
"""
TSWP_COMMENT_FIELD = 2013
"""Message type of ``TSWP.CommentFieldArchive``, the anchor of one comment."""
COMMENT_FIELD_STORAGE_FIELD = 1
"""Field of ``TSWP.CommentFieldArchive`` referencing the comment itself."""
TSD_COMMENT_STORAGE = 3056
"""Message type of ``TSD.CommentStorageArchive``, one comment or one reply."""
COMMENT_TEXT_FIELD = 1
COMMENT_AUTHOR_FIELD = 3
COMMENT_REPLIES_FIELD = 4
"""Fields of ``TSD.CommentStorageArchive``.
Replies are comments in their own right, so a thread is a chain to be followed.
"""
TSK_ANNOTATION_AUTHOR = 212
"""Message type of ``TSK.AnnotationAuthorArchive``, who wrote a comment."""
AUTHOR_NAME_FIELD = 1
"""Field of ``TSK.AnnotationAuthorArchive`` holding the author's name."""
TSWP_NOTE = 2008
"""Message type of ``TSWP.NoteArchive``, one footnote or endnote."""
NOTE_STORAGE_FIELD = 2
"""Field of ``TSWP.NoteArchive`` referencing the storage holding the note's text."""
STORAGE_FOOTNOTE_FIELD = 16
"""Field of ``TSWP.StorageArchive`` holding the footnote run table.
Its entries anchor a note at the character the footnote mark occupies, which is
one of the U+FFFC placeholders in the text.
"""
STORAGE_PAGE_MASTER_FIELD = 17
"""Field of ``TSWP.StorageArchive`` holding the page master run table.
Headers and footers hang off the page master that covers a stretch of the
document rather than off the text itself, so this is the way in to them.
"""
TSP_PACKAGE_METADATA = 11006
"""Message type of ``TSP.PackageMetadata``, which names the container's data files."""
PACKAGE_DATAS_FIELD = 4
"""Field of ``TSP.PackageMetadata`` listing one ``TSP.DataInfo`` per data file."""
DATA_INFO_IDENTIFIER_FIELD = 1
DATA_INFO_PREFERRED_NAME_FIELD = 3
DATA_INFO_NAME_FIELD = 4
"""Fields of ``TSP.DataInfo``.
An image references a data file by identifier; the file itself is a ``Data/``
member of the container, named by ``file_name`` when Pages renamed it on import
and by ``preferred_file_name`` otherwise.
"""
DATA_MEMBER_PREFIX = "Data/"
TST_TILE = 6002
"""Message type of ``TST.Tile``, which lays a table's cells out into rows."""
TST_DATA_LIST = 6005
"""Message type of ``TST.TableDataList``, a table's shared value table.
Cells reference their contents by key rather than holding them, so two cells
with the same text share a single entry.
"""
TABLE_ROWS_FIELD = 6
TABLE_COLS_FIELD = 7
TABLE_HEADER_ROWS_FIELD = 9
TABLE_DATA_STORE_FIELD = 4
"""Fields of ``TST.TableModelArchive``: geometry, header rows, and data store."""
STORE_TILES_FIELD = 3
STORE_STRINGS_FIELD = 4
STORE_RICH_TEXT_FIELD = 17
"""Fields of a table's data store: its tiles, and its two value lists.
A cell holding plain text references the string list; one holding styled text
references the rich text list instead, whose entries point at a whole
``TSWP.StorageArchive``.
"""
LIST_ENTRIES_FIELD = 3
LIST_SEGMENTS_FIELD = 4
"""Fields of ``TST.TableDataList``: its entries, and the segments they spill into.
A list long enough to be split keeps its entries in referenced segments instead,
which have the same entry shape.
"""
ENTRY_KEY_FIELD = 1
ENTRY_STRING_FIELD = 3
ENTRY_RICH_TEXT_FIELD = 9
"""Fields of one value list entry: the key cells reference it by, and its value."""
TST_TEXT_REF = 6218
"""Message type of the indirection a rich text entry points at.
It holds nothing but a reference to the ``TSWP.StorageArchive`` with the text.
"""
TILE_LIST_FIELD = 1
TILE_ENTRY_ROW_FIELD = 1
TILE_ENTRY_TILE_FIELD = 2
"""Fields of the tile list: each entry's first row, and the tile itself."""
STORE_STRINGS_FIELD = 4
STORE_RICH_TEXT_FIELD = 17
"""Fields of a table's data store: its tiles, and its two value lists.
A cell holding plain text references the string list; one holding styled text
references the rich text list instead, whose entries point at a whole
``TSWP.StorageArchive``.
"""
LIST_ENTRIES_FIELD = 3
LIST_SEGMENTS_FIELD = 4
"""Fields of ``TST.TableDataList``: its entries, and the segments they spill into.
A list long enough to be split keeps its entries in referenced segments instead,
which have the same entry shape.
"""
ENTRY_KEY_FIELD = 1
ENTRY_STRING_FIELD = 3
ENTRY_RICH_TEXT_FIELD = 9
"""Fields of one value list entry: the key cells reference it by, and its value."""
TST_TEXT_REF = 6218
"""Message type of the indirection a rich text entry points at.
It holds nothing but a reference to the ``TSWP.StorageArchive`` with the text.
"""
RICH_TEXT_STORAGE = 2001
"""Message type of ``TSWP.StorageArchive``, where a styled cell parks its text.
TSWP is Apple's text engine, so a cell whose text carries formatting keeps it in
the same archive the body of a Pages document uses. Only the text is wanted here
— what a table cell is styled with is not recovered — so the archive is read for
that one field rather than through the text engine proper.
"""
RICH_TEXT_FIELD = 3
"""Field of ``TSWP.StorageArchive`` holding the text itself."""
TILE_ROWS_FIELD = 5
ROW_INDEX_FIELD = 1
ROW_STORAGE_FIELD = 3
ROW_OFFSETS_FIELD = 4
ROW_WIDE_STORAGE_FIELD = 6
ROW_WIDE_OFFSETS_FIELD = 7
ROW_WIDE_OFFSETS_FLAG = 8
"""Fields of ``TST.Tile`` and of one of its rows.
A row holds a packed cell buffer plus one ``int16`` offset per column, where a
negative offset marks a column with no cell. Pages 5.2 moved both to their own
fields and started scaling the offsets by four, keeping the older pair in place
for the benefit of releases that could not read the new one, so the newer pair
is preferred when it is there.
"""
STORAGE_PARAGRAPH_STYLE_FIELD = 5
"""Field of ``TSWP.StorageArchive`` holding the paragraph style run table.
Each entry pairs a character index with a reference to the style that applies
from there. Entries without a reference leave the preceding style in force.
"""
TSWP_LINK_FIELD = 2032
"""Message type of ``TSWP.LinkFieldArchive``, one hyperlink."""
LINK_URL_FIELD = 2
"""Field of ``TSWP.LinkFieldArchive`` holding the address it points at."""
STORAGE_SMART_FIELD = 11
"""Field of ``TSWP.StorageArchive`` holding the smart field run table.
Pages calls a hyperlink a smart field, alongside placeholders and date fields,
so this table is read for links and anything else in it is left alone.
"""
TSWP_LIST_STYLE = 2023
"""Message type of ``TSWP.ListStyleArchive``, which labels a list's levels."""
STORAGE_LIST_DEPTH_FIELD = 6
"""Field of ``TSWP.StorageArchive`` holding each paragraph's nesting depth.
Its entries carry two numbers rather than a reference; the first is the depth,
counted from zero, and a document with no nesting carries the single entry
``(0, 0)``.
"""
STORAGE_LIST_STYLE_FIELD = 7
"""Field of ``TSWP.StorageArchive`` holding the list style run table.
This, not the depth, is what makes a paragraph a list item: Pages leaves a list
style in force over plain paragraphs too, and the style's label type for the
paragraph's depth is what says whether a marker is drawn.
"""
LIST_LABEL_TYPES_FIELD = 11
LIST_STRINGS_FIELD = 16
"""Fields of ``TSWP.ListStyleArchive``, one entry per nesting depth."""
def iwa_style_name(payload: bytes) -> str | None:
"""Read a paragraph style's name out of its ``TSS`` super message.
``TSWP.ParagraphStyleArchive`` wraps a ``TSS.StyleArchive`` that carries the
human-facing name ("Body", "Heading 1"). Anonymous styles — the ones Pages
creates for ad-hoc formatting — have no name and are treated as body text.
Args:
payload: The encoded ``TSWP.ParagraphStyleArchive``.
Returns:
The style name, or None when the style is anonymous.
"""
super_message = read_fields(payload).get(STYLE_SUPER_FIELD, [None])[0]
if not isinstance(super_message, bytes):
return None
name = read_fields(super_message).get(STYLE_NAME_FIELD, [None])[0]
if not isinstance(name, bytes):
return None
try:
return name.decode("utf-8")
except UnicodeDecodeError:
return None
def iwa_storage_text(fields: dict[int, list[int | bytes]]) -> str:
"""Join the text pieces of a ``TSWP.StorageArchive``."""
return "".join(
value.decode("utf-8", errors="replace")
for value in fields.get(STORAGE_TEXT_FIELD, [])
if isinstance(value, bytes)
)
def iwa_storage_runs(
fields: dict[int, list[int | bytes]], objects: dict[int, IWAObject]
) -> StorageRuns:
"""Resolve every run table a ``TSWP.StorageArchive`` carries.
Args:
fields: Decoded fields of the storage.
objects: Every object in the document, keyed by identifier.
Returns:
The tables, each sorted by character index.
"""
return StorageRuns(
styles=iwa_object_runs(
fields,
STORAGE_PARAGRAPH_STYLE_FIELD,
objects,
TSWP_PARAGRAPH_STYLE,
iwa_style_name,
),
characters=iwa_object_runs(
fields,
STORAGE_CHARACTER_STYLE_FIELD,
objects,
TSWP_CHARACTER_STYLE,
iwa_formatting,
),
lists=iwa_object_runs(
fields,
STORAGE_LIST_STYLE_FIELD,
objects,
TSWP_LIST_STYLE,
lambda payload: iwa_list_style(payload, objects),
),
depths=iwa_depth_runs(fields),
links=iwa_object_runs(
fields, STORAGE_SMART_FIELD, objects, TSWP_LINK_FIELD, iwa_link
),
)
def iwa_object_runs(
fields: dict[int, list[int | bytes]],
field: int,
objects: dict[int, IWAObject],
message_type: int,
decode: Callable[[bytes], _T | None],
) -> list[tuple[int, _T | None]]:
"""Resolve one ``TSWP.ObjectAttributeTable`` to (character index, value) pairs.
Every run table of a storage has this shape: entries pairing a character
index with a reference to the object that applies from there. An entry
without a reference clears the value from that character on, which is how
Pages ends a bold phrase or leaves a list.
Args:
fields: Decoded fields of the storage.
field: The storage field holding the table.
objects: Every object in the document, keyed by identifier.
message_type: The message type the referenced objects must have.
decode: Reads one referenced object's payload into a value.
Returns:
Character index and value pairs, in document order.
"""
table = fields.get(field, [])
if not table or not isinstance(table[0], bytes):
return []
runs: list[tuple[int, _T | None]] = []
for entry in safe_fields(table[0]).get(1, []):
if not isinstance(entry, bytes):
continue
parsed = safe_fields(entry)
index = parsed.get(1, [None])[0]
if not isinstance(index, int):
continue
reference = parsed.get(2, [None])[0]
value: _T | None = None
if isinstance(reference, bytes):
target = read_reference(reference)
referenced = objects.get(target) if target is not None else None
if referenced is not None and referenced.message_type == message_type:
value = decode(referenced.payload)
runs.append((index, value))
runs.sort(key=lambda run: run[0])
return runs
def iwa_depth_runs(fields: dict[int, list[int | bytes]]) -> list[tuple[int, int]]:
"""Resolve the list depth table, whose entries hold numbers, not references."""
table = fields.get(STORAGE_LIST_DEPTH_FIELD, [])
if not table or not isinstance(table[0], bytes):
return []
runs: list[tuple[int, int]] = []
for entry in safe_fields(table[0]).get(1, []):
if not isinstance(entry, bytes):
continue
parsed = safe_fields(entry)
index = parsed.get(1, [None])[0]
depth = parsed.get(2, [None])[0]
if isinstance(index, int) and isinstance(depth, int):
runs.append((index, depth))
runs.sort(key=lambda run: run[0])
return runs
def iwa_link(payload: bytes) -> str | None:
"""Read the address a ``TSWP.LinkFieldArchive`` points at."""
url = safe_fields(payload).get(LINK_URL_FIELD, [None])[0]
if not isinstance(url, bytes):
return None
return url.decode("utf-8", errors="replace").strip() or None
def iwa_list_style(payload: bytes, objects: dict[int, IWAObject]) -> ListStyle:
"""Read a ``TSWP.ListStyleArchive`` as its per-depth label ladder.
A style that carries no ladder of its own inherits the one its parent
carries, so the chain is followed until a ladder turns up. Keynote relies on
that: it leaves the ladder on the theme's style and gives the text a style
holding nothing but a parent, so without the chain a deck loses every bullet
it draws. It is the same indirection iWork '09 spells as ``sf:parent-ident``.
Args:
payload: The encoded ``TSWP.ListStyleArchive``.
objects: Every object in the document, keyed by identifier.
Returns:
The ladder in force, which is empty when the chain carries none.
"""
for _ in range(MAX_STYLE_INHERITANCE):
fields = safe_fields(payload)
label_types = tuple(
value
for value in fields.get(LIST_LABEL_TYPES_FIELD, [])
if isinstance(value, int)
)
if label_types:
strings = tuple(
value.decode("utf-8", errors="replace")
for value in fields.get(LIST_STRINGS_FIELD, [])
if isinstance(value, bytes)
)
return ListStyle(label_types, strings)
super_message = fields.get(STYLE_SUPER_FIELD, [None])[0]
if not isinstance(super_message, bytes):
break
parent = iwa_reference_field(super_message, STYLE_PARENT_FIELD)
inherited = objects.get(parent) if parent is not None else None
if inherited is None or inherited.message_type != TSWP_LIST_STYLE:
break
payload = inherited.payload
return ListStyle((), ())
def iwa_formatting(payload: bytes) -> Formatting | None:
"""Read a character style's property map as a :class:`Formatting`."""
properties = safe_fields(payload).get(STYLE_PROPERTIES_FIELD, [None])[0]
if not isinstance(properties, bytes):
return None
decoded = safe_fields(properties)
active = {
label
for field, label in CHARACTER_PROPERTY_LABELS.items()
if any(isinstance(value, int) and value for value in decoded.get(field, []))
}
script = decoded.get(STYLE_SCRIPT_FIELD, [None])[0]
return build_formatting(
active, SCRIPTS.get(script) if isinstance(script, int) else None
)
def iwa_table(model: IWAObject, objects: dict[int, IWAObject]) -> TableData | None:
"""Build table data from one ``TST.TableModelArchive``.
A table keeps its geometry on the model, its cell contents in a shared value
list, and the placement of those values in tiles. Cells reference their value
by key, so equal values share one entry — which is why the tiles have to be
read rather than assuming the value list is already in cell order.
Args:
model: The table's ``TST.TableModelArchive``.
objects: Every object in the document, keyed by identifier.
Returns:
The table, or None when nothing readable could be placed in it.
"""
fields = safe_fields(model.payload)
num_rows = fields.get(TABLE_ROWS_FIELD, [None])[0]
num_cols = fields.get(TABLE_COLS_FIELD, [None])[0]
store_raw = fields.get(TABLE_DATA_STORE_FIELD, [None])[0]
if not isinstance(num_rows, int) or not isinstance(num_cols, int):
return None
if not num_rows or not num_cols or not isinstance(store_raw, bytes):
return None
header_rows = fields.get(TABLE_HEADER_ROWS_FIELD, [0])[0]
if not isinstance(header_rows, int):
header_rows = 0
store = safe_fields(store_raw)
values = iwa_cell_values(store, objects)
cells: list[TableCell] = []
for placed in iwa_placements(store, objects):
if placed.row >= num_rows or placed.col >= num_cols:
continue
text = iwa_cell_text(placed.storage, placed.start, values)
if text is None:
continue
cells.append(
TableCell(
text=text,
start_row_offset_idx=placed.row,
end_row_offset_idx=placed.row + 1,
start_col_offset_idx=placed.col,
end_col_offset_idx=placed.col + 1,
column_header=placed.row < header_rows,
)
)
if not cells:
return None
return TableData(num_rows=num_rows, num_cols=num_cols, table_cells=cells)
def iwa_cell_values(
store: dict[int, list[int | bytes]], objects: dict[int, IWAObject]
) -> CellValues:
"""Read a table's shared value lists, keyed as its cells reference them.
Args:
store: Decoded fields of the table's data store.
objects: Every object in the document, keyed by identifier.
Returns:
The plain strings and the rich text, each keyed by cell reference.
"""
return CellValues(
strings=iwa_value_list(store, STORE_STRINGS_FIELD, objects, iwa_entry_string),
rich_text=iwa_value_list(
store,
STORE_RICH_TEXT_FIELD,
objects,
iwa_entry_rich_text,
),
)
def iwa_value_list(
store: dict[int, list[int | bytes]],
field: int,
objects: dict[int, IWAObject],
decode: Callable[[dict[int, list[int | bytes]], dict[int, IWAObject]], _T | None],
) -> dict[int, _T]:
"""Read one ``TST.TableDataList``, following any segments it spills into."""
reference = store.get(field, [None])[0]
target = read_reference(reference) if isinstance(reference, bytes) else None
data_list = objects.get(target) if target is not None else None
if data_list is None or data_list.message_type != TST_DATA_LIST:
return {}
payloads = [data_list.payload]
for segment in iwa_reference_list(data_list.payload, LIST_SEGMENTS_FIELD):
spilled = objects.get(segment)
if spilled is not None:
payloads.append(spilled.payload)
values: dict[int, _T] = {}
for payload in payloads:
for entry in safe_fields(payload).get(LIST_ENTRIES_FIELD, []):
if not isinstance(entry, bytes):
continue
parsed = safe_fields(entry)
key = parsed.get(ENTRY_KEY_FIELD, [None])[0]
value = decode(parsed, objects)
if isinstance(key, int) and value is not None:
values[key] = value
return values
def iwa_entry_string(
entry: dict[int, list[int | bytes]], objects: dict[int, IWAObject]
) -> str | None:
"""Read a value list entry that holds its string directly."""
value = next(
(v for v in entry.get(ENTRY_STRING_FIELD, []) if isinstance(v, bytes)), None
)
return None if value is None else value.decode("utf-8", errors="replace")
def iwa_entry_rich_text(
entry: dict[int, list[int | bytes]], objects: dict[int, IWAObject]
) -> str | None:
"""Read a value list entry that points at a whole text storage."""
reference = entry.get(ENTRY_RICH_TEXT_FIELD, [None])[0]
target = read_reference(reference) if isinstance(reference, bytes) else None
indirection = objects.get(target) if target is not None else None
if indirection is None or indirection.message_type != TST_TEXT_REF:
return None
storage_id = iwa_reference_field(indirection.payload, 1)
storage = objects.get(storage_id) if storage_id is not None else None
if storage is None or storage.message_type != TSWP_STORAGE_ARCHIVE:
return None
return iwa_storage_text(safe_fields(storage.payload)).strip() or None
def iwa_placements(
store: dict[int, list[int | bytes]], objects: dict[int, IWAObject]
) -> Iterator[Placement]:
"""Walk a table's tiles, yielding where each stored cell sits.
A table taller than one tile is split across several, and each tile numbers
its rows from its own start rather than from the table's, so the entry's
first row is added back here.
Args:
store: The table's decoded data store.
objects: Every object in the document, keyed by identifier.
Yields:
One placement per stored cell, in tile order.
"""
container = store.get(STORE_TILES_FIELD, [None])[0]
if not isinstance(container, bytes):
return
for entry in safe_fields(container).get(TILE_LIST_FIELD, []):
if not isinstance(entry, bytes):
continue
parsed = safe_fields(entry)
first_row = parsed.get(TILE_ENTRY_ROW_FIELD, [0])[0]
reference = parsed.get(TILE_ENTRY_TILE_FIELD, [None])[0]
target = read_reference(reference) if isinstance(reference, bytes) else None
tile = objects.get(target) if target is not None else None
if tile is None or tile.message_type != TST_TILE:
continue
yield from iwa_tile_placements(
tile, first_row if isinstance(first_row, int) else 0
)
def iwa_tile_placements(tile: IWAObject, first_row: int) -> Iterator[Placement]:
"""Walk one tile's rows, placing each cell by its per-column offset."""
for row_message in safe_fields(tile.payload).get(TILE_ROWS_FIELD, []):
if not isinstance(row_message, bytes):
continue
row = safe_fields(row_message)
row_index = row.get(ROW_INDEX_FIELD, [None])[0]
if not isinstance(row_index, int):
continue
storage = row.get(ROW_WIDE_STORAGE_FIELD, [None])[0]
offsets = row.get(ROW_WIDE_OFFSETS_FIELD, [None])[0]
scale = 4 if row.get(ROW_WIDE_OFFSETS_FLAG, [0])[0] else 1
if not isinstance(storage, bytes) or not isinstance(offsets, bytes):
storage = row.get(ROW_STORAGE_FIELD, [None])[0]
offsets = row.get(ROW_OFFSETS_FIELD, [None])[0]
scale = 1
if not isinstance(storage, bytes) or not isinstance(offsets, bytes):
continue
for column in range(len(offsets) // 2):
start = int.from_bytes(
offsets[column * 2 : column * 2 + 2], "little", signed=True
)
if start < 0:
continue
yield Placement(first_row + row_index, column, storage, start * scale)
def read_objects(
archive: zipfile.ZipFile,
infos: list[zipfile.ZipInfo],
max_file_bytes: int,
kind: str,
) -> dict[int, IWAObject]:
"""Read every archived object of an iWork 2013+ container, by identifier.
Args:
archive: The open container, or the inner ``Index.zip`` of one.
infos: Its members.
max_file_bytes: The largest member this is willing to decompress.
kind: What the app calls its documents, for error messages.
Returns:
Every object the container's ``.iwa`` members hold.
Raises:
DocumentLoadError: If a member is larger than ``max_file_bytes``.
"""
objects: dict[int, IWAObject] = {}
for info in infos:
if not info.filename.endswith(".iwa"):
continue
if info.file_size > max_file_bytes:
raise DocumentLoadError(
f"{kind} archive member {info.filename} is {info.file_size} "
f"bytes, exceeding the max_file_bytes limit of "
f"{max_file_bytes}."
)
for obj in iter_objects(archive.read(info)):
objects[obj.identifier] = obj
return objects
def drawable_geometry(payload: bytes) -> Geometry | None:
"""Read where a drawable sits, wherever its geometry turns out to be.
Args:
payload: The encoded drawable, of whichever archive type placed it.
Returns:
The geometry, or None when the descent runs out of supers without
finding one — which is what a drawable that is not positioned, or one
whose archive this does not understand, looks like.
"""
for _ in range(MAX_DRAWABLE_DEPTH):
fields = safe_fields(payload)
position = read_point(fields.get(GEOMETRY_POSITION_FIELD, [None])[0])
size = read_point(fields.get(GEOMETRY_SIZE_FIELD, [None])[0])
if position is not None and size is not None:
return Geometry(position[0], position[1], size[0], size[1])
nested = fields.get(1, [None])[0]
if not isinstance(nested, bytes):
return None
payload = nested
return None
def read_point(raw: int | bytes | None) -> tuple[float, float] | None:
"""Read a ``TSP.Point``, or None when the value is not one.
The check is deliberately strict — two fields, both 32-bit — because the
descent of :func:`drawable_geometry` has no schema to tell it when it has
arrived, and a message that merely starts with two length-delimited fields
must not be mistaken for a position.
Args:
raw: A decoded field value.
Returns:
The point's two coordinates, or None.
"""
if not isinstance(raw, bytes):
return None
fields = safe_fields(raw)
if set(fields) != {POINT_X_FIELD, POINT_Y_FIELD}:
return None
x, y = fields[POINT_X_FIELD][0], fields[POINT_Y_FIELD][0]
if not isinstance(x, bytes) or not isinstance(y, bytes):
return None
if len(x) != 4 or len(y) != 4:
return None
return struct.unpack("<f", x)[0], struct.unpack("<f", y)[0]
def safe_fields(payload: bytes) -> dict[int, list[int | bytes]]:
"""Decode a message, treating an unreadable one as empty.
The table archives carry sub-messages this reader has no need to understand,
some of which use wire types the fields it does want never use. Failing the
whole document over one of them would be wrong.
"""
try:
return read_fields(payload)
except DocumentLoadError:
return {}
class IWAReader:
"""Reads content out of the object graph of an iWork 2013+ document.
Drawables are reached twice over — once from the attachment table of the
text they are anchored in, and once from whatever list the app itself keeps
of them — so every drawable this has already emitted is remembered. That
also bounds the walk: an object graph may contain cycles.
Args:
archive: The container the images are members of, which is the outer
one when the app nested its index in an ``Index.zip``.
objects: Every object in the document, keyed by identifier.
data_prefix: What the container puts in front of its ``Data/`` members,
empty unless the package was flattened into a subdirectory.
"""
def __init__(
self,
archive: zipfile.ZipFile,
objects: dict[int, IWAObject],
data_prefix: str = "",
) -> None:
self._archive = archive
self._objects = objects
self._data_prefix = data_prefix
self._data_files = iwa_data_files(objects)
self._data: dict[int, Picture] = {}
self._emitted: set[int] = set()
def storage_blocks(self, storage: IWAObject) -> list[Block]:
"""Read one ``TSWP.StorageArchive`` as paragraphs and anchored drawables.
Apple marks the anchor of a drawable with U+FFFC inside the text, and the
storage's attachment table says which drawable each one is. The drawable
is emitted straight after the paragraph it is anchored in, which is where
it belongs in the reading order.
Args:
storage: The storage to read.
Returns:
The storage's blocks, in document order.
"""
fields = read_fields(storage.payload)
text = iwa_storage_text(fields)
runs = iwa_storage_runs(fields, self._objects)
attachments = iwa_attachment_runs(fields)
comments = iwa_attachment_runs(fields, STORAGE_COMMENT_FIELD)
blocks: list[Block] = []
offset = 0
for line in text.split("\n"):
end = offset + len(line) + 1
pieces = runs_for(line, offset, runs)
if pieces:
label, level = label_for_style(value_at(runs.styles, offset))
anchors = tuple(
str(field) for index, field in comments if offset <= index < end
)
blocks.append(
Paragraph(
pieces, label, level, list_label_at(runs, offset), anchors
)
)
for index, identifier in attachments:
if offset <= index < end:
blocks.extend(self._drawable_blocks(identifier))
offset = end # the + 1 above covers the newline that split consumed
return blocks
def comments(self, storage: IWAObject) -> list[Comment]:
"""Read the comments attached to the text of one storage.
Pages 5 records a comment as a highlight over the words being commented
on rather than as a character in the text, so the run table gives the
stretch it covers. Each entry names a comment field, which holds the
comment; replies are comments in their own right and are followed as a
chain.
Args:
storage: The storage whose comment table to read.
Returns:
One comment per thread entry, anchored by the field's identifier.
"""
fields = read_fields(storage.payload)
comments: list[Comment] = []
for _, identifier in iwa_attachment_runs(fields, STORAGE_COMMENT_FIELD):
field = self._objects.get(identifier)
if field is None or field.message_type != TSWP_COMMENT_FIELD:
continue
head = iwa_reference_field(field.payload, COMMENT_FIELD_STORAGE_FIELD)
comments.extend(
Comment(text, str(identifier)) for text in self._thread(head)
)
return comments
def _thread(self, identifier: int | None) -> list[str]:
"""Read one comment and its replies, as text prefixed by their authors."""
texts: list[str] = []
pending = [identifier]
seen: set[int] = set()
while pending:
current = pending.pop(0)
if current is None or current in seen:
continue
seen.add(current)
comment = self._objects.get(current)
if comment is None or comment.message_type != TSD_COMMENT_STORAGE:
continue
fields = safe_fields(comment.payload)
raw = fields.get(COMMENT_TEXT_FIELD, [None])[0]
if isinstance(raw, bytes):
text = raw.decode("utf-8", errors="replace").strip()
if text:
texts.append(authored(self._author(comment.payload), text))
pending.extend(iwa_reference_list(comment.payload, COMMENT_REPLIES_FIELD))
return texts
def _author(self, payload: bytes) -> str | None:
"""Read the name of whoever wrote a comment."""
identifier = iwa_reference_field(payload, COMMENT_AUTHOR_FIELD)
author = self._objects.get(identifier) if identifier is not None else None
if author is None or author.message_type != TSK_ANNOTATION_AUTHOR:
return None
name = safe_fields(author.payload).get(AUTHOR_NAME_FIELD, [None])[0]
if not isinstance(name, bytes):
return None
return name.decode("utf-8", errors="replace").strip() or None
def _storage_paragraphs(self, identifier: int | None) -> list[Paragraph]:
"""Read one storage's paragraphs, ignoring anything anchored in it."""
storage = self._objects.get(identifier) if identifier is not None else None
if storage is None or storage.message_type != TSWP_STORAGE_ARCHIVE:
return []
fields = read_fields(storage.payload)
return split_paragraphs(
iwa_storage_text(fields), iwa_storage_runs(fields, self._objects)
)
def pictures(self, identifier: int) -> list[Picture]:
"""Read the pictures one drawable places, looking inside a group.
This is for an app that reads its other drawables itself. Anything else
the drawable holds, such as the text of a shape grouped with a picture,
is left out.
Args:
identifier: The drawable to read.
Returns:
The pictures, in the order the group lists them.
"""
return [
block
for block in self._drawable_blocks(identifier)
if isinstance(block, Picture)
]
def _drawable_blocks(self, identifier: int) -> list[Block]:
"""Read whichever kind of drawable ``identifier`` names."""
if identifier in self._emitted:
return []
self._emitted.add(identifier)
drawable = self._objects.get(identifier)
if drawable is None:
return []
if drawable.message_type == TSWP_DRAWABLE_ATTACHMENT:
anchored = iwa_reference_field(drawable.payload, ATTACHMENT_DRAWABLE_FIELD)
return self._drawable_blocks(anchored) if anchored is not None else []
if drawable.message_type == TSD_IMAGE:
return [self._picture(drawable)]
if drawable.message_type == TST_TABULAR_INFO:
model = iwa_reference_field(drawable.payload, TABULAR_INFO_MODEL_FIELD)
table = self._objects.get(model) if model is not None else None
if table is None or table.message_type != TST_TABLE_MODEL:
return []
data = iwa_table(table, self._objects)
return [data] if data is not None else []
if drawable.message_type == TSD_GROUP:
blocks: list[Block] = []
for child in iwa_reference_list(drawable.payload, GROUP_CHILDREN_FIELD):
blocks.extend(self._drawable_blocks(child))
return blocks
if drawable.message_type == TSWP_SHAPE_INFO:
blocks = []
for storage_id in sorted(iwa_referenced_ids(drawable.payload)):
storage = self._objects.get(storage_id)
if storage is not None and storage.message_type == TSWP_STORAGE_ARCHIVE:
blocks.extend(self.storage_blocks(storage))
return blocks
return []
def data_picture(self, identifier: int) -> Picture:
"""Read the picture one data file holds, for an archive that names it.
A table cell filled with an image names the data itself rather than
through a ``TSD.ImageArchive``. Many cells can name the same data, so
each data file is read only once.
Args:
identifier: The data identifier.
Returns:
The picture. Its data is None when the container does not hold the
file.
"""
if identifier not in self._data:
self._data[identifier] = self._read_data(identifier)
return self._data[identifier]
def _read_data(self, identifier: int) -> Picture:
"""Read the container member that one data identifier names."""
member = self._data_files.get(identifier)
if member is None:
return Picture(None, "")
# Apple writes member names in UTF-8 but does not set the flag that says
# so, and zipfile then reads a name as code page 437, as the ZIP format
# says. A name that is not ASCII is found under that reading.
for name in (member, member.encode("utf-8").decode("cp437")):
try:
return Picture(self._archive.read(self._data_prefix + name), member)
except KeyError:
continue
_log.debug("iWork image data member %s is missing", member)
return Picture(None, member)
def _picture(self, image: IWAObject) -> Picture:
"""Read a ``TSD.ImageArchive`` and the container member holding its bytes."""
fields = safe_fields(image.payload)
named = ""
for field in IMAGE_DATA_FIELDS:
reference = fields.get(field, [None])[0]
if not isinstance(reference, bytes):
continue
data_id = read_reference(reference)
if data_id is None:
continue
picture = self.data_picture(data_id)
if picture.data is not None:
return picture
# Pages names every rendition it knows of, including ones it did
# not write into this container, so keep trying the rest.
named = named or picture.name
return Picture(None, named)
def iwa_reference_field(payload: bytes, field: int) -> int | None:
"""Read the object identifier a message's reference field points at."""
reference = safe_fields(payload).get(field, [None])[0]
if not isinstance(reference, bytes):
return None
return read_reference(reference)
def iwa_reference_list(payload: bytes, field: int) -> list[int]:
"""Read the object identifiers a message's repeated reference field holds."""
identifiers = []
for reference in safe_fields(payload).get(field, []):
if not isinstance(reference, bytes):
continue
target = read_reference(reference)
if target is not None:
identifiers.append(target)
return identifiers
def iwa_attachment_runs(
fields: dict[int, list[int | bytes]],
field: int = STORAGE_ATTACHMENT_FIELD,
) -> list[tuple[int, int]]:
"""Resolve an anchoring run table to (character index, object id) pairs.
Unlike the style tables, an entry here anchors an object at one character
rather than putting a value in force from it, so entries without a reference
carry nothing and are dropped. Attachments, footnotes and page masters all
use this shape.
Args:
fields: Decoded fields of the storage.
field: The storage field holding the table.
Returns:
Character index and object identifier pairs, in document order.
"""
table = fields.get(field, [])
if not table or not isinstance(table[0], bytes):
return []
runs: list[tuple[int, int]] = []
for entry in safe_fields(table[0]).get(1, []):
if not isinstance(entry, bytes):
continue
parsed = safe_fields(entry)
index = parsed.get(1, [None])[0]
reference = parsed.get(2, [None])[0]
if not isinstance(index, int) or not isinstance(reference, bytes):
continue
target = read_reference(reference)
if target is not None:
runs.append((index, target))
runs.sort(key=lambda run: run[0])
return runs
def iwa_data_files(objects: dict[int, IWAObject]) -> dict[int, str]:
"""Map each data identifier to the container member that holds its bytes.
Args:
objects: Every object in the document, keyed by identifier.
Returns:
Data identifiers and the ``Data/`` member names they name.
"""
metadata = next(
(o for o in objects.values() if o.message_type == TSP_PACKAGE_METADATA), None
)
if metadata is None:
return {}
files: dict[int, str] = {}
for entry in safe_fields(metadata.payload).get(PACKAGE_DATAS_FIELD, []):
if not isinstance(entry, bytes):
continue
info = safe_fields(entry)
identifier = info.get(DATA_INFO_IDENTIFIER_FIELD, [None])[0]
name = info.get(DATA_INFO_NAME_FIELD, [None])[0]
if not isinstance(name, bytes):
name = info.get(DATA_INFO_PREFERRED_NAME_FIELD, [None])[0]
if isinstance(identifier, int) and isinstance(name, bytes):
files[identifier] = DATA_MEMBER_PREFIX + name.decode(
"utf-8", errors="replace"
)
return files
def iwa_referenced_ids(payload: bytes, depth: int = 0) -> set[int]:
"""Collect the object identifiers a message references, at any nesting.
Args:
payload: The encoded message to scan.
depth: Current recursion depth, bounded to keep a hostile document from
driving this arbitrarily deep.
Returns:
Every identifier reachable from the message.
"""
if depth > MAX_REFERENCE_DEPTH:
return set()
found: set[int] = set()
for values in safe_fields(payload).values():
for value in values:
if not isinstance(value, bytes):
continue
if len(value) <= REFERENCE_MAX_BYTES:
try:
target = read_reference(value)
except DocumentLoadError:
continue
if isinstance(target, int):
found.add(target)
else:
found |= iwa_referenced_ids(value, depth + 1)
return found