@lark-apaas/coding-steering 0.1.32-dev.4f80f68 → 0.1.32-dev.942e73f
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/package.json +1 -1
- package/steering/design-html/skills/pptx-style-extract/SKILL.md +27 -18
- package/steering/design-html/skills/pptx-style-extract/scripts/census.py +8 -2
- package/steering/design-html/skills/pptx-style-extract/scripts/draft.py +1152 -178
- package/steering/design-html/skills/pptx-style-extract/scripts/extract.py +229 -10
- package/steering/design-html/skills/pptx-style-extract/scripts/ooxml.py +18 -1
- package/steering/design-html/skills/pptx-style-extract/scripts/package.py +643 -40
- package/steering/design-html/skills/pptx-style-extract/scripts/parts.py +19 -3
- package/steering/design-html/skills/pptx-style-extract/scripts/render_pages.py +4 -2
- package/steering/design-html/skills/pptx-style-extract/scripts/test_asset_judgment_package.py +556 -0
- package/steering/design-html/skills/pptx-style-extract/scripts/test_background_composite.py +308 -1
- package/steering/design-html/skills/pptx-style-extract/scripts/test_design_consumer_contract.py +6 -0
- package/steering/design-html/skills/pptx-style-extract/scripts/test_flow_layout_contract.py +157 -7
- package/steering/design-html/skills/pptx-style-extract/scripts/test_layout_css.py +1519 -2
- package/steering/design-html/skills/pptx-style-extract/scripts/test_logo_scope.py +148 -0
- package/steering/design-html/skills/pptx-style-extract/scripts/test_text_role_contract.py +151 -4
- package/steering/design-html/skills/pptx-style-extract/scripts/verify_logo_scope.py +188 -0
- package/steering/design-html/skills/pptx-style-extract/v2-format-spec.md +1 -1
|
@@ -5,18 +5,22 @@
|
|
|
5
5
|
|
|
6
6
|
产出 <stage1-outdir>/l-out/:
|
|
7
7
|
BRIEF.md 唯一必读简报:事实 + 草案依据 + 待判断清单
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
vision-group-*.jpg 候选图与所在页语境拼版
|
|
9
|
+
asset-vision-groups.json 候选来源、位置与预算索引
|
|
10
|
+
manifest.yaml / frontmatter.yaml / layout-controls.yaml / layouts.yaml / body.md
|
|
11
|
+
判断单与坐标事实,可直接进 package.py
|
|
10
12
|
|
|
11
13
|
草案里所有数值都来自 extract.json;凡是需要「像人一样看」才能定的,写成 `TODO:` 行
|
|
12
14
|
(package.py 见 TODO 即 FAIL),由 L 层改掉。
|
|
13
15
|
"""
|
|
14
16
|
import argparse
|
|
15
17
|
import copy
|
|
18
|
+
import glob
|
|
16
19
|
import json
|
|
17
20
|
import os
|
|
18
21
|
import re
|
|
19
22
|
import shutil
|
|
23
|
+
import subprocess
|
|
20
24
|
import sys
|
|
21
25
|
from collections import Counter, defaultdict
|
|
22
26
|
|
|
@@ -29,7 +33,18 @@ OPAQUE_ENOUGH = 128 # 能当背景的最低不透明度:低于半透明就
|
|
|
29
33
|
# 那是叠加装饰不是背景
|
|
30
34
|
FILL_MANY = 5 # 「被大量当填充铺开」的次数下限,用于区分卡片底与偶发用色
|
|
31
35
|
BG_CONTENT_CAP = 5 # 内容页背景收几张:再多消费端也挑不过来,超出的写进 TODO 交人取舍
|
|
32
|
-
|
|
36
|
+
SHEET_BATCH = 12 # 每张联系表最多 12 个候选;候选不截断,超出就继续生成下一张
|
|
37
|
+
CONTEXT_BATCH = 8 # 每张整页语境表最多 8 页;同页只渲染一次
|
|
38
|
+
# 与 studio_server_faas 的批量 vision 图数预算保持一致。单页大组允许 1 张整页图 +
|
|
39
|
+
# 9 张候选;多个小页合组时,所有整页图和候选图合计最多 5 张。
|
|
40
|
+
SINGLE_PAGE_IMAGE_BUDGET = 10
|
|
41
|
+
MULTI_PAGE_IMAGE_BUDGET = 5
|
|
42
|
+
# Skill 侧的总输入上限:限制模型需要读取的拼版数量和总视觉图数,而不是偷偷截断
|
|
43
|
+
# 中间产物。首页、尾页优先;其余超限候选在 gaps 中显式说明。
|
|
44
|
+
VISUAL_PACK_CAP = 5
|
|
45
|
+
VISUAL_INPUT_CAP = 30
|
|
46
|
+
VISUAL_PREVIEW_MAX_EDGE = 1200
|
|
47
|
+
VISUAL_JPEG_QUALITY = 82
|
|
33
48
|
|
|
34
49
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
35
50
|
SKILL_ROOT = os.path.dirname(HERE)
|
|
@@ -598,7 +613,13 @@ def lh_of(t):
|
|
|
598
613
|
|
|
599
614
|
# ---------------------------------------------------------------- 资产
|
|
600
615
|
def probe_image(path):
|
|
601
|
-
info = {
|
|
616
|
+
info = {
|
|
617
|
+
'w': None,
|
|
618
|
+
'h': None,
|
|
619
|
+
'alpha_mean': None,
|
|
620
|
+
'near_blank': False,
|
|
621
|
+
'near_white_ratio': None,
|
|
622
|
+
}
|
|
602
623
|
try:
|
|
603
624
|
from PIL import Image
|
|
604
625
|
except Exception:
|
|
@@ -606,15 +627,69 @@ def probe_image(path):
|
|
|
606
627
|
try:
|
|
607
628
|
im = Image.open(path)
|
|
608
629
|
info['w'], info['h'] = im.size
|
|
630
|
+
preview = im.convert('RGBA').resize((64, 64))
|
|
631
|
+
pixels = (preview.get_flattened_data()
|
|
632
|
+
if hasattr(preview, 'get_flattened_data') else preview.getdata())
|
|
633
|
+
px = list(pixels)
|
|
634
|
+
alpha = [item[3] for item in px]
|
|
609
635
|
if im.mode in ('RGBA', 'LA') or 'transparency' in im.info:
|
|
610
|
-
|
|
611
|
-
info['alpha_mean'] = sum(px) / len(px)
|
|
636
|
+
info['alpha_mean'] = sum(alpha) / len(alpha)
|
|
612
637
|
info['near_blank'] = info['alpha_mean'] < 13 # <5% 不透明度
|
|
638
|
+
visible = [item for item in px if item[3] >= 32]
|
|
639
|
+
if visible:
|
|
640
|
+
near_white = [item for item in visible
|
|
641
|
+
if item[0] >= 235 and item[1] >= 235 and item[2] >= 235]
|
|
642
|
+
info['near_white_ratio'] = round(len(near_white) / float(len(visible)), 3)
|
|
613
643
|
except Exception:
|
|
614
644
|
pass
|
|
615
645
|
return info
|
|
616
646
|
|
|
617
647
|
|
|
648
|
+
def needs_asset_judgment(candidate):
|
|
649
|
+
"""局部图和半透明满屏叠加层需要看图定性;不透明满屏图按背景处理。"""
|
|
650
|
+
effective_alpha = candidate.get('effective_alpha_mean')
|
|
651
|
+
if not candidate.get('fullscreen'):
|
|
652
|
+
return True
|
|
653
|
+
alpha = (effective_alpha if effective_alpha is not None
|
|
654
|
+
else (candidate.get('probe') or {}).get('alpha_mean'))
|
|
655
|
+
return alpha is not None and 13 <= alpha < OPAQUE_ENOUGH
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
def fullscreen_effective_alpha(data, outdir, shapes):
|
|
659
|
+
"""满屏图片的实际平均 alpha,包含图片文件 alpha 与 OOXML 形状透明度。"""
|
|
660
|
+
media_out = {row.get('media'): row.get('out') for row in data.get('media') or []
|
|
661
|
+
if row.get('media') and row.get('out')}
|
|
662
|
+
probed = {}
|
|
663
|
+
effective = {}
|
|
664
|
+
for shape in shapes:
|
|
665
|
+
media = shape.get('media')
|
|
666
|
+
if (shape.get('kind') != 'pic' or not media
|
|
667
|
+
or shape.get('w_pct', 0) < 95 or shape.get('h_pct', 0) < 95):
|
|
668
|
+
continue
|
|
669
|
+
if media not in probed:
|
|
670
|
+
out = media_out.get(media)
|
|
671
|
+
probe = probe_image(os.path.join(outdir, out)) if out else {}
|
|
672
|
+
probed[media] = probe.get('alpha_mean')
|
|
673
|
+
source_alpha = probed[media]
|
|
674
|
+
if source_alpha is None:
|
|
675
|
+
source_alpha = 255.0
|
|
676
|
+
try:
|
|
677
|
+
opacity = float(shape.get('opacity', 1.0))
|
|
678
|
+
except (TypeError, ValueError):
|
|
679
|
+
opacity = 1.0
|
|
680
|
+
alpha = source_alpha * max(0.0, min(opacity, 1.0))
|
|
681
|
+
effective[media] = min(effective.get(media, 255.0), alpha)
|
|
682
|
+
return effective
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
def fullscreen_overlay_media(data, outdir, shapes):
|
|
686
|
+
"""需要模型判断的满屏叠加层媒体。"""
|
|
687
|
+
return {
|
|
688
|
+
media for media, alpha in fullscreen_effective_alpha(data, outdir, shapes).items()
|
|
689
|
+
if 13 <= alpha < OPAQUE_ENOUGH
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
|
|
618
693
|
def bg_busy_map(path, canvas, cells=12):
|
|
619
694
|
"""把背景图切成网格,报每格的**局部对比度**(该格内亮度极差)。
|
|
620
695
|
|
|
@@ -692,7 +767,8 @@ def copy_logo_candidates(outdir, logo_pool):
|
|
|
692
767
|
return rows
|
|
693
768
|
|
|
694
769
|
|
|
695
|
-
def draft_assets(d, outdir, bg_needed=(), cover_media=None, bg_under=None
|
|
770
|
+
def draft_assets(d, outdir, bg_needed=(), cover_media=None, bg_under=None,
|
|
771
|
+
effective_alpha=None):
|
|
696
772
|
imgs = {i['media']: i for i in d['images']}
|
|
697
773
|
cluster_of = {}
|
|
698
774
|
for c in d.get('media_clusters', []):
|
|
@@ -709,16 +785,35 @@ def draft_assets(d, outdir, bg_needed=(), cover_media=None, bg_under=None):
|
|
|
709
785
|
boxes = img.get('boxes') or []
|
|
710
786
|
top = max(boxes, key=lambda b: b.get('count', 0)) if boxes else {}
|
|
711
787
|
parts = top.get('parts') or []
|
|
712
|
-
|
|
788
|
+
placements, seen_placements = [], set()
|
|
789
|
+
for cluster in boxes:
|
|
790
|
+
box = cluster.get('box') or {}
|
|
791
|
+
rounded = [round(box.get(key, 0)) for key in ('x', 'y', 'w', 'h')]
|
|
792
|
+
for part in cluster.get('parts') or []:
|
|
793
|
+
if '/slides/' in part:
|
|
794
|
+
row = {'slide': slide_no(part), 'box': rounded}
|
|
795
|
+
elif '/slideLayouts/' in part:
|
|
796
|
+
row = {'layout': os.path.basename(part), 'box': rounded}
|
|
797
|
+
else:
|
|
798
|
+
continue
|
|
799
|
+
key = (row.get('slide'), row.get('layout'), tuple(rounded))
|
|
800
|
+
if key not in seen_placements:
|
|
801
|
+
seen_placements.add(key)
|
|
802
|
+
placements.append(row)
|
|
803
|
+
placements.sort(key=lambda row: (
|
|
804
|
+
row.get('slide', 9999), row.get('layout', ''), tuple(row['box'])))
|
|
805
|
+
slides = sorted({row['slide'] for row in placements if row.get('slide')})
|
|
713
806
|
cands.append({
|
|
714
807
|
'media': m['media'], 'file': os.path.basename(out_rel), 'out': out_rel,
|
|
715
808
|
'bytes': m.get('bytes'), 'n': img.get('n', m.get('used_n', 0)),
|
|
716
809
|
'has_compressed': bool(m.get('compressed_out')),
|
|
717
810
|
'fullscreen': bool(img.get('fullscreen')), 'w_pct': img.get('max_w_pct', 0),
|
|
718
811
|
'box': top.get('box') or {}, 'slides': slides,
|
|
812
|
+
'placements': placements,
|
|
719
813
|
'layer_only': bool(parts) and not slides,
|
|
720
814
|
'repeat': bool(img.get('repeat_fixed')),
|
|
721
815
|
'cluster': cluster_of.get(m['media']),
|
|
816
|
+
'effective_alpha_mean': (effective_alpha or {}).get(m['media']),
|
|
722
817
|
'probe': probe, 'reasons': m.get('reasons', []),
|
|
723
818
|
})
|
|
724
819
|
|
|
@@ -745,14 +840,18 @@ def draft_assets(d, outdir, bg_needed=(), cover_media=None, bg_under=None):
|
|
|
745
840
|
bg_i = 0
|
|
746
841
|
canvas_w, canvas_h = d['canvas']['px']
|
|
747
842
|
for c in kept:
|
|
748
|
-
|
|
749
|
-
|
|
843
|
+
effective_am = c.get('effective_alpha_mean')
|
|
844
|
+
if (c['fullscreen'] and (c['probe'].get('near_blank')
|
|
845
|
+
or (effective_am is not None and effective_am < 13))):
|
|
846
|
+
rejected.append((c, '近全透明(alpha 均值 %.0f/255),PPT 里看不见'
|
|
847
|
+
% (effective_am if effective_am is not None
|
|
848
|
+
else c['probe']['alpha_mean'])))
|
|
750
849
|
continue
|
|
751
850
|
# 铺满 ≠ 能当背景。背景的定义性属性是**遮盖**:它得挡住底下的东西。一张大半透明
|
|
752
851
|
# 的图铺满整页也遮不住任何像素,它在 PPT 里是叠在幻灯片底色上的一层装饰(顶部
|
|
753
852
|
# 光晕之类),底色才是真背景。实测某模板一张 alpha 均值 30/255、72% 完全透明的
|
|
754
853
|
# 顶部光晕被当成满屏背景收进包,消费端每页铺它,顶部就多出一条原稿没有的浓色带。
|
|
755
|
-
am = c['probe'].get('alpha_mean')
|
|
854
|
+
am = effective_am if effective_am is not None else c['probe'].get('alpha_mean')
|
|
756
855
|
if c['fullscreen'] and am is not None and am < OPAQUE_ENOUGH:
|
|
757
856
|
rejected.append((c, 'alpha 均值只有 %.0f/255,遮不住底下的东西——'
|
|
758
857
|
'它是叠在底色上的装饰层,不是背景' % am))
|
|
@@ -816,13 +915,13 @@ def draft_assets(d, outdir, bg_needed=(), cover_media=None, bg_under=None):
|
|
|
816
915
|
for i, (corner, c) in enumerate(logo_pool):
|
|
817
916
|
b = c['box']
|
|
818
917
|
if i == 0:
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
918
|
+
# 贴角、重复只能说明“像 logo”,不能替模型判定。比如一张产品功能角标也会
|
|
919
|
+
# 同时满足这些结构特征;先作为候选保留在联系表与图片槽中,由模型定为 logo
|
|
920
|
+
# 或 content,避免把内容图直接带进风格包。
|
|
921
|
+
rejected.append((c, '贴角重复小图候选(%.0fx%.0f @ %.0f,%.0f,出现 %d 次,'
|
|
922
|
+
'离画布边 %.0f%%),结合样张判断 logo 或 content'
|
|
923
|
+
% (b.get('w', 0), b.get('h', 0), b.get('x', 0), b.get('y', 0),
|
|
924
|
+
c['n'], corner * 50)))
|
|
826
925
|
else:
|
|
827
926
|
rejected.append((c, '重复小图(%.0fx%.0f @ %.0f,%.0f),贴角程度 %.0f%% 不如首选'
|
|
828
927
|
% (b.get('w', 0), b.get('h', 0), b.get('x', 0), b.get('y', 0),
|
|
@@ -854,6 +953,34 @@ def draft_assets(d, outdir, bg_needed=(), cover_media=None, bg_under=None):
|
|
|
854
953
|
return assets, rejected, todos, alias, {c['media']: c for c in kept}
|
|
855
954
|
|
|
856
955
|
|
|
956
|
+
def cover_background_media(archetypes):
|
|
957
|
+
"""只从模板明确命名的 cover 页型读取封面背景。"""
|
|
958
|
+
return next((archetype['bg_raw'] for archetype in archetypes
|
|
959
|
+
if archetype['name'] == 'cover'), None)
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
def background_decor(background, canvas):
|
|
963
|
+
"""把可直接重放的 PPT 背景声明落到 layouts.md 的最底层装饰。
|
|
964
|
+
|
|
965
|
+
图片背景继续走 `background:` 资产引用;纯色和线性渐变没有可复制的素材文件,
|
|
966
|
+
但同样是页型视觉的一部分,必须随页型输出。path 渐变不能由 CSS 线性渐变准确表达,
|
|
967
|
+
保留给图片/渲染链路而不伪造。
|
|
968
|
+
"""
|
|
969
|
+
if not isinstance(background, dict):
|
|
970
|
+
return None
|
|
971
|
+
if background.get('type') not in ('solid', 'gradient'):
|
|
972
|
+
return None
|
|
973
|
+
if background.get('path'):
|
|
974
|
+
return None
|
|
975
|
+
css = _load_query()._recipe_css(background, None, [], [])
|
|
976
|
+
css = [re.sub(r'\s*\n\s*', ' ', value).strip() for value in css if value]
|
|
977
|
+
if not css:
|
|
978
|
+
return None
|
|
979
|
+
width, height = canvas
|
|
980
|
+
return {'box': [0, 0, width, height], 'geom': 'rect', 'css': '; '.join(css),
|
|
981
|
+
'trace': 'canvas-background'}
|
|
982
|
+
|
|
983
|
+
|
|
857
984
|
# ---------------------------------------------------------------- 版式聚类
|
|
858
985
|
DECOR_MIN = 40.0
|
|
859
986
|
|
|
@@ -951,19 +1078,21 @@ def slot_style(s):
|
|
|
951
1078
|
不会写进消费者产物。
|
|
952
1079
|
"""
|
|
953
1080
|
txt = s.get('text') or {}
|
|
954
|
-
|
|
1081
|
+
inherited = dict((txt.get('lstStyle') or {}).get('lvl1pPr') or {})
|
|
1082
|
+
ls = {}
|
|
955
1083
|
# 四层逐级兜底,按 OOXML 的就近原则:run rPr → 段落 defRPr → 段落 pPr → lstStyle。
|
|
956
1084
|
# 只枚举前几层会整份漏掉——有的导出器把字号全写在 run rPr 上,lstStyle 一个都没有。
|
|
957
1085
|
for para in (txt.get('paragraphs') or []):
|
|
958
|
-
srcs = [r
|
|
1086
|
+
srcs = [r for r in (para.get('runs') or [])]
|
|
959
1087
|
srcs.append(para.get('defRPr') or {})
|
|
960
1088
|
srcs.append({k: v for k, v in para.items() if k not in ('runs', 'defRPr')})
|
|
961
1089
|
for src in srcs:
|
|
962
1090
|
for k, v in (src or {}).items():
|
|
963
1091
|
if v is not None:
|
|
964
1092
|
ls.setdefault(k, v)
|
|
965
|
-
|
|
966
|
-
|
|
1093
|
+
for k, v in inherited.items():
|
|
1094
|
+
if v is not None:
|
|
1095
|
+
ls.setdefault(k, v)
|
|
967
1096
|
if not ls.get('sz_px'):
|
|
968
1097
|
# 仍无声明:退到整形状里出现过的最大字号(generic walk),仍是文件里的值
|
|
969
1098
|
anysz = shape_sz(s)
|
|
@@ -982,9 +1111,16 @@ def slot_style(s):
|
|
|
982
1111
|
css_number(insets.get('lIns', 0) or 0),
|
|
983
1112
|
))
|
|
984
1113
|
if ls.get('sz_px'):
|
|
985
|
-
|
|
1114
|
+
# normAutofit 的 fontScale 是模板让大字装进小框的手段——不乘它,消费端拿到的是
|
|
1115
|
+
# 未缩放字号,字比框高,渐变裁切会把溢出的底部切成透明。缺省 1.0(无 autofit / 无缩放)。
|
|
1116
|
+
scale = body.get('font_scale')
|
|
1117
|
+
raw = ls['sz_px'] * scale if scale else ls['sz_px']
|
|
1118
|
+
size = round(raw)
|
|
986
1119
|
css.append('font-size: %dpx' % size)
|
|
987
1120
|
out['_font_size'] = size
|
|
1121
|
+
typeface = ls.get('ea') or ls.get('latin') or ls.get('cs')
|
|
1122
|
+
if typeface:
|
|
1123
|
+
css.append('font-family: %s' % font_css([typeface]))
|
|
988
1124
|
weight = ls.get('weight') or (700 if ls.get('bold') else None)
|
|
989
1125
|
if weight:
|
|
990
1126
|
css.append('font-weight: %s' % weight)
|
|
@@ -1020,10 +1156,13 @@ def slot_style(s):
|
|
|
1020
1156
|
'l': 'left', 'ctr': 'center', 'r': 'right', 'just': 'justify',
|
|
1021
1157
|
}.get(align, align))
|
|
1022
1158
|
line_spacing = ls.get('lnSpc') or {}
|
|
1159
|
+
# normAutofit 的 lnSpcReduction 与 fontScale 同时把行距压缩,一起缩才装得进原框。
|
|
1160
|
+
reduction = body.get('ln_spc_reduction') or 0
|
|
1023
1161
|
if line_spacing.get('mult'):
|
|
1024
|
-
|
|
1162
|
+
mult = line_spacing['mult'] * 1.2 * (1 - reduction)
|
|
1163
|
+
css.append('line-height: %s' % css_number(mult))
|
|
1025
1164
|
elif line_spacing.get('px'):
|
|
1026
|
-
css.append('line-height: %spx' % css_number(line_spacing['px']))
|
|
1165
|
+
css.append('line-height: %spx' % css_number(line_spacing['px'] * (1 - reduction)))
|
|
1027
1166
|
anchor = body.get('anchor')
|
|
1028
1167
|
if anchor in ('ctr', 'b'):
|
|
1029
1168
|
css += ['display: flex', 'flex-direction: column',
|
|
@@ -1094,6 +1233,9 @@ def layouts_from_template(d, shapes, cW, cH):
|
|
|
1094
1233
|
lay_of_slide = (d.get('reference_graph') or {}).get('layout_of_slide') or {}
|
|
1095
1234
|
used_n = Counter(lay_of_slide.values())
|
|
1096
1235
|
slide_of_layout = {lp: sp for sp, lp in lay_of_slide.items() if used_n[lp] == 1}
|
|
1236
|
+
sample_pages_of_layout = defaultdict(list)
|
|
1237
|
+
for slide_part, layout_part in lay_of_slide.items():
|
|
1238
|
+
sample_pages_of_layout[layout_part].append(slide_no(slide_part))
|
|
1097
1239
|
default_theme = topo.get('default')
|
|
1098
1240
|
multi = len(topo.get('themes') or []) > 1
|
|
1099
1241
|
|
|
@@ -1106,7 +1248,7 @@ def layouts_from_template(d, shapes, cW, cH):
|
|
|
1106
1248
|
phs.sort(key=lambda s: ((s['box'].get('y') or 0), (s['box'].get('x') or 0)))
|
|
1107
1249
|
slots, seen_kind = [], set()
|
|
1108
1250
|
for s in phs:
|
|
1109
|
-
t = PH_TO_TYPE.get((s
|
|
1251
|
+
t = PH_TO_TYPE.get((s.get('ph') or {}).get('type'), 'body')
|
|
1110
1252
|
if t in ('slide-number', 'footer') and not shape_text(s):
|
|
1111
1253
|
continue # 空 chrome 占位符不是实际元素
|
|
1112
1254
|
b = s['box']
|
|
@@ -1150,7 +1292,17 @@ def layouts_from_template(d, shapes, cW, cH):
|
|
|
1150
1292
|
slots, bgm = instance_override(
|
|
1151
1293
|
shapes, inst, slots, bgm, cW, cH, composites)
|
|
1152
1294
|
taken = {tuple(s['box']) for s in slots}
|
|
1153
|
-
decor =
|
|
1295
|
+
decor = []
|
|
1296
|
+
inherited_background = l.get('background')
|
|
1297
|
+
direct_background = next(
|
|
1298
|
+
(slide.get('background') for slide in d.get('slides') or []
|
|
1299
|
+
if slide.get('part') == inst and slide.get('background')),
|
|
1300
|
+
None)
|
|
1301
|
+
background = direct_background or inherited_background
|
|
1302
|
+
background_layer = background_decor(background, (cW, cH))
|
|
1303
|
+
if background_layer:
|
|
1304
|
+
decor.append(background_layer)
|
|
1305
|
+
decor += collect_decor(shapes, inst or l['part'], taken, (cW, cH))
|
|
1154
1306
|
named_role = role_of_name(l.get('name'))
|
|
1155
1307
|
rows.append({'zh': clean_layout_name(l.get('name')),
|
|
1156
1308
|
'role': named_role or 'content', 'role_guessed': named_role is None,
|
|
@@ -1186,6 +1338,8 @@ def layouts_from_template(d, shapes, cW, cH):
|
|
|
1186
1338
|
# 版式名认不出 role 时不装作有把握:置信度降到 low,让 L 层看图定
|
|
1187
1339
|
'pic_n': 0, 'confidence': 'low' if r.get('role_guessed') else 'high',
|
|
1188
1340
|
'theme': r['theme'] if multi else None,
|
|
1341
|
+
'_layout_part': r['part'],
|
|
1342
|
+
'_sample_pages': sorted(sample_pages_of_layout.get(r['part']) or []),
|
|
1189
1343
|
'source': 'layout:' + r['part'].split('/')[-1]})
|
|
1190
1344
|
return arch
|
|
1191
1345
|
|
|
@@ -1301,13 +1455,79 @@ def inherited_text_shapes(layout_shapes, slide_shapes):
|
|
|
1301
1455
|
return out
|
|
1302
1456
|
|
|
1303
1457
|
|
|
1304
|
-
def
|
|
1458
|
+
def slide_image_marks(data, included_fullscreen=()):
|
|
1459
|
+
"""从图片普查补齐形状图片填充;它们没有独立 pic 节点,但仍有媒体与坐标。"""
|
|
1460
|
+
allowed_fullscreen = set(included_fullscreen)
|
|
1461
|
+
out = defaultdict(list)
|
|
1462
|
+
for image in data.get('images') or []:
|
|
1463
|
+
media = image.get('media')
|
|
1464
|
+
if not media or (image.get('fullscreen') and media not in allowed_fullscreen):
|
|
1465
|
+
continue
|
|
1466
|
+
for cluster in image.get('boxes') or []:
|
|
1467
|
+
box = cluster.get('box')
|
|
1468
|
+
if not box or not box.get('w'):
|
|
1469
|
+
continue
|
|
1470
|
+
for part in cluster.get('parts') or []:
|
|
1471
|
+
if '/slides/' not in part and '/slideLayouts/' not in part:
|
|
1472
|
+
continue
|
|
1473
|
+
out[part].append({'media': media, 'box': box})
|
|
1474
|
+
return out
|
|
1475
|
+
|
|
1476
|
+
|
|
1477
|
+
def add_template_image_marks(archetypes, data, included_fullscreen=()):
|
|
1478
|
+
"""把版式和实例页的图片填充补进 form=3 页型。"""
|
|
1479
|
+
marks_by_part = slide_image_marks(data, included_fullscreen)
|
|
1480
|
+
layout_of_slide = (data.get('reference_graph') or {}).get('layout_of_slide') or {}
|
|
1481
|
+
by_layout = {archetype.get('_layout_part'): archetype for archetype in archetypes}
|
|
1482
|
+
for part, marks in marks_by_part.items():
|
|
1483
|
+
layout_part = layout_of_slide.get(part, part)
|
|
1484
|
+
archetype = by_layout.get(layout_part)
|
|
1485
|
+
if not archetype:
|
|
1486
|
+
continue
|
|
1487
|
+
seen = {
|
|
1488
|
+
(slot.get('media'), tuple(slot.get('box') or ()))
|
|
1489
|
+
for slot in archetype.get('slots') or []
|
|
1490
|
+
if slot.get('media')
|
|
1491
|
+
}
|
|
1492
|
+
for mark in marks:
|
|
1493
|
+
box = mark['box']
|
|
1494
|
+
rounded = [round(box.get(key, 0)) for key in ('x', 'y', 'w', 'h')]
|
|
1495
|
+
key = (mark['media'], tuple(rounded))
|
|
1496
|
+
if key in seen:
|
|
1497
|
+
continue
|
|
1498
|
+
seen.add(key)
|
|
1499
|
+
archetype['slots'].append({
|
|
1500
|
+
'role': 'logo',
|
|
1501
|
+
'type': 'pic',
|
|
1502
|
+
'sz': 0,
|
|
1503
|
+
'txt': '',
|
|
1504
|
+
'media': mark['media'],
|
|
1505
|
+
'box': rounded,
|
|
1506
|
+
})
|
|
1507
|
+
|
|
1508
|
+
|
|
1509
|
+
def preserve_image_bearing_groups(kept, ranked):
|
|
1510
|
+
"""有图片实例的孤例保留自己的页型,避免把资产绑定到近似但错误的版式。"""
|
|
1511
|
+
return kept + [
|
|
1512
|
+
group for group in ranked
|
|
1513
|
+
if group not in kept and any(page.get('marks') for page in group[1])
|
|
1514
|
+
]
|
|
1515
|
+
|
|
1516
|
+
|
|
1517
|
+
def draft_layouts(d, outdir, effective_alpha=None):
|
|
1305
1518
|
with open(os.path.join(outdir, 'ref', 'shapes.json'), encoding='utf-8') as stream:
|
|
1306
1519
|
shapes = json.load(stream)['shapes']
|
|
1307
1520
|
cW, cH = d['canvas']['px']
|
|
1521
|
+
if effective_alpha is None:
|
|
1522
|
+
effective_alpha = fullscreen_effective_alpha(d, outdir, shapes)
|
|
1523
|
+
overlay_media = {
|
|
1524
|
+
media for media, alpha in effective_alpha.items()
|
|
1525
|
+
if 13 <= alpha < OPAQUE_ENOUGH
|
|
1526
|
+
}
|
|
1308
1527
|
if (d.get('form_hint') or {}).get('form') == 3:
|
|
1309
1528
|
arch = layouts_from_template(d, shapes, cW, cH)
|
|
1310
1529
|
if len(arch) >= 3:
|
|
1530
|
+
add_template_image_marks(arch, d, overlay_media)
|
|
1311
1531
|
return arch, [], []
|
|
1312
1532
|
by_slide = defaultdict(list)
|
|
1313
1533
|
by_layout = defaultdict(list)
|
|
@@ -1316,15 +1536,16 @@ def draft_layouts(d, outdir):
|
|
|
1316
1536
|
by_slide[s['part']].append(s)
|
|
1317
1537
|
elif s.get('layer') == 'layout':
|
|
1318
1538
|
by_layout[s['part']].append(s)
|
|
1539
|
+
image_marks = slide_image_marks(d, overlay_media)
|
|
1319
1540
|
|
|
1320
1541
|
bg_of_slide, layout_of_slide = {}, {}
|
|
1542
|
+
bg_of_layout = {row['part']: row.get('background') for row in d.get('layouts') or []}
|
|
1321
1543
|
for s in d.get('slides', []):
|
|
1322
1544
|
bg = s.get('background')
|
|
1323
1545
|
bg_of_slide[s['part']] = json.dumps(bg, sort_keys=True) if isinstance(bg, dict) else bg
|
|
1324
1546
|
layout_of_slide[s['part']] = s.get('layout')
|
|
1325
1547
|
# 版式层的满屏底图(form=2 常态:底图挂在 layout 上)
|
|
1326
1548
|
composites = d.get('background_composites') or {}
|
|
1327
|
-
bg_of_layout = {}
|
|
1328
1549
|
for s in shapes:
|
|
1329
1550
|
if s.get('layer') == 'layout' and is_bleed(s) and s.get('media'):
|
|
1330
1551
|
bg_of_layout[s['part']] = s['media']
|
|
@@ -1360,13 +1581,32 @@ def draft_layouts(d, outdir):
|
|
|
1360
1581
|
})
|
|
1361
1582
|
texts.sort(key=lambda t: (-t['sz'], t['box'].get('y', 0)))
|
|
1362
1583
|
visible_shapes = layout_shapes + sh
|
|
1363
|
-
pics = [
|
|
1584
|
+
pics = []
|
|
1585
|
+
for shape in visible_shapes:
|
|
1586
|
+
if shape.get('kind') != 'pic':
|
|
1587
|
+
continue
|
|
1588
|
+
if shape.get('w_pct', 0) < 95 or shape.get('media') in overlay_media:
|
|
1589
|
+
pics.append(shape)
|
|
1364
1590
|
# 小图元素(logo / 角标 / 装饰)逐页记位置,供 archetype 落 slots
|
|
1365
1591
|
marks = [{'media': s['media'], 'box': s['box']} for s in pics
|
|
1366
|
-
if s.get('media') and (s.get('box') or {}).get('w')
|
|
1592
|
+
if s.get('media') and (s.get('box') or {}).get('w')]
|
|
1593
|
+
seen_marks = {
|
|
1594
|
+
(mark['media'], round(mark['box'].get('x', 0)), round(mark['box'].get('y', 0)))
|
|
1595
|
+
for mark in marks
|
|
1596
|
+
}
|
|
1597
|
+
for mark in image_marks.get(part) or []:
|
|
1598
|
+
key = (mark['media'], round(mark['box'].get('x', 0)),
|
|
1599
|
+
round(mark['box'].get('y', 0)))
|
|
1600
|
+
if key not in seen_marks:
|
|
1601
|
+
seen_marks.add(key)
|
|
1602
|
+
marks.append(mark)
|
|
1603
|
+
background = next((s.get('background') for s in d.get('slides') or []
|
|
1604
|
+
if s.get('part') == part and s.get('background')), None)
|
|
1605
|
+
background = background or bg_of_layout.get(layout_part)
|
|
1367
1606
|
pages.append({'part': part, 'no': slide_no(part), 'bg_media': bg_media,
|
|
1368
1607
|
'rendered_bg': rendered_bg,
|
|
1369
|
-
'bg_color': bg_of_slide.get(part), '
|
|
1608
|
+
'bg_color': bg_of_slide.get(part), 'background': background,
|
|
1609
|
+
'texts': texts, 'pic_n': len(pics),
|
|
1370
1610
|
'marks': marks, 'shape_n': len(visible_shapes), 'layout': layout_part})
|
|
1371
1611
|
|
|
1372
1612
|
# 页型的**角色**(封面 / 章节页 / 内容页……)不在这里判:那是看图才能下的结论,
|
|
@@ -1379,6 +1619,7 @@ def draft_layouts(d, outdir):
|
|
|
1379
1619
|
n = len(p['texts'])
|
|
1380
1620
|
return 0 if n <= q1 else (1 if n <= q2 else 2)
|
|
1381
1621
|
|
|
1622
|
+
last_page_no = max((p['no'] for p in pages), default=None)
|
|
1382
1623
|
groups = defaultdict(list)
|
|
1383
1624
|
for p in pages:
|
|
1384
1625
|
if p['no'] == 1:
|
|
@@ -1386,18 +1627,30 @@ def draft_layouts(d, outdir):
|
|
|
1386
1627
|
# 并进别的组就会被代表页顶掉、坐标全丢。这只是不合并,不代表它是封面。
|
|
1387
1628
|
groups[('__first__', -1)] = [p]
|
|
1388
1629
|
continue
|
|
1630
|
+
if p['no'] == last_page_no:
|
|
1631
|
+
# 末页也单独保留完整结构:它可能是封底,也可能只是最后一张内容页,脚本
|
|
1632
|
+
# 不替模型下结论。和首页一样,拆组只避免它被聚类代表页吞掉。
|
|
1633
|
+
groups[('__last__', -2)] = [p]
|
|
1634
|
+
continue
|
|
1389
1635
|
groups[(p['bg_media'] or p['bg_color'] or 'none', density_band(p))].append(p)
|
|
1390
1636
|
|
|
1391
1637
|
ranked = sorted(groups.items(), key=lambda kv: (-len(kv[1]), kv[1][0]['no']))
|
|
1392
1638
|
# 首页所在的组一定收——deck 的第一页是模板的门面,孤例也不能被名额挤掉。
|
|
1393
1639
|
# 这只保证它进包,它是不是封面由看图的人定。
|
|
1394
1640
|
first = [g for g in ranked if g[0][0] == '__first__']
|
|
1395
|
-
|
|
1641
|
+
last = [g for g in ranked if g[0][0] == '__last__']
|
|
1642
|
+
kept = first + last + [
|
|
1643
|
+
g for g in ranked
|
|
1644
|
+
if g not in first and g not in last and len(g[1]) >= 2
|
|
1645
|
+
][:max(0, 8 - len(first) - len(last))]
|
|
1396
1646
|
for g in ranked: # 名额没用满就把最大的孤例页也收进来
|
|
1397
1647
|
if len(kept) >= 8:
|
|
1398
1648
|
break
|
|
1399
1649
|
if g not in kept:
|
|
1400
1650
|
kept.append(g)
|
|
1651
|
+
# 图片用途必须与它实际所在的版式绑定。若把图片孤例并到“最接近”页型,装饰会被
|
|
1652
|
+
# 绑定到错误布局;是否为内容图、logo 墙或装饰由后续模型看图判断,不按图片数量猜。
|
|
1653
|
+
kept = preserve_image_bearing_groups(kept, ranked)
|
|
1401
1654
|
leftover = sorted(p['no'] for g in ranked if g not in kept for p in g[1])
|
|
1402
1655
|
|
|
1403
1656
|
archetypes = []
|
|
@@ -1405,6 +1658,8 @@ def draft_layouts(d, outdir):
|
|
|
1405
1658
|
rep = max(ps, key=lambda p: len(p['texts']))
|
|
1406
1659
|
if bg_raw == '__first__':
|
|
1407
1660
|
bg_raw = rep['bg_media'] or rep['bg_color'] or 'none'
|
|
1661
|
+
elif bg_raw == '__last__':
|
|
1662
|
+
bg_raw = rep['bg_media'] or rep['bg_color'] or 'none'
|
|
1408
1663
|
rendered_bg = rep.get('rendered_bg')
|
|
1409
1664
|
if rendered_bg:
|
|
1410
1665
|
bg_raw = rendered_bg
|
|
@@ -1450,20 +1705,25 @@ def draft_layouts(d, outdir):
|
|
|
1450
1705
|
'_placeholder': t.get('placeholder'),
|
|
1451
1706
|
})
|
|
1452
1707
|
slots.append(row)
|
|
1453
|
-
#
|
|
1708
|
+
# 同组页面上的图片元素按素材+位置去重后落候选 slots。内容图去掉具体资产引用,
|
|
1709
|
+
# 保留通用图片槽;装饰图绑定资产,避免非代表页上的装饰没有进入 layouts。
|
|
1454
1710
|
seen_mark = set()
|
|
1455
|
-
for
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1711
|
+
for page in ps:
|
|
1712
|
+
for mk in page.get('marks') or []:
|
|
1713
|
+
b = mk['box']
|
|
1714
|
+
key = (mk['media'], round(b.get('x', 0)), round(b.get('y', 0)))
|
|
1715
|
+
if key in seen_mark:
|
|
1716
|
+
continue
|
|
1717
|
+
seen_mark.add(key)
|
|
1718
|
+
slots.append({'role': 'logo', 'type': 'pic', 'sz': 0, 'txt': '',
|
|
1719
|
+
'media': mk['media'],
|
|
1720
|
+
'box': [round(b.get('x', 0)), round(b.get('y', 0)),
|
|
1721
|
+
round(b.get('w', 0)), round(b.get('h', 0))]})
|
|
1465
1722
|
taken = {tuple(s['box']) for s in slots}
|
|
1466
1723
|
decor = []
|
|
1724
|
+
background_layer = background_decor(rep.get('background'), (cW, cH))
|
|
1725
|
+
if background_layer:
|
|
1726
|
+
decor.append(background_layer)
|
|
1467
1727
|
seen_decor = set()
|
|
1468
1728
|
for source_part in (rep.get('layout'), rep['part']):
|
|
1469
1729
|
for item in collect_decor(shapes, source_part, taken, (cW, cH)):
|
|
@@ -1475,6 +1735,16 @@ def draft_layouts(d, outdir):
|
|
|
1475
1735
|
'decor': decor,
|
|
1476
1736
|
'pages': sorted(p['no'] for p in ps), 'rep': rep['no'],
|
|
1477
1737
|
'pic_n': rep['pic_n'],
|
|
1738
|
+
'_source_layouts': sorted({
|
|
1739
|
+
p['layout'] for p in ps if p.get('layout')
|
|
1740
|
+
}),
|
|
1741
|
+
'_source_backgrounds': sorted({
|
|
1742
|
+
p.get('rendered_bg') or p.get('bg_media') or p.get('bg_color')
|
|
1743
|
+
for p in ps
|
|
1744
|
+
if p.get('rendered_bg') or p.get('bg_media') or p.get('bg_color')
|
|
1745
|
+
}),
|
|
1746
|
+
'_text_n': len(rep['texts']),
|
|
1747
|
+
'_last_page_candidate': rep['no'] == last_page_no,
|
|
1478
1748
|
'confidence': 'high' if len(ps) >= 3 else
|
|
1479
1749
|
('medium' if len(ps) == 2 else 'low')})
|
|
1480
1750
|
return archetypes, pages, leftover
|
|
@@ -1488,13 +1758,19 @@ def layout_sheet(outdir, archetypes, path):
|
|
|
1488
1758
|
reps = [x for x in reps if x is not None]
|
|
1489
1759
|
if not reps:
|
|
1490
1760
|
return None
|
|
1491
|
-
import subprocess
|
|
1492
|
-
r = subprocess.run([sys.executable, os.path.join(HERE, 'render_pages.py'), outdir,
|
|
1493
|
-
'--pages', 'layouts' if use_layout else 'slides',
|
|
1494
|
-
'--only', ','.join(map(str, reps)), '--no-html'],
|
|
1495
|
-
capture_output=True, text=True)
|
|
1496
1761
|
png_dir = os.path.join(outdir, 'ref', 'rebuild', 'png')
|
|
1497
|
-
if
|
|
1762
|
+
kind = 'layout' if use_layout else 'slide'
|
|
1763
|
+
missing = [no for no in reps
|
|
1764
|
+
if not os.path.exists(os.path.join(png_dir, '%s-%s.png' % (kind, no)))]
|
|
1765
|
+
if missing:
|
|
1766
|
+
import subprocess
|
|
1767
|
+
r = subprocess.run([sys.executable, os.path.join(HERE, 'render_pages.py'), outdir,
|
|
1768
|
+
'--pages', 'layouts' if use_layout else 'slides',
|
|
1769
|
+
'--only', ','.join(map(str, missing)), '--no-html'],
|
|
1770
|
+
capture_output=True, text=True)
|
|
1771
|
+
if r.returncode:
|
|
1772
|
+
return None
|
|
1773
|
+
if not os.path.isdir(png_dir):
|
|
1498
1774
|
return None
|
|
1499
1775
|
try:
|
|
1500
1776
|
from PIL import Image, ImageDraw
|
|
@@ -1510,7 +1786,7 @@ def layout_sheet(outdir, archetypes, path):
|
|
|
1510
1786
|
x = pad + (i % cols) * (cw + pad)
|
|
1511
1787
|
y = pad + (i // cols) * (ch + pad + lab)
|
|
1512
1788
|
no = a.get('rep_layout') if use_layout else a.get('rep')
|
|
1513
|
-
f = os.path.join(png_dir, '%s-%s.png' % (
|
|
1789
|
+
f = os.path.join(png_dir, '%s-%s.png' % (kind, no))
|
|
1514
1790
|
if os.path.exists(f):
|
|
1515
1791
|
im = Image.open(f).convert('RGB')
|
|
1516
1792
|
im.thumbnail((cw, ch))
|
|
@@ -1527,7 +1803,7 @@ def layout_sheet(outdir, archetypes, path):
|
|
|
1527
1803
|
return path
|
|
1528
1804
|
|
|
1529
1805
|
|
|
1530
|
-
def contact_sheet(outdir, cands, path):
|
|
1806
|
+
def contact_sheet(outdir, cands, path, start_index=1):
|
|
1531
1807
|
try:
|
|
1532
1808
|
from PIL import Image, ImageDraw
|
|
1533
1809
|
except Exception:
|
|
@@ -1557,19 +1833,548 @@ def contact_sheet(outdir, cands, path):
|
|
|
1557
1833
|
dr.text((x + 8, y + 8), 'unreadable', fill=(200, 0, 0))
|
|
1558
1834
|
dr.rectangle([x, y, x + cell, y + cell], outline=(120, 120, 128))
|
|
1559
1835
|
dr.text((x + 2, y + cell + 4), '[%d] %s %dx%d used=%d'
|
|
1560
|
-
% (
|
|
1836
|
+
% (c.get('_candidate_index', start_index + idx), c['file'],
|
|
1837
|
+
c['probe'].get('w') or 0,
|
|
1838
|
+
c['probe'].get('h') or 0, c['n']),
|
|
1561
1839
|
fill=(20, 20, 24))
|
|
1562
1840
|
sheet.save(path, optimize=True)
|
|
1563
1841
|
return path
|
|
1564
1842
|
|
|
1565
1843
|
|
|
1844
|
+
def contact_sheets(outdir, cands, ldir):
|
|
1845
|
+
paths = []
|
|
1846
|
+
legacy = os.path.join(ldir, 'contact-sheet.png')
|
|
1847
|
+
if os.path.exists(legacy):
|
|
1848
|
+
os.remove(legacy)
|
|
1849
|
+
for start in range(0, len(cands), SHEET_BATCH):
|
|
1850
|
+
batch = cands[start:start + SHEET_BATCH]
|
|
1851
|
+
path = os.path.join(ldir, 'contact-sheet-%d.png' % (start // SHEET_BATCH + 1))
|
|
1852
|
+
if contact_sheet(outdir, batch, path, start + 1):
|
|
1853
|
+
paths.append(path)
|
|
1854
|
+
if paths:
|
|
1855
|
+
shutil.copy2(paths[0], legacy)
|
|
1856
|
+
return paths
|
|
1857
|
+
|
|
1858
|
+
|
|
1859
|
+
def asset_vision_contexts(candidates):
|
|
1860
|
+
"""把同一素材的每个归纳页型实例放进对应语境,不只展示最早出现的页面。"""
|
|
1861
|
+
contexts = []
|
|
1862
|
+
for candidate in candidates:
|
|
1863
|
+
placements = candidate.get('placements') or []
|
|
1864
|
+
instance_placements = [row for row in placements if row.get('slide')]
|
|
1865
|
+
if not instance_placements:
|
|
1866
|
+
row = dict(candidate)
|
|
1867
|
+
row['source_placements'] = placements
|
|
1868
|
+
contexts.append(row)
|
|
1869
|
+
continue
|
|
1870
|
+
seen = set()
|
|
1871
|
+
id_counts = Counter()
|
|
1872
|
+
for placement in instance_placements:
|
|
1873
|
+
box = tuple(placement['box'])
|
|
1874
|
+
layout = placement.get('archetype')
|
|
1875
|
+
key = (layout, box) if layout else (placement['slide'], box)
|
|
1876
|
+
if key in seen:
|
|
1877
|
+
continue
|
|
1878
|
+
seen.add(key)
|
|
1879
|
+
row = dict(candidate)
|
|
1880
|
+
base_id = '%s-s%d' % (candidate['id'], placement['slide'])
|
|
1881
|
+
id_counts[base_id] += 1
|
|
1882
|
+
row['id'] = (base_id if id_counts[base_id] == 1
|
|
1883
|
+
else '%s-%d' % (base_id, id_counts[base_id]))
|
|
1884
|
+
row['placements'] = [placement]
|
|
1885
|
+
row['slides'] = [placement['slide']]
|
|
1886
|
+
row['layout'] = layout
|
|
1887
|
+
row['source_placements'] = placements
|
|
1888
|
+
contexts.append(row)
|
|
1889
|
+
return contexts
|
|
1890
|
+
|
|
1891
|
+
|
|
1892
|
+
def _group_input_count(group):
|
|
1893
|
+
# 与 FaaS 一致:每页桶预留一张页面语境图;无实例页的版式候选也占一个图位。
|
|
1894
|
+
page_count = len([page for page in group['pages'] if page > 0]) or 1
|
|
1895
|
+
return len(group['candidates']) + page_count
|
|
1896
|
+
|
|
1897
|
+
|
|
1898
|
+
def build_asset_vision_groups(candidates):
|
|
1899
|
+
"""按 FaaS 的 10/5 图数预算,把候选按所在页组合成视觉判断批次。"""
|
|
1900
|
+
buckets = defaultdict(list)
|
|
1901
|
+
for candidate in asset_vision_contexts(candidates):
|
|
1902
|
+
page = next((row['slide'] for row in candidate.get('placements') or []
|
|
1903
|
+
if row.get('slide')), 0)
|
|
1904
|
+
buckets[page].append(candidate)
|
|
1905
|
+
|
|
1906
|
+
groups, queued = [], []
|
|
1907
|
+
|
|
1908
|
+
def flush_small_pages():
|
|
1909
|
+
if not queued:
|
|
1910
|
+
return
|
|
1911
|
+
current, current_pages, inputs = [], [], 0
|
|
1912
|
+
for page, page_candidates in queued:
|
|
1913
|
+
page_inputs = 1 + len(page_candidates)
|
|
1914
|
+
if current and inputs + page_inputs > MULTI_PAGE_IMAGE_BUDGET:
|
|
1915
|
+
groups.append({'pages': current_pages, 'candidates': current})
|
|
1916
|
+
current, current_pages, inputs = [], [], 0
|
|
1917
|
+
current.extend(page_candidates)
|
|
1918
|
+
current_pages.append(page)
|
|
1919
|
+
inputs += page_inputs
|
|
1920
|
+
if current:
|
|
1921
|
+
groups.append({'pages': current_pages, 'candidates': current})
|
|
1922
|
+
del queued[:]
|
|
1923
|
+
|
|
1924
|
+
for page in sorted(buckets):
|
|
1925
|
+
page_candidates = buckets[page]
|
|
1926
|
+
page_inputs = 1 + len(page_candidates)
|
|
1927
|
+
if page_inputs > MULTI_PAGE_IMAGE_BUDGET:
|
|
1928
|
+
flush_small_pages()
|
|
1929
|
+
per_group = SINGLE_PAGE_IMAGE_BUDGET - 1
|
|
1930
|
+
for start in range(0, len(page_candidates), per_group):
|
|
1931
|
+
groups.append({
|
|
1932
|
+
'pages': [page],
|
|
1933
|
+
'candidates': page_candidates[start:start + per_group],
|
|
1934
|
+
})
|
|
1935
|
+
continue
|
|
1936
|
+
queued.append((page, page_candidates))
|
|
1937
|
+
flush_small_pages()
|
|
1938
|
+
|
|
1939
|
+
for index, group in enumerate(groups, 1):
|
|
1940
|
+
group['id'] = 'vision-%d' % index
|
|
1941
|
+
group['input_count'] = _group_input_count(group)
|
|
1942
|
+
return groups
|
|
1943
|
+
|
|
1944
|
+
|
|
1945
|
+
def select_asset_vision_groups(groups, slide_count):
|
|
1946
|
+
"""受总预算约束选择视觉批次:首页和尾页的所有可容纳分批优先于中间页。"""
|
|
1947
|
+
if not groups:
|
|
1948
|
+
return [], []
|
|
1949
|
+
first_page = 1
|
|
1950
|
+
last_page = slide_count or max(
|
|
1951
|
+
(page for group in groups for page in group['pages'] if page > 0), default=0)
|
|
1952
|
+
selected, selected_ids, inputs = [], set(), 0
|
|
1953
|
+
|
|
1954
|
+
def add(group):
|
|
1955
|
+
nonlocal inputs
|
|
1956
|
+
if (group['id'] in selected_ids or len(selected) >= VISUAL_PACK_CAP
|
|
1957
|
+
or inputs + group['input_count'] > VISUAL_INPUT_CAP):
|
|
1958
|
+
return False
|
|
1959
|
+
selected.append(group)
|
|
1960
|
+
selected_ids.add(group['id'])
|
|
1961
|
+
inputs += group['input_count']
|
|
1962
|
+
return True
|
|
1963
|
+
|
|
1964
|
+
# 首尾页先于中间页保留全部可容纳分批。交错加入避免首页多批先占满总预算,尾页
|
|
1965
|
+
# 连首批都进不去;单页 deck 不重复扫描。
|
|
1966
|
+
priority_batches = [
|
|
1967
|
+
[group for group in groups if page in group['pages']]
|
|
1968
|
+
for page in dict.fromkeys((first_page, last_page))
|
|
1969
|
+
]
|
|
1970
|
+
for batch_index in range(max(map(len, priority_batches), default=0)):
|
|
1971
|
+
for batches in priority_batches:
|
|
1972
|
+
if batch_index < len(batches):
|
|
1973
|
+
add(batches[batch_index])
|
|
1974
|
+
|
|
1975
|
+
# 首尾的第一个批次已经保证;剩余按页码保留前段内容,优先丢弃尾页之前的后段。
|
|
1976
|
+
remainder = sorted(
|
|
1977
|
+
(group for group in groups if group['id'] not in selected_ids),
|
|
1978
|
+
key=lambda group: (
|
|
1979
|
+
min((page for page in group['pages'] if page > 0), default=999999),
|
|
1980
|
+
group['id'],
|
|
1981
|
+
),
|
|
1982
|
+
)
|
|
1983
|
+
for group in remainder:
|
|
1984
|
+
add(group)
|
|
1985
|
+
|
|
1986
|
+
selected.sort(key=lambda group: (
|
|
1987
|
+
min((page for page in group['pages'] if page > 0), default=999999), group['id']))
|
|
1988
|
+
omitted = [group for group in groups if group['id'] not in selected_ids]
|
|
1989
|
+
return selected, omitted
|
|
1990
|
+
|
|
1991
|
+
|
|
1992
|
+
def _safe_remove(pattern):
|
|
1993
|
+
for path in glob.glob(pattern):
|
|
1994
|
+
try:
|
|
1995
|
+
os.remove(path)
|
|
1996
|
+
except OSError:
|
|
1997
|
+
pass
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
def _fit_image(image, width, height):
|
|
2001
|
+
copy = image.copy()
|
|
2002
|
+
copy.thumbnail((width, height))
|
|
2003
|
+
return copy
|
|
2004
|
+
|
|
2005
|
+
|
|
2006
|
+
def _draw_checkerboard(draw, box, size=14):
|
|
2007
|
+
x, y, w, h = box
|
|
2008
|
+
for row in range(0, h, size):
|
|
2009
|
+
for col in range(0, w, size):
|
|
2010
|
+
if (row // size + col // size) % 2 == 0:
|
|
2011
|
+
draw.rectangle([x + col, y + row, x + col + size - 1, y + row + size - 1],
|
|
2012
|
+
fill=(214, 214, 218))
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
def _candidate_is_visual_risk(candidate):
|
|
2016
|
+
probe = candidate.get('probe') or {}
|
|
2017
|
+
alpha = candidate.get('effective_alpha_mean')
|
|
2018
|
+
if alpha is None:
|
|
2019
|
+
alpha = probe.get('alpha_mean')
|
|
2020
|
+
return ((alpha is not None and alpha < 230)
|
|
2021
|
+
or (probe.get('near_white_ratio') or 0) >= 0.7)
|
|
2022
|
+
|
|
2023
|
+
|
|
2024
|
+
def _paste_candidate_preview(sheet, draw, image, box, dark=False):
|
|
2025
|
+
x, y, w, h = box
|
|
2026
|
+
if dark:
|
|
2027
|
+
draw.rectangle([x, y, x + w, y + h], fill=(54, 54, 58))
|
|
2028
|
+
else:
|
|
2029
|
+
_draw_checkerboard(draw, box)
|
|
2030
|
+
preview = _fit_image(image.convert('RGBA'), w - 8, h - 8)
|
|
2031
|
+
px = x + (w - preview.width) // 2
|
|
2032
|
+
py = y + (h - preview.height) // 2
|
|
2033
|
+
sheet.paste(preview, (px, py), preview)
|
|
2034
|
+
|
|
2035
|
+
|
|
2036
|
+
def _placement_text(candidate):
|
|
2037
|
+
rows = []
|
|
2038
|
+
for placement in candidate.get('placements') or []:
|
|
2039
|
+
box = placement['box']
|
|
2040
|
+
if placement.get('slide'):
|
|
2041
|
+
rows.append('s%d@%d,%d,%d,%d' % (
|
|
2042
|
+
placement['slide'], box[0], box[1], box[2], box[3]))
|
|
2043
|
+
else:
|
|
2044
|
+
rows.append('%s@%d,%d,%d,%d' % (
|
|
2045
|
+
placement.get('layout') or 'layout', box[0], box[1], box[2], box[3]))
|
|
2046
|
+
return ';'.join(rows)
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
def _save_visual_sheet(sheet, path):
|
|
2050
|
+
if max(sheet.size) > VISUAL_PREVIEW_MAX_EDGE:
|
|
2051
|
+
ratio = VISUAL_PREVIEW_MAX_EDGE / float(max(sheet.size))
|
|
2052
|
+
sheet = sheet.resize((max(1, round(sheet.width * ratio)),
|
|
2053
|
+
max(1, round(sheet.height * ratio))))
|
|
2054
|
+
sheet.save(path, 'JPEG', quality=VISUAL_JPEG_QUALITY, optimize=True, progressive=True)
|
|
2055
|
+
|
|
2056
|
+
|
|
2057
|
+
def render_asset_vision_pages(outdir, pages):
|
|
2058
|
+
"""视觉判断必须有页面语境;截图失败时中止草案而非让模型盲判。"""
|
|
2059
|
+
if not pages:
|
|
2060
|
+
return None
|
|
2061
|
+
result = subprocess.run(
|
|
2062
|
+
[sys.executable, os.path.join(HERE, 'render_pages.py'), outdir,
|
|
2063
|
+
'--pages', 'slides', '--only', ','.join(map(str, pages)), '--no-html'],
|
|
2064
|
+
capture_output=True, text=True,
|
|
2065
|
+
)
|
|
2066
|
+
png_dir = os.path.join(outdir, 'ref', 'rebuild', 'png')
|
|
2067
|
+
missing = [
|
|
2068
|
+
page for page in pages
|
|
2069
|
+
if not os.path.isfile(os.path.join(png_dir, 'slide-%d.png' % page))
|
|
2070
|
+
]
|
|
2071
|
+
if result.returncode or missing:
|
|
2072
|
+
detail = (result.stderr or result.stdout or '').strip().splitlines()
|
|
2073
|
+
raise RuntimeError(
|
|
2074
|
+
'视觉判断所需页面截图生成失败%s%s' % (
|
|
2075
|
+
'(缺第%s页)' % '、'.join(map(str, missing)) if missing else '',
|
|
2076
|
+
':' + detail[-1] if detail else '',
|
|
2077
|
+
)
|
|
2078
|
+
)
|
|
2079
|
+
return png_dir
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
class VisionContextError(RuntimeError):
|
|
2083
|
+
pass
|
|
2084
|
+
|
|
2085
|
+
|
|
2086
|
+
def has_pillow():
|
|
2087
|
+
try:
|
|
2088
|
+
from PIL import Image # noqa: F401
|
|
2089
|
+
except Exception:
|
|
2090
|
+
return False
|
|
2091
|
+
return True
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
def asset_vision_group_sheet(outdir, group, png_dir, path):
|
|
2095
|
+
"""把一组整页语境和候选图做成可索引拼版;每张候选保持独立卡片。"""
|
|
2096
|
+
try:
|
|
2097
|
+
from PIL import Image, ImageDraw
|
|
2098
|
+
except Exception:
|
|
2099
|
+
return None
|
|
2100
|
+
|
|
2101
|
+
candidates = group['candidates']
|
|
2102
|
+
page_count = len([page for page in group['pages'] if page > 0])
|
|
2103
|
+
page_w, page_h = 440, 248
|
|
2104
|
+
cell, pad, label_h = 220, 16, 42
|
|
2105
|
+
asset_cols = 3
|
|
2106
|
+
asset_rows = max(1, (len(candidates) + asset_cols - 1) // asset_cols)
|
|
2107
|
+
page_rows = max(1, (page_count + 1) // 2) if page_count else 0
|
|
2108
|
+
page_cols = min(2, page_count) if page_count else 0
|
|
2109
|
+
width = max(2 * (page_w + pad) + pad if page_cols else 0,
|
|
2110
|
+
asset_cols * (cell + pad) + pad)
|
|
2111
|
+
header_h = 28
|
|
2112
|
+
page_area_h = (page_rows * (page_h + label_h + pad) + pad) if page_rows else 0
|
|
2113
|
+
asset_top = header_h + page_area_h
|
|
2114
|
+
height = asset_top + asset_rows * (cell + label_h + pad) + pad
|
|
2115
|
+
sheet = Image.new('RGB', (width, height), (245, 245, 247))
|
|
2116
|
+
draw = ImageDraw.Draw(sheet)
|
|
2117
|
+
draw.text((pad, 7), '%s inputs=%d pages=%s' % (
|
|
2118
|
+
group['id'], group['input_count'],
|
|
2119
|
+
','.join(map(str, group['pages'])) or 'layout'), fill=(20, 20, 24))
|
|
2120
|
+
|
|
2121
|
+
for index, page in enumerate([page for page in group['pages'] if page > 0]):
|
|
2122
|
+
x = pad + (index % 2) * (page_w + pad)
|
|
2123
|
+
y = header_h + (index // 2) * (page_h + label_h + pad)
|
|
2124
|
+
source = (os.path.join(png_dir, 'slide-%d.png' % page)
|
|
2125
|
+
if png_dir else None)
|
|
2126
|
+
if source and os.path.exists(source):
|
|
2127
|
+
try:
|
|
2128
|
+
image = Image.open(source).convert('RGB')
|
|
2129
|
+
image = _fit_image(image, page_w, page_h)
|
|
2130
|
+
sheet.paste(image, (x + (page_w - image.width) // 2,
|
|
2131
|
+
y + (page_h - image.height) // 2))
|
|
2132
|
+
except Exception as exc:
|
|
2133
|
+
raise VisionContextError('视觉判断所需页面截图不可读取:第%d页' % page) from exc
|
|
2134
|
+
else:
|
|
2135
|
+
raise VisionContextError('视觉判断所需页面截图缺失:第%d页' % page)
|
|
2136
|
+
draw.rectangle([x, y, x + page_w, y + page_h], outline=(120, 120, 128))
|
|
2137
|
+
draw.text((x + 2, y + page_h + 5), '[page %d] context for candidates below' % page,
|
|
2138
|
+
fill=(20, 20, 24))
|
|
2139
|
+
|
|
2140
|
+
for index, candidate in enumerate(candidates):
|
|
2141
|
+
x = pad + (index % asset_cols) * (cell + pad)
|
|
2142
|
+
y = asset_top + (index // asset_cols) * (cell + label_h + pad)
|
|
2143
|
+
image_path = os.path.join(outdir, candidate['out'])
|
|
2144
|
+
try:
|
|
2145
|
+
image = Image.open(image_path)
|
|
2146
|
+
if _candidate_is_visual_risk(candidate):
|
|
2147
|
+
half = (cell - 3) // 2
|
|
2148
|
+
_paste_candidate_preview(sheet, draw, image, (x, y, half, cell))
|
|
2149
|
+
_paste_candidate_preview(sheet, draw, image, (x + half + 3, y, cell - half - 3, cell),
|
|
2150
|
+
dark=True)
|
|
2151
|
+
else:
|
|
2152
|
+
_paste_candidate_preview(sheet, draw, image, (x, y, cell, cell))
|
|
2153
|
+
except Exception:
|
|
2154
|
+
draw.text((x + 8, y + 8), 'unreadable', fill=(200, 0, 0))
|
|
2155
|
+
draw.rectangle([x, y, x + cell, y + cell], outline=(120, 120, 128))
|
|
2156
|
+
probe = candidate.get('probe') or {}
|
|
2157
|
+
alpha = candidate.get('effective_alpha_mean')
|
|
2158
|
+
if alpha is None:
|
|
2159
|
+
alpha = probe.get('alpha_mean')
|
|
2160
|
+
risk = (' a=%s w=%s' % (
|
|
2161
|
+
'?' if alpha is None else round(alpha),
|
|
2162
|
+
'?' if probe.get('near_white_ratio') is None
|
|
2163
|
+
else round(probe['near_white_ratio'] * 100),
|
|
2164
|
+
)) if _candidate_is_visual_risk(candidate) else ''
|
|
2165
|
+
draw.text((x + 2, y + cell + 3), '[%s] %s %dx%d%s' % (
|
|
2166
|
+
candidate['id'], candidate['file'], probe.get('w') or 0, probe.get('h') or 0, risk),
|
|
2167
|
+
fill=(20, 20, 24))
|
|
2168
|
+
first = (candidate.get('placements') or [{}])[0]
|
|
2169
|
+
total_placements = len(candidate.get('source_placements') or
|
|
2170
|
+
candidate.get('placements') or [])
|
|
2171
|
+
if first.get('slide'):
|
|
2172
|
+
box = first['box']
|
|
2173
|
+
draw.text((x + 2, y + cell + 18), 's%d @%d,%d %dx%d seen=%d' % (
|
|
2174
|
+
first['slide'], box[0], box[1], box[2], box[3],
|
|
2175
|
+
total_placements), fill=(20, 20, 24))
|
|
2176
|
+
else:
|
|
2177
|
+
draw.text((x + 2, y + cell + 18), 'layout x%d' % len(candidate.get('placements') or []),
|
|
2178
|
+
fill=(20, 20, 24))
|
|
2179
|
+
_save_visual_sheet(sheet, path)
|
|
2180
|
+
return path
|
|
2181
|
+
|
|
2182
|
+
|
|
2183
|
+
def emit_asset_vision_groups(outdir, candidates, slide_count, ldir):
|
|
2184
|
+
"""生成受预算约束的拼版和结构化索引,返回已选/未选组。"""
|
|
2185
|
+
groups = build_asset_vision_groups(candidates)
|
|
2186
|
+
selected, omitted = select_asset_vision_groups(groups, slide_count)
|
|
2187
|
+
_safe_remove(os.path.join(ldir, 'vision-group-*.jpg'))
|
|
2188
|
+
_safe_remove(os.path.join(ldir, 'contact-sheet-*.png'))
|
|
2189
|
+
_safe_remove(os.path.join(ldir, 'contact-sheet.png'))
|
|
2190
|
+
_safe_remove(os.path.join(ldir, 'asset-context-sheet-*.png'))
|
|
2191
|
+
|
|
2192
|
+
paths = []
|
|
2193
|
+
if not has_pillow():
|
|
2194
|
+
omitted = groups
|
|
2195
|
+
selected = []
|
|
2196
|
+
else:
|
|
2197
|
+
pages = sorted({page for group in selected for page in group['pages'] if page > 0})
|
|
2198
|
+
png_dir = render_asset_vision_pages(outdir, pages)
|
|
2199
|
+
try:
|
|
2200
|
+
for index, group in enumerate(selected, 1):
|
|
2201
|
+
path = os.path.join(ldir, 'vision-group-%d.jpg' % index)
|
|
2202
|
+
if asset_vision_group_sheet(outdir, group, png_dir, path):
|
|
2203
|
+
paths.append(path)
|
|
2204
|
+
group['sheet'] = os.path.basename(path)
|
|
2205
|
+
if selected and len(paths) != len(selected):
|
|
2206
|
+
raise RuntimeError('视觉判断拼版生成失败')
|
|
2207
|
+
except VisionContextError:
|
|
2208
|
+
raise
|
|
2209
|
+
except Exception:
|
|
2210
|
+
_safe_remove(os.path.join(ldir, 'vision-group-*.jpg'))
|
|
2211
|
+
selected, omitted, paths = [], groups, []
|
|
2212
|
+
if paths:
|
|
2213
|
+
# 旧流程只认 contact-sheet.png;保留首个视觉组的 PNG 别名,新的 BRIEF 不再要求读它。
|
|
2214
|
+
try:
|
|
2215
|
+
from PIL import Image
|
|
2216
|
+
legacy = os.path.join(ldir, 'contact-sheet-1.png')
|
|
2217
|
+
Image.open(paths[0]).convert('RGB').save(legacy, 'PNG', optimize=True)
|
|
2218
|
+
shutil.copy2(legacy, os.path.join(ldir, 'contact-sheet.png'))
|
|
2219
|
+
except Exception:
|
|
2220
|
+
pass
|
|
2221
|
+
|
|
2222
|
+
def serialize(group):
|
|
2223
|
+
return {
|
|
2224
|
+
'id': group['id'],
|
|
2225
|
+
'sheet': group.get('sheet'),
|
|
2226
|
+
'pages': group['pages'],
|
|
2227
|
+
'input_count': group['input_count'],
|
|
2228
|
+
'candidates': [{
|
|
2229
|
+
'id': candidate['id'],
|
|
2230
|
+
'source_media': candidate['file'],
|
|
2231
|
+
'source_px': [candidate['probe'].get('w'), candidate['probe'].get('h')],
|
|
2232
|
+
'bytes': candidate.get('bytes'),
|
|
2233
|
+
'repeat_count': candidate.get('n'),
|
|
2234
|
+
'fullscreen': candidate.get('fullscreen'),
|
|
2235
|
+
'effective_alpha_mean': candidate.get('effective_alpha_mean'),
|
|
2236
|
+
'near_white_ratio': candidate['probe'].get('near_white_ratio'),
|
|
2237
|
+
'placements': candidate.get('placements') or [],
|
|
2238
|
+
'source_placements': candidate.get('source_placements') or
|
|
2239
|
+
candidate.get('placements') or [],
|
|
2240
|
+
} for candidate in group['candidates']],
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
index = {
|
|
2244
|
+
'version': 2,
|
|
2245
|
+
'limits': {
|
|
2246
|
+
'single_page_image_budget': SINGLE_PAGE_IMAGE_BUDGET,
|
|
2247
|
+
'multi_page_image_budget': MULTI_PAGE_IMAGE_BUDGET,
|
|
2248
|
+
'pack_cap': VISUAL_PACK_CAP,
|
|
2249
|
+
'input_cap': VISUAL_INPUT_CAP,
|
|
2250
|
+
},
|
|
2251
|
+
'selected': [serialize(group) for group in selected],
|
|
2252
|
+
'omitted': [serialize(group) for group in omitted],
|
|
2253
|
+
}
|
|
2254
|
+
with open(os.path.join(ldir, 'asset-vision-groups.json'), 'w', encoding='utf-8') as stream:
|
|
2255
|
+
json.dump(index, stream, ensure_ascii=False, indent=2)
|
|
2256
|
+
stream.write('\n')
|
|
2257
|
+
return selected, omitted, paths
|
|
2258
|
+
|
|
2259
|
+
|
|
2260
|
+
def asset_context_sheets(outdir, cands, ldir):
|
|
2261
|
+
"""按候选主所在页去重拼整页语境,供模型识别 logo 墙和装饰用途。"""
|
|
2262
|
+
reviewed = [c for c in cands if needs_asset_judgment(c)]
|
|
2263
|
+
pages = []
|
|
2264
|
+
seen = set()
|
|
2265
|
+
for c in reviewed:
|
|
2266
|
+
page = next((no for no in c.get('slides') or [] if no and no != 9999), None)
|
|
2267
|
+
if page is not None and page not in seen:
|
|
2268
|
+
seen.add(page)
|
|
2269
|
+
pages.append(page)
|
|
2270
|
+
if not pages:
|
|
2271
|
+
return []
|
|
2272
|
+
import subprocess
|
|
2273
|
+
result = subprocess.run(
|
|
2274
|
+
[sys.executable, os.path.join(HERE, 'render_pages.py'), outdir,
|
|
2275
|
+
'--pages', 'slides', '--only', ','.join(map(str, pages)), '--no-html'],
|
|
2276
|
+
capture_output=True, text=True,
|
|
2277
|
+
)
|
|
2278
|
+
png_dir = os.path.join(outdir, 'ref', 'rebuild', 'png')
|
|
2279
|
+
if result.returncode or not os.path.isdir(png_dir):
|
|
2280
|
+
return []
|
|
2281
|
+
try:
|
|
2282
|
+
from PIL import Image, ImageDraw
|
|
2283
|
+
except Exception:
|
|
2284
|
+
return []
|
|
2285
|
+
paths = []
|
|
2286
|
+
candidate_ids = defaultdict(list)
|
|
2287
|
+
for index, c in enumerate(cands, 1):
|
|
2288
|
+
if not needs_asset_judgment(c):
|
|
2289
|
+
continue
|
|
2290
|
+
for page in c.get('slides') or []:
|
|
2291
|
+
if page in seen:
|
|
2292
|
+
candidate_ids[page].append(index)
|
|
2293
|
+
for start in range(0, len(pages), CONTEXT_BATCH):
|
|
2294
|
+
batch = pages[start:start + CONTEXT_BATCH]
|
|
2295
|
+
cols, cw, ch, pad, lab = 2, 480, 270, 16, 22
|
|
2296
|
+
rows = (len(batch) + cols - 1) // cols
|
|
2297
|
+
sheet = Image.new('RGB', (cols * (cw + pad) + pad,
|
|
2298
|
+
rows * (ch + pad + lab) + pad), (245, 245, 247))
|
|
2299
|
+
draw = ImageDraw.Draw(sheet)
|
|
2300
|
+
for offset, page in enumerate(batch):
|
|
2301
|
+
x = pad + (offset % cols) * (cw + pad)
|
|
2302
|
+
y = pad + (offset // cols) * (ch + pad + lab)
|
|
2303
|
+
source = os.path.join(png_dir, 'slide-%d.png' % page)
|
|
2304
|
+
if os.path.exists(source):
|
|
2305
|
+
image = Image.open(source).convert('RGB')
|
|
2306
|
+
image.thumbnail((cw, ch))
|
|
2307
|
+
sheet.paste(image, (x, y))
|
|
2308
|
+
draw.rectangle([x, y, x + cw, y + ch], outline=(120, 120, 128))
|
|
2309
|
+
draw.text((x + 2, y + ch + 5), 'slide %d candidates=%s'
|
|
2310
|
+
% (page, ','.join(map(str, candidate_ids[page]))),
|
|
2311
|
+
fill=(20, 20, 24))
|
|
2312
|
+
path = os.path.join(ldir, 'asset-context-sheet-%d.png'
|
|
2313
|
+
% (start // CONTEXT_BATCH + 1))
|
|
2314
|
+
sheet.save(path, optimize=True)
|
|
2315
|
+
paths.append(path)
|
|
2316
|
+
return paths
|
|
2317
|
+
|
|
2318
|
+
|
|
1566
2319
|
# ---------------------------------------------------------------- 落盘
|
|
1567
2320
|
def write(p, s):
|
|
1568
2321
|
with open(p, 'w', encoding='utf-8') as f:
|
|
1569
2322
|
f.write(s)
|
|
1570
2323
|
|
|
1571
2324
|
|
|
1572
|
-
def
|
|
2325
|
+
def bound_visual_candidates(candidates, archetypes):
|
|
2326
|
+
"""只把最终有图片槽的候选交给判断单;其余仍留在联系表供视觉核对。"""
|
|
2327
|
+
bound_files = {
|
|
2328
|
+
s.get('source_media')
|
|
2329
|
+
for a in archetypes
|
|
2330
|
+
for s in a.get('slots') or []
|
|
2331
|
+
if s.get('source_media')
|
|
2332
|
+
}
|
|
2333
|
+
return [c for c in candidates if c.get('file') in bound_files]
|
|
2334
|
+
|
|
2335
|
+
|
|
2336
|
+
def visual_slot_candidates(candidates, archetypes):
|
|
2337
|
+
"""把候选绑定到最终槽位;页面截图只作该槽位的视觉语境。"""
|
|
2338
|
+
source_candidates = {
|
|
2339
|
+
candidate.get('file'): candidate
|
|
2340
|
+
for candidate in bound_visual_candidates(candidates, archetypes)
|
|
2341
|
+
}
|
|
2342
|
+
rows, seen = [], set()
|
|
2343
|
+
for archetype in archetypes:
|
|
2344
|
+
pages = set(archetype.get('pages') or archetype.get('_sample_pages') or [])
|
|
2345
|
+
for slot in archetype.get('slots') or []:
|
|
2346
|
+
source = slot.get('source_media')
|
|
2347
|
+
raw_box = slot.get('box')
|
|
2348
|
+
candidate = source_candidates.get(source)
|
|
2349
|
+
if not pages or not candidate or not raw_box or not needs_asset_judgment(candidate):
|
|
2350
|
+
continue
|
|
2351
|
+
box = [round(value) for value in raw_box]
|
|
2352
|
+
key = (source, archetype['name'], tuple(box))
|
|
2353
|
+
if key in seen:
|
|
2354
|
+
continue
|
|
2355
|
+
seen.add(key)
|
|
2356
|
+
source_placements = candidate.get('placements') or []
|
|
2357
|
+
matching = [
|
|
2358
|
+
placement for placement in source_placements
|
|
2359
|
+
if tuple(placement.get('box') or ()) == tuple(box)
|
|
2360
|
+
and placement.get('slide') in pages
|
|
2361
|
+
]
|
|
2362
|
+
if not matching:
|
|
2363
|
+
continue
|
|
2364
|
+
slide = matching[0]['slide']
|
|
2365
|
+
row = dict(candidate)
|
|
2366
|
+
row['placements'] = [{
|
|
2367
|
+
'slide': slide,
|
|
2368
|
+
'box': box,
|
|
2369
|
+
'archetype': archetype['name'],
|
|
2370
|
+
}]
|
|
2371
|
+
row['slides'] = [slide] if slide else []
|
|
2372
|
+
row['source_placements'] = source_placements
|
|
2373
|
+
rows.append(row)
|
|
2374
|
+
return rows
|
|
2375
|
+
|
|
2376
|
+
|
|
2377
|
+
def emit_manifest(d, assets, vision_groups, ldir, archetypes=()):
|
|
1573
2378
|
L = ['version: alpha',
|
|
1574
2379
|
'name: TODO-style-name # 英文 kebab,体现气质,不要用文件名',
|
|
1575
2380
|
'name_zh: TODO中文名',
|
|
@@ -1590,6 +2395,37 @@ def emit_manifest(d, assets, ldir):
|
|
|
1590
2395
|
L.append(' on-bg: %s' % (a.get('on_bg') or 'light'))
|
|
1591
2396
|
if a['use_full']:
|
|
1592
2397
|
L.append(' use_full: true')
|
|
2398
|
+
if vision_groups:
|
|
2399
|
+
L += [
|
|
2400
|
+
'asset_vision_groups:',
|
|
2401
|
+
' # 每项对应拼版中的一个候选实例;同源图在不同页型/位置可分别定性。',
|
|
2402
|
+
' # 取值与 FaaS 对齐:logo|slogan|background|texture|icon|decorative|illustration|photo|chart|screenshot|footer-copyright|page-number|watermark|content-image|unknown。',
|
|
2403
|
+
]
|
|
2404
|
+
for group in vision_groups:
|
|
2405
|
+
for candidate in group['candidates']:
|
|
2406
|
+
placement = (candidate.get('placements') or [{}])[0]
|
|
2407
|
+
L.append(' - id: %s' % candidate['id'])
|
|
2408
|
+
L.append(' source_media: %s' % q(candidate['file']))
|
|
2409
|
+
if placement.get('box'):
|
|
2410
|
+
L.append(' box: %s' % placement['box'])
|
|
2411
|
+
L.append(' visual_kind: TODO-visual-kind-%s # %s;视觉组 %s'
|
|
2412
|
+
% (candidate['id'], candidate['id'], group['id']))
|
|
2413
|
+
L += [
|
|
2414
|
+
'asset_decisions:',
|
|
2415
|
+
' # 仅在不在视觉预算内的图片、或需要覆盖已有判断时追加。',
|
|
2416
|
+
' # 位置例外写 box;同图同坐标跨页型不同,再补 layout。',
|
|
2417
|
+
' # - {source_media: example.png, visual_kind: chart}',
|
|
2418
|
+
' # - {source_media: example.png, layout: layout-2, box: [0, 0, 100, 100], visual_kind: decorative}',
|
|
2419
|
+
]
|
|
2420
|
+
if any(decor.get('trace') == 'canvas-background'
|
|
2421
|
+
for archetype in archetypes
|
|
2422
|
+
for decor in archetype.get('decor') or []):
|
|
2423
|
+
canvas = d['canvas']['px']
|
|
2424
|
+
L += [
|
|
2425
|
+
'derived:',
|
|
2426
|
+
' - value: "[0, 0, %d, %d]"' % (canvas[0], canvas[1]),
|
|
2427
|
+
' reason: "PPT 背景铺满画布"',
|
|
2428
|
+
]
|
|
1593
2429
|
write(os.path.join(ldir, 'manifest.yaml'), '\n'.join(L) + '\n')
|
|
1594
2430
|
|
|
1595
2431
|
|
|
@@ -1686,6 +2522,11 @@ def draft_flow(a, facts, canvas):
|
|
|
1686
2522
|
cur.append(items[i + 1])
|
|
1687
2523
|
regions.append(cur)
|
|
1688
2524
|
|
|
2525
|
+
# 整页左右边距 = 所有内容的横向外包络,作为各区带的缺省。
|
|
2526
|
+
lefts = [s['box'][0] for s in items]
|
|
2527
|
+
rights = [s['box'][0] + s['box'][2] for s in items]
|
|
2528
|
+
page_margin = [min(lefts), cW - max(rights)]
|
|
2529
|
+
|
|
1689
2530
|
out = []
|
|
1690
2531
|
for reg in regions:
|
|
1691
2532
|
if not reg:
|
|
@@ -1708,8 +2549,17 @@ def draft_flow(a, facts, canvas):
|
|
|
1708
2549
|
if len(rows) > 1:
|
|
1709
2550
|
row_gap = round(rows[1][0]['box'][1]
|
|
1710
2551
|
- (rows[0][0]['box'][1] + rows[0][0]['box'][3]))
|
|
1711
|
-
|
|
1712
|
-
|
|
2552
|
+
region = {'kind': 'grid', 'cols': cols, 'gap': [max(col_gap, 0), max(row_gap, 0)],
|
|
2553
|
+
'items': rows[0]}
|
|
2554
|
+
# 卡片组的横向范围常和整页不同(标题贴左、卡片居中)。整页边距是所有元素的
|
|
2555
|
+
# 外包络,直接套给居中卡片组会把它拉偏成左对齐。区带范围和整页明显不一致时,
|
|
2556
|
+
# 落这个区带自己的左右边距,消费端把网格放进它再填 1fr。按落盘的整数比较,
|
|
2557
|
+
# 亚像素噪声不触发多余的区带边距。
|
|
2558
|
+
reg_margin = [min(s['box'][0] for s in rows[0]),
|
|
2559
|
+
cW - max(s['box'][0] + s['box'][2] for s in rows[0])]
|
|
2560
|
+
if [int(reg_margin[0]), int(reg_margin[1])] != [int(page_margin[0]), int(page_margin[1])]:
|
|
2561
|
+
region['margin'] = reg_margin
|
|
2562
|
+
out.append(region)
|
|
1713
2563
|
elif len(rows) == len(reg):
|
|
1714
2564
|
# 每行一个元素 = 真的竖着排
|
|
1715
2565
|
inner = 0
|
|
@@ -1724,9 +2574,7 @@ def draft_flow(a, facts, canvas):
|
|
|
1724
2574
|
out.append({'kind': 'free', 'items': fixed})
|
|
1725
2575
|
if len(out) < 2:
|
|
1726
2576
|
return None
|
|
1727
|
-
|
|
1728
|
-
rights = [s['box'][0] + s['box'][2] for s in items]
|
|
1729
|
-
return {'top': items[0]['box'][1], 'margin': [min(lefts), cW - max(rights)],
|
|
2577
|
+
return {'top': items[0]['box'][1], 'margin': page_margin,
|
|
1730
2578
|
'gap': round(cut), 'regions': out}
|
|
1731
2579
|
|
|
1732
2580
|
|
|
@@ -1875,7 +2723,70 @@ def structure_facts(archetypes, d, shapes):
|
|
|
1875
2723
|
return out, recipes
|
|
1876
2724
|
|
|
1877
2725
|
|
|
2726
|
+
LAYOUT_CONTROL_KEYS = {
|
|
2727
|
+
'names', 'roles', 'text_roles', 'layout_modes', 'bg_rules',
|
|
2728
|
+
}
|
|
2729
|
+
|
|
2730
|
+
|
|
2731
|
+
def emit_layout_controls(layout_lines, ldir, text_role_candidates, flow_archetypes):
|
|
2732
|
+
"""从兼容用的 layouts.yaml 分出模型只需编辑的判断区。
|
|
2733
|
+
|
|
2734
|
+
旧判断单把控制项和每个 slot 的坐标正文混在一起。模型为补一个角色读取整份文件,
|
|
2735
|
+
在版式很多的模板上会把时间耗在无须判断的数值上。仍保留旧文件给既有调用方;
|
|
2736
|
+
新文件只承载最终可覆盖它的五个顶层判断区。
|
|
2737
|
+
"""
|
|
2738
|
+
blocks, current = {}, None
|
|
2739
|
+
for line in layout_lines:
|
|
2740
|
+
match = re.match(r'^([A-Za-z_][\w-]*):', line)
|
|
2741
|
+
if match:
|
|
2742
|
+
current = match.group(1)
|
|
2743
|
+
if current in LAYOUT_CONTROL_KEYS:
|
|
2744
|
+
blocks[current] = [line]
|
|
2745
|
+
continue
|
|
2746
|
+
if current in blocks:
|
|
2747
|
+
blocks[current].append(line)
|
|
2748
|
+
|
|
2749
|
+
controls = [
|
|
2750
|
+
'# 版式判断控制区 —— 只读并编辑本文件;不要打开或修改 layouts.yaml。',
|
|
2751
|
+
'# package.py 会用本文件覆盖 layouts.yaml 的同名判断区,后者仅保留坐标事实与兼容输入。',
|
|
2752
|
+
'# 可编辑顶层键仅为 names / roles / text_roles / layout_modes / bg_rules。',
|
|
2753
|
+
]
|
|
2754
|
+
for key in ('names', 'roles'):
|
|
2755
|
+
if key in blocks:
|
|
2756
|
+
controls.extend([''] + blocks[key])
|
|
2757
|
+
if text_role_candidates:
|
|
2758
|
+
controls += [
|
|
2759
|
+
'',
|
|
2760
|
+
'text_roles:',
|
|
2761
|
+
'# 默认文字槽都是 body;仅把确认属于 title|subtitle|header|footer 的例外填为',
|
|
2762
|
+
'# <id>: title(不要给普通正文补 body)。候选对应的原始槽位在下列注释中。',
|
|
2763
|
+
]
|
|
2764
|
+
for role_id, slot in text_role_candidates:
|
|
2765
|
+
controls.append('# %s:%s' % (
|
|
2766
|
+
role_id, (slot.get('txt') or '(无样本文字)')[:60]))
|
|
2767
|
+
if flow_archetypes:
|
|
2768
|
+
controls += [
|
|
2769
|
+
'',
|
|
2770
|
+
'layout_modes:',
|
|
2771
|
+
'# 默认 slots。只有样张明确需要内容随高度重排时,取消注释并填 `<页型>: flow`。',
|
|
2772
|
+
]
|
|
2773
|
+
controls.extend('# %s: flow' % archetype['name'] for archetype in flow_archetypes)
|
|
2774
|
+
if 'bg_rules' in blocks:
|
|
2775
|
+
controls += [
|
|
2776
|
+
'',
|
|
2777
|
+
'# 只编辑本草案已列出的真实图片背景;它们都已被 layouts 中的 background: 引用。',
|
|
2778
|
+
'# 纯色、渐变、外框、几何装饰和透明叠层不新建 bg_rules;没有本段就保持没有。',
|
|
2779
|
+
] + blocks['bg_rules']
|
|
2780
|
+
write(os.path.join(ldir, 'layout-controls.yaml'), '\n'.join(controls) + '\n')
|
|
2781
|
+
|
|
2782
|
+
|
|
1878
2783
|
def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
2784
|
+
sampled_archetypes = {
|
|
2785
|
+
id(archetype) for archetype in archetypes
|
|
2786
|
+
if (archetype.get('rep') is not None
|
|
2787
|
+
or archetype.get('pages')
|
|
2788
|
+
or archetype.get('_sample_pages'))
|
|
2789
|
+
}
|
|
1879
2790
|
prefilled = sum(1 for a in archetypes if a.get('zh'))
|
|
1880
2791
|
L = ['# 判断单草案 —— package.py 读它产出 layouts.md,deck 的版式坐标从 layouts.md 读。',
|
|
1881
2792
|
'# 只改 names / roles / text_roles / layout_modes / bg_rules 五段(都是扁平键值,'
|
|
@@ -1903,11 +2814,16 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
1903
2814
|
for a in need_role:
|
|
1904
2815
|
szs = sorted({round(s['sz']) for s in a['slots'] if s.get('sz')}, reverse=True)
|
|
1905
2816
|
L.append(' %s: TODO角色 # 代表页 %s,共 %d 页;文字块 %d 个,字号 %s;'
|
|
1906
|
-
'图片 %d 张%s'
|
|
2817
|
+
'图片 %d 张%s%s'
|
|
1907
2818
|
% (a['name'], a['rep'], len(a['pages']),
|
|
1908
2819
|
len([s for s in a['slots'] if not s.get('asset')]),
|
|
1909
2820
|
'/'.join(str(x) for x in szs[:5]) or '未声明',
|
|
1910
|
-
a.get('pic_n') or 0, ';有满屏底图' if a.get('bg_raw') else ''
|
|
2821
|
+
a.get('pic_n') or 0, ';有满屏底图' if a.get('bg_raw') else '',
|
|
2822
|
+
';末页候选,结合样张判断 closing 或实际角色'
|
|
2823
|
+
if a.get('_last_page_candidate') else ''))
|
|
2824
|
+
# 普通正文先保持 body:它是安全且可消费的默认值。标题/页眉/页脚的少量例外依然
|
|
2825
|
+
# 要由模型看样张后写入 text_roles;把每一个正文槽都做成 TODO 会迫使模型逐行复述
|
|
2826
|
+
# 近百个显然的 body,挤占真正的视觉判断时间。
|
|
1911
2827
|
text_role_ids = {}
|
|
1912
2828
|
for a in archetypes:
|
|
1913
2829
|
index = 0
|
|
@@ -1917,23 +2833,12 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
1917
2833
|
index += 1
|
|
1918
2834
|
text_role_ids[id(slot)] = '%s-text-%d' % (a['name'], index)
|
|
1919
2835
|
if text_role_ids:
|
|
1920
|
-
L.append('
|
|
1921
|
-
|
|
1922
|
-
for slot in a.get('slots') or []:
|
|
1923
|
-
role_id = text_role_ids.get(id(slot))
|
|
1924
|
-
if not role_id:
|
|
1925
|
-
continue
|
|
1926
|
-
L.append(' %s: TODO文本角色 # 来源 %s;占位符 %s;样例 %s;'
|
|
1927
|
-
'box %s;字号 %s;css %s'
|
|
1928
|
-
% (role_id, slot.get('_source_layer') or '-',
|
|
1929
|
-
slot.get('_placeholder') or '-', q(slot.get('txt') or ''),
|
|
1930
|
-
slot.get('box'), round(slot.get('sz') or 0),
|
|
1931
|
-
q(slot.get('css') or '未声明')))
|
|
2836
|
+
L.append('# 文字槽默认均为 body。看样张后,只把确实属于 title|subtitle|header|footer 的'
|
|
2837
|
+
'例外追加到 text_roles:;不要为普通正文逐条补 body。')
|
|
1932
2838
|
flow_archetypes = [a for a in archetypes if a.get('flow')]
|
|
1933
2839
|
if flow_archetypes:
|
|
1934
|
-
L.append('
|
|
1935
|
-
|
|
1936
|
-
L.append(' %s: TODO布局模式 # 依据见 layouts 段该页型上方的结构事实' % a['name'])
|
|
2840
|
+
L.append('# 同时有 flow 与 slots 时默认保留 slots,保证固定构图可消费。'
|
|
2841
|
+
'只有样张明确需要内容随高度重排时,才在 layout_modes: 中写 <页型>: flow。')
|
|
1937
2842
|
# 禁放区是**背景图**的属性,不是页型的属性——按背景资产分组,页型再多也不涨
|
|
1938
2843
|
bgs = []
|
|
1939
2844
|
for a in archetypes:
|
|
@@ -1962,10 +2867,18 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
1962
2867
|
y1 = max(b[1] + b[3] for b in boxes)
|
|
1963
2868
|
L.append(' text_safe: [%d, %d, %d, %d] # 由该背景各页型的槽位并集算出'
|
|
1964
2869
|
% (x0, y0, x1 - x0, y1 - y0))
|
|
1965
|
-
|
|
2870
|
+
elif any(id(archetype) in sampled_archetypes
|
|
2871
|
+
for archetype in archetypes if archetype['bg'] == bg):
|
|
1966
2872
|
L.append(' text_safe: TODO安全文字区[x,y,w,h](该背景下没有任何槽位可依据)')
|
|
1967
|
-
|
|
1968
|
-
|
|
2873
|
+
else:
|
|
2874
|
+
L.append(' text_safe: [0, 0, 0, 0] # 未见对应样张,没有可依据的文字区')
|
|
2875
|
+
if any(id(archetype) in sampled_archetypes
|
|
2876
|
+
for archetype in archetypes if archetype['bg'] == bg):
|
|
2877
|
+
L.append(' avoid: TODO禁放区列表;无禁放区写 [],有则写 [{box: [x,y,w,h], reason: "..."}]')
|
|
2878
|
+
L.append(' pairing_rule: "TODO这张背景上标题/正文/图表要避让哪些区域"')
|
|
2879
|
+
else:
|
|
2880
|
+
L.append(' avoid: [] # 未见对应样张,不额外推断禁放区')
|
|
2881
|
+
L.append(' pairing_rule: "未见对应样张;沿用该页型已有槽位"')
|
|
1969
2882
|
L.append('layouts:')
|
|
1970
2883
|
for a in archetypes:
|
|
1971
2884
|
fx = (facts or {}).get(a['name']) or {}
|
|
@@ -2010,6 +2923,10 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
2010
2923
|
L.append(' - kind: grid')
|
|
2011
2924
|
L.append(' cols: %d' % r['cols'])
|
|
2012
2925
|
L.append(' gap: [%d, %d]' % tuple(r['gap']))
|
|
2926
|
+
if r.get('margin'):
|
|
2927
|
+
L.append(' margin: [%d, %d] # 本区带自己的左右边距,'
|
|
2928
|
+
'和整页 margin 不同(居中卡片组不跟标题的左边距)'
|
|
2929
|
+
% tuple(r['margin']))
|
|
2013
2930
|
elif r['kind'] == 'free':
|
|
2014
2931
|
L.append(' - kind: free # 推不出规整结构,按 slots 的坐标摆')
|
|
2015
2932
|
else:
|
|
@@ -2037,6 +2954,9 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
2037
2954
|
extra += ', css: "%s"' % str(child['css']).replace('"', "'")
|
|
2038
2955
|
if child.get('asset'):
|
|
2039
2956
|
extra += ', asset: %s' % child['asset']
|
|
2957
|
+
if child.get('source_media'):
|
|
2958
|
+
extra += ', source_media: %s' % child['source_media']
|
|
2959
|
+
extra += ', source_box: %s' % child['box']
|
|
2040
2960
|
L.append(' - {role: %s, type: %s%s}'
|
|
2041
2961
|
% (child['role'], child['type'], extra))
|
|
2042
2962
|
continue
|
|
@@ -2055,6 +2975,10 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
2055
2975
|
extra += ', css: "%s"' % str(s['css']).replace('"', "'")
|
|
2056
2976
|
if s.get('asset'):
|
|
2057
2977
|
extra += ', asset: %s' % s['asset']
|
|
2978
|
+
if s.get('source_media'):
|
|
2979
|
+
extra += ', source_media: %s' % s['source_media']
|
|
2980
|
+
if r['kind'] != 'free':
|
|
2981
|
+
extra += ', source_box: %s' % s['box']
|
|
2058
2982
|
L.append(' - {role: %s, type: %s%s}' % (s['role'], s['type'], extra))
|
|
2059
2983
|
L.append(' slots:')
|
|
2060
2984
|
for s in a['slots']:
|
|
@@ -2064,6 +2988,8 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
2064
2988
|
extra = ''
|
|
2065
2989
|
if s.get('asset'):
|
|
2066
2990
|
extra += ', asset: %s' % s['asset']
|
|
2991
|
+
if s.get('source_media'):
|
|
2992
|
+
extra += ', source_media: %s' % s['source_media']
|
|
2067
2993
|
if s.get('css') is not None:
|
|
2068
2994
|
extra += ', css: "%s"' % str(s['css']).replace('"', "'")
|
|
2069
2995
|
L.append(' - {role: %s, box: %s, type: %s%s}'
|
|
@@ -2075,9 +3001,19 @@ def emit_layouts(archetypes, ldir, busy_hints=None, facts=None, recipes=None):
|
|
|
2075
3001
|
% (dcr['box'], dcr['geom'], dcr['css'].replace('"', "'")))
|
|
2076
3002
|
L.append(' confidence: %s' % a.get('confidence', 'medium'))
|
|
2077
3003
|
write(os.path.join(ldir, 'layouts.yaml'), '\n'.join(L) + '\n')
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
3004
|
+
emit_layout_controls(L, ldir, [
|
|
3005
|
+
(text_role_ids[id(slot)], slot)
|
|
3006
|
+
for archetype in archetypes
|
|
3007
|
+
for slot in archetype.get('slots') or []
|
|
3008
|
+
if id(archetype) in sampled_archetypes and id(slot) in text_role_ids
|
|
3009
|
+
], [
|
|
3010
|
+
archetype for archetype in flow_archetypes
|
|
3011
|
+
if id(archetype) in sampled_archetypes
|
|
3012
|
+
])
|
|
3013
|
+
|
|
3014
|
+
|
|
3015
|
+
def emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, ldir,
|
|
3016
|
+
has_asset_candidates=False):
|
|
2081
3017
|
"""design.md 正文。
|
|
2082
3018
|
|
|
2083
3019
|
每条规则只出现一次——同一条散在 Fast Path / Usage / Background Safety /
|
|
@@ -2086,7 +3022,6 @@ def emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, l
|
|
|
2086
3022
|
"""
|
|
2087
3023
|
canvas = d['canvas']['px']
|
|
2088
3024
|
cover = next((a for a in assets if a['id'] == 'bg-cover'), None)
|
|
2089
|
-
logo = next((a for a in assets if a['kind'] == 'logo'), None)
|
|
2090
3025
|
imp, webs = import_line(fonts)
|
|
2091
3026
|
sidecar = '`layouts.md`'
|
|
2092
3027
|
|
|
@@ -2096,6 +3031,10 @@ def emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, l
|
|
|
2096
3031
|
% len(archetypes)) if (d.get('form_hint') or {}).get('form') == 3 else
|
|
2097
3032
|
('%d 页样张归纳出 %d 种页型。' % (d['counts']['slides'], len(archetypes))))
|
|
2098
3033
|
L += ['', '## Usage', '',
|
|
3034
|
+
'**生成前必须完整阅读本 `design.md` 和 %s,确认全部页型后再开始搭页。**'
|
|
3035
|
+
'不能只看摘要、前几个页型或 `## Layouts` 清单;后续页型同样可能定义背景、'
|
|
3036
|
+
'安全区、资产和固定元素。' % sidecar,
|
|
3037
|
+
'',
|
|
2099
3038
|
'搭一页 PPT 六步,中间四步的数据都在 %s:' % sidecar, '']
|
|
2100
3039
|
L += ['1. **定画布** —— 舞台按 `layouts.md` 的 `canvas` 设成 %d×%d,'
|
|
2101
3040
|
'别套用默认尺寸:源模板的长宽比不一定是 16:9,套错了整页坐标全偏。'
|
|
@@ -2104,15 +3043,19 @@ def emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, l
|
|
|
2104
3043
|
'2. **挑页型** —— 在 %s 里按用途选一个 archetype(清单见下面 Layouts 段)。'
|
|
2105
3044
|
'页数多于页型时,挑最接近的一个原样套用它的 slot:用不到的槽删掉,'
|
|
2106
3045
|
'内容比槽多就按同类槽的间距等距加,**坐标一律沿用该页型给的那套,不要自己另起网格**。'
|
|
2107
|
-
|
|
3046
|
+
'每个生成页面的 `<section>` 都写 `data-pptx-layout="<页型名>"`,'
|
|
3047
|
+
'交付前据此核验 logo 只出现在该页型的资产槽中。' % sidecar,
|
|
2108
3048
|
'3. **按页型给的形态落元素** —— 页型给 `flow` 就用流式,给 `slots` 就用绝对,'
|
|
2109
3049
|
'两者只会出现一个。'
|
|
2110
|
-
'**flow**:整块用一个纵向 flex 容器,`top` 是它的起始 y,`margin`
|
|
3050
|
+
'**flow**:整块用一个纵向 flex 容器,`top` 是它的起始 y,`margin` 是整块的左右边距,'
|
|
2111
3051
|
'`gap` 是区带之间的间距;`regions` 从上往下依次排,**每个区带的高度由它自己的'
|
|
2112
3052
|
'内容决定,不要写死高度**——上面的区带内容变多时,下面的自然被推下去,这正是'
|
|
2113
3053
|
'这套表达要解决的事。区带内部:`kind: grid` 用 `grid-template-columns: repeat(cols, 1fr)` '
|
|
2114
3054
|
'配 `gap: [行间距, 列间距]`;`kind: stack` 用纵向 flex 配 `gap`;`kind: free` '
|
|
2115
|
-
'按 item 自带的 `box`
|
|
3055
|
+
'按 item 自带的 `box` 绝对定位。区带自带 `margin: [左, 右]` 时用它的、'
|
|
3056
|
+
'覆盖整块的 `margin`(模板里居中的卡片组和贴左的标题横向范围本就不同);'
|
|
3057
|
+
'没带就用整块的 `margin`。`grid` 在自己这份左右边距里再 `repeat(cols, 1fr)`。'
|
|
3058
|
+
'`grid` 里的 `role: group` 是一张卡片:'
|
|
2116
3059
|
'group 的 `css` 用于外层容器,内部 `items` 按顺序纵向排布并使用 group 的 `gap`。'
|
|
2117
3060
|
'每个 `role: container` 的项是容器,把它的 `css` 逐项原样写进 style,内容放进去;'
|
|
2118
3061
|
'其中没有 `border-radius` 就按 `0`,不得自行补圆角。',
|
|
@@ -2134,7 +3077,7 @@ def emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, l
|
|
|
2134
3077
|
'7. **保持标题结构** —— 有合适页型可参考时,沿用该页型已有的标题层级与局部 '
|
|
2135
3078
|
'`css`;只渲染该页型已有的文字槽,背景中已经可见的固定标题不再创建文本,'
|
|
2136
3079
|
'页型没有 `subtitle` 槽就不新增副标题。没有合适参考时,按本包整体视觉组织标题。']
|
|
2137
|
-
if assets:
|
|
3080
|
+
if assets or has_asset_candidates:
|
|
2138
3081
|
L += ['', '资产文件(背景由页型的 `background` 字段指定,'
|
|
2139
3082
|
'图片资产的位置由该页型 `slots` 里带 `asset` 的槽给出):', '',
|
|
2140
3083
|
'{{ASSET_TABLE}}', '',
|
|
@@ -2168,15 +3111,7 @@ def emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, l
|
|
|
2168
3111
|
L.append('- 封面页铺满 `bg-cover`,整幅覆盖 %dx%d 画布。' % (canvas[0], canvas[1]))
|
|
2169
3112
|
if any(a['role'] == 'content' for a in assets):
|
|
2170
3113
|
L.append('- 内容页的背景由该页型的 `background` 字段指定,整幅铺满。')
|
|
2171
|
-
|
|
2172
|
-
# 点名哪几个页型带 logo。只说「位置去 slots 里查」的话,读起来像是每个页型都有
|
|
2173
|
-
# 这个槽、去查就行——而「不放」是靠该页型 slots 里缺这一项来表达的,要消费端
|
|
2174
|
-
# 自己做否定式推理才能得出。正面点名比让它去发现缺席可靠。
|
|
2175
|
-
with_logo = [a['name'] for a in archetypes
|
|
2176
|
-
if any(str(s.get('asset') or '') == logo['id'] for s in a['slots'])]
|
|
2177
|
-
L.append('- `%s` 只出现在这些页型上:%s;其余页型不放。位置取该页型 `slots` 里 '
|
|
2178
|
-
'`role: logo` 那一项的 `box`,原样使用该文件、保持原比例。'
|
|
2179
|
-
% (logo['id'], '、'.join('`%s`' % x for x in with_logo) or '(无)'))
|
|
3114
|
+
L.append('{{LOGO_RULES}}')
|
|
2180
3115
|
L += ['- 坐标、字号、色值、资产位置以 %s 为准;本文件的 Colors / Typography 是可用值的清单。'
|
|
2181
3116
|
% sidecar,
|
|
2182
3117
|
'- 强调色族以 Colors 和 %s 的 slot CSS 为主;必要时可以使用 Colors 之外的颜色,'
|
|
@@ -2200,9 +3135,20 @@ def emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, l
|
|
|
2200
3135
|
|
|
2201
3136
|
|
|
2202
3137
|
def emit_brief(d, ctx, ldir):
|
|
2203
|
-
(tokens, rest, fonts, roles, assets, rejected, todos, archetypes, cands,
|
|
2204
|
-
leftover, lsheet
|
|
3138
|
+
(tokens, rest, fonts, roles, assets, rejected, todos, archetypes, cands, sheets,
|
|
3139
|
+
selected_vision_groups, omitted_vision_groups, leftover, lsheet) = ctx
|
|
2205
3140
|
canvas = d['canvas']['px']
|
|
3141
|
+
def sample_pages(archetype):
|
|
3142
|
+
return archetype.get('pages') or archetype.get('_sample_pages') or []
|
|
3143
|
+
|
|
3144
|
+
sampled_archetypes = [
|
|
3145
|
+
archetype for archetype in archetypes
|
|
3146
|
+
if archetype.get('rep') is not None or sample_pages(archetype)
|
|
3147
|
+
]
|
|
3148
|
+
template_only_archetypes = [
|
|
3149
|
+
archetype for archetype in archetypes
|
|
3150
|
+
if archetype not in sampled_archetypes
|
|
3151
|
+
]
|
|
2206
3152
|
L = ['# 抽取简报(第 1/3 步产物;改完草案跑 package.py 出包)', '',
|
|
2207
3153
|
'源:`%s` 画布 %dx%d %d 页 / %d 版式 主题 %s form=%s'
|
|
2208
3154
|
% (d['source']['filename'], canvas[0], canvas[1], d['counts']['slides'],
|
|
@@ -2212,9 +3158,9 @@ def emit_brief(d, ctx, ldir):
|
|
|
2212
3158
|
# 待判断清单从草案实时扫 TODO 生成,不写死:写死的清单会和草案对不上——
|
|
2213
3159
|
# 既漏掉后加的段(模型读到一半才发现还有活),又在草案已预填时还催人去填。
|
|
2214
3160
|
HINT = {'manifest.yaml': '看两张图定气质',
|
|
2215
|
-
'
|
|
3161
|
+
'layout-controls.yaml': '看 layout-sheet.png;只改这个控制区',
|
|
2216
3162
|
'body.md': 'Colors 用途列草案已填好,觉得不对再改'}
|
|
2217
|
-
for fn in ('manifest.yaml', 'body.md', '
|
|
3163
|
+
for fn in ('manifest.yaml', 'body.md', 'layout-controls.yaml', 'frontmatter.yaml'):
|
|
2218
3164
|
path = os.path.join(ldir, fn)
|
|
2219
3165
|
if not os.path.exists(path):
|
|
2220
3166
|
continue
|
|
@@ -2237,21 +3183,34 @@ def emit_brief(d, ctx, ldir):
|
|
|
2237
3183
|
'(%s)' % hint if hint else ''))
|
|
2238
3184
|
for t in todos:
|
|
2239
3185
|
L.append('- ' + t)
|
|
2240
|
-
L += ['', '##
|
|
2241
|
-
'
|
|
2242
|
-
|
|
2243
|
-
|
|
3186
|
+
L += ['', '## 资产判断(按视觉组一次看完)', '',
|
|
3187
|
+
('视觉判断拼版:%s。每张都含候选独立卡与所在页截图;只读这些拼版,不逐张打开素材。'
|
|
3188
|
+
% '、'.join('`l-out/%s`' % os.path.basename(path) for path in sheets))
|
|
3189
|
+
if sheets else '(未生成视觉拼版;不要给图片候选定性,已按内容图保留位置并在 gaps 说明。)',
|
|
3190
|
+
'`l-out/asset-vision-groups.json` 记录每张候选的原图尺寸、所有页内位置和尺寸;'
|
|
3191
|
+
'透明/近白候选在拼版中同时给棋盘格和深灰底预览。',
|
|
3192
|
+
'按每个候选实例填 `asset_vision_groups.visual_kind`;同源图在不同页型/位置可不同。'
|
|
3193
|
+
'第三方 logo 墙属于 `content-image`,不是 deck 的 `logo`。',
|
|
3194
|
+
'',
|
|
3195
|
+
'| ID | 文件 | 原图 | 出现 | 页 | 所有位置 |', '|---|---|---|---|---|---|']
|
|
2244
3196
|
decided = {a['src']['file']: a['id'] for a in assets}
|
|
2245
3197
|
why = {c['file']: r for c, r in rejected}
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
3198
|
+
selected_candidates = {
|
|
3199
|
+
candidate['file']: candidate
|
|
3200
|
+
for group in selected_vision_groups
|
|
3201
|
+
for candidate in group['candidates']
|
|
3202
|
+
}
|
|
3203
|
+
for c in selected_candidates.values():
|
|
3204
|
+
L.append('| `%s` | `%s` | %sx%s | %d | %s | `%s` |' % (
|
|
3205
|
+
c['id'], c['file'], c['probe'].get('w') or '?', c['probe'].get('h') or '?',
|
|
3206
|
+
c['n'], ','.join(map(str, c['slides'])) or 'layout', _placement_text(c)))
|
|
3207
|
+
if omitted_vision_groups:
|
|
3208
|
+
omitted_pages = sorted({
|
|
3209
|
+
page for group in omitted_vision_groups for page in group['pages'] if page > 0
|
|
3210
|
+
})
|
|
2252
3211
|
L.append('')
|
|
2253
|
-
L.append('
|
|
2254
|
-
|
|
3212
|
+
L.append('未进视觉预算:%s;对应 slot 默认保留内容图片位置,不会自动升为风格资产。'
|
|
3213
|
+
% ('第%s页' % '、'.join(map(str, omitted_pages)) if omitted_pages else '版式候选'))
|
|
2255
3214
|
L += ['', '## 颜色(草案 token 已写进 frontmatter.yaml)', '',
|
|
2256
3215
|
'| token | hex | 出现 |', '|---|---|---|']
|
|
2257
3216
|
for name, r in tokens:
|
|
@@ -2267,24 +3226,55 @@ def emit_brief(d, ctx, ldir):
|
|
|
2267
3226
|
L.append('')
|
|
2268
3227
|
L.append('字号轴:' + '、'.join('%s=%dpx(n=%d)' % (k, round(v['sz_px']), v['n'])
|
|
2269
3228
|
for k, v in roles.items()))
|
|
2270
|
-
L += ['', '##
|
|
2271
|
-
'`l-out/layout-sheet.png` 是各页型代表页的重建图——**看它给页型起名**,'
|
|
2272
|
-
|
|
3229
|
+
L += ['', '## 版式聚类(判断项在 layout-controls.yaml,坐标事实在 layouts.yaml)', '',
|
|
3230
|
+
('`l-out/layout-sheet.png` 是各页型代表页的重建图——**看它给页型起名**,'
|
|
3231
|
+
'不用再逐页查 shapes。' if sampled_archetypes else
|
|
3232
|
+
'`l-out/layout-sheet.png` 是模板版式层的重建图;用它看整体视觉即可,'
|
|
3233
|
+
'没有对应样张的版式已按模板名称预填,不逐项改名或判角色。')
|
|
3234
|
+
if lsheet else '(未生成版式图,按下面的 slot 原文命名)', '',
|
|
2273
3235
|
'| archetype | 页数 | 代表页 | 背景 | slot 数 |', '|---|---|---|---|---|']
|
|
2274
|
-
for a in
|
|
3236
|
+
for a in sampled_archetypes:
|
|
3237
|
+
pages = sample_pages(a)
|
|
3238
|
+
representative = a.get('rep') or (pages[0] if pages else None)
|
|
2275
3239
|
L.append('| `%s` | %d | %s | %s | %d |' % (
|
|
2276
|
-
a['name'], len(
|
|
3240
|
+
a['name'], len(pages), representative, a['bg'] or '(无资产底图)', len(a['slots'])))
|
|
3241
|
+
if template_only_archetypes:
|
|
3242
|
+
L.append('')
|
|
3243
|
+
L.append('另有 %d 个模板声明版式没有对应样张:名称、角色和坐标已预填并会进入最终包;'
|
|
3244
|
+
'除非当前样张直接证明不对,不需要逐项判断。'
|
|
3245
|
+
% len(template_only_archetypes))
|
|
3246
|
+
sampled_pages = {
|
|
3247
|
+
page: archetype
|
|
3248
|
+
for archetype in sampled_archetypes
|
|
3249
|
+
for page in sample_pages(archetype)
|
|
3250
|
+
}
|
|
3251
|
+
first_page = 1
|
|
3252
|
+
last_page = d['counts']['slides']
|
|
3253
|
+
if first_page in sampled_pages:
|
|
3254
|
+
first_archetype = sampled_pages[first_page]
|
|
3255
|
+
L.append('')
|
|
3256
|
+
L.append('第 1 页实际使用页型:`%s`。若样张确为封面,只在 `roles.%s` 填 `cover`,'
|
|
3257
|
+
'不要按页型名称猜。'
|
|
3258
|
+
% (first_archetype['name'], first_archetype['name']))
|
|
3259
|
+
if last_page != first_page and last_page in sampled_pages:
|
|
3260
|
+
last_archetype = sampled_pages[last_page]
|
|
3261
|
+
L.append('第 %d 页实际使用页型:`%s`。若样张确为封底,只在 `roles.%s` 填 `closing`,'
|
|
3262
|
+
'不要按页型名称猜。'
|
|
3263
|
+
% (last_page, last_archetype['name'], last_archetype['name']))
|
|
2277
3264
|
if leftover:
|
|
2278
3265
|
L += ['', '未归入 archetype 的页:%s —— 都是单页孤例,需要就自己补一个 archetype。'
|
|
2279
3266
|
% ', '.join(map(str, leftover))]
|
|
2280
|
-
L += ['', '
|
|
2281
|
-
for a in
|
|
2282
|
-
|
|
3267
|
+
L += ['', '有样张页型的 slot 原文(据此起中文页型名,并在 text_roles 判断文本角色):', '']
|
|
3268
|
+
for a in sampled_archetypes:
|
|
3269
|
+
pages = sample_pages(a)
|
|
3270
|
+
representative = a.get('rep') or (pages[0] if pages else None)
|
|
3271
|
+
L.append('- `%s`(第 %s 页,覆盖 %s)' % (a['name'], representative, pages))
|
|
2283
3272
|
for s in a['slots']:
|
|
2284
3273
|
L.append(' - %s %spx 「%s」' % (s['role'], round(s['sz']), s['txt']))
|
|
2285
3274
|
L += ['', '## 下一步', '',
|
|
2286
|
-
'1.
|
|
2287
|
-
'2.
|
|
3275
|
+
'1. 并行看全部 `vision-group-*.jpg` 和 `layout-sheet.png`;'
|
|
3276
|
+
'2. 先填 asset_vision_groups,再用少量 asset_decisions 写例外,最后一次批量改完其它 TODO;'
|
|
3277
|
+
'3. 只改 `layout-controls.yaml` 的版式判断项,再跑 `package.py`。']
|
|
2288
3278
|
write(os.path.join(ldir, 'BRIEF.md'), '\n'.join(L) + '\n')
|
|
2289
3279
|
|
|
2290
3280
|
|
|
@@ -2302,50 +3292,22 @@ def main(argv=None):
|
|
|
2302
3292
|
cusage = color_usage(all_shapes, d)
|
|
2303
3293
|
tokens, rest, rows = draft_colors(d, cusage)
|
|
2304
3294
|
fonts = draft_fonts(d)
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
#
|
|
2308
|
-
#
|
|
2309
|
-
|
|
2310
|
-
# 那个页型:deck 的第 1 页就是封面,这是版式无关的事实。
|
|
2311
|
-
cover_media = next((a['bg_raw'] for a in archetypes if a['name'] == 'cover'), None)
|
|
2312
|
-
if not cover_media:
|
|
2313
|
-
cover_media = next((a['bg_raw'] for a in archetypes
|
|
2314
|
-
if 1 in (a.get('pages') or ())), None)
|
|
3295
|
+
effective_alpha = fullscreen_effective_alpha(d, outdir, all_shapes)
|
|
3296
|
+
archetypes, pages, leftover = draft_layouts(d, outdir, effective_alpha)
|
|
3297
|
+
# 只有模板已声明 cover 页型时才能直读它的封面背景。首页和末页会单独保留样张,
|
|
3298
|
+
# 但它们的角色仍由模型看图判断,不能因为页码就自动升格为 cover / closing。
|
|
3299
|
+
cover_media = cover_background_media(archetypes)
|
|
2315
3300
|
exported_media = {m['media'] for m in d.get('media', []) if m.get('exported')}
|
|
2316
3301
|
bg_needed = {a['bg_raw'] for a in archetypes if a['bg_raw'] in exported_media}
|
|
2317
3302
|
bg_under = {p['no']: p.get('rendered_bg') or p['bg_media'] for p in pages}
|
|
2318
|
-
assets, rejected, todos, alias, pool = draft_assets(
|
|
3303
|
+
assets, rejected, todos, alias, pool = draft_assets(
|
|
3304
|
+
d, outdir, bg_needed, cover_media, bg_under, effective_alpha)
|
|
2319
3305
|
media_to_asset = {a['src']['media']: a['id'] for a in assets}
|
|
2320
3306
|
for m, w in (alias or {}).items():
|
|
2321
3307
|
if w in media_to_asset:
|
|
2322
3308
|
media_to_asset.setdefault(m, media_to_asset[w])
|
|
2323
3309
|
|
|
2324
|
-
# 版式里那些贴在装饰容器上的小图(图标托底圆里的图标之类):不进包的话,消费端只看到
|
|
2325
|
-
# 一个空圆,只能自己编图形。它们是版式的一部分,按 icon 收进来。
|
|
2326
|
-
ICON_CAP = 12
|
|
2327
|
-
ICON_BUDGET = 3 * 1024 * 1024 # 图标是小件,占包体不该超过背景
|
|
2328
3310
|
cW, cH = d['canvas']['px']
|
|
2329
|
-
icon_i, icon_bytes = 0, 0
|
|
2330
|
-
for a in archetypes:
|
|
2331
|
-
for s in a['slots']:
|
|
2332
|
-
m = s.get('media')
|
|
2333
|
-
if not m or media_to_asset.get(m) or media_to_asset.get(alias.get(m, m)):
|
|
2334
|
-
continue
|
|
2335
|
-
c = pool.get(alias.get(m, m)) or pool.get(m)
|
|
2336
|
-
if not c or not c.get('out') or icon_i >= ICON_CAP:
|
|
2337
|
-
continue
|
|
2338
|
-
if icon_bytes + (c.get('bytes') or 0) > ICON_BUDGET:
|
|
2339
|
-
continue
|
|
2340
|
-
if s['box'][2] > cW * 0.25 or s['box'][3] > cH * 0.25:
|
|
2341
|
-
continue # 不是图标,是内容配图,交给消费端自备
|
|
2342
|
-
icon_i += 1
|
|
2343
|
-
icon_bytes += c.get('bytes') or 0
|
|
2344
|
-
aid = 'icon-%d' % icon_i
|
|
2345
|
-
assets.append({'id': aid, 'kind': 'icon', 'role': None, 'src': c, 'use_full': False})
|
|
2346
|
-
media_to_asset[c['media']] = aid
|
|
2347
|
-
media_to_asset[m] = aid
|
|
2348
|
-
dropped_slots = []
|
|
2349
3311
|
for a in archetypes:
|
|
2350
3312
|
a['bg'] = media_to_asset.get(a['bg_raw'])
|
|
2351
3313
|
# 版式自带的图片元素:映射到资产 id。映射不到时**保留槽位但不写 asset**——
|
|
@@ -2356,22 +3318,25 @@ def main(argv=None):
|
|
|
2356
3318
|
if s.get('media'):
|
|
2357
3319
|
aid = media_to_asset.get(s['media'])
|
|
2358
3320
|
if not aid:
|
|
2359
|
-
s['
|
|
3321
|
+
c = pool.get(alias.get(s['media'], s['media'])) or pool.get(s['media'])
|
|
3322
|
+
s['role'] = 'asset-candidate'
|
|
3323
|
+
if c:
|
|
3324
|
+
s['source_media'] = c['file']
|
|
2360
3325
|
s.pop('media', None)
|
|
2361
|
-
dropped_slots.append((a['name'], s['box']))
|
|
2362
3326
|
keep.append(s)
|
|
2363
3327
|
continue
|
|
2364
3328
|
s['asset'] = aid
|
|
3329
|
+
c = pool.get(alias.get(s['media'], s['media'])) or pool.get(s['media'])
|
|
3330
|
+
if c:
|
|
3331
|
+
s['source_media'] = c['file']
|
|
2365
3332
|
# role 跟着资产走:图标槽写成 logo 会让消费端把它当品牌标识,每页都摆一个
|
|
2366
3333
|
s['role'] = next((x['kind'] for x in assets if x['id'] == aid), s['role'])
|
|
2367
3334
|
keep.append(s)
|
|
2368
3335
|
a['slots'] = keep
|
|
2369
3336
|
roles = draft_scale(d, archetypes)
|
|
2370
3337
|
slot_added = cover_slot_colors(tokens, archetypes, rows, cusage)
|
|
2371
|
-
#
|
|
2372
|
-
#
|
|
2373
|
-
# (n=1),按出现次数排序时排在最末——实测被 cands[:12] 截掉,模型于是把 bg-cover
|
|
2374
|
-
# 换成了已经在用的内容页背景,封面与内容页字节相同,封面主视觉整个丢失。
|
|
3338
|
+
# 局部图和半透明满屏叠加层必须结合页面语境定性。候选在本阶段按图片槽过滤:
|
|
3339
|
+
# 没有最终槽位的媒体无需让模型判断;有槽位但超出视觉预算的则保留通用 pic 槽。
|
|
2375
3340
|
decided_c = sorted([a['src'] for a in assets], key=lambda c: (-c['n'], c['file']))
|
|
2376
3341
|
other_c = sorted([c for c, _ in rejected], key=lambda c: (-c['n'], c['file']))
|
|
2377
3342
|
cands, seen_file = [], set()
|
|
@@ -2379,16 +3344,28 @@ def main(argv=None):
|
|
|
2379
3344
|
if c['file'] not in seen_file:
|
|
2380
3345
|
seen_file.add(c['file'])
|
|
2381
3346
|
cands.append(c)
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
3347
|
+
review_candidates = []
|
|
3348
|
+
for candidate_index, candidate in enumerate(
|
|
3349
|
+
visual_slot_candidates(cands, archetypes), 1):
|
|
3350
|
+
row = dict(candidate)
|
|
3351
|
+
row['id'] = 'asset-%d' % candidate_index
|
|
3352
|
+
review_candidates.append(row)
|
|
3353
|
+
selected_vision_groups, omitted_vision_groups, sheets = emit_asset_vision_groups(
|
|
3354
|
+
outdir, review_candidates, d['counts']['slides'], ldir)
|
|
2388
3355
|
lsheet = layout_sheet(outdir, archetypes, os.path.join(ldir, 'layout-sheet.png'))
|
|
2389
3356
|
|
|
2390
3357
|
anchors = draft_anchors(d, tokens, fonts, roles, assets, archetypes)
|
|
2391
3358
|
gaps, exceptions = [], []
|
|
3359
|
+
if omitted_vision_groups:
|
|
3360
|
+
omitted_pages = sorted({
|
|
3361
|
+
page for group in omitted_vision_groups for page in group['pages'] if page > 0
|
|
3362
|
+
})
|
|
3363
|
+
if omitted_pages:
|
|
3364
|
+
gaps.append('视觉略过:%s页' % '/'.join(map(str, omitted_pages)))
|
|
3365
|
+
else:
|
|
3366
|
+
gaps.append('视觉判断超预算,未覆盖版式候选')
|
|
3367
|
+
if review_candidates and not sheets:
|
|
3368
|
+
gaps.append('视觉拼版不可用,图片候选按内容图保留,未做风格定性。')
|
|
2392
3369
|
for c, why in rejected:
|
|
2393
3370
|
if '近全透明' in why:
|
|
2394
3371
|
gaps.append('母版/版式里的 %s 是%s,不是设计资产,任何情况下不要当背景用。' % (c['file'], why))
|
|
@@ -2407,10 +3384,6 @@ def main(argv=None):
|
|
|
2407
3384
|
gaps.append('%s%s按名额截断:普查到 %d 个,包内留了 %d 个%s。'
|
|
2408
3385
|
% (kind, at, e['total'], e['kept'],
|
|
2409
3386
|
';' + e['advice'] if e['advice'] else ''))
|
|
2410
|
-
if dropped_slots:
|
|
2411
|
-
gaps.append('这些图标槽的源图没有随包分发(超出图标配额或不适合进包):%s。'
|
|
2412
|
-
'槽位保留了坐标,渲染时留空或用中性占位,不要自造图形去填。'
|
|
2413
|
-
% '、'.join('%s %s' % (n, b) for n, b in dropped_slots[:8]))
|
|
2414
3387
|
# 「没命中映射表」不等于「装不上」:降级目标本身(Noto Sans SC 之类)和 Office 出厂体
|
|
2415
3388
|
# 都不在 match 列里,但它们本来就可用。真正危险的是**既没命中、又不是已知可用字体**的
|
|
2416
3389
|
# 那种——design.md 的字体栈里留着一个消费端装不上的商业字体名,且没有任何降级说明。
|
|
@@ -2438,7 +3411,7 @@ def main(argv=None):
|
|
|
2438
3411
|
exceptions.append('源 deck 第 %s 页是单页孤例,没有归纳成 archetype;需要类似构图时按最接近的页型改。'
|
|
2439
3412
|
% '、'.join(map(str, leftover)))
|
|
2440
3413
|
|
|
2441
|
-
emit_manifest(d, assets, ldir)
|
|
3414
|
+
emit_manifest(d, assets, selected_vision_groups, ldir, archetypes)
|
|
2442
3415
|
emit_frontmatter(d, tokens, fonts, roles, anchors, gaps, ldir)
|
|
2443
3416
|
# 每张背景量一次局部对比度,作为「哪里不能压文字」的客观依据摆进判断单。
|
|
2444
3417
|
# 只报测到的数,不替人填 avoid——哪块算主体、要不要避让,是看图才能定的。
|
|
@@ -2453,16 +3426,17 @@ def main(argv=None):
|
|
|
2453
3426
|
for a in archetypes:
|
|
2454
3427
|
a['flow'] = draft_flow(a, facts.get(a['name']) or {}, (cW, cH))
|
|
2455
3428
|
emit_layouts(archetypes, ldir, busy_hints, facts, recipes)
|
|
2456
|
-
emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, ldir
|
|
2457
|
-
|
|
2458
|
-
|
|
3429
|
+
emit_body(d, tokens, fonts, roles, assets, archetypes, exceptions, cusage, ldir,
|
|
3430
|
+
has_asset_candidates=any(needs_asset_judgment(c) for c in cands))
|
|
3431
|
+
emit_brief(d, (tokens, rest, fonts, roles, assets, rejected, todos, archetypes, cands, sheets,
|
|
3432
|
+
selected_vision_groups, omitted_vision_groups, leftover, lsheet), ldir)
|
|
2459
3433
|
|
|
2460
3434
|
# 这几行落在模型判断「skill 是不是做完了」的那一刻。只报数就会被读成「包已生成」,
|
|
2461
3435
|
# 于是判断和打包整段被跳过,deck 拿不到任何版式坐标。所以这里报进度与下一条命令。
|
|
2462
3436
|
print('第 1/3 步完成,判断单草案 -> %s' % ldir)
|
|
2463
3437
|
print(' 待你确认:资产 %d(%s) 版式 %d 色 %d 字体 %d'
|
|
2464
3438
|
% (len(assets), ', '.join(x['id'] for x in assets), len(archetypes), len(tokens), len(fonts)))
|
|
2465
|
-
print(' 第 2 步 读 l-out/BRIEF.md
|
|
3439
|
+
print(' 第 2 步 读 l-out/BRIEF.md,并行看视觉组拼版与版式图;版式判断只改 layout-controls.yaml')
|
|
2466
3440
|
print(' 第 3 步 package.py 产出 design.md + layouts.md —— deck 的版式坐标只从这两份读')
|
|
2467
3441
|
sys.stdout.flush()
|
|
2468
3442
|
return 0
|