Complete phase two benchmarks, plot previews and static export workflow

This commit is contained in:
2026-09-07 02:54:52 +08:00
parent 95095197df
commit 89df10bc4e
59 changed files with 2535 additions and 83 deletions
+140
View File
@@ -0,0 +1,140 @@
"""Bounded raster-only resource boundary. No URLs, XML or filesystem paths accepted."""
import base64
import hashlib
import threading
from io import BytesIO
from PIL import Image
from app.errors import ApiError
_math_lock = threading.Lock()
def enrich_document(document, file_path=None):
"""Embed local vault images and bounded MathText. Unsupported TeX stays explicit."""
from app.config import get_settings
from urllib.parse import unquote, urlsplit
vault = get_settings().vault_path.resolve()
base = (vault / (file_path or '')).parent if file_path else vault
warnings = []
count = total = pixels = 0
def visit(node):
nonlocal count, total, pixels
if node.type in {'image','math_block','math_inline'} or node.attributes.get('static_png'):
count += 1
try:
if count > 64: raise ValueError('resource count')
if node.attributes.get('static_png'):
raw = node.attributes['static_png']
elif node.type == 'image':
src = str(node.attributes.get('src',''))
if urlsplit(src).scheme or src.startswith('//'): raise ValueError('remote image')
path = (base / unquote(src)).resolve()
if not path.is_relative_to(vault) or path.suffix.lower() not in {'.png','.jpg','.jpeg','.webp'} or path.stat().st_size > 2_000_000:
raise ValueError('image path or budget')
raw = path.read_bytes()
else:
source = node.text
depth = 0
for char in source:
depth += (char == '{') - (char == '}')
if depth > 20: raise ValueError('math depth')
if len(source) > 512 or depth != 0: raise ValueError('math budget')
from matplotlib.mathtext import math_to_image
with _math_lock:
out = BytesIO()
math_to_image('$'+source+'$', out, dpi=180, format='png', color='black')
raw = out.getvalue()
with Image.open(BytesIO(raw)) as image:
pixels += image.width * image.height
if pixels > 16_000_000: raise ValueError('document pixels')
if image.width * image.height > 4_000_000: raise ValueError('image dimensions')
out = BytesIO()
# Flatten alpha on white for portable print/Word output.
rgba=image.convert('RGBA'); background=Image.new('RGBA',rgba.size,'white')
background.alpha_composite(rgba); background.convert('RGB').save(out,'PNG')
png=out.getvalue();total += len(png)
if total > 8_000_000: raise ValueError('resource bytes')
node.attributes['static_png']=png
except Exception:
node.attributes.pop('static_png', None)
warnings.append('图片无法内嵌(仅支持 Vault 内 PNG/JPEG/WebP),已保留替代文字' if node.type=='image'
else '公式超出 MathText 语法或资源预算,已保留源码' if node.type.startswith('math')
else '静态图表超过文档资源预算,已保留源码')
for child in node.children: visit(child)
for child in document.children: visit(child)
return warnings
def source_hash(source):
return hashlib.sha256(source.strip().encode()).hexdigest()
def validate_assets(assets):
result = {}
total = pixels = 0
for asset in assets:
try:
raw = base64.b64decode(asset.png_base64, validate=True)
total += len(raw)
if total > 8 * 1024 * 1024:
raise ValueError('asset budget')
with Image.open(BytesIO(raw)) as image:
pixels += image.width * image.height
if pixels > 16_000_000: raise ValueError('document pixel budget')
if image.format != 'PNG' or image.width * image.height > 4_000_000:
raise ValueError('image budget')
image.load()
out = BytesIO()
rgba = image.convert('RGBA')
background = Image.new('RGBA', rgba.size, 'white')
background.alpha_composite(rgba)
background.convert('RGB').save(out, 'PNG')
key = (asset.kind, asset.source_hash)
if key in result:
raise ValueError('duplicate asset')
result[key] = out.getvalue()
except Exception as exc:
raise ApiError(422, 'EXPORT_ASSET_INVALID', 'Invalid PNG or resource budget exceeded.') from exc
return result
def attach_assets(document, assets):
def visit(node):
source = node.attributes.get('src', '') if node.type == 'image' else node.text
key = (node.type, source_hash(source))
if key in assets:
node.attributes['static_png'] = assets[key]
for child in node.children:
visit(child)
for child in document.children:
visit(child)
def plot_png(plot):
"""DOCX consumes the same clipped geometry as SVG/PDF, rendered at 2x."""
from app.plot.render import compute_geometry, _sx, _sy, _fmt_num
from PIL import ImageDraw, ImageFont
geo = compute_geometry(plot)
image = Image.new('RGB', (geo.width * 2, (geo.height + ((len(plot.expressions)+1)//2)*24) * 2), 'white')
draw = ImageDraw.Draw(image)
from app.export.fonts import FONT_PATH
font = ImageFont.truetype(str(FONT_PATH), 20) if FONT_PATH else ImageFont.load_default(size=20)
def line(points, color, width=2):
draw.line([(x * 2, y * 2) for x, y in points], fill=color, width=width)
sx = lambda x: _sx(x, geo.xmin, geo.xmax)
sy = lambda y: _sy(y, geo.ymin, geo.ymax)
for x in geo.xticks:
if geo.grid: line([(sx(x),52),(sx(x),428)], '#d0d7de')
draw.text((sx(x)*2, sy(geo.x_axis_y)*2+8), _fmt_num(x), fill='#57606a', font=font)
for y in geo.yticks:
if geo.grid: line([(52,sy(y)),(588,sy(y))], '#d0d7de')
draw.text((max(0,sx(geo.y_axis_x)*2-75),sy(y)*2), _fmt_num(y), fill='#57606a', font=font)
line([(52,sy(geo.x_axis_y)),(588,sy(geo.x_axis_y))], '#57606a')
line([(sx(geo.y_axis_x),52),(sx(geo.y_axis_x),428)], '#57606a')
for segments, color in zip(geo.polylines,geo.colors):
for segment in segments:
if len(segment)>1: line(segment,color,3)
if geo.xlabel:
draw.text((geo.width, (geo.height - 18)*2), geo.xlabel, fill='#1f2328', font=font, anchor='mm')
if geo.ylabel:
# Horizontal at the upper-left margin keeps CJK labels readable in Word.
draw.text((24, 24), geo.ylabel, fill='#1f2328', font=font)
for index, expression in enumerate(plot.expressions):
draw.text((48+(index%2)*620,geo.height*2+index//2*48),expression.label or 'y = '+expression.expression,fill=geo.colors[index],font=font)
out=BytesIO(); image.save(out,'PNG')
return out.getvalue(), geo.warnings
+31 -3
View File
@@ -1,7 +1,7 @@
"""DocxExporterDocument AST → DOCXpython-docx)。
v1 为文本优先:标题/段落/行内强调与链接/列表/引用/表格/代码块/数学文本均可导出;
function_plot 与 mermaid 保留源码占位并记 warning。中文字体通过 Normal 样式挂载
标题段落、列表、表格等使用原生 Word 元素;函数图、已准备的 Mermaid、
受支持的公式与 Vault 图片使用静态图片,无法表示的资源保留源码并记 warning。中文字体通过 Normal 样式挂载
w:eastAsia=宋体,保证 Word 打开时中文正常显示;bold/italic 由 Word 原生渲染。
"""
@@ -50,6 +50,8 @@ class DocxExporter:
def render(self, document: Document, options: ExportOptions) -> ExportResult:
"""同步渲染;CPU 密集,调用方应放入线程执行,避免阻塞事件循环。"""
from app.export.exporters._common import FunctionPlotBudget
self._plot_budget = FunctionPlotBudget()
self._doc = DocxDocument()
self._configure_normal_style()
self._configure_page(options)
@@ -109,6 +111,13 @@ class DocxExporter:
self._render_block(child, warnings)
def _render_block(self, node: DocumentNode, warnings: list[str]) -> None:
if node.attributes.get('static_png'):
from PIL import Image
png = node.attributes['static_png']
with Image.open(BytesIO(png)) as image:
width = min(5.8, image.width / (180 if node.type == 'math_block' else 96))
self._doc.add_picture(BytesIO(png), width=Inches(width))
return
handler = getattr(self, f"_block_{node.type}", None)
if handler is not None:
handler(node, warnings)
@@ -270,7 +279,20 @@ class DocxExporter:
self._block_code_block(node, warnings)
def _block_function_plot(self, node: DocumentNode, warnings: list[str]) -> None:
warnings.append(PLOT_PLACEHOLDER_WARNING)
from app.plot.parser import parse_source
from app.export.assets import plot_png
over = self._plot_budget.check_count()
if not over:
parsed = parse_source(node.text)
warnings.extend(d.message for d in parsed.diagnostics)
if parsed.plot:
over = self._plot_budget.check_nodes(parsed.plot.node_count)
if not over:
png, messages = plot_png(parsed.plot)
warnings.extend(messages)
self._doc.add_picture(BytesIO(png), width=Inches(5.8))
return
warnings.append(over or '函数图像无法绘制,已保留源码')
self._block_code_block(node, warnings)
def _block_math_block(self, node: DocumentNode, warnings: list[str]) -> None:
@@ -303,6 +325,12 @@ class DocxExporter:
bold: bool = False,
italic: bool = False,
) -> None:
if node.attributes.get('static_png'):
from PIL import Image
with Image.open(BytesIO(node.attributes['static_png'])) as image:
width = min(5.8, image.width / (180 if node.type.startswith('math') else 96))
paragraph.add_run().add_picture(BytesIO(node.attributes['static_png']), width=Inches(width))
return
t = node.type
if t == "text":
self._add_run(paragraph, node.text, bold=bold, italic=italic)
+12
View File
@@ -151,6 +151,16 @@ class HtmlExporter:
return "".join(self._render_node(child, warnings) for child in children)
def _render_node(self, node: DocumentNode, warnings: list[str]) -> str:
if node.attributes.get('static_png'):
import base64
data = base64.b64encode(node.attributes['static_png']).decode()
from PIL import Image
from io import BytesIO
width = ''
if node.type.startswith('math'):
with Image.open(BytesIO(node.attributes['static_png'])) as image:
width = f'width:{image.width*96/180:.1f}px;vertical-align:middle;'
return f'<img alt="{html.escape(node.text or node.type)}" src="data:image/png;base64,{data}" style="{width}max-width:100%">'
handler = getattr(self, f"_render_{node.type}", None)
if handler is not None:
return handler(node, warnings)
@@ -257,6 +267,8 @@ class HtmlExporter:
warnings.append(f"函数图像:解析或渲染失败,已回退占位({exc}")
return f'<pre class="function-plot">{html.escape(node.text)}</pre>'
warnings.extend(rendered.warnings)
from app.plot.render import theme_svg
rendered.content = theme_svg(rendered.content, self._options.theme_id)
return f'<figure class="function-plot">{rendered.content}</figure>'
def _render_math_block(self, node: DocumentNode, warnings: list[str]) -> str:
+19 -2
View File
@@ -42,8 +42,7 @@ from app.export.exporters._common import (
from app.plot.render_reportlab import render_drawing
from app.plot.renderer import FunctionPlotStaticRenderer, StaticRenderRequest
_FONT = "STSong-Light"
pdfmetrics.registerFont(UnicodeCIDFont(_FONT))
from app.export.fonts import FONT as _FONT
_MIME = "application/pdf"
@@ -122,6 +121,7 @@ class PdfExporter:
self._styles = _make_styles()
warnings: list[str] = []
print_theme_warning(options, warnings, "PDF")
if _FONT == "STSong-Light": warnings.append("PDF 使用 CID 字体,阅读器需提供中文字体;可配置 APP_EXPORT_FONT 嵌入 TrueType 字体")
page = _PAGE_SIZES.get((options.page_size or "A4").lower(), A4)
self._options = options
@@ -169,6 +169,14 @@ class PdfExporter:
self._render_block(child, story, warnings)
def _render_block(self, node: DocumentNode, story: list, warnings: list[str]) -> None:
if node.attributes.get('static_png'):
from reportlab.platypus import Image
image = Image(BytesIO(node.attributes['static_png']))
scale = min(1, self._plot_width / image.imageWidth, 600 / image.imageHeight)
image.drawWidth = image.imageWidth * scale
image.drawHeight = image.imageHeight * scale
story.append(image)
return
handler = getattr(self, f"_block_{node.type}", None)
if handler is not None:
handler(node, story, warnings)
@@ -362,6 +370,15 @@ class PdfExporter:
return "".join(self._render_inline_node(child, warnings) for child in children)
def _render_inline_node(self, node: DocumentNode, warnings: list[str]) -> str:
if node.attributes.get('static_png'):
import base64
from PIL import Image as PILImage
raw = node.attributes['static_png']
with PILImage.open(BytesIO(raw)) as image:
scale = min(.4 if node.type.startswith('math') else 1, 350/image.width, 160/image.height)
width, height = image.width*scale, image.height*scale
data = base64.b64encode(raw).decode()
return f'<img src="data:image/png;base64,{data}" width="{width}" height="{height}" valign="middle"/>'
t = node.type
if t == "text":
return _html.escape(node.text)
+22
View File
@@ -0,0 +1,22 @@
"""Embed an available CJK TrueType font; retain the portable CID fallback."""
import os
from pathlib import Path
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
def register_font():
candidates = [os.getenv('APP_EXPORT_FONT',''),
str(Path(os.getenv('WINDIR','C:/Windows'))/'Fonts/simsun.ttc'),
'/usr/share/fonts/truetype/arphic/uming.ttc']
for candidate in candidates:
if candidate and Path(candidate).is_file():
try:
pdfmetrics.registerFont(TTFont('NotesExportCJK',candidate,subfontIndex=0))
return 'NotesExportCJK', Path(candidate)
except Exception:
continue
pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))
return 'STSong-Light', None
FONT, FONT_PATH = register_font()
+11 -2
View File
@@ -182,12 +182,15 @@ async def _resolve_source(source: ExportSource) -> tuple[str, str, dict | None]:
f"markdown source exceeds {MAX_MARKDOWN_CHARS} characters",
{"size": len(markdown), "limit": MAX_MARKDOWN_CHARS},
)
return markdown, "", None
return markdown, "", {"file_path": source.file_path} if source.file_path else None
async def create_export(request: ExportRequest) -> ExportJob:
"""创建导出任务,立即返回 queued 的 ExportJob,由后台 Task 渲染。"""
markdown, title, metadata = await _resolve_source(request.source)
title = request.title or title
from app.export.assets import validate_assets
assets = await asyncio.to_thread(validate_assets, request.assets)
if not _evict_terminal():
raise ApiError(
@@ -207,7 +210,7 @@ async def create_export(request: ExportRequest) -> ExportJob:
_jobs[job_id] = job
_cancel_flags[job_id] = asyncio.Event()
_tasks[job_id] = asyncio.create_task(
_execute(job_id, request.format, markdown, title, metadata, request.options)
_execute(job_id, request.format, markdown, title, metadata, request.options, assets)
)
return job
@@ -245,6 +248,7 @@ async def _execute(
title: str,
metadata: dict | None,
options: ExportOptions,
assets: dict | None = None,
) -> None:
"""后台渲染:排队 → 解析 → 导出 → 写文件 → 挂载产物元信息。"""
cancel_event = _cancel_flags[job_id]
@@ -273,10 +277,15 @@ async def _execute(
# 使运行中的取消能在渲染边界生效;写文件前再次检查取消。
document = await asyncio.to_thread(parse_document, markdown)
document.attributes["title"] = title
from app.export.assets import attach_assets
attach_assets(document, assets or {})
if metadata:
document.attributes["metadata"] = metadata
from app.export.assets import enrich_document
resource_warnings = await asyncio.to_thread(enrich_document, document, (metadata or {}).get('file_path'))
result = await asyncio.to_thread(_render_document, document, options, format)
result.warnings[:0] = resource_warnings
if cancel_event.is_set():
raise ExportCancelled()
if len(result.content) > MAX_EXPORT_BYTES:
+1
View File
@@ -1,5 +1,6 @@
"""Export palettes are fixed data; arbitrary theme CSS is never executed."""
PALETTES = {
'ocean-blue': ('#edf5fa','#ffffff','#183a50','#46667a','#e6f1f8','#a6c5d9','#086b9c'),
'light': ('#f6f7f9','#ffffff','#1f2328','#57606a','#eaeef2','#d0d7de','#0969da'),
'dark': ('#010409','#0d1117','#e6edf3','#b1bac4','#21262d','#57606a','#79c0ff'),
'sepia': ('#eee5d2','#faf4e6','#463b2d','#6b5943','#eae0cd','#b5a58b','#80532a'),