@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
|
@@ -4,12 +4,23 @@ import os
|
|
|
4
4
|
import sys
|
|
5
5
|
import tempfile
|
|
6
6
|
import unittest
|
|
7
|
+
from unittest import mock
|
|
7
8
|
|
|
8
9
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
9
10
|
|
|
11
|
+
import draft # noqa: E402
|
|
10
12
|
from check_v2 import Pack, rule_v2_16 # noqa: E402
|
|
11
|
-
from draft import
|
|
12
|
-
|
|
13
|
+
from draft import (asset_vision_contexts, bound_visual_candidates, # noqa: E402
|
|
14
|
+
build_asset_vision_groups, contact_sheets, draft_layouts,
|
|
15
|
+
cover_background_media,
|
|
16
|
+
emit_asset_vision_groups, emit_layouts, emit_manifest,
|
|
17
|
+
fullscreen_overlay_media,
|
|
18
|
+
needs_asset_judgment, preserve_image_bearing_groups,
|
|
19
|
+
select_asset_vision_groups, slide_image_marks, slot_style,
|
|
20
|
+
visual_slot_candidates)
|
|
21
|
+
from package import (FONTSIZE_RE, Fail, apply_asset_decisions, # noqa: E402
|
|
22
|
+
build_layouts_md, resolve_asset_candidate_lines,
|
|
23
|
+
split_top_blocks)
|
|
13
24
|
|
|
14
25
|
|
|
15
26
|
class LayoutCssTest(unittest.TestCase):
|
|
@@ -62,8 +73,13 @@ class LayoutCssTest(unittest.TestCase):
|
|
|
62
73
|
emit_layouts([archetype], output_dir)
|
|
63
74
|
with open(os.path.join(output_dir, 'layouts.yaml'), encoding='utf-8') as stream:
|
|
64
75
|
layouts_yaml = stream.read()
|
|
76
|
+
with open(os.path.join(output_dir, 'layout-controls.yaml'), encoding='utf-8') as stream:
|
|
77
|
+
layout_controls = stream.read()
|
|
65
78
|
layouts_md = build_layouts_md(split_top_blocks(layouts_yaml), (1920, 1080))
|
|
66
79
|
|
|
80
|
+
self.assertIn('names:', layout_controls)
|
|
81
|
+
self.assertNotIn('\nlayouts:\n', layout_controls)
|
|
82
|
+
self.assertNotIn('box: [100, 80, 800, 160]', layout_controls)
|
|
67
83
|
self.assertIn(
|
|
68
84
|
'css: "box-sizing: border-box; padding: 8px 10px 6px 12px; '
|
|
69
85
|
'font-size: 48px; font-weight: 600; font-style: italic; '
|
|
@@ -93,6 +109,1507 @@ class LayoutCssTest(unittest.TestCase):
|
|
|
93
109
|
self.assertEqual(len(result.fails), 1)
|
|
94
110
|
self.assertIn('旧样式键 align/size', result.fails[0])
|
|
95
111
|
|
|
112
|
+
def test_normautofit_fontscale_shrinks_emitted_font_size(self):
|
|
113
|
+
# 章节大号数字:160px 字号靠 normAutofit fontScale 0.9 装进 144px 的框。
|
|
114
|
+
# 不乘 fontScale,消费端拿到 160px,字比框高,渐变裁切把底部切成透明。
|
|
115
|
+
shape = {
|
|
116
|
+
'text': {
|
|
117
|
+
'bodyPr': {'anchor': 't', 'font_scale': 0.9, 'ln_spc_reduction': 0.1},
|
|
118
|
+
'lstStyle': {'lvl1pPr': {'sz_px': 160, 'lnSpc': {'mult': 1.0}}},
|
|
119
|
+
'paragraphs': [{'runs': [{'text': '01.'}]}],
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
style = slot_style(shape)
|
|
123
|
+
self.assertIn('font-size: 144px', style['css'])
|
|
124
|
+
self.assertNotIn('font-size: 160px', style['css'])
|
|
125
|
+
# lnSpcReduction 0.1 把 1.0*1.2 的行高压到 1.08
|
|
126
|
+
self.assertIn('line-height: 1.08', style['css'])
|
|
127
|
+
|
|
128
|
+
def test_missing_autofit_leaves_font_size_untouched(self):
|
|
129
|
+
# 无 normAutofit(或无 fontScale)时零影响:字号原样、行高不缩。
|
|
130
|
+
shape = {
|
|
131
|
+
'text': {
|
|
132
|
+
'bodyPr': {'anchor': 't'},
|
|
133
|
+
'lstStyle': {'lvl1pPr': {'sz_px': 160, 'lnSpc': {'mult': 1.0}}},
|
|
134
|
+
'paragraphs': [{'runs': [{'text': '01.'}]}],
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
style = slot_style(shape)
|
|
138
|
+
self.assertIn('font-size: 160px', style['css'])
|
|
139
|
+
self.assertIn('line-height: 1.2', style['css'])
|
|
140
|
+
|
|
141
|
+
def test_run_level_style_is_emitted_from_flat_ooxml_record(self):
|
|
142
|
+
# read_txbody 把 a:rPr 的属性直接展开到 run;没有嵌套 rPr 字段。
|
|
143
|
+
shape = {
|
|
144
|
+
'text': {
|
|
145
|
+
'bodyPr': {'anchor': 't'},
|
|
146
|
+
'paragraphs': [{
|
|
147
|
+
'runs': [{
|
|
148
|
+
'text': '品牌标题',
|
|
149
|
+
'sz_px': 52,
|
|
150
|
+
'weight': 700,
|
|
151
|
+
'latin': 'Brand Sans',
|
|
152
|
+
'color': {'resolved': '#C1121F'},
|
|
153
|
+
}],
|
|
154
|
+
}],
|
|
155
|
+
},
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
style = slot_style(shape)
|
|
159
|
+
|
|
160
|
+
self.assertIn('font-size: 52px', style['css'])
|
|
161
|
+
self.assertIn(
|
|
162
|
+
'font-family: "Brand Sans", "PingFang SC", "Microsoft YaHei", sans-serif',
|
|
163
|
+
style['css'],
|
|
164
|
+
)
|
|
165
|
+
self.assertIn('font-weight: 700', style['css'])
|
|
166
|
+
self.assertIn('color: #C1121F', style['css'])
|
|
167
|
+
|
|
168
|
+
def test_run_level_style_overrides_inherited_list_style(self):
|
|
169
|
+
shape = {
|
|
170
|
+
'text': {
|
|
171
|
+
'lstStyle': {
|
|
172
|
+
'lvl1pPr': {
|
|
173
|
+
'sz_px': 20,
|
|
174
|
+
'latin': 'Inherited Sans',
|
|
175
|
+
'color': {'resolved': '#111111'},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
'paragraphs': [{
|
|
179
|
+
'runs': [{
|
|
180
|
+
'text': '直接格式',
|
|
181
|
+
'sz_px': 52,
|
|
182
|
+
'latin': 'Direct Sans',
|
|
183
|
+
'color': {'resolved': '#C1121F'},
|
|
184
|
+
}],
|
|
185
|
+
}],
|
|
186
|
+
},
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
style = slot_style(shape)
|
|
190
|
+
|
|
191
|
+
self.assertIn('font-size: 52px', style['css'])
|
|
192
|
+
self.assertIn('font-family: "Direct Sans"', style['css'])
|
|
193
|
+
self.assertIn('color: #C1121F', style['css'])
|
|
194
|
+
self.assertNotIn('font-size: 20px', style['css'])
|
|
195
|
+
self.assertNotIn('Inherited Sans', style['css'])
|
|
196
|
+
|
|
197
|
+
def test_asset_decisions_keep_content_slots_and_bind_texture_slots(self):
|
|
198
|
+
manifest = {
|
|
199
|
+
'assets': [],
|
|
200
|
+
'asset_decisions': [
|
|
201
|
+
{'source_media': 'chart.png', 'decision': 'content'},
|
|
202
|
+
{'source_media': 'ornament.png', 'decision': 'texture'},
|
|
203
|
+
],
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
207
|
+
lines = resolve_asset_candidate_lines([
|
|
208
|
+
' - {role: asset-candidate, box: [10, 20, 300, 200], type: pic, '
|
|
209
|
+
'source_media: chart.png}',
|
|
210
|
+
' - {role: asset-candidate, box: [20, 30, 80, 80], type: pic, '
|
|
211
|
+
'source_media: ornament.png}',
|
|
212
|
+
], decisions, asset_ids)
|
|
213
|
+
|
|
214
|
+
self.assertEqual(len(lines), 2)
|
|
215
|
+
self.assertNotIn('chart.png', lines[0])
|
|
216
|
+
self.assertNotIn('source_media', lines[0])
|
|
217
|
+
self.assertNotIn('asset:', lines[0])
|
|
218
|
+
self.assertIn('role: pic', lines[0])
|
|
219
|
+
self.assertNotIn('source_media', lines[1])
|
|
220
|
+
self.assertIn('role: texture', lines[1])
|
|
221
|
+
self.assertIn('asset: texture-1', lines[1])
|
|
222
|
+
self.assertEqual(manifest['assets'], [{
|
|
223
|
+
'id': 'texture-1',
|
|
224
|
+
'source_media': 'ornament.png',
|
|
225
|
+
'kind': 'texture',
|
|
226
|
+
}])
|
|
227
|
+
|
|
228
|
+
def test_faas_visual_groups_map_content_and_decorative_without_leaking_to_pack(self):
|
|
229
|
+
manifest = {
|
|
230
|
+
'assets': [],
|
|
231
|
+
'asset_vision_groups': [
|
|
232
|
+
{
|
|
233
|
+
'id': 'vision-1',
|
|
234
|
+
'source_media': '[partner-logo.png, ornament.png]',
|
|
235
|
+
'visual_kind': 'content-image',
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
'id': 'vision-2',
|
|
239
|
+
'source_media': '[illustration.png]',
|
|
240
|
+
'visual_kind': 'illustration',
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
'asset_decisions': [
|
|
244
|
+
{'source_media': 'ornament.png', 'visual_kind': 'decorative'},
|
|
245
|
+
],
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
249
|
+
lines = resolve_asset_candidate_lines([
|
|
250
|
+
' - {role: asset-candidate, box: [10, 20, 300, 200], type: pic, '
|
|
251
|
+
'source_media: partner-logo.png}',
|
|
252
|
+
' - {role: asset-candidate, box: [20, 30, 80, 80], type: pic, '
|
|
253
|
+
'source_media: ornament.png}',
|
|
254
|
+
' - {role: asset-candidate, box: [40, 50, 60, 60], type: pic, '
|
|
255
|
+
'source_media: illustration.png}',
|
|
256
|
+
], decisions, asset_ids)
|
|
257
|
+
|
|
258
|
+
self.assertEqual(decisions['partner-logo.png'], 'content')
|
|
259
|
+
self.assertEqual(decisions['ornament.png'], 'texture')
|
|
260
|
+
self.assertEqual(decisions['illustration.png'], 'texture')
|
|
261
|
+
self.assertEqual(
|
|
262
|
+
[asset['kind'] for asset in manifest['assets']],
|
|
263
|
+
['texture', 'texture'],
|
|
264
|
+
)
|
|
265
|
+
self.assertEqual(lines[0], ' - {role: pic, box: [10, 20, 300, 200], type: pic}')
|
|
266
|
+
self.assertIn('asset: texture-1', lines[1])
|
|
267
|
+
self.assertIn('asset: texture-2', lines[2])
|
|
268
|
+
|
|
269
|
+
def test_same_source_same_box_uses_its_layout_specific_vision_decision(self):
|
|
270
|
+
manifest = {
|
|
271
|
+
'assets': [],
|
|
272
|
+
'asset_vision_groups': [
|
|
273
|
+
{
|
|
274
|
+
'id': 'vision-1',
|
|
275
|
+
'source_media': 'shared.png',
|
|
276
|
+
'layout': 'layout-1',
|
|
277
|
+
'box': '[100, 200, 80, 80]',
|
|
278
|
+
'visual_kind': 'content-image',
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
'id': 'vision-2',
|
|
282
|
+
'source_media': 'shared.png',
|
|
283
|
+
'layout': 'layout-2',
|
|
284
|
+
'box': '[100, 200, 80, 80]',
|
|
285
|
+
'visual_kind': 'decorative',
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
291
|
+
lines = resolve_asset_candidate_lines([
|
|
292
|
+
' layout-1:',
|
|
293
|
+
' slots:',
|
|
294
|
+
' - {role: asset-candidate, box: [100, 200, 80, 80], type: pic, '
|
|
295
|
+
'source_media: shared.png}',
|
|
296
|
+
' layout-2:',
|
|
297
|
+
' slots:',
|
|
298
|
+
' - {role: asset-candidate, box: [100, 200, 80, 80], type: pic, '
|
|
299
|
+
'source_media: shared.png}',
|
|
300
|
+
], decisions, asset_ids)
|
|
301
|
+
|
|
302
|
+
self.assertEqual(lines[2], ' - {role: pic, box: [100, 200, 80, 80], type: pic}')
|
|
303
|
+
self.assertIn('role: texture', lines[5])
|
|
304
|
+
self.assertIn('asset: texture-1', lines[5])
|
|
305
|
+
|
|
306
|
+
def test_build_layouts_keeps_instance_only_asset_decisions(self):
|
|
307
|
+
manifest = {
|
|
308
|
+
'assets': [],
|
|
309
|
+
'asset_vision_groups': [{
|
|
310
|
+
'id': 'vision-1',
|
|
311
|
+
'source_media': 'cover-illustration.png',
|
|
312
|
+
'layout': 'layout-1',
|
|
313
|
+
'box': '[100, 200, 80, 80]',
|
|
314
|
+
'visual_kind': 'illustration',
|
|
315
|
+
}],
|
|
316
|
+
}
|
|
317
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
318
|
+
layouts_yaml = '''\
|
|
319
|
+
names:
|
|
320
|
+
layout-1: "封面"
|
|
321
|
+
roles:
|
|
322
|
+
layout-1: cover
|
|
323
|
+
layouts:
|
|
324
|
+
layout-1:
|
|
325
|
+
role: content
|
|
326
|
+
slots:
|
|
327
|
+
- {role: asset-candidate, box: [100, 200, 80, 80], type: pic, source_media: cover-illustration.png}
|
|
328
|
+
confidence: high
|
|
329
|
+
'''
|
|
330
|
+
|
|
331
|
+
layouts_md = build_layouts_md(
|
|
332
|
+
split_top_blocks(layouts_yaml), (1920, 1080), decisions, asset_ids)
|
|
333
|
+
|
|
334
|
+
self.assertIn(
|
|
335
|
+
'{role: texture, box: [100, 200, 80, 80], type: pic, asset: texture-1}',
|
|
336
|
+
layouts_md,
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
def test_duplicate_instance_vision_decisions_are_idempotent(self):
|
|
340
|
+
manifest = {
|
|
341
|
+
'assets': [],
|
|
342
|
+
'asset_vision_groups': [
|
|
343
|
+
{
|
|
344
|
+
'id': 'vision-1',
|
|
345
|
+
'source_media': 'cover-illustration.png',
|
|
346
|
+
'layout': 'layout-1',
|
|
347
|
+
'box': '[100, 200, 80, 80]',
|
|
348
|
+
'visual_kind': 'illustration',
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
'id': 'vision-2',
|
|
352
|
+
'source_media': 'cover-illustration.png',
|
|
353
|
+
'layout': 'layout-1',
|
|
354
|
+
'box': '[100, 200, 80, 80]',
|
|
355
|
+
'visual_kind': 'illustration',
|
|
356
|
+
},
|
|
357
|
+
],
|
|
358
|
+
'asset_decisions': [{
|
|
359
|
+
'source_media': 'cover-illustration.png',
|
|
360
|
+
'layout': 'layout-1',
|
|
361
|
+
'box': '[100, 200, 80, 80]',
|
|
362
|
+
'visual_kind': 'illustration',
|
|
363
|
+
}],
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
367
|
+
lines = resolve_asset_candidate_lines([
|
|
368
|
+
' layout-1:',
|
|
369
|
+
' slots:',
|
|
370
|
+
' - {role: asset-candidate, box: [100, 200, 80, 80], type: pic, '
|
|
371
|
+
'source_media: cover-illustration.png}',
|
|
372
|
+
], decisions, asset_ids)
|
|
373
|
+
|
|
374
|
+
self.assertIn('role: texture', lines[2])
|
|
375
|
+
self.assertIn('asset: texture-1', lines[2])
|
|
376
|
+
self.assertEqual(manifest['assets'], [{
|
|
377
|
+
'id': 'texture-1',
|
|
378
|
+
'source_media': 'cover-illustration.png',
|
|
379
|
+
'kind': 'texture',
|
|
380
|
+
}])
|
|
381
|
+
|
|
382
|
+
def test_duplicate_instance_vision_decisions_reject_conflicting_kinds(self):
|
|
383
|
+
manifest = {
|
|
384
|
+
'assets': [],
|
|
385
|
+
'asset_vision_groups': [
|
|
386
|
+
{
|
|
387
|
+
'id': 'vision-1',
|
|
388
|
+
'source_media': 'cover-illustration.png',
|
|
389
|
+
'layout': 'layout-1',
|
|
390
|
+
'box': '[100, 200, 80, 80]',
|
|
391
|
+
'visual_kind': 'illustration',
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
'id': 'vision-2',
|
|
395
|
+
'source_media': 'cover-illustration.png',
|
|
396
|
+
'layout': 'layout-1',
|
|
397
|
+
'box': '[100, 200, 80, 80]',
|
|
398
|
+
'visual_kind': 'decorative',
|
|
399
|
+
},
|
|
400
|
+
],
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
with self.assertRaisesRegex(Fail, '实例判断冲突'):
|
|
404
|
+
apply_asset_decisions(manifest)
|
|
405
|
+
|
|
406
|
+
def test_asset_decision_overrides_a_vision_group_instance(self):
|
|
407
|
+
manifest = {
|
|
408
|
+
'assets': [],
|
|
409
|
+
'asset_vision_groups': [{
|
|
410
|
+
'id': 'vision-1',
|
|
411
|
+
'source_media': 'cover-illustration.png',
|
|
412
|
+
'layout': 'layout-1',
|
|
413
|
+
'box': '[100, 200, 80, 80]',
|
|
414
|
+
'visual_kind': 'illustration',
|
|
415
|
+
}],
|
|
416
|
+
'asset_decisions': [{
|
|
417
|
+
'source_media': 'cover-illustration.png',
|
|
418
|
+
'layout': 'layout-1',
|
|
419
|
+
'box': '[100, 200, 80, 80]',
|
|
420
|
+
'visual_kind': 'content-image',
|
|
421
|
+
}],
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
425
|
+
|
|
426
|
+
self.assertEqual(
|
|
427
|
+
decisions.for_slot(
|
|
428
|
+
'cover-illustration.png',
|
|
429
|
+
'box: [100, 200, 80, 80]',
|
|
430
|
+
'layout-1',
|
|
431
|
+
),
|
|
432
|
+
'content',
|
|
433
|
+
)
|
|
434
|
+
self.assertEqual(asset_ids, {})
|
|
435
|
+
self.assertEqual(manifest['assets'], [])
|
|
436
|
+
|
|
437
|
+
def test_instance_content_override_removes_unused_source_asset(self):
|
|
438
|
+
manifest = {
|
|
439
|
+
'assets': [{
|
|
440
|
+
'id': 'texture-old',
|
|
441
|
+
'source_media': 'cover-illustration.png',
|
|
442
|
+
'kind': 'texture',
|
|
443
|
+
}],
|
|
444
|
+
'asset_vision_groups': [{
|
|
445
|
+
'id': 'vision-1',
|
|
446
|
+
'source_media': 'cover-illustration.png',
|
|
447
|
+
'visual_kind': 'illustration',
|
|
448
|
+
}],
|
|
449
|
+
'asset_decisions': [{
|
|
450
|
+
'source_media': 'cover-illustration.png',
|
|
451
|
+
'layout': 'layout-1',
|
|
452
|
+
'box': '[100, 200, 80, 80]',
|
|
453
|
+
'visual_kind': 'content-image',
|
|
454
|
+
}],
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
decisions, asset_ids = apply_asset_decisions(
|
|
458
|
+
manifest,
|
|
459
|
+
bound_sources={'cover-illustration.png'},
|
|
460
|
+
bound_slots={
|
|
461
|
+
'cover-illustration.png': {('layout-1', (100, 200, 80, 80))},
|
|
462
|
+
},
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
self.assertEqual(
|
|
466
|
+
decisions.for_slot(
|
|
467
|
+
'cover-illustration.png',
|
|
468
|
+
'box: [100, 200, 80, 80]',
|
|
469
|
+
'layout-1',
|
|
470
|
+
),
|
|
471
|
+
'content',
|
|
472
|
+
)
|
|
473
|
+
self.assertEqual(asset_ids, {})
|
|
474
|
+
self.assertEqual(manifest['assets'], [])
|
|
475
|
+
|
|
476
|
+
def test_instance_decision_requires_an_exact_layout_slot(self):
|
|
477
|
+
manifest = {
|
|
478
|
+
'assets': [],
|
|
479
|
+
'asset_vision_groups': [{
|
|
480
|
+
'id': 'vision-1',
|
|
481
|
+
'source_media': 'cover-illustration.png',
|
|
482
|
+
'layout': 'layout-mistyped',
|
|
483
|
+
'box': '[100, 200, 80, 80]',
|
|
484
|
+
'visual_kind': 'illustration',
|
|
485
|
+
}],
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
with self.assertRaisesRegex(Fail, '实例没有对应图片槽'):
|
|
489
|
+
apply_asset_decisions(
|
|
490
|
+
manifest,
|
|
491
|
+
bound_sources={'cover-illustration.png'},
|
|
492
|
+
bound_slots={
|
|
493
|
+
'cover-illustration.png': {('layout-1', (100, 200, 80, 80))},
|
|
494
|
+
},
|
|
495
|
+
)
|
|
496
|
+
|
|
497
|
+
def test_instance_decision_without_layout_matches_any_layout_at_its_box(self):
|
|
498
|
+
manifest = {
|
|
499
|
+
'assets': [],
|
|
500
|
+
'asset_vision_groups': [{
|
|
501
|
+
'id': 'vision-1',
|
|
502
|
+
'source_media': 'cover-illustration.png',
|
|
503
|
+
'box': '[100, 200, 80, 80]',
|
|
504
|
+
'visual_kind': 'illustration',
|
|
505
|
+
}],
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
_, asset_ids = apply_asset_decisions(
|
|
509
|
+
manifest,
|
|
510
|
+
bound_sources={'cover-illustration.png'},
|
|
511
|
+
bound_slots={
|
|
512
|
+
'cover-illustration.png': {('layout-1', (100, 200, 80, 80))},
|
|
513
|
+
},
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
self.assertEqual(asset_ids, {'cover-illustration.png': 'texture-1'})
|
|
517
|
+
|
|
518
|
+
def test_source_asset_decision_overrides_a_vision_group_instance(self):
|
|
519
|
+
manifest = {
|
|
520
|
+
'assets': [],
|
|
521
|
+
'asset_vision_groups': [{
|
|
522
|
+
'id': 'vision-1',
|
|
523
|
+
'source_media': 'cover-illustration.png',
|
|
524
|
+
'layout': 'layout-1',
|
|
525
|
+
'box': '[100, 200, 80, 80]',
|
|
526
|
+
'visual_kind': 'illustration',
|
|
527
|
+
}],
|
|
528
|
+
'asset_decisions': [{
|
|
529
|
+
'source_media': 'cover-illustration.png',
|
|
530
|
+
'visual_kind': 'content-image',
|
|
531
|
+
}],
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
535
|
+
|
|
536
|
+
self.assertEqual(
|
|
537
|
+
decisions.for_slot(
|
|
538
|
+
'cover-illustration.png',
|
|
539
|
+
'box: [100, 200, 80, 80]',
|
|
540
|
+
'layout-1',
|
|
541
|
+
),
|
|
542
|
+
'content',
|
|
543
|
+
)
|
|
544
|
+
self.assertEqual(asset_ids, {})
|
|
545
|
+
self.assertEqual(manifest['assets'], [])
|
|
546
|
+
|
|
547
|
+
def test_source_asset_decision_overrides_every_instance_of_its_source(self):
|
|
548
|
+
manifest = {
|
|
549
|
+
'assets': [],
|
|
550
|
+
'asset_vision_groups': [
|
|
551
|
+
{
|
|
552
|
+
'id': 'vision-1',
|
|
553
|
+
'source_media': 'cover-illustration.png',
|
|
554
|
+
'layout': 'layout-1',
|
|
555
|
+
'box': '[100, 200, 80, 80]',
|
|
556
|
+
'visual_kind': 'illustration',
|
|
557
|
+
},
|
|
558
|
+
{
|
|
559
|
+
'id': 'vision-2',
|
|
560
|
+
'source_media': 'cover-illustration.png',
|
|
561
|
+
'layout': 'layout-2',
|
|
562
|
+
'box': '[200, 200, 80, 80]',
|
|
563
|
+
'visual_kind': 'decorative',
|
|
564
|
+
},
|
|
565
|
+
],
|
|
566
|
+
'asset_decisions': [{
|
|
567
|
+
'source_media': 'cover-illustration.png',
|
|
568
|
+
'visual_kind': 'content-image',
|
|
569
|
+
}],
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
573
|
+
|
|
574
|
+
self.assertEqual(
|
|
575
|
+
decisions.for_scope(
|
|
576
|
+
'cover-illustration.png', 'layout-1', (100, 200, 80, 80)),
|
|
577
|
+
'content',
|
|
578
|
+
)
|
|
579
|
+
self.assertEqual(
|
|
580
|
+
decisions.for_scope(
|
|
581
|
+
'cover-illustration.png', 'layout-2', (200, 200, 80, 80)),
|
|
582
|
+
'content',
|
|
583
|
+
)
|
|
584
|
+
self.assertEqual(asset_ids, {})
|
|
585
|
+
self.assertEqual(manifest['assets'], [])
|
|
586
|
+
|
|
587
|
+
def test_asset_decisions_reject_conflicting_instance_kinds(self):
|
|
588
|
+
manifest = {
|
|
589
|
+
'assets': [],
|
|
590
|
+
'asset_decisions': [
|
|
591
|
+
{
|
|
592
|
+
'source_media': 'cover-illustration.png',
|
|
593
|
+
'layout': 'layout-1',
|
|
594
|
+
'box': '[100, 200, 80, 80]',
|
|
595
|
+
'visual_kind': 'illustration',
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
'source_media': 'cover-illustration.png',
|
|
599
|
+
'layout': 'layout-1',
|
|
600
|
+
'box': '[100, 200, 80, 80]',
|
|
601
|
+
'visual_kind': 'decorative',
|
|
602
|
+
},
|
|
603
|
+
],
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
with self.assertRaisesRegex(Fail, '实例判断冲突'):
|
|
607
|
+
apply_asset_decisions(manifest)
|
|
608
|
+
|
|
609
|
+
def test_conflicting_background_roles_are_rejected(self):
|
|
610
|
+
manifest = {
|
|
611
|
+
'assets': [],
|
|
612
|
+
'asset_vision_groups': [
|
|
613
|
+
{
|
|
614
|
+
'id': 'vision-1',
|
|
615
|
+
'source_media': 'shared-background.png',
|
|
616
|
+
'visual_kind': 'background',
|
|
617
|
+
'role': 'cover',
|
|
618
|
+
},
|
|
619
|
+
{
|
|
620
|
+
'id': 'vision-2',
|
|
621
|
+
'source_media': 'shared-background.png',
|
|
622
|
+
'visual_kind': 'background',
|
|
623
|
+
'role': 'content',
|
|
624
|
+
},
|
|
625
|
+
],
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
with self.assertRaisesRegex(Fail, 'source_media role 冲突'):
|
|
629
|
+
apply_asset_decisions(manifest)
|
|
630
|
+
|
|
631
|
+
def test_asset_decisions_reject_conflicting_background_roles(self):
|
|
632
|
+
manifest = {
|
|
633
|
+
'assets': [],
|
|
634
|
+
'asset_decisions': [
|
|
635
|
+
{
|
|
636
|
+
'source_media': 'shared-background.png',
|
|
637
|
+
'visual_kind': 'background',
|
|
638
|
+
'role': 'cover',
|
|
639
|
+
},
|
|
640
|
+
{
|
|
641
|
+
'source_media': 'shared-background.png',
|
|
642
|
+
'visual_kind': 'background',
|
|
643
|
+
'role': 'content',
|
|
644
|
+
},
|
|
645
|
+
],
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
with self.assertRaisesRegex(Fail, 'source_media role 冲突'):
|
|
649
|
+
apply_asset_decisions(manifest)
|
|
650
|
+
|
|
651
|
+
def test_asset_decision_rejects_a_conflicting_vision_background_role(self):
|
|
652
|
+
manifest = {
|
|
653
|
+
'assets': [],
|
|
654
|
+
'asset_vision_groups': [{
|
|
655
|
+
'id': 'vision-1',
|
|
656
|
+
'source_media': 'shared-background.png',
|
|
657
|
+
'visual_kind': 'background',
|
|
658
|
+
'role': 'cover',
|
|
659
|
+
}],
|
|
660
|
+
'asset_decisions': [{
|
|
661
|
+
'source_media': 'shared-background.png',
|
|
662
|
+
'visual_kind': 'background',
|
|
663
|
+
'role': 'content',
|
|
664
|
+
}],
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
with self.assertRaisesRegex(Fail, 'source_media role 冲突'):
|
|
668
|
+
apply_asset_decisions(manifest)
|
|
669
|
+
|
|
670
|
+
def test_background_roles_conflict_at_the_same_slot_across_sources(self):
|
|
671
|
+
manifest = {
|
|
672
|
+
'assets': [],
|
|
673
|
+
'asset_vision_groups': [
|
|
674
|
+
{
|
|
675
|
+
'id': 'vision-cover',
|
|
676
|
+
'source_media': 'cover-bg.png',
|
|
677
|
+
'layout': 'cover',
|
|
678
|
+
'box': '[0, 0, 1920, 1080]',
|
|
679
|
+
'visual_kind': 'background',
|
|
680
|
+
'role': 'cover',
|
|
681
|
+
},
|
|
682
|
+
{
|
|
683
|
+
'id': 'vision-content',
|
|
684
|
+
'source_media': 'content-bg.png',
|
|
685
|
+
'layout': 'cover',
|
|
686
|
+
'box': '[0, 0, 1920, 1080]',
|
|
687
|
+
'visual_kind': 'background',
|
|
688
|
+
'role': 'content',
|
|
689
|
+
},
|
|
690
|
+
],
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
with self.assertRaisesRegex(Fail, '背景实例 role 冲突'):
|
|
694
|
+
apply_asset_decisions(manifest)
|
|
695
|
+
|
|
696
|
+
def test_same_source_instances_bind_assets_by_their_final_decisions(self):
|
|
697
|
+
manifest = {
|
|
698
|
+
'assets': [],
|
|
699
|
+
'asset_vision_groups': [
|
|
700
|
+
{
|
|
701
|
+
'id': 'vision-1',
|
|
702
|
+
'source_media': 'shared.png',
|
|
703
|
+
'layout': 'layout-1',
|
|
704
|
+
'box': '[100, 200, 80, 80]',
|
|
705
|
+
'visual_kind': 'logo',
|
|
706
|
+
},
|
|
707
|
+
{
|
|
708
|
+
'id': 'vision-2',
|
|
709
|
+
'source_media': 'shared.png',
|
|
710
|
+
'layout': 'layout-2',
|
|
711
|
+
'box': '[100, 200, 80, 80]',
|
|
712
|
+
'visual_kind': 'decorative',
|
|
713
|
+
},
|
|
714
|
+
],
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
decisions, asset_ids = apply_asset_decisions(manifest)
|
|
718
|
+
lines = resolve_asset_candidate_lines([
|
|
719
|
+
' layout-1:',
|
|
720
|
+
' slots:',
|
|
721
|
+
' - {role: asset-candidate, box: [100, 200, 80, 80], type: pic, '
|
|
722
|
+
'source_media: shared.png}',
|
|
723
|
+
' layout-2:',
|
|
724
|
+
' slots:',
|
|
725
|
+
' - {role: asset-candidate, box: [100, 200, 80, 80], type: pic, '
|
|
726
|
+
'source_media: shared.png}',
|
|
727
|
+
], decisions, asset_ids)
|
|
728
|
+
|
|
729
|
+
self.assertIn('role: logo', lines[2])
|
|
730
|
+
self.assertIn('asset: logo-1', lines[2])
|
|
731
|
+
self.assertIn('role: texture', lines[5])
|
|
732
|
+
self.assertIn('asset: texture-1', lines[5])
|
|
733
|
+
self.assertEqual(
|
|
734
|
+
[(asset['id'], asset['kind']) for asset in manifest['assets']],
|
|
735
|
+
[('logo-1', 'logo'), ('texture-1', 'texture')],
|
|
736
|
+
)
|
|
737
|
+
|
|
738
|
+
def test_asset_decisions_do_not_reuse_an_existing_asset_id(self):
|
|
739
|
+
manifest = {
|
|
740
|
+
'assets': [
|
|
741
|
+
{
|
|
742
|
+
'id': 'logo-primary',
|
|
743
|
+
'source_media': 'candidate.png',
|
|
744
|
+
'kind': 'logo',
|
|
745
|
+
},
|
|
746
|
+
{
|
|
747
|
+
'id': 'texture-1',
|
|
748
|
+
'source_media': 'existing.png',
|
|
749
|
+
'kind': 'texture',
|
|
750
|
+
},
|
|
751
|
+
],
|
|
752
|
+
'asset_decisions': [
|
|
753
|
+
{'source_media': 'candidate.png', 'decision': 'texture'},
|
|
754
|
+
],
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
_, asset_ids = apply_asset_decisions(manifest)
|
|
758
|
+
|
|
759
|
+
self.assertEqual(asset_ids['existing.png'], 'texture-1')
|
|
760
|
+
self.assertEqual(asset_ids['candidate.png'], 'texture-2')
|
|
761
|
+
self.assertEqual(
|
|
762
|
+
[asset['id'] for asset in manifest['assets']],
|
|
763
|
+
['texture-2', 'texture-1'],
|
|
764
|
+
)
|
|
765
|
+
|
|
766
|
+
def test_asset_decisions_reject_candidates_without_layout_slots(self):
|
|
767
|
+
manifest = {
|
|
768
|
+
'assets': [],
|
|
769
|
+
'asset_decisions': [
|
|
770
|
+
{'source_media': 'orphan-decoration.png', 'decision': 'texture'},
|
|
771
|
+
],
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
with self.assertRaisesRegex(Fail, '没有对应图片槽'):
|
|
775
|
+
apply_asset_decisions(manifest, bound_sources=set())
|
|
776
|
+
|
|
777
|
+
def test_asset_vision_groups_reject_candidates_without_layout_slots(self):
|
|
778
|
+
manifest = {
|
|
779
|
+
'assets': [],
|
|
780
|
+
'asset_vision_groups': [{
|
|
781
|
+
'id': 'vision-1',
|
|
782
|
+
'source_media': '[orphan-decoration.png]',
|
|
783
|
+
'visual_kind': 'decorative',
|
|
784
|
+
}],
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
with self.assertRaisesRegex(Fail, '没有对应图片槽'):
|
|
788
|
+
apply_asset_decisions(manifest, bound_sources=set())
|
|
789
|
+
|
|
790
|
+
def test_translucent_fullscreen_decoration_requires_model_judgment(self):
|
|
791
|
+
candidate = {
|
|
792
|
+
'id': 'asset-1',
|
|
793
|
+
'file': 'glow.png',
|
|
794
|
+
'fullscreen': True,
|
|
795
|
+
'slides': [2],
|
|
796
|
+
'probe': {'alpha_mean': 80},
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
800
|
+
emit_manifest({'theme_topology': {}}, [], [{
|
|
801
|
+
'id': 'vision-1',
|
|
802
|
+
'pages': [2],
|
|
803
|
+
'candidates': [dict(candidate, placements=[{
|
|
804
|
+
'slide': 2,
|
|
805
|
+
'archetype': 'layout-2',
|
|
806
|
+
'box': [10, 20, 30, 40],
|
|
807
|
+
}])],
|
|
808
|
+
}], output_dir)
|
|
809
|
+
with open(os.path.join(output_dir, 'manifest.yaml'), encoding='utf-8') as stream:
|
|
810
|
+
manifest = stream.read()
|
|
811
|
+
|
|
812
|
+
self.assertIn('source_media: glow.png', manifest)
|
|
813
|
+
self.assertNotIn(
|
|
814
|
+
' layout: layout-2\n box: [10, 20, 30, 40]',
|
|
815
|
+
manifest,
|
|
816
|
+
)
|
|
817
|
+
self.assertIn('box: [10, 20, 30, 40]', manifest)
|
|
818
|
+
self.assertIn('visual_kind: TODO-visual-kind-asset-1', manifest)
|
|
819
|
+
|
|
820
|
+
def test_asset_vision_groups_follow_faas_page_budgets(self):
|
|
821
|
+
candidates = [
|
|
822
|
+
{
|
|
823
|
+
'id': 'asset-%d' % index,
|
|
824
|
+
'file': 'image-%d.png' % index,
|
|
825
|
+
'placements': [{'slide': 2, 'box': [index, 0, 10, 10]}],
|
|
826
|
+
'slides': [2],
|
|
827
|
+
}
|
|
828
|
+
for index in range(1, 11)
|
|
829
|
+
] + [
|
|
830
|
+
{
|
|
831
|
+
'id': 'asset-11',
|
|
832
|
+
'file': 'image-11.png',
|
|
833
|
+
'placements': [{'slide': 3, 'box': [0, 0, 10, 10]}],
|
|
834
|
+
'slides': [3],
|
|
835
|
+
},
|
|
836
|
+
{
|
|
837
|
+
'id': 'asset-12',
|
|
838
|
+
'file': 'image-12.png',
|
|
839
|
+
'placements': [{'slide': 4, 'box': [0, 0, 10, 10]}],
|
|
840
|
+
'slides': [4],
|
|
841
|
+
},
|
|
842
|
+
]
|
|
843
|
+
|
|
844
|
+
groups = build_asset_vision_groups(candidates)
|
|
845
|
+
|
|
846
|
+
self.assertEqual(
|
|
847
|
+
[(group['pages'], len(group['candidates']), group['input_count']) for group in groups],
|
|
848
|
+
[([2], 9, 10), ([2], 1, 2), ([3, 4], 2, 4)],
|
|
849
|
+
)
|
|
850
|
+
|
|
851
|
+
def test_asset_vision_selection_keeps_all_first_and_last_page_batches(self):
|
|
852
|
+
groups = [
|
|
853
|
+
{
|
|
854
|
+
'id': 'vision-%d' % index,
|
|
855
|
+
'pages': [index],
|
|
856
|
+
'candidates': [],
|
|
857
|
+
'input_count': 1,
|
|
858
|
+
}
|
|
859
|
+
for index in range(1, 9)
|
|
860
|
+
] + [{
|
|
861
|
+
'id': 'vision-9',
|
|
862
|
+
'pages': [8],
|
|
863
|
+
'candidates': [],
|
|
864
|
+
'input_count': 1,
|
|
865
|
+
}]
|
|
866
|
+
|
|
867
|
+
selected, omitted = select_asset_vision_groups(groups, 8)
|
|
868
|
+
|
|
869
|
+
self.assertEqual([group['id'] for group in selected], [
|
|
870
|
+
'vision-1', 'vision-2', 'vision-3', 'vision-8', 'vision-9',
|
|
871
|
+
])
|
|
872
|
+
self.assertEqual([group['id'] for group in omitted], [
|
|
873
|
+
'vision-4', 'vision-5', 'vision-6', 'vision-7',
|
|
874
|
+
])
|
|
875
|
+
|
|
876
|
+
def test_asset_vision_selection_interleaves_first_and_last_page_batches(self):
|
|
877
|
+
groups = [
|
|
878
|
+
{
|
|
879
|
+
'id': 'first-%d' % index,
|
|
880
|
+
'pages': [1],
|
|
881
|
+
'candidates': [],
|
|
882
|
+
'input_count': 1,
|
|
883
|
+
}
|
|
884
|
+
for index in range(1, 4)
|
|
885
|
+
] + [
|
|
886
|
+
{
|
|
887
|
+
'id': 'last-%d' % index,
|
|
888
|
+
'pages': [8],
|
|
889
|
+
'candidates': [],
|
|
890
|
+
'input_count': 1,
|
|
891
|
+
}
|
|
892
|
+
for index in range(1, 4)
|
|
893
|
+
]
|
|
894
|
+
|
|
895
|
+
selected, omitted = select_asset_vision_groups(groups, 8)
|
|
896
|
+
|
|
897
|
+
self.assertEqual(
|
|
898
|
+
{group['id'] for group in selected},
|
|
899
|
+
{'first-1', 'first-2', 'first-3', 'last-1', 'last-2'},
|
|
900
|
+
)
|
|
901
|
+
self.assertEqual([group['id'] for group in omitted], ['last-3'])
|
|
902
|
+
|
|
903
|
+
def test_asset_vision_groups_degrade_when_pillow_is_unavailable(self):
|
|
904
|
+
candidates = [{
|
|
905
|
+
'id': 'asset-1',
|
|
906
|
+
'file': 'ornament.png',
|
|
907
|
+
'out': 'media-out/ornament.png',
|
|
908
|
+
'probe': {'w': 80, 'h': 80},
|
|
909
|
+
'placements': [{'slide': 1, 'box': [10, 20, 80, 80]}],
|
|
910
|
+
}]
|
|
911
|
+
|
|
912
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
913
|
+
lout_dir = os.path.join(output_dir, 'l-out')
|
|
914
|
+
os.makedirs(lout_dir)
|
|
915
|
+
with mock.patch.object(draft, 'has_pillow', return_value=False):
|
|
916
|
+
selected, omitted, sheets = emit_asset_vision_groups(
|
|
917
|
+
output_dir, candidates, 1, lout_dir)
|
|
918
|
+
|
|
919
|
+
self.assertEqual(selected, [])
|
|
920
|
+
self.assertEqual(len(omitted), 1)
|
|
921
|
+
self.assertEqual(sheets, [])
|
|
922
|
+
self.assertTrue(os.path.isfile(
|
|
923
|
+
os.path.join(lout_dir, 'asset-vision-groups.json')))
|
|
924
|
+
|
|
925
|
+
def test_brief_blocks_asset_judgment_when_visual_sheets_are_unavailable(self):
|
|
926
|
+
data = {
|
|
927
|
+
'source': {'filename': 'template.pptx'},
|
|
928
|
+
'canvas': {'px': [1920, 1080]},
|
|
929
|
+
'counts': {'slides': 1, 'layouts': 1},
|
|
930
|
+
'theme_topology': {'themes': ['single']},
|
|
931
|
+
'form_hint': {'form': 2},
|
|
932
|
+
}
|
|
933
|
+
archetype = {'name': 'layout-1', 'pages': [1], 'rep': 1, 'bg': None, 'slots': []}
|
|
934
|
+
|
|
935
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
936
|
+
draft.emit_brief(
|
|
937
|
+
data,
|
|
938
|
+
([], [], [], {}, [], [], [], [archetype], [], [],
|
|
939
|
+
[], [{'id': 'vision-1', 'pages': [1], 'candidates': []}], [], None),
|
|
940
|
+
output_dir,
|
|
941
|
+
)
|
|
942
|
+
with open(os.path.join(output_dir, 'BRIEF.md'), encoding='utf-8') as stream:
|
|
943
|
+
brief = stream.read()
|
|
944
|
+
|
|
945
|
+
self.assertIn('不要给图片候选定性', brief)
|
|
946
|
+
self.assertNotIn('逐张看 `media-out/`', brief)
|
|
947
|
+
|
|
948
|
+
def test_brief_does_not_assign_unsampled_template_layouts_to_the_model(self):
|
|
949
|
+
data = {
|
|
950
|
+
'source': {'filename': 'template.pptx'},
|
|
951
|
+
'canvas': {'px': [1920, 1080]},
|
|
952
|
+
'counts': {'slides': 1, 'layouts': 2},
|
|
953
|
+
'theme_topology': {'themes': ['single']},
|
|
954
|
+
'form_hint': {'form': 3},
|
|
955
|
+
}
|
|
956
|
+
sampled = {
|
|
957
|
+
'name': 'cover',
|
|
958
|
+
'pages': [1],
|
|
959
|
+
'rep': 1,
|
|
960
|
+
'bg': None,
|
|
961
|
+
'slots': [{'role': 'title', 'sz': 48, 'txt': '封面'}],
|
|
962
|
+
}
|
|
963
|
+
template_only = {
|
|
964
|
+
'name': 'content',
|
|
965
|
+
'pages': [],
|
|
966
|
+
'rep': None,
|
|
967
|
+
'bg': None,
|
|
968
|
+
'slots': [{'role': 'body', 'sz': 24, 'txt': '模板占位文本'}],
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
972
|
+
draft.emit_brief(
|
|
973
|
+
data,
|
|
974
|
+
([], [], [], {}, [], [], [], [sampled, template_only], [], [],
|
|
975
|
+
[], [], [], None),
|
|
976
|
+
output_dir,
|
|
977
|
+
)
|
|
978
|
+
with open(os.path.join(output_dir, 'BRIEF.md'), encoding='utf-8') as stream:
|
|
979
|
+
brief = stream.read()
|
|
980
|
+
|
|
981
|
+
self.assertIn('另有 1 个模板声明版式没有对应样张', brief)
|
|
982
|
+
self.assertIn('- `cover`(第 1 页,覆盖 [1])', brief)
|
|
983
|
+
self.assertNotIn('模板占位文本', brief)
|
|
984
|
+
|
|
985
|
+
def test_brief_uses_form3_sample_pages_for_real_layout_mapping(self):
|
|
986
|
+
data = {
|
|
987
|
+
'source': {'filename': 'template.pptx'},
|
|
988
|
+
'canvas': {'px': [1920, 1080]},
|
|
989
|
+
'counts': {'slides': 1, 'layouts': 2},
|
|
990
|
+
'theme_topology': {'themes': ['single']},
|
|
991
|
+
'form_hint': {'form': 3},
|
|
992
|
+
}
|
|
993
|
+
sampled = {
|
|
994
|
+
'name': 'content',
|
|
995
|
+
'pages': [],
|
|
996
|
+
'_sample_pages': [1],
|
|
997
|
+
'rep': None,
|
|
998
|
+
'bg': None,
|
|
999
|
+
'slots': [{'role': 'title', 'sz': 48, 'txt': '首页标题'}],
|
|
1000
|
+
}
|
|
1001
|
+
template_only = {
|
|
1002
|
+
'name': 'cover',
|
|
1003
|
+
'pages': [],
|
|
1004
|
+
'_sample_pages': [],
|
|
1005
|
+
'rep': None,
|
|
1006
|
+
'bg': None,
|
|
1007
|
+
'slots': [{'role': 'body', 'sz': 24, 'txt': '模板占位文本'}],
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1011
|
+
draft.emit_brief(
|
|
1012
|
+
data,
|
|
1013
|
+
([], [], [], {}, [], [], [], [sampled, template_only], [], [],
|
|
1014
|
+
[], [], [], None),
|
|
1015
|
+
output_dir,
|
|
1016
|
+
)
|
|
1017
|
+
with open(os.path.join(output_dir, 'BRIEF.md'), encoding='utf-8') as stream:
|
|
1018
|
+
brief = stream.read()
|
|
1019
|
+
|
|
1020
|
+
self.assertIn('| `content` | 1 | 1 |', brief)
|
|
1021
|
+
self.assertIn('第 1 页实际使用页型:`content`。若样张确为封面,只在 `roles.content` 填 `cover`',
|
|
1022
|
+
brief)
|
|
1023
|
+
self.assertIn('- `content`(第 1 页,覆盖 [1])', brief)
|
|
1024
|
+
self.assertIn('另有 1 个模板声明版式没有对应样张', brief)
|
|
1025
|
+
self.assertNotIn('模板占位文本', brief)
|
|
1026
|
+
|
|
1027
|
+
def test_unsampled_template_background_does_not_add_control_todos(self):
|
|
1028
|
+
sampled = {
|
|
1029
|
+
'name': 'cover',
|
|
1030
|
+
'zh': '封面',
|
|
1031
|
+
'role': 'cover',
|
|
1032
|
+
'pages': [1],
|
|
1033
|
+
'rep': 1,
|
|
1034
|
+
'bg': None,
|
|
1035
|
+
'slots': [],
|
|
1036
|
+
'decor': [],
|
|
1037
|
+
'pic_n': 0,
|
|
1038
|
+
'confidence': 'high',
|
|
1039
|
+
}
|
|
1040
|
+
template_only = {
|
|
1041
|
+
'name': 'content',
|
|
1042
|
+
'zh': '内容页',
|
|
1043
|
+
'role': 'content',
|
|
1044
|
+
'pages': [],
|
|
1045
|
+
'rep': None,
|
|
1046
|
+
'bg': 'bg-template-only',
|
|
1047
|
+
'slots': [],
|
|
1048
|
+
'decor': [],
|
|
1049
|
+
'pic_n': 0,
|
|
1050
|
+
'confidence': 'low',
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1054
|
+
emit_layouts([sampled, template_only], output_dir)
|
|
1055
|
+
with open(os.path.join(output_dir, 'layout-controls.yaml'),
|
|
1056
|
+
encoding='utf-8') as stream:
|
|
1057
|
+
controls = stream.read()
|
|
1058
|
+
|
|
1059
|
+
background_block = controls.split('bg-template-only:', 1)[1]
|
|
1060
|
+
self.assertIn('text_safe: [0, 0, 0, 0]', background_block)
|
|
1061
|
+
self.assertIn('avoid: []', background_block)
|
|
1062
|
+
self.assertNotIn('TODO', background_block)
|
|
1063
|
+
|
|
1064
|
+
def test_form3_sample_page_background_adds_control_todos(self):
|
|
1065
|
+
sampled = {
|
|
1066
|
+
'name': 'content',
|
|
1067
|
+
'zh': '内容页',
|
|
1068
|
+
'role': 'content',
|
|
1069
|
+
'pages': [],
|
|
1070
|
+
'_sample_pages': [1],
|
|
1071
|
+
'rep': None,
|
|
1072
|
+
'bg': 'bg-sampled',
|
|
1073
|
+
'slots': [],
|
|
1074
|
+
'decor': [],
|
|
1075
|
+
'pic_n': 0,
|
|
1076
|
+
'confidence': 'high',
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1080
|
+
emit_layouts([sampled], output_dir)
|
|
1081
|
+
with open(os.path.join(output_dir, 'layout-controls.yaml'),
|
|
1082
|
+
encoding='utf-8') as stream:
|
|
1083
|
+
controls = stream.read()
|
|
1084
|
+
|
|
1085
|
+
background_block = controls.split('bg-sampled:', 1)[1]
|
|
1086
|
+
self.assertIn('text_safe: TODO安全文字区', background_block)
|
|
1087
|
+
self.assertIn('avoid: TODO禁放区列表', background_block)
|
|
1088
|
+
self.assertIn('pairing_rule: "TODO', background_block)
|
|
1089
|
+
|
|
1090
|
+
def test_background_controls_only_edit_existing_image_backgrounds(self):
|
|
1091
|
+
archetype = {
|
|
1092
|
+
'name': 'content',
|
|
1093
|
+
'zh': '内容页',
|
|
1094
|
+
'role': 'content',
|
|
1095
|
+
'pages': [1],
|
|
1096
|
+
'rep': 1,
|
|
1097
|
+
'bg': 'bg-content',
|
|
1098
|
+
'slots': [],
|
|
1099
|
+
'decor': [],
|
|
1100
|
+
'pic_n': 0,
|
|
1101
|
+
'confidence': 'high',
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1105
|
+
emit_layouts([archetype], output_dir)
|
|
1106
|
+
with open(os.path.join(output_dir, 'layout-controls.yaml'),
|
|
1107
|
+
encoding='utf-8') as stream:
|
|
1108
|
+
controls = stream.read()
|
|
1109
|
+
|
|
1110
|
+
self.assertIn('只编辑本草案已列出的真实图片背景', controls)
|
|
1111
|
+
self.assertIn('纯色、渐变、外框、几何装饰和透明叠层不新建 bg_rules', controls)
|
|
1112
|
+
self.assertIn('bg-content:', controls)
|
|
1113
|
+
|
|
1114
|
+
def test_template_layout_keeps_inherited_linear_gradient_background_as_decor(self):
|
|
1115
|
+
layout_part = 'ppt/slideLayouts/slideLayout1.xml'
|
|
1116
|
+
data = {
|
|
1117
|
+
'canvas': {'px': [1920, 1080]},
|
|
1118
|
+
'form_hint': {'form': 3},
|
|
1119
|
+
'layouts': [{
|
|
1120
|
+
'part': layout_part,
|
|
1121
|
+
'name': '内容',
|
|
1122
|
+
'used_by_slides': 1,
|
|
1123
|
+
'background': {
|
|
1124
|
+
'type': 'gradient',
|
|
1125
|
+
'stops': [
|
|
1126
|
+
{'pos': 0, 'color': {'resolved': '#4B5CF5'}},
|
|
1127
|
+
{'pos': 100, 'color': {'resolved': '#233AFB'}},
|
|
1128
|
+
],
|
|
1129
|
+
'angle_deg': 0,
|
|
1130
|
+
},
|
|
1131
|
+
}, {
|
|
1132
|
+
'part': 'ppt/slideLayouts/slideLayout2.xml',
|
|
1133
|
+
'name': '目录',
|
|
1134
|
+
'used_by_slides': 0,
|
|
1135
|
+
}, {
|
|
1136
|
+
'part': 'ppt/slideLayouts/slideLayout3.xml',
|
|
1137
|
+
'name': '封底',
|
|
1138
|
+
'used_by_slides': 0,
|
|
1139
|
+
}],
|
|
1140
|
+
'slides': [{
|
|
1141
|
+
'part': 'ppt/slides/slide1.xml',
|
|
1142
|
+
'layout': layout_part,
|
|
1143
|
+
'background': None,
|
|
1144
|
+
}],
|
|
1145
|
+
'images': [],
|
|
1146
|
+
'media': [],
|
|
1147
|
+
'theme_topology': {'themes': ['single'], 'per_master': [], 'default': 'single'},
|
|
1148
|
+
'reference_graph': {
|
|
1149
|
+
'master_of_layout': {},
|
|
1150
|
+
'layout_of_slide': {'ppt/slides/slide1.xml': layout_part},
|
|
1151
|
+
},
|
|
1152
|
+
'background_composites': {},
|
|
1153
|
+
}
|
|
1154
|
+
shapes = []
|
|
1155
|
+
for index, part in enumerate((layout_part,
|
|
1156
|
+
'ppt/slideLayouts/slideLayout2.xml',
|
|
1157
|
+
'ppt/slideLayouts/slideLayout3.xml'), 1):
|
|
1158
|
+
shapes.append({
|
|
1159
|
+
'part': part,
|
|
1160
|
+
'layer': 'layout',
|
|
1161
|
+
'kind': 'sp',
|
|
1162
|
+
'box': {'x': 100, 'y': 100, 'w': 600, 'h': 100},
|
|
1163
|
+
'ph': {'type': 'title', 'idx': str(index)},
|
|
1164
|
+
'text': {'paragraphs': [{'runs': [{'text': 'Title %d' % index,
|
|
1165
|
+
'sz_px': 40}]}]},
|
|
1166
|
+
})
|
|
1167
|
+
|
|
1168
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1169
|
+
os.makedirs(os.path.join(output_dir, 'ref'))
|
|
1170
|
+
with open(os.path.join(output_dir, 'ref', 'shapes.json'), 'w',
|
|
1171
|
+
encoding='utf-8') as stream:
|
|
1172
|
+
import json
|
|
1173
|
+
json.dump({'shapes': shapes}, stream)
|
|
1174
|
+
archetypes, _, _ = draft_layouts(data, output_dir)
|
|
1175
|
+
emit_layouts(archetypes, output_dir)
|
|
1176
|
+
emit_manifest(data, [], [], output_dir, archetypes)
|
|
1177
|
+
with open(os.path.join(output_dir, 'layouts.yaml'), encoding='utf-8') as stream:
|
|
1178
|
+
layouts_yaml = stream.read()
|
|
1179
|
+
with open(os.path.join(output_dir, 'manifest.yaml'), encoding='utf-8') as stream:
|
|
1180
|
+
manifest = stream.read()
|
|
1181
|
+
|
|
1182
|
+
self.assertIn('box: [0, 0, 1920, 1080]', layouts_yaml)
|
|
1183
|
+
self.assertIn('background-image: linear-gradient(90deg, #4B5CF5 0%, #233AFB 100%)',
|
|
1184
|
+
layouts_yaml)
|
|
1185
|
+
self.assertIn('value: "[0, 0, 1920, 1080]"', manifest)
|
|
1186
|
+
self.assertIn('reason: "PPT 背景铺满画布"', manifest)
|
|
1187
|
+
|
|
1188
|
+
def test_asset_vision_groups_fail_when_page_screenshot_is_unreadable(self):
|
|
1189
|
+
candidates = [{
|
|
1190
|
+
'id': 'asset-1',
|
|
1191
|
+
'file': 'ornament.png',
|
|
1192
|
+
'out': 'media-out/ornament.png',
|
|
1193
|
+
'probe': {'w': 80, 'h': 80},
|
|
1194
|
+
'placements': [{'slide': 1, 'box': [10, 20, 80, 80]}],
|
|
1195
|
+
}]
|
|
1196
|
+
|
|
1197
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1198
|
+
png_dir = os.path.join(output_dir, 'ref', 'rebuild', 'png')
|
|
1199
|
+
lout_dir = os.path.join(output_dir, 'l-out')
|
|
1200
|
+
os.makedirs(png_dir)
|
|
1201
|
+
os.makedirs(lout_dir)
|
|
1202
|
+
with open(os.path.join(png_dir, 'slide-1.png'), 'w', encoding='utf-8') as stream:
|
|
1203
|
+
stream.write('not-a-png')
|
|
1204
|
+
with mock.patch.object(draft, 'render_asset_vision_pages', return_value=png_dir):
|
|
1205
|
+
with self.assertRaisesRegex(RuntimeError, '页面截图不可读取'):
|
|
1206
|
+
emit_asset_vision_groups(output_dir, candidates, 1, lout_dir)
|
|
1207
|
+
|
|
1208
|
+
def test_bound_visual_candidates_omit_unbound_media(self):
|
|
1209
|
+
bound = {
|
|
1210
|
+
'file': 'corner-ornament.png',
|
|
1211
|
+
'fullscreen': False,
|
|
1212
|
+
'slides': [2],
|
|
1213
|
+
'probe': {'alpha_mean': 255},
|
|
1214
|
+
}
|
|
1215
|
+
unbound = {
|
|
1216
|
+
'file': 'unused-master.svg',
|
|
1217
|
+
'fullscreen': False,
|
|
1218
|
+
'slides': [],
|
|
1219
|
+
'probe': {'alpha_mean': 255},
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
archetypes = [{'slots': [{'source_media': 'corner-ornament.png'}]}]
|
|
1223
|
+
|
|
1224
|
+
self.assertEqual(
|
|
1225
|
+
bound_visual_candidates([bound, unbound], archetypes),
|
|
1226
|
+
[bound],
|
|
1227
|
+
)
|
|
1228
|
+
|
|
1229
|
+
def test_visual_slot_candidates_use_final_slot_geometry(self):
|
|
1230
|
+
candidate = {
|
|
1231
|
+
'file': 'shared-illustration.png',
|
|
1232
|
+
'fullscreen': False,
|
|
1233
|
+
'probe': {'alpha_mean': 255},
|
|
1234
|
+
'placements': [
|
|
1235
|
+
{'slide': 3, 'box': [1002, 285, 768, 647]},
|
|
1236
|
+
{'slide': 8, 'box': [114, 285, 768, 647]},
|
|
1237
|
+
],
|
|
1238
|
+
}
|
|
1239
|
+
archetypes = [{
|
|
1240
|
+
'name': 'section',
|
|
1241
|
+
'pages': [8],
|
|
1242
|
+
'rep': 8,
|
|
1243
|
+
'slots': [{
|
|
1244
|
+
'source_media': 'shared-illustration.png',
|
|
1245
|
+
'box': [114, 285, 768, 647],
|
|
1246
|
+
}],
|
|
1247
|
+
}]
|
|
1248
|
+
|
|
1249
|
+
rows = visual_slot_candidates([candidate], archetypes)
|
|
1250
|
+
|
|
1251
|
+
self.assertEqual(rows[0]['placements'], [{
|
|
1252
|
+
'slide': 8,
|
|
1253
|
+
'box': [114, 285, 768, 647],
|
|
1254
|
+
'archetype': 'section',
|
|
1255
|
+
}])
|
|
1256
|
+
|
|
1257
|
+
def test_first_page_background_is_not_automatically_a_cover(self):
|
|
1258
|
+
archetypes = [{
|
|
1259
|
+
'name': 'layout-1',
|
|
1260
|
+
'pages': [1],
|
|
1261
|
+
'bg_raw': 'ppt/media/first-page.png',
|
|
1262
|
+
}]
|
|
1263
|
+
|
|
1264
|
+
self.assertIsNone(cover_background_media(archetypes))
|
|
1265
|
+
self.assertEqual(
|
|
1266
|
+
cover_background_media(archetypes + [{
|
|
1267
|
+
'name': 'cover',
|
|
1268
|
+
'pages': [],
|
|
1269
|
+
'bg_raw': 'ppt/media/template-cover.png',
|
|
1270
|
+
}]),
|
|
1271
|
+
'ppt/media/template-cover.png',
|
|
1272
|
+
)
|
|
1273
|
+
|
|
1274
|
+
def test_visual_slot_candidates_skip_unsampled_template_layouts(self):
|
|
1275
|
+
candidate = {
|
|
1276
|
+
'file': 'template-ornament.png',
|
|
1277
|
+
'fullscreen': False,
|
|
1278
|
+
'probe': {'alpha_mean': 255},
|
|
1279
|
+
'placements': [{'slide': 1, 'box': [10, 20, 80, 80]}],
|
|
1280
|
+
}
|
|
1281
|
+
archetypes = [{
|
|
1282
|
+
'name': 'template-only',
|
|
1283
|
+
'pages': [],
|
|
1284
|
+
'_sample_pages': [],
|
|
1285
|
+
'rep': None,
|
|
1286
|
+
'slots': [{
|
|
1287
|
+
'source_media': 'template-ornament.png',
|
|
1288
|
+
'box': [10, 20, 80, 80],
|
|
1289
|
+
}],
|
|
1290
|
+
}]
|
|
1291
|
+
|
|
1292
|
+
self.assertEqual(visual_slot_candidates([candidate], archetypes), [])
|
|
1293
|
+
|
|
1294
|
+
def test_visual_slot_candidates_skip_template_only_slot_without_page_context(self):
|
|
1295
|
+
candidate = {
|
|
1296
|
+
'file': 'template-ornament.png',
|
|
1297
|
+
'fullscreen': False,
|
|
1298
|
+
'probe': {'alpha_mean': 255},
|
|
1299
|
+
'placements': [{'layout': 'slideLayout-1', 'box': [10, 20, 80, 80]}],
|
|
1300
|
+
}
|
|
1301
|
+
archetypes = [{
|
|
1302
|
+
'name': 'sampled-layout',
|
|
1303
|
+
'pages': [1],
|
|
1304
|
+
'rep': 1,
|
|
1305
|
+
'slots': [{
|
|
1306
|
+
'source_media': 'template-ornament.png',
|
|
1307
|
+
'box': [10, 20, 80, 80],
|
|
1308
|
+
}],
|
|
1309
|
+
}]
|
|
1310
|
+
|
|
1311
|
+
self.assertEqual(visual_slot_candidates([candidate], archetypes), [])
|
|
1312
|
+
|
|
1313
|
+
def test_deterministic_fullscreen_images_skip_model_judgment(self):
|
|
1314
|
+
self.assertFalse(needs_asset_judgment({
|
|
1315
|
+
'fullscreen': True,
|
|
1316
|
+
'probe': {'alpha_mean': 255, 'near_blank': False},
|
|
1317
|
+
}))
|
|
1318
|
+
self.assertFalse(needs_asset_judgment({
|
|
1319
|
+
'fullscreen': True,
|
|
1320
|
+
'probe': {'alpha_mean': 5, 'near_blank': True},
|
|
1321
|
+
}))
|
|
1322
|
+
self.assertTrue(needs_asset_judgment({
|
|
1323
|
+
'fullscreen': False,
|
|
1324
|
+
'probe': {'alpha_mean': 5, 'near_blank': True},
|
|
1325
|
+
}))
|
|
1326
|
+
|
|
1327
|
+
def test_shape_fill_images_are_exposed_as_slide_marks(self):
|
|
1328
|
+
data = {
|
|
1329
|
+
'images': [{
|
|
1330
|
+
'media': 'ppt/media/fill.png',
|
|
1331
|
+
'fullscreen': False,
|
|
1332
|
+
'boxes': [{
|
|
1333
|
+
'box': {'x': 120, 'y': 240, 'w': 360, 'h': 420},
|
|
1334
|
+
'parts': ['ppt/slides/slide2.xml'],
|
|
1335
|
+
}],
|
|
1336
|
+
}],
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
marks = slide_image_marks(data)
|
|
1340
|
+
|
|
1341
|
+
self.assertEqual(marks['ppt/slides/slide2.xml'], [{
|
|
1342
|
+
'media': 'ppt/media/fill.png',
|
|
1343
|
+
'box': {'x': 120, 'y': 240, 'w': 360, 'h': 420},
|
|
1344
|
+
}])
|
|
1345
|
+
|
|
1346
|
+
def test_form3_layouts_include_instance_image_fills(self):
|
|
1347
|
+
layouts = []
|
|
1348
|
+
shapes = []
|
|
1349
|
+
for index in range(1, 4):
|
|
1350
|
+
part = 'ppt/slideLayouts/slideLayout%d.xml' % index
|
|
1351
|
+
layouts.append({
|
|
1352
|
+
'part': part,
|
|
1353
|
+
'name': 'Layout %d' % index,
|
|
1354
|
+
'used_by_slides': 1,
|
|
1355
|
+
})
|
|
1356
|
+
shapes.append({
|
|
1357
|
+
'part': part,
|
|
1358
|
+
'layer': 'layout',
|
|
1359
|
+
'kind': 'sp',
|
|
1360
|
+
'box': {'x': 10, 'y': 10, 'w': 500, 'h': 100},
|
|
1361
|
+
'ph': {'type': 'title', 'idx': str(index)},
|
|
1362
|
+
'text': {'paragraphs': [{'runs': [{
|
|
1363
|
+
'text': 'Title %d' % index,
|
|
1364
|
+
'sz_px': 40,
|
|
1365
|
+
}]}]},
|
|
1366
|
+
})
|
|
1367
|
+
data = {
|
|
1368
|
+
'canvas': {'px': [1920, 1080]},
|
|
1369
|
+
'form_hint': {'form': 3},
|
|
1370
|
+
'layouts': layouts,
|
|
1371
|
+
'images': [{
|
|
1372
|
+
'media': 'ppt/media/fill.png',
|
|
1373
|
+
'fullscreen': False,
|
|
1374
|
+
'boxes': [{
|
|
1375
|
+
'box': {'x': 120, 'y': 240, 'w': 360, 'h': 420},
|
|
1376
|
+
'parts': ['ppt/slides/slide1.xml'],
|
|
1377
|
+
}],
|
|
1378
|
+
}],
|
|
1379
|
+
'media': [],
|
|
1380
|
+
'theme_topology': {
|
|
1381
|
+
'themes': ['single'],
|
|
1382
|
+
'per_master': [],
|
|
1383
|
+
'default': 'single',
|
|
1384
|
+
},
|
|
1385
|
+
'reference_graph': {
|
|
1386
|
+
'master_of_layout': {},
|
|
1387
|
+
'layout_of_slide': {
|
|
1388
|
+
'ppt/slides/slide1.xml': 'ppt/slideLayouts/slideLayout1.xml',
|
|
1389
|
+
},
|
|
1390
|
+
},
|
|
1391
|
+
'background_composites': {},
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1395
|
+
os.makedirs(os.path.join(output_dir, 'ref'))
|
|
1396
|
+
with open(os.path.join(output_dir, 'ref', 'shapes.json'), 'w',
|
|
1397
|
+
encoding='utf-8') as stream:
|
|
1398
|
+
import json
|
|
1399
|
+
json.dump({'shapes': shapes}, stream)
|
|
1400
|
+
archetypes, _, _ = draft_layouts(data, output_dir)
|
|
1401
|
+
|
|
1402
|
+
self.assertTrue(any(
|
|
1403
|
+
slot.get('media') == 'ppt/media/fill.png'
|
|
1404
|
+
for archetype in archetypes
|
|
1405
|
+
for slot in archetype['slots']
|
|
1406
|
+
))
|
|
1407
|
+
|
|
1408
|
+
def test_form3_layouts_include_translucent_fullscreen_overlay_as_candidate(self):
|
|
1409
|
+
layouts = []
|
|
1410
|
+
shapes = []
|
|
1411
|
+
for index in range(1, 4):
|
|
1412
|
+
part = 'ppt/slideLayouts/slideLayout%d.xml' % index
|
|
1413
|
+
layouts.append({
|
|
1414
|
+
'part': part,
|
|
1415
|
+
'name': 'Layout %d' % index,
|
|
1416
|
+
'used_by_slides': 1,
|
|
1417
|
+
})
|
|
1418
|
+
shapes.append({
|
|
1419
|
+
'part': part,
|
|
1420
|
+
'layer': 'layout',
|
|
1421
|
+
'kind': 'sp',
|
|
1422
|
+
'box': {'x': 10, 'y': 10, 'w': 500, 'h': 100},
|
|
1423
|
+
'ph': {'type': 'title', 'idx': str(index)},
|
|
1424
|
+
'text': {'paragraphs': [{'runs': [{
|
|
1425
|
+
'text': 'Title %d' % index,
|
|
1426
|
+
'sz_px': 40,
|
|
1427
|
+
}]}]},
|
|
1428
|
+
})
|
|
1429
|
+
overlay_media = 'ppt/media/overlay.png'
|
|
1430
|
+
data = {
|
|
1431
|
+
'canvas': {'px': [1920, 1080]},
|
|
1432
|
+
'form_hint': {'form': 3},
|
|
1433
|
+
'layouts': layouts,
|
|
1434
|
+
'images': [{
|
|
1435
|
+
'media': overlay_media,
|
|
1436
|
+
'fullscreen': True,
|
|
1437
|
+
'boxes': [{
|
|
1438
|
+
'box': {'x': 0, 'y': 0, 'w': 1920, 'h': 1080},
|
|
1439
|
+
'parts': ['ppt/slides/slide1.xml'],
|
|
1440
|
+
}],
|
|
1441
|
+
}],
|
|
1442
|
+
'media': [],
|
|
1443
|
+
'theme_topology': {
|
|
1444
|
+
'themes': ['single'],
|
|
1445
|
+
'per_master': [],
|
|
1446
|
+
'default': 'single',
|
|
1447
|
+
},
|
|
1448
|
+
'reference_graph': {
|
|
1449
|
+
'master_of_layout': {},
|
|
1450
|
+
'layout_of_slide': {
|
|
1451
|
+
'ppt/slides/slide1.xml': 'ppt/slideLayouts/slideLayout1.xml',
|
|
1452
|
+
},
|
|
1453
|
+
},
|
|
1454
|
+
'background_composites': {},
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1458
|
+
os.makedirs(os.path.join(output_dir, 'ref'))
|
|
1459
|
+
with open(os.path.join(output_dir, 'ref', 'shapes.json'), 'w',
|
|
1460
|
+
encoding='utf-8') as stream:
|
|
1461
|
+
import json
|
|
1462
|
+
json.dump({'shapes': shapes}, stream)
|
|
1463
|
+
archetypes, _, _ = draft_layouts(
|
|
1464
|
+
data, output_dir, effective_alpha={overlay_media: 64})
|
|
1465
|
+
|
|
1466
|
+
self.assertTrue(any(
|
|
1467
|
+
slot.get('media') == overlay_media
|
|
1468
|
+
for archetype in archetypes
|
|
1469
|
+
for slot in archetype['slots']
|
|
1470
|
+
))
|
|
1471
|
+
|
|
1472
|
+
def test_shape_opacity_marks_opaque_fullscreen_media_as_overlay(self):
|
|
1473
|
+
data = {
|
|
1474
|
+
'media': [{
|
|
1475
|
+
'media': 'ppt/media/overlay.png',
|
|
1476
|
+
'out': 'media-out/overlay.png',
|
|
1477
|
+
}],
|
|
1478
|
+
'images': [{
|
|
1479
|
+
'media': 'ppt/media/overlay.png',
|
|
1480
|
+
'fullscreen': True,
|
|
1481
|
+
}],
|
|
1482
|
+
}
|
|
1483
|
+
shapes = [{
|
|
1484
|
+
'kind': 'pic',
|
|
1485
|
+
'media': 'ppt/media/overlay.png',
|
|
1486
|
+
'w_pct': 100,
|
|
1487
|
+
'h_pct': 100,
|
|
1488
|
+
'opacity': 0.25,
|
|
1489
|
+
}]
|
|
1490
|
+
|
|
1491
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1492
|
+
media_dir = os.path.join(output_dir, 'media-out')
|
|
1493
|
+
os.makedirs(media_dir)
|
|
1494
|
+
try:
|
|
1495
|
+
from PIL import Image
|
|
1496
|
+
except ImportError:
|
|
1497
|
+
self.skipTest('Pillow unavailable')
|
|
1498
|
+
Image.new('RGB', (16, 9), (20, 40, 60)).save(
|
|
1499
|
+
os.path.join(media_dir, 'overlay.png'))
|
|
1500
|
+
|
|
1501
|
+
overlays = fullscreen_overlay_media(data, output_dir, shapes)
|
|
1502
|
+
|
|
1503
|
+
self.assertEqual(overlays, {'ppt/media/overlay.png'})
|
|
1504
|
+
|
|
1505
|
+
def test_image_bearing_orphan_group_is_kept_without_count_threshold(self):
|
|
1506
|
+
kept = [('common', [{'no': 2, 'marks': []}])]
|
|
1507
|
+
ranked = kept + [
|
|
1508
|
+
('one-image', [{'no': 5, 'marks': [{
|
|
1509
|
+
'media': 'ppt/media/ornament.png',
|
|
1510
|
+
'box': {'x': 40, 'y': 120, 'w': 80, 'h': 240},
|
|
1511
|
+
}]}]),
|
|
1512
|
+
('no-image', [{'no': 6, 'marks': []}]),
|
|
1513
|
+
]
|
|
1514
|
+
|
|
1515
|
+
selected = preserve_image_bearing_groups(kept, ranked)
|
|
1516
|
+
|
|
1517
|
+
self.assertEqual([group[0] for group in selected], ['common', 'one-image'])
|
|
1518
|
+
|
|
1519
|
+
def test_asset_vision_contexts_keep_same_source_per_layout_and_box(self):
|
|
1520
|
+
contexts = asset_vision_contexts([{
|
|
1521
|
+
'id': 'asset-1',
|
|
1522
|
+
'file': 'shared.png',
|
|
1523
|
+
'placements': [
|
|
1524
|
+
{'slide': 2, 'archetype': 'layout-1', 'box': [10, 20, 80, 80]},
|
|
1525
|
+
{'slide': 3, 'archetype': 'layout-2', 'box': [10, 20, 80, 80]},
|
|
1526
|
+
{'slide': 4, 'archetype': 'layout-2', 'box': [10, 20, 80, 80]},
|
|
1527
|
+
{'slide': 4, 'archetype': 'layout-2', 'box': [100, 20, 80, 80]},
|
|
1528
|
+
],
|
|
1529
|
+
}])
|
|
1530
|
+
|
|
1531
|
+
self.assertEqual(
|
|
1532
|
+
[(row['id'], row['layout'], row['placements'][0]['slide'])
|
|
1533
|
+
for row in contexts],
|
|
1534
|
+
[
|
|
1535
|
+
('asset-1-s2', 'layout-1', 2),
|
|
1536
|
+
('asset-1-s3', 'layout-2', 3),
|
|
1537
|
+
('asset-1-s4', 'layout-2', 4),
|
|
1538
|
+
],
|
|
1539
|
+
)
|
|
1540
|
+
|
|
1541
|
+
def test_contact_sheets_cover_all_candidates_without_truncation(self):
|
|
1542
|
+
try:
|
|
1543
|
+
from PIL import Image
|
|
1544
|
+
except ImportError:
|
|
1545
|
+
self.skipTest('Pillow unavailable')
|
|
1546
|
+
|
|
1547
|
+
with tempfile.TemporaryDirectory() as output_dir:
|
|
1548
|
+
media_dir = os.path.join(output_dir, 'media-out')
|
|
1549
|
+
lout_dir = os.path.join(output_dir, 'l-out')
|
|
1550
|
+
os.makedirs(media_dir)
|
|
1551
|
+
os.makedirs(lout_dir)
|
|
1552
|
+
candidates = []
|
|
1553
|
+
for index in range(13):
|
|
1554
|
+
filename = 'image-%02d.png' % index
|
|
1555
|
+
Image.new('RGBA', (8, 8), (index, 20, 30, 255)).save(
|
|
1556
|
+
os.path.join(media_dir, filename))
|
|
1557
|
+
candidates.append({
|
|
1558
|
+
'file': filename,
|
|
1559
|
+
'out': 'media-out/' + filename,
|
|
1560
|
+
'n': 1,
|
|
1561
|
+
'probe': {'w': 8, 'h': 8},
|
|
1562
|
+
})
|
|
1563
|
+
|
|
1564
|
+
sheets = contact_sheets(output_dir, candidates, lout_dir)
|
|
1565
|
+
legacy_exists = os.path.exists(os.path.join(lout_dir, 'contact-sheet.png'))
|
|
1566
|
+
|
|
1567
|
+
self.assertEqual(len(sheets), 2)
|
|
1568
|
+
self.assertEqual(
|
|
1569
|
+
[os.path.basename(path) for path in sheets],
|
|
1570
|
+
['contact-sheet-1.png', 'contact-sheet-2.png'],
|
|
1571
|
+
)
|
|
1572
|
+
self.assertTrue(legacy_exists)
|
|
1573
|
+
|
|
1574
|
+
def test_content_candidates_at_same_geometry_collapse_to_one_slot(self):
|
|
1575
|
+
lines = resolve_asset_candidate_lines([
|
|
1576
|
+
' - {role: asset-candidate, box: [10, 20, 300, 200], type: pic, '
|
|
1577
|
+
'source_media: chart-a.png, css: "object-fit: contain"}',
|
|
1578
|
+
' - {role: asset-candidate, box: [10, 20, 300, 200], type: pic, '
|
|
1579
|
+
'source_media: chart-b.png}',
|
|
1580
|
+
], {
|
|
1581
|
+
'chart-a.png': 'content',
|
|
1582
|
+
'chart-b.png': 'content',
|
|
1583
|
+
}, {})
|
|
1584
|
+
|
|
1585
|
+
self.assertEqual(lines, [
|
|
1586
|
+
' - {role: pic, box: [10, 20, 300, 200], type: pic, css: "object-fit: contain"}',
|
|
1587
|
+
])
|
|
1588
|
+
|
|
1589
|
+
def test_content_candidates_at_same_geometry_remain_in_distinct_layouts(self):
|
|
1590
|
+
lines = resolve_asset_candidate_lines([
|
|
1591
|
+
' layout-a:',
|
|
1592
|
+
' slots:',
|
|
1593
|
+
' - {role: asset-candidate, box: [10, 20, 300, 200], type: pic, '
|
|
1594
|
+
'source_media: chart-a.png}',
|
|
1595
|
+
' layout-b:',
|
|
1596
|
+
' slots:',
|
|
1597
|
+
' - {role: asset-candidate, box: [10, 20, 300, 200], type: pic, '
|
|
1598
|
+
'source_media: chart-b.png}',
|
|
1599
|
+
], {
|
|
1600
|
+
'chart-a.png': 'content',
|
|
1601
|
+
'chart-b.png': 'content',
|
|
1602
|
+
}, {})
|
|
1603
|
+
|
|
1604
|
+
self.assertEqual(lines, [
|
|
1605
|
+
' layout-a:',
|
|
1606
|
+
' slots:',
|
|
1607
|
+
' - {role: pic, box: [10, 20, 300, 200], type: pic}',
|
|
1608
|
+
' layout-b:',
|
|
1609
|
+
' slots:',
|
|
1610
|
+
' - {role: pic, box: [10, 20, 300, 200], type: pic}',
|
|
1611
|
+
])
|
|
1612
|
+
|
|
96
1613
|
|
|
97
1614
|
if __name__ == '__main__':
|
|
98
1615
|
unittest.main()
|