dknowc-dsh 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/package.json +2 -2
- package/skills/dknowc-official-doc-writer/SKILL.md +17 -8
- package/skills/dknowc-official-doc-writer/reference/sample_search_result.md +48 -0
- package/skills/dknowc-official-doc-writer/reference/sample_trace_report.html +75 -0
- package/skills/dknowc-official-doc-writer/reference/search_intro.md +72 -8
- package/skills/dknowc-official-doc-writer/scripts/register.mjs +1 -1
- package/skills/dknowc-ppt-assistant/README.md +54 -0
- package/skills/dknowc-ppt-assistant/SKILL.md +151 -0
- package/skills/dknowc-ppt-assistant/THIRD_PARTY_NOTICES.md +36 -0
- package/skills/dknowc-ppt-assistant/official-docs/output/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/official-docs/search-results/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/projects/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/references/content-pack.md +55 -0
- package/skills/dknowc-ppt-assistant/references/material_usage.md +51 -0
- package/skills/dknowc-ppt-assistant/references/sample_effect.html +89 -0
- package/skills/dknowc-ppt-assistant/references/sample_search_result.md +48 -0
- package/skills/dknowc-ppt-assistant/references/search_intro.md +79 -0
- package/skills/dknowc-ppt-assistant/references/style-presets.md +126 -0
- package/skills/dknowc-ppt-assistant/references/svg-authoring.md +81 -0
- package/skills/dknowc-ppt-assistant/references/upstream-docs/svg-pipeline.md +842 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/01_cover.svg +28 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/02_quote.svg +38 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/03_origin.svg +96 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/README.md +15 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/design_spec.md +408 -0
- package/skills/dknowc-ppt-assistant/references/upstream-example/spec_lock.md +78 -0
- package/skills/dknowc-ppt-assistant/scripts/adapt_mcp_result.py +279 -0
- package/skills/dknowc-ppt-assistant/scripts/check_release.py +106 -0
- package/skills/dknowc-ppt-assistant/scripts/compact_svg_coordinates.py +345 -0
- package/skills/dknowc-ppt-assistant/scripts/config.py +707 -0
- package/skills/dknowc-ppt-assistant/scripts/console_encoding.py +33 -0
- package/skills/dknowc-ppt-assistant/scripts/error_helper.py +434 -0
- package/skills/dknowc-ppt-assistant/scripts/finalize_svg.py +507 -0
- package/skills/dknowc-ppt-assistant/scripts/hyperlink_contract.py +399 -0
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +182 -0
- package/skills/dknowc-ppt-assistant/scripts/language_tags.py +283 -0
- package/skills/dknowc-ppt-assistant/scripts/mcp_direct.py +158 -0
- package/skills/dknowc-ppt-assistant/scripts/native_payloads.py +655 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_animation_presets.json +3440 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_animations.py +3965 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_effects.py +143 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_opc_validation.py +346 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/__init__.py +130 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/LICENSE-APACHE-2.0.txt +202 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/LICENSE-OPEN-XML-SDK-MIT.txt +23 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/NOTICE.md +54 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/presetShapeDefinitions.xml +19898 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/presetShapeSemantics.json +1217 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/data/shape_type_values.txt +187 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/errors.py +39 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/formula.py +336 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/loader.py +364 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/models.py +201 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/registry.py +270 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/semantic_hash.py +456 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/semantics.py +543 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/xml_safety.py +21 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/__init__.py +21 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/animation_import.py +287 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/chart_to_svg.py +2377 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/chartex_to_svg.py +422 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/color_resolver.py +894 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/converter.py +1143 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/custgeom_to_svg.py +165 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/effect_to_svg.py +410 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/emu_units.py +258 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/fill_to_svg.py +816 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/formula_import.py +611 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/hyperlinks.py +93 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/import_diagnostics.py +37 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/ln_to_svg.py +362 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/normalized_chart_svg.py +1217 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/ooxml_loader.py +506 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/pic_to_svg.py +653 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_authoring.py +894 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_registry_to_svg.py +216 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_svg_markup.py +198 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/prstgeom_to_svg.py +162 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/shape_walker.py +556 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/slide_to_svg.py +2002 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/tbl_to_svg.py +1841 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/transition_import.py +201 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/txbody_to_svg.py +1433 -0
- package/skills/dknowc-ppt-assistant/scripts/pptx_transitions.py +3153 -0
- package/skills/dknowc-ppt-assistant/scripts/preset_shape_svg.py +450 -0
- package/skills/dknowc-ppt-assistant/scripts/project_utils.py +645 -0
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +183 -0
- package/skills/dknowc-ppt-assistant/scripts/register_template.py +1327 -0
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +1048 -0
- package/skills/dknowc-ppt-assistant/scripts/resource_paths.py +282 -0
- package/skills/dknowc-ppt-assistant/scripts/shape_boolean_svg.py +189 -0
- package/skills/dknowc-ppt-assistant/scripts/slide_roster.py +44 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/__init__.py +12 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/align_embed_images.py +672 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/crop_images.py +370 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/embed_icons.py +564 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/embed_images.py +288 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/fix_image_aspect.py +389 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/flatten_tspan.py +948 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/svg_rect_to_path.py +346 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/__init__.py +15 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/checker.py +7791 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/cli.py +193 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/svg_contracts.py +1121 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/xml_support.py +35 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_quality_checker.py +59 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/__init__.py +37 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/animation_config.py +1622 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/canvas_contract.py +226 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/__init__.py +1 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/context.py +291 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/converter.py +1980 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/elements.py +5272 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/hyperlinks.py +162 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/paths.py +847 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/styles.py +752 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/text_properties.py +796 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/theme_colors.py +283 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/theme_fonts.py +330 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/utils.py +3555 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/geometry_properties.py +140 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/__init__.py +356 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chart_data.py +1449 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chart_style.py +886 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chart_xml.py +1495 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/chartex.py +272 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/fallback_hash.py +161 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula.py +243 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_ast.py +592 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_compiler.py +96 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_omml.py +1049 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_parser.py +2181 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_profile.py +739 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_run_properties.py +234 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/inline_formula.py +311 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/marker_attributes.py +182 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/marker_common.py +806 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/marker_status.py +190 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/table.py +1113 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/workbook.py +216 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/__init__.py +1 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/builder.py +6273 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/cli.py +2059 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/dimensions.py +189 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/discovery.py +120 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/media.py +160 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/narration.py +696 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/notes.py +336 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/slide_xml.py +138 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/template_structure.py +3457 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/template_validation.py +1536 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/semantic_markers.py +240 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/shape_boolean.py +1355 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/text_outline.py +1121 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/tspan_flattener.py +64 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/use_expander.py +577 -0
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx.py +21 -0
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +336 -0
- package/skills/dknowc-ppt-assistant/scripts/update_spec.py +404 -0
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +142 -0
- package/skills/dknowc-ppt-assistant/workflows/routing.md +38 -0
- package/skills/dknowc-trusted-consulting/SKILL.md +21 -5
- package/skills/dknowc-trusted-consulting/reference/consult_intro.md +79 -0
- package/skills/dknowc-trusted-consulting/reference/sample_consult_answer.md +31 -0
- package/skills/dknowc-trusted-consulting/reference/sample_trace_report.html +76 -0
- package/skills/dknowc-trusted-consulting/scripts/register_key.mjs +1 -0
- package/skills/dknowc-trusted-search/SKILL.md +30 -2
- package/skills/dknowc-trusted-search/reference/sample_search_result.md +33 -0
- package/skills/dknowc-trusted-search/reference/sample_trace_report.html +450 -0
- package/skills/dknowc-trusted-search/reference/search_intro.md +79 -0
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +1 -0
- package/src/index.js +7 -2
|
@@ -0,0 +1,1433 @@
|
|
|
1
|
+
"""DrawingML <p:txBody> -> SVG <text> conversion.
|
|
2
|
+
|
|
3
|
+
Reverse of svg_to_pptx/drawingml/elements.py convert_text.
|
|
4
|
+
|
|
5
|
+
Strategy (v1):
|
|
6
|
+
- Each <a:p> paragraph emits one <text> element (one line of baseline).
|
|
7
|
+
Multiple <a:r> runs in one paragraph become <tspan>s sharing the text
|
|
8
|
+
element's x.
|
|
9
|
+
- Vertical layout: y of first paragraph is determined by anchor (t/ctr/b)
|
|
10
|
+
and tIns/bIns. Subsequent paragraphs stack downward with line height
|
|
11
|
+
derived from the largest font in the paragraph * 1.2 (default leading).
|
|
12
|
+
- Horizontal layout: text-anchor follows pPr@algn. x is computed from the
|
|
13
|
+
text frame plus lIns/rIns and the alignment.
|
|
14
|
+
- No automatic word wrap (PPT's wrap is layout-time; v1 trusts the existing
|
|
15
|
+
text frame width and emits text as-is). a:br produces an explicit linebreak.
|
|
16
|
+
- Bullet points (a:buChar / a:buAutoNum) are rendered as literal prefixes
|
|
17
|
+
so the visual lands without relying on PowerPoint list semantics.
|
|
18
|
+
|
|
19
|
+
Color / font / size attributes propagate from a:rPr; missing attributes fall
|
|
20
|
+
back to paragraph/list defaults, endParaRPr, or spec-default values.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
from dataclasses import dataclass, field
|
|
26
|
+
from typing import Callable
|
|
27
|
+
from xml.etree import ElementTree as ET
|
|
28
|
+
|
|
29
|
+
from svg_to_pptx.drawingml.utils import detect_text_lang, is_cjk_char
|
|
30
|
+
|
|
31
|
+
from .color_resolver import ColorPalette, find_color_elem, resolve_color
|
|
32
|
+
from .emu_units import (
|
|
33
|
+
NS, Xfrm, fmt_num, emu_to_px, format_ooxml_alpha,
|
|
34
|
+
hundredths_pt_to_px,
|
|
35
|
+
)
|
|
36
|
+
from .fill_to_svg import resolve_fill
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# ---------------------------------------------------------------------------
|
|
40
|
+
# Defaults (matches DrawingML spec)
|
|
41
|
+
# ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
# Default body insets when bodyPr omits them: 0.1 inch left/right, 0.05 top/bot.
|
|
44
|
+
DEFAULT_INSETS_EMU = {"l": 91440, "t": 45720, "r": 91440, "b": 45720}
|
|
45
|
+
|
|
46
|
+
# Default font size = 1800 (= 18 pt = 24 px). Spec is actually 1800 (18pt).
|
|
47
|
+
DEFAULT_FONT_SIZE_PX = 24.0
|
|
48
|
+
DEFAULT_LINE_HEIGHT_RATIO = 1.2 # leading multiplier
|
|
49
|
+
DEFAULT_FILL_HEX = "#000000"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class TextRun:
|
|
54
|
+
"""A single run with resolved style + text."""
|
|
55
|
+
|
|
56
|
+
text: str
|
|
57
|
+
font_size_px: float
|
|
58
|
+
font_family: str # full font-family stack (latin, ea fallback joined)
|
|
59
|
+
fill: str
|
|
60
|
+
fill_opacity: float = 1.0
|
|
61
|
+
defs: list[str] = field(default_factory=list)
|
|
62
|
+
bold: bool = False
|
|
63
|
+
italic: bool = False
|
|
64
|
+
underline: bool = False
|
|
65
|
+
strikethrough: bool = False
|
|
66
|
+
letter_spacing_px: float = 0.0
|
|
67
|
+
is_break: bool = False # marks an a:br within a paragraph
|
|
68
|
+
hyperlink_href: str | None = None
|
|
69
|
+
formula_latex: str | None = None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
HyperlinkResolver = Callable[[str, str], str | None]
|
|
73
|
+
InlineFormulaResolver = Callable[[ET.Element], tuple[str | None, str]]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass
|
|
77
|
+
class TextParagraph:
|
|
78
|
+
"""One <a:p>: a list of runs sharing alignment + level."""
|
|
79
|
+
|
|
80
|
+
runs: list[TextRun] = field(default_factory=list)
|
|
81
|
+
align: str = "l" # l / ctr / r / just / dist
|
|
82
|
+
level: int = 0
|
|
83
|
+
indent_px: float = 0.0
|
|
84
|
+
margin_left_px: float = 0.0
|
|
85
|
+
line_height_ratio: float = DEFAULT_LINE_HEIGHT_RATIO
|
|
86
|
+
space_before_px: float = 0.0
|
|
87
|
+
space_after_px: float = 0.0
|
|
88
|
+
empty_line_font_size_px: float = DEFAULT_FONT_SIZE_PX
|
|
89
|
+
bullet_prefix: str = "" # rendered prefix like '• ' or '1. '
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass
|
|
93
|
+
class TextResult:
|
|
94
|
+
"""Resolved text body ready for SVG emission.
|
|
95
|
+
|
|
96
|
+
`svg` is one or more <text> elements, already absolutely positioned
|
|
97
|
+
inside the slide coordinate system. `defs` holds text gradient fills.
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
svg: str = ""
|
|
101
|
+
defs: list[str] = field(default_factory=list)
|
|
102
|
+
contains_inline_formula: bool = False
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
VERTICAL_TEXT_MODES = {"eaVert", "vert", "wordArtVert", "wordArtVertRtl"}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# ---------------------------------------------------------------------------
|
|
109
|
+
# Public API
|
|
110
|
+
# ---------------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
def convert_txbody(
|
|
113
|
+
tx_body: ET.Element | None,
|
|
114
|
+
xfrm: Xfrm,
|
|
115
|
+
palette: ColorPalette | None,
|
|
116
|
+
*,
|
|
117
|
+
theme_fonts: dict[str, str] | None = None,
|
|
118
|
+
slide_number: int | None = None,
|
|
119
|
+
default_fill: str = DEFAULT_FILL_HEX,
|
|
120
|
+
default_font_size_px: float = DEFAULT_FONT_SIZE_PX,
|
|
121
|
+
fallback_lst_styles: tuple[ET.Element, ...] = (),
|
|
122
|
+
fallback_run_props: tuple[ET.Element, ...] = (),
|
|
123
|
+
id_prefix: str = "txt",
|
|
124
|
+
id_seq: list[int] | None = None,
|
|
125
|
+
hyperlink_resolver: HyperlinkResolver | None = None,
|
|
126
|
+
inline_formula_resolver: InlineFormulaResolver | None = None,
|
|
127
|
+
) -> TextResult:
|
|
128
|
+
"""Convert <p:txBody> under the given shape geometry to SVG <text>(s)."""
|
|
129
|
+
if tx_body is None:
|
|
130
|
+
return TextResult()
|
|
131
|
+
|
|
132
|
+
body_pr = tx_body.find("a:bodyPr", NS)
|
|
133
|
+
paragraphs = _parse_paragraphs(
|
|
134
|
+
tx_body, palette, theme_fonts or {}, default_fill=default_fill,
|
|
135
|
+
default_font_size_px=default_font_size_px,
|
|
136
|
+
fallback_lst_styles=fallback_lst_styles,
|
|
137
|
+
fallback_run_props=fallback_run_props,
|
|
138
|
+
slide_number=slide_number, id_prefix=id_prefix, id_seq=id_seq,
|
|
139
|
+
hyperlink_resolver=hyperlink_resolver,
|
|
140
|
+
inline_formula_resolver=inline_formula_resolver,
|
|
141
|
+
)
|
|
142
|
+
if not paragraphs or not _has_visible_text(paragraphs):
|
|
143
|
+
return TextResult()
|
|
144
|
+
|
|
145
|
+
# Insets + anchor + wrap
|
|
146
|
+
lins = _read_emu_attr(body_pr, "lIns", DEFAULT_INSETS_EMU["l"])
|
|
147
|
+
tins = _read_emu_attr(body_pr, "tIns", DEFAULT_INSETS_EMU["t"])
|
|
148
|
+
rins = _read_emu_attr(body_pr, "rIns", DEFAULT_INSETS_EMU["r"])
|
|
149
|
+
bins = _read_emu_attr(body_pr, "bIns", DEFAULT_INSETS_EMU["b"])
|
|
150
|
+
anchor = body_pr.attrib.get("anchor", "t") if body_pr is not None else "t"
|
|
151
|
+
wrap_mode = body_pr.attrib.get("wrap", "square") if body_pr is not None else "square"
|
|
152
|
+
respect_edge_spacing = (
|
|
153
|
+
body_pr is not None
|
|
154
|
+
and body_pr.attrib.get("spcFirstLastPara") in {"1", "true"}
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
inner_x = xfrm.x + lins
|
|
158
|
+
inner_y = xfrm.y + tins
|
|
159
|
+
inner_w = max(xfrm.w - lins - rins, 1.0)
|
|
160
|
+
inner_h = max(xfrm.h - tins - bins, 1.0)
|
|
161
|
+
|
|
162
|
+
# Pre-wrap each paragraph into concrete display lines.
|
|
163
|
+
wrap_width = inner_w if wrap_mode == "square" else float("inf")
|
|
164
|
+
para_lines: list[list[list[TextRun]]] = [
|
|
165
|
+
_wrap_paragraph_into_lines(p, wrap_width) for p in paragraphs
|
|
166
|
+
]
|
|
167
|
+
|
|
168
|
+
# Pre-compute heights to support anchor=ctr / b
|
|
169
|
+
para_heights = [
|
|
170
|
+
_paragraph_height_from_lines(p, lines)
|
|
171
|
+
for p, lines in zip(paragraphs, para_lines)
|
|
172
|
+
]
|
|
173
|
+
space_before = [paragraph.space_before_px for paragraph in paragraphs]
|
|
174
|
+
space_after = [paragraph.space_after_px for paragraph in paragraphs]
|
|
175
|
+
if not respect_edge_spacing:
|
|
176
|
+
space_before[0] = 0.0
|
|
177
|
+
space_after[-1] = 0.0
|
|
178
|
+
total_h = sum(
|
|
179
|
+
before + height + after
|
|
180
|
+
for before, height, after in zip(
|
|
181
|
+
space_before,
|
|
182
|
+
para_heights,
|
|
183
|
+
space_after,
|
|
184
|
+
)
|
|
185
|
+
)
|
|
186
|
+
if anchor == "ctr":
|
|
187
|
+
cursor_y = inner_y + max(0.0, (inner_h - total_h) / 2.0)
|
|
188
|
+
elif anchor == "b":
|
|
189
|
+
cursor_y = inner_y + max(0.0, inner_h - total_h)
|
|
190
|
+
else:
|
|
191
|
+
cursor_y = inner_y
|
|
192
|
+
|
|
193
|
+
bottom_y = inner_y + inner_h
|
|
194
|
+
text_blocks: list[str] = []
|
|
195
|
+
for para, lines, height, before, after in zip(
|
|
196
|
+
paragraphs,
|
|
197
|
+
para_lines,
|
|
198
|
+
para_heights,
|
|
199
|
+
space_before,
|
|
200
|
+
space_after,
|
|
201
|
+
):
|
|
202
|
+
cursor_y += before
|
|
203
|
+
visible_lines = _clip_lines_to_bottom(para, lines, cursor_y, bottom_y)
|
|
204
|
+
if visible_lines:
|
|
205
|
+
text_blocks.append(
|
|
206
|
+
_emit_paragraph(para, visible_lines, inner_x, inner_w, cursor_y)
|
|
207
|
+
)
|
|
208
|
+
cursor_y += height + after
|
|
209
|
+
if cursor_y >= bottom_y:
|
|
210
|
+
break
|
|
211
|
+
|
|
212
|
+
svg = "\n".join(text_blocks)
|
|
213
|
+
return TextResult(
|
|
214
|
+
svg=svg,
|
|
215
|
+
defs=_collect_text_defs(paragraphs),
|
|
216
|
+
contains_inline_formula="data-pptx-inline-formula=" in svg,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def is_vertical_txbody(tx_body: ET.Element | None, xfrm: Xfrm | None = None) -> bool:
|
|
221
|
+
if tx_body is None:
|
|
222
|
+
return False
|
|
223
|
+
body_pr = tx_body.find("a:bodyPr", NS)
|
|
224
|
+
if body_pr is None:
|
|
225
|
+
return False
|
|
226
|
+
if body_pr.attrib.get("vert") in VERTICAL_TEXT_MODES:
|
|
227
|
+
return True
|
|
228
|
+
return _looks_like_auto_stacked_cjk(tx_body, body_pr, xfrm)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def convert_vertical_txbody(
|
|
232
|
+
tx_body: ET.Element | None,
|
|
233
|
+
xfrm: Xfrm,
|
|
234
|
+
palette: ColorPalette | None,
|
|
235
|
+
*,
|
|
236
|
+
theme_fonts: dict[str, str] | None = None,
|
|
237
|
+
slide_number: int | None = None,
|
|
238
|
+
default_fill: str = DEFAULT_FILL_HEX,
|
|
239
|
+
default_font_size_px: float = DEFAULT_FONT_SIZE_PX,
|
|
240
|
+
fallback_lst_styles: tuple[ET.Element, ...] = (),
|
|
241
|
+
fallback_run_props: tuple[ET.Element, ...] = (),
|
|
242
|
+
id_prefix: str = "txt",
|
|
243
|
+
id_seq: list[int] | None = None,
|
|
244
|
+
hyperlink_resolver: HyperlinkResolver | None = None,
|
|
245
|
+
inline_formula_resolver: InlineFormulaResolver | None = None,
|
|
246
|
+
) -> TextResult:
|
|
247
|
+
"""Render East Asian vertical text as upright stacked glyphs.
|
|
248
|
+
|
|
249
|
+
PowerPoint often combines ``bodyPr@vert=eaVert`` with a rotated text box.
|
|
250
|
+
Rendering the text inside the rotated shape group makes Chinese glyphs lie
|
|
251
|
+
sideways. This helper computes the final rotated box and places glyphs
|
|
252
|
+
upright in slide coordinates.
|
|
253
|
+
"""
|
|
254
|
+
if tx_body is None:
|
|
255
|
+
return TextResult()
|
|
256
|
+
|
|
257
|
+
paragraphs = _parse_paragraphs(
|
|
258
|
+
tx_body, palette, theme_fonts or {}, default_fill=default_fill,
|
|
259
|
+
default_font_size_px=default_font_size_px,
|
|
260
|
+
fallback_lst_styles=fallback_lst_styles,
|
|
261
|
+
fallback_run_props=fallback_run_props,
|
|
262
|
+
slide_number=slide_number, id_prefix=id_prefix, id_seq=id_seq,
|
|
263
|
+
hyperlink_resolver=hyperlink_resolver,
|
|
264
|
+
inline_formula_resolver=inline_formula_resolver,
|
|
265
|
+
)
|
|
266
|
+
runs = [
|
|
267
|
+
run
|
|
268
|
+
for para in paragraphs
|
|
269
|
+
for run in para.runs
|
|
270
|
+
if not run.is_break and run.text
|
|
271
|
+
]
|
|
272
|
+
if not runs:
|
|
273
|
+
return TextResult()
|
|
274
|
+
|
|
275
|
+
box_x, box_y, box_w, box_h = _rotated_bbox(xfrm)
|
|
276
|
+
center_x = box_x + box_w / 2.0
|
|
277
|
+
|
|
278
|
+
glyphs: list[tuple[str, TextRun]] = []
|
|
279
|
+
for run in runs:
|
|
280
|
+
for char in run.text:
|
|
281
|
+
glyphs.append((" " if char in "\t\r\n" else char, run))
|
|
282
|
+
|
|
283
|
+
if not glyphs:
|
|
284
|
+
return TextResult()
|
|
285
|
+
|
|
286
|
+
advances = [glyph_run.font_size_px * 1.05 for _, glyph_run in glyphs]
|
|
287
|
+
total_h = sum(advances)
|
|
288
|
+
top_y = box_y + max(0.0, (box_h - total_h) / 2.0)
|
|
289
|
+
|
|
290
|
+
bottom_y = box_y + box_h
|
|
291
|
+
spans: list[str] = []
|
|
292
|
+
cursor_y = top_y
|
|
293
|
+
first_run: TextRun | None = None
|
|
294
|
+
first_baseline: float | None = None
|
|
295
|
+
previous_baseline: float | None = None
|
|
296
|
+
for (char, run), advance in zip(glyphs, advances):
|
|
297
|
+
if cursor_y + advance > bottom_y:
|
|
298
|
+
break
|
|
299
|
+
baseline_y = cursor_y + run.font_size_px * 0.85
|
|
300
|
+
tspan_attrs = _run_tspan_attrs(run)
|
|
301
|
+
if first_run is None:
|
|
302
|
+
first_run = run
|
|
303
|
+
first_baseline = baseline_y
|
|
304
|
+
run_span = f"<tspan{tspan_attrs}>{_xml_escape(char)}</tspan>"
|
|
305
|
+
spans.append(_wrap_run_hyperlink(run_span, run))
|
|
306
|
+
else:
|
|
307
|
+
dy = baseline_y - (previous_baseline or baseline_y)
|
|
308
|
+
run_span = f"<tspan{tspan_attrs}>{_xml_escape(char)}</tspan>"
|
|
309
|
+
linked_span = _wrap_run_hyperlink(run_span, run)
|
|
310
|
+
spans.append(
|
|
311
|
+
f'<tspan x="{fmt_num(center_x)}" dy="{fmt_num(dy)}">'
|
|
312
|
+
f"{linked_span}</tspan>"
|
|
313
|
+
)
|
|
314
|
+
previous_baseline = baseline_y
|
|
315
|
+
cursor_y += advance
|
|
316
|
+
|
|
317
|
+
if first_run is None or first_baseline is None:
|
|
318
|
+
return TextResult()
|
|
319
|
+
|
|
320
|
+
attrs = _text_base_attrs(first_run, center_x, first_baseline, "middle")
|
|
321
|
+
return TextResult(
|
|
322
|
+
svg=f"<text{attrs}>{''.join(spans)}</text>",
|
|
323
|
+
defs=_collect_text_defs(paragraphs),
|
|
324
|
+
contains_inline_formula=False,
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _rotated_bbox(xfrm: Xfrm) -> tuple[float, float, float, float]:
|
|
329
|
+
rot = round(xfrm.rot) % 360
|
|
330
|
+
cx = xfrm.x + xfrm.w / 2.0
|
|
331
|
+
cy = xfrm.y + xfrm.h / 2.0
|
|
332
|
+
if rot in (90, 270):
|
|
333
|
+
return cx - xfrm.h / 2.0, cy - xfrm.w / 2.0, xfrm.h, xfrm.w
|
|
334
|
+
return xfrm.x, xfrm.y, xfrm.w, xfrm.h
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def _looks_like_auto_stacked_cjk(
|
|
338
|
+
tx_body: ET.Element,
|
|
339
|
+
body_pr: ET.Element,
|
|
340
|
+
xfrm: Xfrm | None,
|
|
341
|
+
) -> bool:
|
|
342
|
+
"""Detect PowerPoint's narrow-box CJK vertical layout without vert=eaVert."""
|
|
343
|
+
if xfrm is None or xfrm.w <= 0 or xfrm.h <= 0:
|
|
344
|
+
return False
|
|
345
|
+
if body_pr.attrib.get("wrap", "square") != "square":
|
|
346
|
+
return False
|
|
347
|
+
if xfrm.w > 64 or xfrm.h < xfrm.w * 2.4:
|
|
348
|
+
return False
|
|
349
|
+
|
|
350
|
+
text = _plain_text(tx_body)
|
|
351
|
+
chars = [ch for ch in text if not ch.isspace()]
|
|
352
|
+
if len(chars) < 3 or len(chars) > 16:
|
|
353
|
+
return False
|
|
354
|
+
cjk_count = sum(1 for ch in chars if _is_cjk(ch))
|
|
355
|
+
if cjk_count / len(chars) < 0.8:
|
|
356
|
+
return False
|
|
357
|
+
|
|
358
|
+
lins = _read_emu_attr(body_pr, "lIns", DEFAULT_INSETS_EMU["l"])
|
|
359
|
+
rins = _read_emu_attr(body_pr, "rIns", DEFAULT_INSETS_EMU["r"])
|
|
360
|
+
inner_w = max(xfrm.w - lins - rins, 1.0)
|
|
361
|
+
return inner_w <= DEFAULT_FONT_SIZE_PX
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def _plain_text(tx_body: ET.Element) -> str:
|
|
365
|
+
"""Return concatenated literal text for layout heuristics."""
|
|
366
|
+
parts: list[str] = []
|
|
367
|
+
for text_elem in tx_body.findall(".//a:t", NS):
|
|
368
|
+
if text_elem.text:
|
|
369
|
+
parts.append(text_elem.text)
|
|
370
|
+
return "".join(parts)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
# ---------------------------------------------------------------------------
|
|
374
|
+
# Parsing helpers
|
|
375
|
+
# ---------------------------------------------------------------------------
|
|
376
|
+
|
|
377
|
+
def _read_emu_attr(elem: ET.Element | None, attr: str, default_emu: int) -> float:
|
|
378
|
+
"""Read an EMU integer attribute and return px."""
|
|
379
|
+
if elem is None:
|
|
380
|
+
return emu_to_px(default_emu)
|
|
381
|
+
val = elem.attrib.get(attr)
|
|
382
|
+
if val is None:
|
|
383
|
+
return emu_to_px(default_emu)
|
|
384
|
+
try:
|
|
385
|
+
return emu_to_px(int(val))
|
|
386
|
+
except ValueError:
|
|
387
|
+
return emu_to_px(default_emu)
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def _parse_paragraphs(
|
|
391
|
+
tx_body: ET.Element,
|
|
392
|
+
palette: ColorPalette | None,
|
|
393
|
+
theme_fonts: dict[str, str],
|
|
394
|
+
*,
|
|
395
|
+
default_fill: str = DEFAULT_FILL_HEX,
|
|
396
|
+
default_font_size_px: float = DEFAULT_FONT_SIZE_PX,
|
|
397
|
+
fallback_lst_styles: tuple[ET.Element, ...] = (),
|
|
398
|
+
fallback_run_props: tuple[ET.Element, ...] = (),
|
|
399
|
+
slide_number: int | None = None,
|
|
400
|
+
id_prefix: str = "txt",
|
|
401
|
+
id_seq: list[int] | None = None,
|
|
402
|
+
hyperlink_resolver: HyperlinkResolver | None = None,
|
|
403
|
+
inline_formula_resolver: InlineFormulaResolver | None = None,
|
|
404
|
+
) -> list[TextParagraph]:
|
|
405
|
+
"""Walk <a:p> children producing TextParagraph objects."""
|
|
406
|
+
paragraphs: list[TextParagraph] = []
|
|
407
|
+
autonum_state: dict[int, int] = {}
|
|
408
|
+
lst_style = tx_body.find("a:lstStyle", NS)
|
|
409
|
+
lst_styles = (
|
|
410
|
+
(lst_style,) + fallback_lst_styles
|
|
411
|
+
if lst_style is not None else fallback_lst_styles
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
for p_elem in tx_body.findall("a:p", NS):
|
|
415
|
+
para = _parse_paragraph(
|
|
416
|
+
p_elem, palette, theme_fonts, autonum_state,
|
|
417
|
+
lst_styles=lst_styles,
|
|
418
|
+
fallback_run_props=fallback_run_props,
|
|
419
|
+
default_fill=default_fill,
|
|
420
|
+
default_font_size_px=default_font_size_px,
|
|
421
|
+
slide_number=slide_number,
|
|
422
|
+
id_prefix=id_prefix, id_seq=id_seq,
|
|
423
|
+
hyperlink_resolver=hyperlink_resolver,
|
|
424
|
+
inline_formula_resolver=inline_formula_resolver,
|
|
425
|
+
)
|
|
426
|
+
paragraphs.append(para)
|
|
427
|
+
|
|
428
|
+
return paragraphs
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def _parse_paragraph(
|
|
432
|
+
p_elem: ET.Element,
|
|
433
|
+
palette: ColorPalette | None,
|
|
434
|
+
theme_fonts: dict[str, str],
|
|
435
|
+
autonum_state: dict[int, int],
|
|
436
|
+
*,
|
|
437
|
+
lst_styles: tuple[ET.Element, ...] = (),
|
|
438
|
+
fallback_run_props: tuple[ET.Element, ...] = (),
|
|
439
|
+
default_fill: str = DEFAULT_FILL_HEX,
|
|
440
|
+
default_font_size_px: float = DEFAULT_FONT_SIZE_PX,
|
|
441
|
+
slide_number: int | None = None,
|
|
442
|
+
id_prefix: str = "txt",
|
|
443
|
+
id_seq: list[int] | None = None,
|
|
444
|
+
hyperlink_resolver: HyperlinkResolver | None = None,
|
|
445
|
+
inline_formula_resolver: InlineFormulaResolver | None = None,
|
|
446
|
+
) -> TextParagraph:
|
|
447
|
+
para = TextParagraph()
|
|
448
|
+
|
|
449
|
+
p_pr = p_elem.find("a:pPr", NS)
|
|
450
|
+
if p_pr is not None:
|
|
451
|
+
try:
|
|
452
|
+
para.level = int(p_pr.attrib.get("lvl", "0"))
|
|
453
|
+
except ValueError:
|
|
454
|
+
para.level = 0
|
|
455
|
+
|
|
456
|
+
para_style_chain = (p_pr,) + _lst_style_level_prs(lst_styles, para.level)
|
|
457
|
+
para.align = _attr_chain(para_style_chain, "algn") or "l"
|
|
458
|
+
para.margin_left_px = _emu_px_attr_chain(para_style_chain, "marL", 0.0)
|
|
459
|
+
para.indent_px = _emu_px_attr_chain(para_style_chain, "indent", 0.0)
|
|
460
|
+
para.line_height_ratio = _line_height_ratio(para_style_chain)
|
|
461
|
+
para.space_before_px = _spacing_points_px(para_style_chain, "a:spcBef/a:spcPts")
|
|
462
|
+
para.space_after_px = _spacing_points_px(para_style_chain, "a:spcAft/a:spcPts")
|
|
463
|
+
para.bullet_prefix = _resolve_bullet_prefix(
|
|
464
|
+
para_style_chain, para.level, autonum_state,
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
# Default endParaRPr style (applies if a run has no rPr)
|
|
468
|
+
end_rpr = p_elem.find("a:endParaRPr", NS)
|
|
469
|
+
# defRPr from pPr and txBody/lstStyle, both optional.
|
|
470
|
+
def_rpr = p_pr.find("a:defRPr", NS) if p_pr is not None else None
|
|
471
|
+
list_def_rpr = _child_chain(para_style_chain[1:], "a:defRPr")
|
|
472
|
+
para.empty_line_font_size_px = _font_size_px(
|
|
473
|
+
(end_rpr, def_rpr, list_def_rpr) + fallback_run_props,
|
|
474
|
+
default_font_size_px,
|
|
475
|
+
)
|
|
476
|
+
|
|
477
|
+
def resolved_run(text: str, rpr: ET.Element | None) -> TextRun:
|
|
478
|
+
return _build_run(
|
|
479
|
+
text, rpr, end_rpr, palette, theme_fonts,
|
|
480
|
+
def_rpr=def_rpr,
|
|
481
|
+
list_def_rpr=list_def_rpr,
|
|
482
|
+
fallback_run_props=fallback_run_props,
|
|
483
|
+
default_fill=default_fill,
|
|
484
|
+
default_font_size_px=default_font_size_px,
|
|
485
|
+
id_prefix=id_prefix, id_seq=id_seq,
|
|
486
|
+
hyperlink_resolver=hyperlink_resolver,
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
for child in list(p_elem):
|
|
490
|
+
if not isinstance(child.tag, str):
|
|
491
|
+
continue
|
|
492
|
+
local = child.tag.split("}", 1)[-1]
|
|
493
|
+
if local == "r":
|
|
494
|
+
rpr = child.find("a:rPr", NS)
|
|
495
|
+
text_elem = child.find("a:t", NS)
|
|
496
|
+
text = text_elem.text or "" if text_elem is not None else ""
|
|
497
|
+
para.runs.append(resolved_run(text, rpr))
|
|
498
|
+
elif local == "br":
|
|
499
|
+
break_rpr = child.find("a:rPr", NS)
|
|
500
|
+
para.runs.append(TextRun(
|
|
501
|
+
text="",
|
|
502
|
+
font_size_px=_font_size_px(
|
|
503
|
+
(break_rpr, def_rpr, list_def_rpr, end_rpr)
|
|
504
|
+
+ fallback_run_props,
|
|
505
|
+
default_font_size_px,
|
|
506
|
+
),
|
|
507
|
+
font_family="sans-serif",
|
|
508
|
+
fill=default_fill,
|
|
509
|
+
is_break=True,
|
|
510
|
+
))
|
|
511
|
+
elif local == "fld":
|
|
512
|
+
# Slide SVGs have a concrete page context, so resolve slide-number
|
|
513
|
+
# fields there. Standalone master/layout renders keep the literal
|
|
514
|
+
# fallback because one shared part can serve many slide numbers.
|
|
515
|
+
rpr = child.find("a:rPr", NS)
|
|
516
|
+
text_elem = child.find("a:t", NS)
|
|
517
|
+
text = text_elem.text or "" if text_elem is not None else ""
|
|
518
|
+
field_type = child.attrib.get("type", "").strip().lower()
|
|
519
|
+
if field_type == "slidenum" and slide_number is not None:
|
|
520
|
+
text = str(slide_number)
|
|
521
|
+
if text:
|
|
522
|
+
para.runs.append(resolved_run(text, rpr))
|
|
523
|
+
elif (
|
|
524
|
+
child.tag
|
|
525
|
+
== "{http://schemas.microsoft.com/office/drawing/2010/main}m"
|
|
526
|
+
and inline_formula_resolver is not None
|
|
527
|
+
):
|
|
528
|
+
latex, preview = inline_formula_resolver(child)
|
|
529
|
+
if preview:
|
|
530
|
+
formula_rpr = next(
|
|
531
|
+
child.iter(f"{{{NS['a']}}}rPr"),
|
|
532
|
+
None,
|
|
533
|
+
)
|
|
534
|
+
run = resolved_run(preview, formula_rpr)
|
|
535
|
+
run.formula_latex = latex
|
|
536
|
+
para.runs.append(run)
|
|
537
|
+
|
|
538
|
+
return para
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def _font_size_px(
|
|
542
|
+
sources: tuple[ET.Element | None, ...],
|
|
543
|
+
default_font_size_px: float,
|
|
544
|
+
) -> float:
|
|
545
|
+
"""Resolve one effective DrawingML run size into SVG pixels."""
|
|
546
|
+
return hundredths_pt_to_px(
|
|
547
|
+
_attr_chain(sources, "sz"),
|
|
548
|
+
default_font_size_px,
|
|
549
|
+
)
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def _build_run(
|
|
553
|
+
text: str,
|
|
554
|
+
rpr: ET.Element | None,
|
|
555
|
+
end_rpr: ET.Element | None,
|
|
556
|
+
palette: ColorPalette | None,
|
|
557
|
+
theme_fonts: dict[str, str],
|
|
558
|
+
*,
|
|
559
|
+
def_rpr: ET.Element | None = None,
|
|
560
|
+
list_def_rpr: ET.Element | None = None,
|
|
561
|
+
fallback_run_props: tuple[ET.Element, ...] = (),
|
|
562
|
+
default_fill: str = DEFAULT_FILL_HEX,
|
|
563
|
+
default_font_size_px: float = DEFAULT_FONT_SIZE_PX,
|
|
564
|
+
id_prefix: str = "txt",
|
|
565
|
+
id_seq: list[int] | None = None,
|
|
566
|
+
hyperlink_resolver: HyperlinkResolver | None = None,
|
|
567
|
+
) -> TextRun:
|
|
568
|
+
"""Resolve a single <a:r> run from its rPr and fallback run properties."""
|
|
569
|
+
style_chain = (
|
|
570
|
+
rpr, def_rpr, list_def_rpr, end_rpr,
|
|
571
|
+
) + fallback_run_props
|
|
572
|
+
# font-size: rPr > pPr/defRPr > lstStyle/lvlNpPr/defRPr > endParaRPr > default
|
|
573
|
+
font_size_px = _font_size_px(style_chain, default_font_size_px)
|
|
574
|
+
# Bold / italic
|
|
575
|
+
bold = _attr_chain(style_chain, "b") == "1"
|
|
576
|
+
italic = _attr_chain(style_chain, "i") == "1"
|
|
577
|
+
# Underline / strike
|
|
578
|
+
u_val = _attr_chain(style_chain, "u")
|
|
579
|
+
underline = u_val not in (None, "", "none")
|
|
580
|
+
strike_val = _attr_chain(style_chain, "strike")
|
|
581
|
+
strikethrough = strike_val in ("sngStrike", "dblStrike")
|
|
582
|
+
|
|
583
|
+
# Letter spacing (rPr@spc, in 1/100 pt)
|
|
584
|
+
spc = _attr_chain(style_chain, "spc")
|
|
585
|
+
letter_spacing_px = 0.0
|
|
586
|
+
if spc is not None:
|
|
587
|
+
try:
|
|
588
|
+
letter_spacing_px = float(spc) / 100.0 * 4.0 / 3.0 # pt -> px
|
|
589
|
+
except ValueError:
|
|
590
|
+
pass
|
|
591
|
+
|
|
592
|
+
# Color
|
|
593
|
+
fill = default_fill
|
|
594
|
+
fill_opacity = 1.0
|
|
595
|
+
defs: list[str] = []
|
|
596
|
+
color_source = None
|
|
597
|
+
for src in style_chain:
|
|
598
|
+
if src is None:
|
|
599
|
+
continue
|
|
600
|
+
grad = src.find("a:gradFill", NS)
|
|
601
|
+
if grad is not None:
|
|
602
|
+
grad_fill = resolve_fill(
|
|
603
|
+
grad, palette,
|
|
604
|
+
id_prefix=id_prefix,
|
|
605
|
+
id_seq=id_seq,
|
|
606
|
+
)
|
|
607
|
+
if grad_fill.attrs.get("fill"):
|
|
608
|
+
fill = grad_fill.attrs["fill"]
|
|
609
|
+
fill_opacity = 1.0
|
|
610
|
+
defs.extend(grad_fill.defs)
|
|
611
|
+
color_source = None
|
|
612
|
+
break
|
|
613
|
+
solid = src.find("a:solidFill", NS)
|
|
614
|
+
if solid is not None:
|
|
615
|
+
color_source = solid
|
|
616
|
+
break
|
|
617
|
+
if color_source is not None:
|
|
618
|
+
color_elem = find_color_elem(color_source)
|
|
619
|
+
hex_, alpha = resolve_color(color_elem, palette)
|
|
620
|
+
if hex_:
|
|
621
|
+
fill = hex_
|
|
622
|
+
fill_opacity = alpha
|
|
623
|
+
|
|
624
|
+
# Font typeface
|
|
625
|
+
latin_face = _typeface_chain(style_chain, "latin")
|
|
626
|
+
ea_face = _typeface_chain(style_chain, "ea")
|
|
627
|
+
cs_face = _typeface_chain(style_chain, "cs")
|
|
628
|
+
lang = _attr_chain(style_chain, "lang")
|
|
629
|
+
alt_lang = _attr_chain(style_chain, "altLang")
|
|
630
|
+
|
|
631
|
+
# Resolve theme refs (e.g. typeface="+mn-lt" / "+mj-ea")
|
|
632
|
+
latin_face = _resolve_theme_typeface(
|
|
633
|
+
latin_face,
|
|
634
|
+
theme_fonts,
|
|
635
|
+
text=text,
|
|
636
|
+
lang=lang,
|
|
637
|
+
alt_lang=alt_lang,
|
|
638
|
+
)
|
|
639
|
+
ea_face = _resolve_theme_typeface(
|
|
640
|
+
ea_face,
|
|
641
|
+
theme_fonts,
|
|
642
|
+
text=text,
|
|
643
|
+
lang=lang,
|
|
644
|
+
alt_lang=alt_lang,
|
|
645
|
+
)
|
|
646
|
+
cs_face = _resolve_theme_typeface(
|
|
647
|
+
cs_face,
|
|
648
|
+
theme_fonts,
|
|
649
|
+
text=text,
|
|
650
|
+
lang=lang,
|
|
651
|
+
alt_lang=alt_lang,
|
|
652
|
+
)
|
|
653
|
+
|
|
654
|
+
font_family = _build_font_stack(latin_face, ea_face, cs_face)
|
|
655
|
+
hyperlink_href: str | None = None
|
|
656
|
+
if rpr is not None and hyperlink_resolver is not None:
|
|
657
|
+
hyperlink = rpr.find("a:hlinkClick", NS)
|
|
658
|
+
if hyperlink is not None:
|
|
659
|
+
hyperlink_href = hyperlink_resolver(
|
|
660
|
+
hyperlink.attrib.get(f"{{{NS['r']}}}id", ""),
|
|
661
|
+
hyperlink.attrib.get("action", ""),
|
|
662
|
+
)
|
|
663
|
+
|
|
664
|
+
return TextRun(
|
|
665
|
+
text=text,
|
|
666
|
+
font_size_px=font_size_px,
|
|
667
|
+
font_family=font_family,
|
|
668
|
+
fill=fill,
|
|
669
|
+
fill_opacity=fill_opacity,
|
|
670
|
+
defs=defs,
|
|
671
|
+
bold=bold,
|
|
672
|
+
italic=italic,
|
|
673
|
+
underline=underline,
|
|
674
|
+
strikethrough=strikethrough,
|
|
675
|
+
letter_spacing_px=letter_spacing_px,
|
|
676
|
+
hyperlink_href=hyperlink_href,
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
def _lst_style_level_prs(
|
|
681
|
+
lst_styles: tuple[ET.Element, ...],
|
|
682
|
+
level: int,
|
|
683
|
+
) -> tuple[ET.Element, ...]:
|
|
684
|
+
"""Return txBody/lstStyle paragraph properties for a paragraph level."""
|
|
685
|
+
level_idx = min(max(level, 0), 8) + 1
|
|
686
|
+
level_prs: list[ET.Element] = []
|
|
687
|
+
for lst_style in lst_styles:
|
|
688
|
+
lvl_pr = lst_style.find(f"a:lvl{level_idx}pPr", NS)
|
|
689
|
+
if lvl_pr is not None:
|
|
690
|
+
level_prs.append(lvl_pr)
|
|
691
|
+
return tuple(level_prs)
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def _child_chain(
|
|
695
|
+
sources: tuple[ET.Element | None, ...],
|
|
696
|
+
path: str,
|
|
697
|
+
) -> ET.Element | None:
|
|
698
|
+
for src in sources:
|
|
699
|
+
if src is None:
|
|
700
|
+
continue
|
|
701
|
+
child = src.find(path, NS)
|
|
702
|
+
if child is not None:
|
|
703
|
+
return child
|
|
704
|
+
return None
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
def _emu_px_attr_chain(
|
|
708
|
+
sources: tuple[ET.Element | None, ...],
|
|
709
|
+
attr: str,
|
|
710
|
+
default: float,
|
|
711
|
+
) -> float:
|
|
712
|
+
value = _attr_chain(sources, attr)
|
|
713
|
+
if value is None:
|
|
714
|
+
return default
|
|
715
|
+
try:
|
|
716
|
+
return emu_to_px(int(value))
|
|
717
|
+
except ValueError:
|
|
718
|
+
return default
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
def _line_height_ratio(sources: tuple[ET.Element | None, ...]) -> float:
|
|
722
|
+
ln_spc = _child_chain(sources, "a:lnSpc")
|
|
723
|
+
if ln_spc is None:
|
|
724
|
+
return DEFAULT_LINE_HEIGHT_RATIO
|
|
725
|
+
spc_pct = ln_spc.find("a:spcPct", NS)
|
|
726
|
+
if spc_pct is None:
|
|
727
|
+
return DEFAULT_LINE_HEIGHT_RATIO
|
|
728
|
+
try:
|
|
729
|
+
return float(spc_pct.attrib.get("val", "100000")) / 100000.0
|
|
730
|
+
except ValueError:
|
|
731
|
+
return DEFAULT_LINE_HEIGHT_RATIO
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
def _spacing_points_px(
|
|
735
|
+
sources: tuple[ET.Element | None, ...],
|
|
736
|
+
path: str,
|
|
737
|
+
) -> float:
|
|
738
|
+
spacing = _child_chain(sources, path)
|
|
739
|
+
if spacing is None:
|
|
740
|
+
return 0.0
|
|
741
|
+
try:
|
|
742
|
+
return hundredths_pt_to_px(int(spacing.attrib.get("val", "0")))
|
|
743
|
+
except ValueError:
|
|
744
|
+
return 0.0
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
def _attr_chain(sources: tuple[ET.Element | None, ...], attr: str) -> str | None:
|
|
748
|
+
"""Return the first non-empty value of `attr` from any source element."""
|
|
749
|
+
for src in sources:
|
|
750
|
+
if src is None:
|
|
751
|
+
continue
|
|
752
|
+
v = src.attrib.get(attr)
|
|
753
|
+
if v is not None:
|
|
754
|
+
return v
|
|
755
|
+
return None
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
def _typeface(rpr: ET.Element | None, child_tag: str) -> str | None:
|
|
759
|
+
if rpr is None:
|
|
760
|
+
return None
|
|
761
|
+
elem = rpr.find(f"a:{child_tag}", NS)
|
|
762
|
+
if elem is None:
|
|
763
|
+
return None
|
|
764
|
+
val = elem.attrib.get("typeface")
|
|
765
|
+
return val or None
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
def _typeface_chain(
|
|
769
|
+
sources: tuple[ET.Element | None, ...],
|
|
770
|
+
child_tag: str,
|
|
771
|
+
) -> str | None:
|
|
772
|
+
for src in sources:
|
|
773
|
+
face = _typeface(src, child_tag)
|
|
774
|
+
if face:
|
|
775
|
+
return face
|
|
776
|
+
return None
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
def _theme_script_from_lang(lang: str | None) -> str | None:
|
|
780
|
+
"""Map a DrawingML language tag to one theme supplemental-script key."""
|
|
781
|
+
if not lang:
|
|
782
|
+
return None
|
|
783
|
+
normalized = lang.strip().replace("_", "-").lower()
|
|
784
|
+
if not normalized:
|
|
785
|
+
return None
|
|
786
|
+
parts = normalized.split("-")
|
|
787
|
+
primary = parts[0]
|
|
788
|
+
if primary == "ja":
|
|
789
|
+
return "Jpan"
|
|
790
|
+
if primary == "ko":
|
|
791
|
+
return "Hang"
|
|
792
|
+
if primary != "zh":
|
|
793
|
+
return None
|
|
794
|
+
if any(part in {"hant", "cht", "tw", "hk", "mo"} for part in parts[1:]):
|
|
795
|
+
return "Hant"
|
|
796
|
+
return "Hans"
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
def _theme_script_from_text(text: str) -> str | None:
|
|
800
|
+
"""Infer a CJK theme script from glyph ranges, defaulting plain Han to Hans."""
|
|
801
|
+
if any(
|
|
802
|
+
0x3100 <= ord(char) <= 0x312F
|
|
803
|
+
or 0x31A0 <= ord(char) <= 0x31BF
|
|
804
|
+
for char in text
|
|
805
|
+
):
|
|
806
|
+
return "Hant"
|
|
807
|
+
return {
|
|
808
|
+
"ko-KR": "Hang",
|
|
809
|
+
"ja-JP": "Jpan",
|
|
810
|
+
"zh-CN": "Hans",
|
|
811
|
+
}.get(detect_text_lang(text))
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
def _run_theme_script(
|
|
815
|
+
text: str,
|
|
816
|
+
lang: str | None,
|
|
817
|
+
alt_lang: str | None,
|
|
818
|
+
) -> str | None:
|
|
819
|
+
"""Resolve EA script from run language first, then alternate language/text."""
|
|
820
|
+
return (
|
|
821
|
+
_theme_script_from_lang(lang)
|
|
822
|
+
or _theme_script_from_lang(alt_lang)
|
|
823
|
+
or _theme_script_from_text(text)
|
|
824
|
+
)
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
def _resolve_theme_typeface(
|
|
828
|
+
face: str | None,
|
|
829
|
+
theme_fonts: dict[str, str],
|
|
830
|
+
*,
|
|
831
|
+
text: str = "",
|
|
832
|
+
lang: str | None = None,
|
|
833
|
+
alt_lang: str | None = None,
|
|
834
|
+
) -> str | None:
|
|
835
|
+
"""Resolve DrawingML major/minor Latin, EA, and complex-script tokens."""
|
|
836
|
+
if not face or not face.startswith("+"):
|
|
837
|
+
return face
|
|
838
|
+
code = face[1:]
|
|
839
|
+
if code == "mj-lt":
|
|
840
|
+
return theme_fonts.get("majorLatin") or face
|
|
841
|
+
if code == "mn-lt":
|
|
842
|
+
return theme_fonts.get("minorLatin") or face
|
|
843
|
+
if code in {"mj-ea", "mn-ea"}:
|
|
844
|
+
prefix = "major" if code.startswith("mj") else "minor"
|
|
845
|
+
script = _run_theme_script(text, lang, alt_lang)
|
|
846
|
+
script_face = (
|
|
847
|
+
theme_fonts.get(f"{prefix}Script{script}")
|
|
848
|
+
if script is not None else None
|
|
849
|
+
)
|
|
850
|
+
return (
|
|
851
|
+
theme_fonts.get(f"{prefix}EastAsia")
|
|
852
|
+
or script_face
|
|
853
|
+
or theme_fonts.get(f"{prefix}Latin")
|
|
854
|
+
or face
|
|
855
|
+
)
|
|
856
|
+
if code == "mj-cs":
|
|
857
|
+
return (
|
|
858
|
+
theme_fonts.get("majorComplexScript")
|
|
859
|
+
or theme_fonts.get("majorLatin")
|
|
860
|
+
or face
|
|
861
|
+
)
|
|
862
|
+
if code == "mn-cs":
|
|
863
|
+
return (
|
|
864
|
+
theme_fonts.get("minorComplexScript")
|
|
865
|
+
or theme_fonts.get("minorLatin")
|
|
866
|
+
or face
|
|
867
|
+
)
|
|
868
|
+
return face
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
def _build_font_stack(latin: str | None, ea: str | None, cs: str | None) -> str:
|
|
872
|
+
"""Build a CSS font-family stack: original PPT names first, then fallbacks."""
|
|
873
|
+
parts: list[str] = []
|
|
874
|
+
seen: set[str] = set()
|
|
875
|
+
for face in (latin, ea, cs):
|
|
876
|
+
if face and face not in seen:
|
|
877
|
+
parts.append(_quote_font(face))
|
|
878
|
+
seen.add(face)
|
|
879
|
+
# Generic fallback so the browser can render even if PPT fonts are absent.
|
|
880
|
+
parts.append("sans-serif")
|
|
881
|
+
return ", ".join(parts)
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
def _quote_font(name: str) -> str:
|
|
885
|
+
"""Quote a font name if it contains spaces or non-ASCII chars.
|
|
886
|
+
|
|
887
|
+
Uses XML entity-escaped double quotes (") so the resulting CSS string
|
|
888
|
+
survives being embedded inside an SVG attribute that itself uses double
|
|
889
|
+
quotes. CSS parsers accept the unescaped form after attribute parsing.
|
|
890
|
+
"""
|
|
891
|
+
if any(c.isspace() or ord(c) > 127 for c in name):
|
|
892
|
+
return f""{name}""
|
|
893
|
+
return name
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
def _resolve_bullet_prefix(
|
|
897
|
+
sources: tuple[ET.Element | None, ...],
|
|
898
|
+
level: int,
|
|
899
|
+
autonum_state: dict[int, int],
|
|
900
|
+
) -> str:
|
|
901
|
+
"""Render bullet glyphs / numbering as a literal text prefix."""
|
|
902
|
+
bu_none = _child_chain(sources, "a:buNone")
|
|
903
|
+
if bu_none is not None:
|
|
904
|
+
autonum_state.pop(level, None)
|
|
905
|
+
return ""
|
|
906
|
+
bu_char = _child_chain(sources, "a:buChar")
|
|
907
|
+
if bu_char is not None:
|
|
908
|
+
ch = bu_char.attrib.get("char", "•")
|
|
909
|
+
return f"{ch} "
|
|
910
|
+
bu_auto = _child_chain(sources, "a:buAutoNum")
|
|
911
|
+
if bu_auto is not None:
|
|
912
|
+
start_at = bu_auto.attrib.get("startAt")
|
|
913
|
+
if start_at is not None:
|
|
914
|
+
try:
|
|
915
|
+
autonum_state[level] = int(start_at)
|
|
916
|
+
except ValueError:
|
|
917
|
+
autonum_state[level] = 1
|
|
918
|
+
else:
|
|
919
|
+
autonum_state[level] = autonum_state.get(level, 0) + 1
|
|
920
|
+
return _format_auto_number(
|
|
921
|
+
autonum_state[level],
|
|
922
|
+
bu_auto.attrib.get("type", "arabicPeriod"),
|
|
923
|
+
)
|
|
924
|
+
return ""
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
def _format_auto_number(value: int, kind: str) -> str:
|
|
928
|
+
lower = kind.lower()
|
|
929
|
+
if "alphalc" in lower:
|
|
930
|
+
token = _alpha_number(value, uppercase=False)
|
|
931
|
+
elif "alphauc" in lower:
|
|
932
|
+
token = _alpha_number(value, uppercase=True)
|
|
933
|
+
elif "romanlc" in lower:
|
|
934
|
+
token = _roman_number(value).lower()
|
|
935
|
+
elif "romanuc" in lower:
|
|
936
|
+
token = _roman_number(value).upper()
|
|
937
|
+
else:
|
|
938
|
+
token = str(value)
|
|
939
|
+
|
|
940
|
+
if "parenboth" in lower:
|
|
941
|
+
return f"({token}) "
|
|
942
|
+
if "parenr" in lower:
|
|
943
|
+
return f"{token}) "
|
|
944
|
+
if "period" in lower:
|
|
945
|
+
return f"{token}. "
|
|
946
|
+
return f"{token} "
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
def _alpha_number(value: int, *, uppercase: bool) -> str:
|
|
950
|
+
value = max(1, value)
|
|
951
|
+
chars: list[str] = []
|
|
952
|
+
while value:
|
|
953
|
+
value -= 1
|
|
954
|
+
chars.append(chr(ord("A") + (value % 26)))
|
|
955
|
+
value //= 26
|
|
956
|
+
text = "".join(reversed(chars))
|
|
957
|
+
return text if uppercase else text.lower()
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
def _roman_number(value: int) -> str:
|
|
961
|
+
value = max(1, min(value, 3999))
|
|
962
|
+
parts: list[str] = []
|
|
963
|
+
for n, token in (
|
|
964
|
+
(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"),
|
|
965
|
+
(100, "C"), (90, "XC"), (50, "L"), (40, "XL"),
|
|
966
|
+
(10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I"),
|
|
967
|
+
):
|
|
968
|
+
while value >= n:
|
|
969
|
+
parts.append(token)
|
|
970
|
+
value -= n
|
|
971
|
+
return "".join(parts)
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
# ---------------------------------------------------------------------------
|
|
975
|
+
# Layout / emission
|
|
976
|
+
# ---------------------------------------------------------------------------
|
|
977
|
+
|
|
978
|
+
def _has_visible_text(paragraphs: list[TextParagraph]) -> bool:
|
|
979
|
+
for p in paragraphs:
|
|
980
|
+
for r in p.runs:
|
|
981
|
+
if r.text.strip():
|
|
982
|
+
return True
|
|
983
|
+
return False
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
def _collect_text_defs(paragraphs: list[TextParagraph]) -> list[str]:
|
|
987
|
+
"""Return unique text fill defs referenced by parsed runs."""
|
|
988
|
+
defs: list[str] = []
|
|
989
|
+
seen: set[str] = set()
|
|
990
|
+
for para in paragraphs:
|
|
991
|
+
for run in para.runs:
|
|
992
|
+
for item in run.defs:
|
|
993
|
+
if item not in seen:
|
|
994
|
+
defs.append(item)
|
|
995
|
+
seen.add(item)
|
|
996
|
+
return defs
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
# ---------------------------------------------------------------------------
|
|
1000
|
+
# Word-wrap / text measurement
|
|
1001
|
+
# ---------------------------------------------------------------------------
|
|
1002
|
+
|
|
1003
|
+
def _is_cjk(ch: str) -> bool:
|
|
1004
|
+
"""Check if a character is CJK (Chinese/Japanese/Korean) or full-width."""
|
|
1005
|
+
return is_cjk_char(ch)
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
def _char_width(ch: str, font_size: float, bold: bool) -> float:
|
|
1009
|
+
"""Estimate a single character's rendered width in pixels.
|
|
1010
|
+
|
|
1011
|
+
Mirrors svg_to_pptx/drawingml/utils.py estimate_text_width so wrapping breaks
|
|
1012
|
+
align with the same heuristic used to estimate text-box sizes elsewhere.
|
|
1013
|
+
"""
|
|
1014
|
+
if _is_cjk(ch):
|
|
1015
|
+
w = font_size # CJK is approximately 1em per glyph
|
|
1016
|
+
elif ch == ' ':
|
|
1017
|
+
w = font_size * 0.3
|
|
1018
|
+
elif ch in 'mMwWOQ%':
|
|
1019
|
+
w = font_size * 0.75
|
|
1020
|
+
elif ch in 'iIlj!|':
|
|
1021
|
+
w = font_size * 0.3
|
|
1022
|
+
elif ch.isdigit():
|
|
1023
|
+
# digits are tabular (uniform ~0.55em) in most UI fonts, including
|
|
1024
|
+
# '1' — classing it with 'il|' under-sizes the width and makes
|
|
1025
|
+
# renderers that ignore wrap="none" (LibreOffice) wrap the line
|
|
1026
|
+
w = font_size * 0.55
|
|
1027
|
+
else:
|
|
1028
|
+
w = font_size * 0.55
|
|
1029
|
+
# Bold Latin generally expands a little. CJK glyphs keep their em advance
|
|
1030
|
+
# in common PPT fonts; applying the bold multiplier causes short Chinese
|
|
1031
|
+
# titles such as "少年强国说" to wrap even though PowerPoint keeps them on
|
|
1032
|
+
# one line.
|
|
1033
|
+
if bold and not _is_cjk(ch):
|
|
1034
|
+
w *= 1.05
|
|
1035
|
+
return w
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
def _estimate_run_width(text: str, run: TextRun) -> float:
|
|
1039
|
+
glyph_width = sum(_char_width(c, run.font_size_px, run.bold) for c in text)
|
|
1040
|
+
tracking_width = run.letter_spacing_px * max(len(text) - 1, 0)
|
|
1041
|
+
return (glyph_width + tracking_width) * 1.05
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
def _advance_width(ch: str, index_in_segment: int, run: TextRun) -> float:
|
|
1045
|
+
"""Return the width added by one character inside a measured line segment."""
|
|
1046
|
+
tracking = run.letter_spacing_px if index_in_segment > 0 else 0.0
|
|
1047
|
+
return _char_width(ch, run.font_size_px, run.bold) + tracking
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
def _find_break_point(
|
|
1051
|
+
text: str, start: int, max_width: float, run: TextRun,
|
|
1052
|
+
) -> tuple[int, float]:
|
|
1053
|
+
"""Find the longest prefix of text[start:] that fits in max_width.
|
|
1054
|
+
|
|
1055
|
+
Returns (end_index, used_width). Prefers breaking after whitespace, after
|
|
1056
|
+
CJK characters, or after hyphens. If even the first character doesn't fit,
|
|
1057
|
+
returns (start, 0.0) — the caller should flush the current line first.
|
|
1058
|
+
"""
|
|
1059
|
+
cur_w = 0.0
|
|
1060
|
+
last_break = start
|
|
1061
|
+
last_break_w = 0.0
|
|
1062
|
+
|
|
1063
|
+
for i in range(start, len(text)):
|
|
1064
|
+
ch = text[i]
|
|
1065
|
+
ch_w = _advance_width(ch, i - start, run)
|
|
1066
|
+
if cur_w + ch_w > max_width:
|
|
1067
|
+
if last_break > start:
|
|
1068
|
+
return last_break, last_break_w
|
|
1069
|
+
return start, 0.0
|
|
1070
|
+
cur_w += ch_w
|
|
1071
|
+
# Update last_break point
|
|
1072
|
+
if ch.isspace() or _is_cjk(ch) or ch in "-—、,。!?:;":
|
|
1073
|
+
last_break = i + 1
|
|
1074
|
+
last_break_w = cur_w
|
|
1075
|
+
# Whole rest fits
|
|
1076
|
+
return len(text), cur_w
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
def _wrap_paragraph_into_lines(
|
|
1080
|
+
para: TextParagraph,
|
|
1081
|
+
max_width: float,
|
|
1082
|
+
) -> list[list[TextRun]]:
|
|
1083
|
+
"""Split a paragraph's runs into display lines respecting `max_width`.
|
|
1084
|
+
|
|
1085
|
+
Each line is a list of (possibly truncated) TextRuns. Explicit a:br runs
|
|
1086
|
+
force a new line. When max_width is +inf the original runs are returned
|
|
1087
|
+
unchanged (one logical line per a:br segment).
|
|
1088
|
+
|
|
1089
|
+
Bullet prefix is prepended to the first non-empty run if present.
|
|
1090
|
+
"""
|
|
1091
|
+
lines: list[list[TextRun]] = [[]]
|
|
1092
|
+
cur_w = 0.0
|
|
1093
|
+
|
|
1094
|
+
if _should_keep_single_line(para, max_width):
|
|
1095
|
+
return [[_copy_run(run, text=run.text) for run in para.runs if not run.is_break and run.text]]
|
|
1096
|
+
|
|
1097
|
+
if para.bullet_prefix and para.runs:
|
|
1098
|
+
first_run = next((r for r in para.runs if not r.is_break), None)
|
|
1099
|
+
if first_run is not None:
|
|
1100
|
+
bullet_run = _copy_run(first_run, text=para.bullet_prefix)
|
|
1101
|
+
lines[-1].append(bullet_run)
|
|
1102
|
+
cur_w = _estimate_run_width(para.bullet_prefix, bullet_run)
|
|
1103
|
+
|
|
1104
|
+
for run in para.runs:
|
|
1105
|
+
if run.is_break:
|
|
1106
|
+
# Keep the break on the line it terminates so consecutive breaks
|
|
1107
|
+
# form a break-only empty line. Visible text owns a non-empty
|
|
1108
|
+
# line's height; the break rPr owns only that empty line.
|
|
1109
|
+
lines[-1].append(run)
|
|
1110
|
+
lines.append([])
|
|
1111
|
+
cur_w = 0.0
|
|
1112
|
+
continue
|
|
1113
|
+
if not run.text:
|
|
1114
|
+
continue
|
|
1115
|
+
if run.formula_latex is not None:
|
|
1116
|
+
width = _estimate_run_width(run.text, run)
|
|
1117
|
+
if lines[-1] and cur_w + width > max_width:
|
|
1118
|
+
lines.append([])
|
|
1119
|
+
cur_w = 0.0
|
|
1120
|
+
lines[-1].append(_copy_run(run, text=run.text))
|
|
1121
|
+
cur_w += width
|
|
1122
|
+
continue
|
|
1123
|
+
text = run.text
|
|
1124
|
+
i = 0
|
|
1125
|
+
while i < len(text):
|
|
1126
|
+
avail = max_width - cur_w
|
|
1127
|
+
if avail <= 0 and lines[-1]:
|
|
1128
|
+
# Line is full; start a new one
|
|
1129
|
+
lines.append([])
|
|
1130
|
+
cur_w = 0.0
|
|
1131
|
+
avail = max_width
|
|
1132
|
+
|
|
1133
|
+
end, used = _find_break_point(text, i, avail, run)
|
|
1134
|
+
if end == i:
|
|
1135
|
+
# Nothing fits even from a fresh line — force one char to avoid
|
|
1136
|
+
# an infinite loop.
|
|
1137
|
+
if lines[-1]:
|
|
1138
|
+
lines.append([])
|
|
1139
|
+
cur_w = 0.0
|
|
1140
|
+
continue
|
|
1141
|
+
end = i + 1
|
|
1142
|
+
used = _advance_width(text[i], 0, run)
|
|
1143
|
+
|
|
1144
|
+
chunk = text[i:end]
|
|
1145
|
+
lines[-1].append(_copy_run(run, text=chunk))
|
|
1146
|
+
cur_w += used
|
|
1147
|
+
i = end
|
|
1148
|
+
|
|
1149
|
+
if i < len(text):
|
|
1150
|
+
# More to render — wrap to next line
|
|
1151
|
+
lines.append([])
|
|
1152
|
+
cur_w = 0.0
|
|
1153
|
+
|
|
1154
|
+
return lines
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
def _should_keep_single_line(para: TextParagraph, max_width: float) -> bool:
|
|
1158
|
+
if max_width == float("inf") or para.bullet_prefix:
|
|
1159
|
+
return False
|
|
1160
|
+
if any(run.is_break for run in para.runs):
|
|
1161
|
+
return False
|
|
1162
|
+
|
|
1163
|
+
text_runs = [run for run in para.runs if run.text]
|
|
1164
|
+
if not text_runs:
|
|
1165
|
+
return False
|
|
1166
|
+
text = "".join(run.text for run in text_runs)
|
|
1167
|
+
non_space_count = sum(1 for ch in text if not ch.isspace())
|
|
1168
|
+
|
|
1169
|
+
# Short labels/titles are usually intentionally single-line in PPT. Let
|
|
1170
|
+
# them overflow slightly rather than inventing a line break from imperfect
|
|
1171
|
+
# font metrics or alignment spaces.
|
|
1172
|
+
if non_space_count <= 18:
|
|
1173
|
+
return True
|
|
1174
|
+
|
|
1175
|
+
estimated = sum(_estimate_run_width(run.text, run) for run in text_runs)
|
|
1176
|
+
return estimated <= max_width * 1.12
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
def _copy_run(run: TextRun, *, text: str) -> TextRun:
|
|
1180
|
+
return TextRun(
|
|
1181
|
+
text=text,
|
|
1182
|
+
font_size_px=run.font_size_px,
|
|
1183
|
+
font_family=run.font_family,
|
|
1184
|
+
fill=run.fill,
|
|
1185
|
+
fill_opacity=run.fill_opacity,
|
|
1186
|
+
defs=list(run.defs),
|
|
1187
|
+
bold=run.bold,
|
|
1188
|
+
italic=run.italic,
|
|
1189
|
+
underline=run.underline,
|
|
1190
|
+
strikethrough=run.strikethrough,
|
|
1191
|
+
letter_spacing_px=run.letter_spacing_px,
|
|
1192
|
+
hyperlink_href=run.hyperlink_href,
|
|
1193
|
+
formula_latex=run.formula_latex,
|
|
1194
|
+
)
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
def _paragraph_height_from_lines(p: TextParagraph,
|
|
1198
|
+
lines: list[list[TextRun]]) -> float:
|
|
1199
|
+
"""Total px height after wrapping. Each line uses its own max font size."""
|
|
1200
|
+
if not lines:
|
|
1201
|
+
return p.empty_line_font_size_px * p.line_height_ratio
|
|
1202
|
+
height = 0.0
|
|
1203
|
+
for line in lines:
|
|
1204
|
+
height += _line_height(p, line)
|
|
1205
|
+
return height
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
def _line_height(p: TextParagraph, line: list[TextRun]) -> float:
|
|
1209
|
+
return _line_font_size(p, line) * p.line_height_ratio
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
def _line_font_size(p: TextParagraph, line: list[TextRun]) -> float:
|
|
1213
|
+
visible_sizes = [
|
|
1214
|
+
run.font_size_px
|
|
1215
|
+
for run in line
|
|
1216
|
+
if not run.is_break and run.text
|
|
1217
|
+
]
|
|
1218
|
+
if visible_sizes:
|
|
1219
|
+
return max(visible_sizes)
|
|
1220
|
+
break_sizes = [run.font_size_px for run in line if run.is_break]
|
|
1221
|
+
return max(break_sizes, default=p.empty_line_font_size_px)
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
def _clip_lines_to_bottom(
|
|
1225
|
+
para: TextParagraph,
|
|
1226
|
+
lines: list[list[TextRun]],
|
|
1227
|
+
top_y: float,
|
|
1228
|
+
bottom_y: float,
|
|
1229
|
+
) -> list[list[TextRun]]:
|
|
1230
|
+
"""Return the leading display lines whose line boxes fit in the text frame."""
|
|
1231
|
+
visible: list[list[TextRun]] = []
|
|
1232
|
+
cursor_y = top_y
|
|
1233
|
+
for line in lines:
|
|
1234
|
+
line_h = _line_height(para, line)
|
|
1235
|
+
# PowerPoint lets the first line that starts within the box render even
|
|
1236
|
+
# when it slightly exceeds the bottom — only suppress lines whose top
|
|
1237
|
+
# is already at/below the bottom edge.
|
|
1238
|
+
if cursor_y >= bottom_y:
|
|
1239
|
+
break
|
|
1240
|
+
visible.append(line)
|
|
1241
|
+
cursor_y += line_h
|
|
1242
|
+
return visible
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
def _paragraph_height(p: TextParagraph) -> float:
|
|
1246
|
+
"""Legacy helper kept for callers that don't pre-wrap (currently unused)."""
|
|
1247
|
+
lines = _wrap_paragraph_into_lines(p, float("inf"))
|
|
1248
|
+
return _paragraph_height_from_lines(p, lines)
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
def _emit_paragraph(
|
|
1252
|
+
para: TextParagraph,
|
|
1253
|
+
lines: list[list[TextRun]],
|
|
1254
|
+
inner_x: float, inner_w: float,
|
|
1255
|
+
top_y: float,
|
|
1256
|
+
) -> str:
|
|
1257
|
+
"""Render a paragraph (already split into lines) as one <text> element.
|
|
1258
|
+
|
|
1259
|
+
Each pre-wrapped display line becomes a sequence of <tspan>s: the first
|
|
1260
|
+
tspan on a line carries the explicit x and dy (line-height advance);
|
|
1261
|
+
subsequent tspans on the same line inherit x.
|
|
1262
|
+
"""
|
|
1263
|
+
align = para.align
|
|
1264
|
+
if align == "ctr":
|
|
1265
|
+
anchor_x = inner_x + inner_w / 2.0
|
|
1266
|
+
text_anchor = "middle"
|
|
1267
|
+
elif align == "r":
|
|
1268
|
+
anchor_x = inner_x + inner_w
|
|
1269
|
+
text_anchor = "end"
|
|
1270
|
+
else: # 'l' / 'just' / 'dist' / unknown
|
|
1271
|
+
anchor_x = inner_x + para.indent_px + para.margin_left_px
|
|
1272
|
+
text_anchor = "start"
|
|
1273
|
+
|
|
1274
|
+
if not lines:
|
|
1275
|
+
return ""
|
|
1276
|
+
|
|
1277
|
+
visible_lines = [
|
|
1278
|
+
[run for run in line if not run.is_break and run.text]
|
|
1279
|
+
for line in lines
|
|
1280
|
+
]
|
|
1281
|
+
first_line_idx = next(
|
|
1282
|
+
(index for index, line in enumerate(visible_lines) if line),
|
|
1283
|
+
None,
|
|
1284
|
+
)
|
|
1285
|
+
if first_line_idx is None:
|
|
1286
|
+
return ""
|
|
1287
|
+
|
|
1288
|
+
first_run = visible_lines[first_line_idx][0]
|
|
1289
|
+
first_baseline = top_y + 0.85 * first_run.font_size_px
|
|
1290
|
+
|
|
1291
|
+
spans: list[str] = []
|
|
1292
|
+
for line_idx, line in enumerate(visible_lines):
|
|
1293
|
+
line_advance = None
|
|
1294
|
+
if line_idx > 0:
|
|
1295
|
+
height_line = (
|
|
1296
|
+
lines[line_idx - 1]
|
|
1297
|
+
if line_idx <= first_line_idx else lines[line_idx]
|
|
1298
|
+
)
|
|
1299
|
+
line_advance = _line_height(para, height_line)
|
|
1300
|
+
if not line:
|
|
1301
|
+
if line_advance is not None:
|
|
1302
|
+
spans.append(
|
|
1303
|
+
f'<tspan x="{fmt_num(anchor_x)}" '
|
|
1304
|
+
f'dy="{fmt_num(line_advance)}"></tspan>'
|
|
1305
|
+
)
|
|
1306
|
+
continue
|
|
1307
|
+
line_has_hyperlink = any(run.hyperlink_href for run in line)
|
|
1308
|
+
if line_has_hyperlink:
|
|
1309
|
+
run_spans = ''.join(
|
|
1310
|
+
_wrap_run_hyperlink(
|
|
1311
|
+
_run_tspan_markup(run),
|
|
1312
|
+
run,
|
|
1313
|
+
)
|
|
1314
|
+
for run in line
|
|
1315
|
+
)
|
|
1316
|
+
position_attrs = (
|
|
1317
|
+
f' x="{fmt_num(anchor_x)}" dy="{fmt_num(line_advance)}"'
|
|
1318
|
+
if line_advance is not None
|
|
1319
|
+
else ''
|
|
1320
|
+
)
|
|
1321
|
+
spans.append(f'<tspan{position_attrs}>{run_spans}</tspan>')
|
|
1322
|
+
continue
|
|
1323
|
+
for run_idx, run in enumerate(line):
|
|
1324
|
+
attrs = _run_tspan_attrs(run)
|
|
1325
|
+
if run_idx == 0 and line_advance is not None:
|
|
1326
|
+
if run.formula_latex is not None:
|
|
1327
|
+
spans.append(
|
|
1328
|
+
f'<tspan x="{fmt_num(anchor_x)}" '
|
|
1329
|
+
f'dy="{fmt_num(line_advance)}">'
|
|
1330
|
+
f'{_run_tspan_markup(run)}</tspan>'
|
|
1331
|
+
)
|
|
1332
|
+
else:
|
|
1333
|
+
spans.append(
|
|
1334
|
+
f'<tspan x="{fmt_num(anchor_x)}" '
|
|
1335
|
+
f'dy="{fmt_num(line_advance)}"'
|
|
1336
|
+
f'{attrs}>{_xml_escape(run.text)}</tspan>'
|
|
1337
|
+
)
|
|
1338
|
+
else:
|
|
1339
|
+
spans.append(_run_tspan_markup(run))
|
|
1340
|
+
|
|
1341
|
+
base_attrs = _text_base_attrs(first_run, anchor_x, first_baseline, text_anchor)
|
|
1342
|
+
return f"<text{base_attrs}>{''.join(spans)}</text>"
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
def _text_base_attrs(run: TextRun | None, x: float, y: float,
|
|
1346
|
+
text_anchor: str) -> str:
|
|
1347
|
+
parts = [
|
|
1348
|
+
f'x="{fmt_num(x)}"',
|
|
1349
|
+
f'y="{fmt_num(y)}"',
|
|
1350
|
+
f'text-anchor="{text_anchor}"',
|
|
1351
|
+
'xml:space="preserve"',
|
|
1352
|
+
]
|
|
1353
|
+
if run is None:
|
|
1354
|
+
return " " + " ".join(parts)
|
|
1355
|
+
parts.append(f'font-family="{run.font_family}"')
|
|
1356
|
+
parts.append(f'font-size="{fmt_num(run.font_size_px)}"')
|
|
1357
|
+
parts.append(f'fill="{run.fill}"')
|
|
1358
|
+
if run.fill_opacity < 1.0:
|
|
1359
|
+
parts.append(
|
|
1360
|
+
f'fill-opacity="{format_ooxml_alpha(run.fill_opacity)}"'
|
|
1361
|
+
)
|
|
1362
|
+
if run.bold:
|
|
1363
|
+
parts.append('font-weight="bold"')
|
|
1364
|
+
if run.italic:
|
|
1365
|
+
parts.append('font-style="italic"')
|
|
1366
|
+
if run.underline and run.strikethrough:
|
|
1367
|
+
parts.append('text-decoration="underline line-through"')
|
|
1368
|
+
elif run.underline:
|
|
1369
|
+
parts.append('text-decoration="underline"')
|
|
1370
|
+
elif run.strikethrough:
|
|
1371
|
+
parts.append('text-decoration="line-through"')
|
|
1372
|
+
if run.letter_spacing_px:
|
|
1373
|
+
parts.append(f'letter-spacing="{fmt_num(run.letter_spacing_px)}"')
|
|
1374
|
+
return " " + " ".join(parts)
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
def _run_tspan_attrs(run: TextRun) -> str:
|
|
1378
|
+
"""Per-run overrides on a <tspan>. Only emit attributes that differ from
|
|
1379
|
+
the run that drove the parent <text> (we keep things simple: emit only
|
|
1380
|
+
overrides that can plausibly change run-to-run, never re-emit common
|
|
1381
|
+
defaults). For v1 we always emit fill, font family, and font size so each
|
|
1382
|
+
imported run keeps its resolved typeface even when adjacent runs differ.
|
|
1383
|
+
"""
|
|
1384
|
+
parts = [
|
|
1385
|
+
f'fill="{run.fill}"',
|
|
1386
|
+
f'font-family="{run.font_family}"',
|
|
1387
|
+
f'font-size="{fmt_num(run.font_size_px)}"',
|
|
1388
|
+
]
|
|
1389
|
+
if run.fill_opacity < 1.0:
|
|
1390
|
+
parts.append(
|
|
1391
|
+
f'fill-opacity="{format_ooxml_alpha(run.fill_opacity)}"'
|
|
1392
|
+
)
|
|
1393
|
+
if run.bold:
|
|
1394
|
+
parts.append('font-weight="bold"')
|
|
1395
|
+
if run.italic:
|
|
1396
|
+
parts.append('font-style="italic"')
|
|
1397
|
+
if run.underline and run.strikethrough:
|
|
1398
|
+
parts.append('text-decoration="underline line-through"')
|
|
1399
|
+
elif run.underline:
|
|
1400
|
+
parts.append('text-decoration="underline"')
|
|
1401
|
+
elif run.strikethrough:
|
|
1402
|
+
parts.append('text-decoration="line-through"')
|
|
1403
|
+
if run.letter_spacing_px:
|
|
1404
|
+
parts.append(f'letter-spacing="{fmt_num(run.letter_spacing_px)}"')
|
|
1405
|
+
return " " + " ".join(parts)
|
|
1406
|
+
|
|
1407
|
+
|
|
1408
|
+
def _run_tspan_markup(run: TextRun) -> str:
|
|
1409
|
+
formula_attr = ""
|
|
1410
|
+
if run.formula_latex is not None:
|
|
1411
|
+
formula_attr = (
|
|
1412
|
+
' data-pptx-inline-formula="'
|
|
1413
|
+
+ _xml_escape(run.formula_latex)
|
|
1414
|
+
+ '"'
|
|
1415
|
+
)
|
|
1416
|
+
return (
|
|
1417
|
+
f"<tspan{_run_tspan_attrs(run)}{formula_attr}>"
|
|
1418
|
+
f"{_xml_escape(run.text)}</tspan>"
|
|
1419
|
+
)
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
def _wrap_run_hyperlink(markup: str, run: TextRun) -> str:
|
|
1423
|
+
"""Wrap one visible SVG run in the canonical hyperlink carrier."""
|
|
1424
|
+
if not run.hyperlink_href:
|
|
1425
|
+
return markup
|
|
1426
|
+
return f'<a href="{_xml_escape(run.hyperlink_href)}">{markup}</a>'
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
def _xml_escape(text: str) -> str:
|
|
1430
|
+
return (text.replace("&", "&")
|
|
1431
|
+
.replace("<", "<")
|
|
1432
|
+
.replace(">", ">")
|
|
1433
|
+
.replace('"', """))
|