-
Notifications
You must be signed in to change notification settings - Fork 661
/
Copy pathgen-xml.ts
1888 lines (1715 loc) · 108 KB
/
gen-xml.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* PptxGenJS: XML Generation
*/
import {
BULLET_TYPES,
CRLF,
DEF_BULLET_MARGIN,
DEF_CELL_MARGIN_PT,
DEF_PRES_LAYOUT_NAME,
DEF_TEXT_GLOW,
DEF_TEXT_SHADOW,
EMU,
LAYOUT_IDX_SERIES_BASE,
PLACEHOLDER_TYPES,
SLDNUMFLDID,
SLIDE_OBJECT_TYPES,
} from './core-enums'
import {
IPresentationProps,
ISlideObject,
ISlideRel,
ISlideRelChart,
ISlideRelMedia,
ObjectOptions,
PresSlide,
ShadowProps,
SlideLayout,
TableCell,
TableCellProps,
TextProps,
TextPropsOptions,
} from './core-interfaces'
import {
convertRotationDegrees,
createColorElement,
createGlowElement,
encodeXmlEntities,
genXmlColorSelection,
getSmartParseNumber,
getUuid,
inch2Emu,
valToPts,
} from './gen-utils'
let imageSizingXml = {
cover: function (imgSize, boxDim) {
let imgRatio = imgSize.h / imgSize.w,
boxRatio = boxDim.h / boxDim.w,
isBoxBased = boxRatio > imgRatio,
width = isBoxBased ? boxDim.h / imgRatio : boxDim.w,
height = isBoxBased ? boxDim.h : boxDim.w * imgRatio,
hzPerc = Math.round(1e5 * 0.5 * (1 - boxDim.w / width)),
vzPerc = Math.round(1e5 * 0.5 * (1 - boxDim.h / height))
return '<a:srcRect l="' + hzPerc + '" r="' + hzPerc + '" t="' + vzPerc + '" b="' + vzPerc + '"/><a:stretch/>'
},
contain: function (imgSize, boxDim) {
let imgRatio = imgSize.h / imgSize.w,
boxRatio = boxDim.h / boxDim.w,
widthBased = boxRatio > imgRatio,
width = widthBased ? boxDim.w : boxDim.h / imgRatio,
height = widthBased ? boxDim.w * imgRatio : boxDim.h,
hzPerc = Math.round(1e5 * 0.5 * (1 - boxDim.w / width)),
vzPerc = Math.round(1e5 * 0.5 * (1 - boxDim.h / height))
return '<a:srcRect l="' + hzPerc + '" r="' + hzPerc + '" t="' + vzPerc + '" b="' + vzPerc + '"/><a:stretch/>'
},
crop: function (imageSize, boxDim) {
let l = boxDim.x,
r = imageSize.w - (boxDim.x + boxDim.w),
t = boxDim.y,
b = imageSize.h - (boxDim.y + boxDim.h),
lPerc = Math.round(1e5 * (l / imageSize.w)),
rPerc = Math.round(1e5 * (r / imageSize.w)),
tPerc = Math.round(1e5 * (t / imageSize.h)),
bPerc = Math.round(1e5 * (b / imageSize.h))
return '<a:srcRect l="' + lPerc + '" r="' + rPerc + '" t="' + tPerc + '" b="' + bPerc + '"/><a:stretch/>'
},
}
/**
* Transforms a slide or slideLayout to resulting XML string - Creates `ppt/slide*.xml`
* @param {PresSlide|SlideLayout} slideObject - slide object created within createSlideObject
* @return {string} XML string with <p:cSld> as the root
*/
function slideObjectToXml(slide: PresSlide | SlideLayout): string {
let strSlideXml: string = slide._name ? '<p:cSld name="' + slide._name + '">' : '<p:cSld>'
let intTableNum: number = 1
// STEP 1: Add background
if (slide.bkgd) {
strSlideXml += genXmlColorSelection(null, slide.bkgd)
} else if (!slide.bkgd && slide._name && slide._name === DEF_PRES_LAYOUT_NAME) {
// NOTE: Default [white] background is needed on slideMaster1.xml to avoid gray background in Keynote (and Finder previews)
strSlideXml += '<p:bg><p:bgRef idx="1001"><a:schemeClr val="bg1"/></p:bgRef></p:bg>'
}
// STEP 2: Add background image (using Strech) (if any)
if (slide._bkgdImgRid) {
// FIXME: We should be doing this in the slideLayout...
strSlideXml +=
'<p:bg>' +
'<p:bgPr><a:blipFill dpi="0" rotWithShape="1">' +
'<a:blip r:embed="rId' +
slide._bkgdImgRid +
'"><a:lum/></a:blip>' +
'<a:srcRect/><a:stretch><a:fillRect/></a:stretch></a:blipFill>' +
'<a:effectLst/></p:bgPr>' +
'</p:bg>'
}
// STEP 3: Continue slide by starting spTree node
strSlideXml += '<p:spTree>'
strSlideXml += '<p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr>'
strSlideXml += '<p:grpSpPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/>'
strSlideXml += '<a:chOff x="0" y="0"/><a:chExt cx="0" cy="0"/></a:xfrm></p:grpSpPr>'
// STEP 4: Loop over all Slide.data objects and add them to this slide
slide._slideObjects.forEach((slideItemObj: ISlideObject, idx: number) => {
let x = 0,
y = 0,
cx = getSmartParseNumber('75%', 'X', slide._presLayout),
cy = 0
let placeholderObj: ISlideObject
let locationAttr = ''
if (
(slide as PresSlide)._slideLayout !== undefined &&
(slide as PresSlide)._slideLayout._slideObjects !== undefined &&
slideItemObj.options &&
slideItemObj.options.placeholder
) {
placeholderObj = (slide as PresSlide)._slideLayout._slideObjects.filter(
(object: ISlideObject) => object.options.placeholder === slideItemObj.options.placeholder
)[0]
}
// A: Set option vars
slideItemObj.options = slideItemObj.options || {}
if (typeof slideItemObj.options.x !== 'undefined') x = getSmartParseNumber(slideItemObj.options.x, 'X', slide._presLayout)
if (typeof slideItemObj.options.y !== 'undefined') y = getSmartParseNumber(slideItemObj.options.y, 'Y', slide._presLayout)
if (typeof slideItemObj.options.w !== 'undefined') cx = getSmartParseNumber(slideItemObj.options.w, 'X', slide._presLayout)
if (typeof slideItemObj.options.h !== 'undefined') cy = getSmartParseNumber(slideItemObj.options.h, 'Y', slide._presLayout)
// If using a placeholder then inherit it's position
if (placeholderObj) {
if (placeholderObj.options.x || placeholderObj.options.x === 0) x = getSmartParseNumber(placeholderObj.options.x, 'X', slide._presLayout)
if (placeholderObj.options.y || placeholderObj.options.y === 0) y = getSmartParseNumber(placeholderObj.options.y, 'Y', slide._presLayout)
if (placeholderObj.options.w || placeholderObj.options.w === 0) cx = getSmartParseNumber(placeholderObj.options.w, 'X', slide._presLayout)
if (placeholderObj.options.h || placeholderObj.options.h === 0) cy = getSmartParseNumber(placeholderObj.options.h, 'Y', slide._presLayout)
}
//
if (slideItemObj.options.flipH) locationAttr += ' flipH="1"'
if (slideItemObj.options.flipV) locationAttr += ' flipV="1"'
if (slideItemObj.options.rotate) locationAttr += ' rot="' + convertRotationDegrees(slideItemObj.options.rotate) + '"'
// B: Add OBJECT to the current Slide
switch (slideItemObj._type) {
case SLIDE_OBJECT_TYPES.table:
let arrTabRows = slideItemObj.arrTabRows
let objTabOpts = slideItemObj.options
let intColCnt = 0,
intColW = 0
let cellOpts: TableCellProps
// Calc number of columns
// NOTE: Cells may have a colspan, so merely taking the length of the [0] (or any other) row is not
// ....: sufficient to determine column count. Therefore, check each cell for a colspan and total cols as reqd
arrTabRows[0].forEach(cell => {
cellOpts = cell.options || null
intColCnt += cellOpts && cellOpts.colspan ? Number(cellOpts.colspan) : 1
})
// STEP 1: Start Table XML
// NOTE: Non-numeric cNvPr id values will trigger "presentation needs repair" type warning in MS-PPT-2013
let strXml =
'<p:graphicFrame>' +
' <p:nvGraphicFramePr>' +
' <p:cNvPr id="' +
(intTableNum * slide._slideNum + 1) +
'" name="Table ' +
intTableNum * slide._slideNum +
'"/>' +
' <p:cNvGraphicFramePr><a:graphicFrameLocks noGrp="1"/></p:cNvGraphicFramePr>' +
' <p:nvPr><p:extLst><p:ext uri="{D42A27DB-BD31-4B8C-83A1-F6EECF244321}"><p14:modId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="1579011935"/></p:ext></p:extLst></p:nvPr>' +
' </p:nvGraphicFramePr>' +
' <p:xfrm>' +
' <a:off x="' +
(x || (x === 0 ? 0 : EMU)) +
'" y="' +
(y || (y === 0 ? 0 : EMU)) +
'"/>' +
' <a:ext cx="' +
(cx || (cx === 0 ? 0 : EMU)) +
'" cy="' +
(cy || EMU) +
'"/>' +
' </p:xfrm>' +
' <a:graphic>' +
' <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table">' +
' <a:tbl>' +
' <a:tblPr/>'
// + ' <a:tblPr bandRow="1"/>';
// TODO: Support banded rows, first/last row, etc.
// NOTE: Banding, etc. only shows when using a table style! (or set alt row color if banding)
// <a:tblPr firstCol="0" firstRow="0" lastCol="0" lastRow="0" bandCol="0" bandRow="1">
// STEP 2: Set column widths
// Evenly distribute cols/rows across size provided when applicable (calc them if only overall dimensions were provided)
// A: Col widths provided?
if (Array.isArray(objTabOpts.colW)) {
strXml += '<a:tblGrid>'
for (let col = 0; col < intColCnt; col++) {
let w = inch2Emu(objTabOpts.colW[col])
if (w == null || isNaN(w)) {
w = (typeof slideItemObj.options.w === 'number' ? slideItemObj.options.w : 1) / intColCnt
}
strXml += '<a:gridCol w="' + Math.round(w) + '"/>'
}
strXml += '</a:tblGrid>'
}
// B: Table Width provided without colW? Then distribute cols
else {
intColW = objTabOpts.colW ? objTabOpts.colW : EMU
if (slideItemObj.options.w && !objTabOpts.colW) intColW = Math.round((typeof slideItemObj.options.w === 'number' ? slideItemObj.options.w : 1) / intColCnt)
strXml += '<a:tblGrid>'
for (let colw = 0; colw < intColCnt; colw++) {
strXml += '<a:gridCol w="' + intColW + '"/>'
}
strXml += '</a:tblGrid>'
}
// STEP 3: Build our row arrays into an actual grid to match the XML we will be building next (ISSUE #36)
// Note row arrays can arrive "lopsided" as in row1:[1,2,3] row2:[3] when first two cols rowspan!,
// so a simple loop below in XML building wont suffice to build table correctly.
// We have to build an actual grid now
/*
EX: (A0:rowspan=3, B1:rowspan=2, C1:colspan=2)
/------|------|------|------\
| A0 | B0 | C0 | D0 |
| | B1 | C1 | |
| | | C2 | D2 |
\------|------|------|------/
*/
// A: add _hmerge cell for colspan. should reserve rowspan
arrTabRows.forEach(cells => {
for (let cIdx = 0; cIdx < cells.length; ) {
let cell = cells[cIdx]
let colspan = cell.options?.colspan
let rowspan = cell.options?.rowspan
if (colspan && colspan > 1) {
let vMergeCells = new Array(colspan - 1).fill(undefined).map(_ => {
return { _type: SLIDE_OBJECT_TYPES.tablecell, options: { rowspan }, _hmerge: true } as const
})
cells.splice(cIdx + 1, 0, ...vMergeCells)
cIdx += colspan
} else {
cIdx += 1
}
}
})
// B: add _vmerge cell for rowspan. should reserve colspan/_hmerge
arrTabRows.forEach((cells, rIdx) => {
let nextRow = arrTabRows[rIdx + 1]
if (!nextRow) return
cells.forEach((cell, cIdx) => {
let rowspan = cell._rowContinue || cell.options?.rowspan
let colspan = cell.options?.colspan
let _hmerge = cell._hmerge
if (rowspan && rowspan > 1) {
let hMergeCell = { _type: SLIDE_OBJECT_TYPES.tablecell, options: { colspan }, _rowContinue: rowspan - 1, _vmerge: true, _hmerge } as const
nextRow.splice(cIdx, 0, hMergeCell)
}
})
})
// STEP 4: Build table rows/cells
arrTabRows.forEach((cells, rIdx) => {
// A: Table Height provided without rowH? Then distribute rows
let intRowH = 0 // IMPORTANT: Default must be zero for auto-sizing to work
if (Array.isArray(objTabOpts.rowH) && objTabOpts.rowH[rIdx]) intRowH = inch2Emu(Number(objTabOpts.rowH[rIdx]))
else if (objTabOpts.rowH && !isNaN(Number(objTabOpts.rowH))) intRowH = inch2Emu(Number(objTabOpts.rowH))
else if (slideItemObj.options.cy || slideItemObj.options.h)
intRowH = Math.round(
(slideItemObj.options.h ? inch2Emu(slideItemObj.options.h) : typeof slideItemObj.options.cy === 'number' ? slideItemObj.options.cy : 1) /
arrTabRows.length
)
// B: Start row
strXml += `<a:tr h="${intRowH}">`
// C: Loop over each CELL
cells.forEach(cellObj => {
let cell: TableCell = cellObj
let cellSpanAttrs = {
rowSpan: cell.options?.rowspan > 1 ? cell.options.rowspan : undefined,
gridSpan: cell.options?.colspan > 1 ? cell.options.colspan : undefined,
vMerge: cell._vmerge ? 1 : undefined,
hMerge: cell._hmerge ? 1 : undefined,
}
let cellSpanAttrStr = Object.keys(cellSpanAttrs)
.map(k => [k, cellSpanAttrs[k]])
.filter(([_k, v]) => !!v)
.map(([k, v]) => `${k}="${v}"`)
.join(' ')
if (cellSpanAttrStr) cellSpanAttrStr = ' ' + cellSpanAttrStr
// 1: COLSPAN/ROWSPAN: Add dummy cells for any active colspan/rowspan
if (cell._hmerge || cell._vmerge) {
strXml += `<a:tc${cellSpanAttrStr}><a:tcPr/></a:tc>`
return
}
// 2: OPTIONS: Build/set cell options
let cellOpts = cell.options || ({} as TableCell['options'])
cell.options = cellOpts
// B: Inherit some options from table when cell options dont exist
// @see: http://officeopenxml.com/drwTableCellProperties-alignment.php
;['align', 'bold', 'border', 'color', 'fill', 'fontFace', 'fontSize', 'margin', 'underline', 'valign'].forEach(name => {
if (objTabOpts[name] && !cellOpts[name] && cellOpts[name] !== 0) cellOpts[name] = objTabOpts[name]
})
let cellValign = cellOpts.valign
? ' anchor="' +
cellOpts.valign
.replace(/^c$/i, 'ctr')
.replace(/^m$/i, 'ctr')
.replace('center', 'ctr')
.replace('middle', 'ctr')
.replace('top', 't')
.replace('btm', 'b')
.replace('bottom', 'b') +
'"'
: ''
let fillColor =
cell._optImp && cell._optImp.fill && cell._optImp.fill.color
? cell._optImp.fill.color
: cell._optImp && cell._optImp.fill && typeof cell._optImp.fill === 'string'
? cell._optImp.fill
: ''
fillColor =
fillColor || (cellOpts.fill && cellOpts.fill.color) ? cellOpts.fill.color : cellOpts.fill && typeof cellOpts.fill === 'string' ? cellOpts.fill : ''
let cellFill = fillColor ? `<a:solidFill>${createColorElement(fillColor)}</a:solidFill>` : ''
let cellMargin = cellOpts.margin === 0 || cellOpts.margin ? cellOpts.margin : DEF_CELL_MARGIN_PT
if (!Array.isArray(cellMargin) && typeof cellMargin === 'number') cellMargin = [cellMargin, cellMargin, cellMargin, cellMargin]
let cellMarginXml = ` marL="${valToPts(cellMargin[3])}" marR="${valToPts(cellMargin[1])}" marT="${valToPts(cellMargin[0])}" marB="${valToPts(
cellMargin[2]
)}"`
// FUTURE: Cell NOWRAP property (text wrap: add to a:tcPr (horzOverflow="overflow" or whatever options exist)
// 4: Set CELL content and properties ==================================
strXml += `<a:tc${cellSpanAttrStr}>${genXmlTextBody(cell)}<a:tcPr${cellMarginXml}${cellValign}>`
//strXml += `<a:tc${cellColspan}${cellRowspan}>${genXmlTextBody(cell)}<a:tcPr${cellMarginXml}${cellValign}${cellTextDir}>`
// FIXME: 20200525: ^^^
// <a:tcPr marL="38100" marR="38100" marT="38100" marB="38100" vert="vert270">
// 5: Borders: Add any borders
if (cellOpts.border && Array.isArray(cellOpts.border)) {
// NOTE: *** IMPORTANT! *** LRTB order matters! (Reorder a line below to watch the borders go wonky in MS-PPT-2013!!)
;[
{ idx: 3, name: 'lnL' },
{ idx: 1, name: 'lnR' },
{ idx: 0, name: 'lnT' },
{ idx: 2, name: 'lnB' },
].forEach(obj => {
if (cellOpts.border[obj.idx].type !== 'none') {
strXml += `<a:${obj.name} w="${valToPts(cellOpts.border[obj.idx].pt)}" cap="flat" cmpd="sng" algn="ctr">`
strXml += `<a:solidFill>${createColorElement(cellOpts.border[obj.idx].color)}</a:solidFill>`
strXml += `<a:prstDash val="${
cellOpts.border[obj.idx].type === 'dash' ? 'sysDash' : 'solid'
}"/><a:round/><a:headEnd type="none" w="med" len="med"/><a:tailEnd type="none" w="med" len="med"/>`
strXml += `</a:${obj.name}>`
} else {
strXml += `<a:${obj.name} w="0" cap="flat" cmpd="sng" algn="ctr"><a:noFill/></a:${obj.name}>`
}
})
}
// 6: Close cell Properties & Cell
strXml += cellFill
strXml += ' </a:tcPr>'
strXml += ' </a:tc>'
})
// D: Complete row
strXml += '</a:tr>'
})
// STEP 5: Complete table
strXml += ' </a:tbl>'
strXml += ' </a:graphicData>'
strXml += ' </a:graphic>'
strXml += '</p:graphicFrame>'
// STEP 6: Set table XML
strSlideXml += strXml
// LAST: Increment counter
intTableNum++
break
case SLIDE_OBJECT_TYPES.text:
case SLIDE_OBJECT_TYPES.placeholder:
let shapeName = slideItemObj.options.shapeName ? encodeXmlEntities(slideItemObj.options.shapeName) : `Object${idx + 1}`
// Lines can have zero cy, but text should not
if (!slideItemObj.options.line && cy === 0) cy = EMU * 0.3
// Margin/Padding/Inset for textboxes
if (slideItemObj.options.margin && Array.isArray(slideItemObj.options.margin)) {
slideItemObj.options._bodyProp.lIns = valToPts(slideItemObj.options.margin[0] || 0)
slideItemObj.options._bodyProp.rIns = valToPts(slideItemObj.options.margin[1] || 0)
slideItemObj.options._bodyProp.bIns = valToPts(slideItemObj.options.margin[2] || 0)
slideItemObj.options._bodyProp.tIns = valToPts(slideItemObj.options.margin[3] || 0)
} else if (typeof slideItemObj.options.margin === 'number') {
slideItemObj.options._bodyProp.lIns = valToPts(slideItemObj.options.margin)
slideItemObj.options._bodyProp.rIns = valToPts(slideItemObj.options.margin)
slideItemObj.options._bodyProp.bIns = valToPts(slideItemObj.options.margin)
slideItemObj.options._bodyProp.tIns = valToPts(slideItemObj.options.margin)
}
// A: Start SHAPE =======================================================
strSlideXml += '<p:sp>'
// B: The addition of the "txBox" attribute is the sole determiner of if an object is a shape or textbox
strSlideXml += `<p:nvSpPr><p:cNvPr id="${idx + 2}" name="${shapeName}">`
// <Hyperlink>
if (slideItemObj.options.hyperlink && slideItemObj.options.hyperlink.url)
strSlideXml +=
'<a:hlinkClick r:id="rId' +
slideItemObj.options.hyperlink._rId +
'" tooltip="' +
(slideItemObj.options.hyperlink.tooltip ? encodeXmlEntities(slideItemObj.options.hyperlink.tooltip) : '') +
'"/>'
if (slideItemObj.options.hyperlink && slideItemObj.options.hyperlink.slide)
strSlideXml +=
'<a:hlinkClick r:id="rId' +
slideItemObj.options.hyperlink._rId +
'" tooltip="' +
(slideItemObj.options.hyperlink.tooltip ? encodeXmlEntities(slideItemObj.options.hyperlink.tooltip) : '') +
'" action="ppaction://hlinksldjump"/>'
// </Hyperlink>
strSlideXml += '</p:cNvPr>'
strSlideXml += '<p:cNvSpPr' + (slideItemObj.options && slideItemObj.options.isTextBox ? ' txBox="1"/>' : '/>')
strSlideXml += `<p:nvPr>${slideItemObj._type === 'placeholder' ? genXmlPlaceholder(slideItemObj) : genXmlPlaceholder(placeholderObj)}</p:nvPr>`
strSlideXml += '</p:nvSpPr><p:spPr>'
strSlideXml += `<a:xfrm${locationAttr}>`
strSlideXml += `<a:off x="${x}" y="${y}"/>`
strSlideXml += `<a:ext cx="${cx}" cy="${cy}"/></a:xfrm>`
strSlideXml += '<a:prstGeom prst="' + slideItemObj.shape + '"><a:avLst>'
if (slideItemObj.options.rectRadius) {
strSlideXml += `<a:gd name="adj" fmla="val ${Math.round((slideItemObj.options.rectRadius * EMU * 100000) / Math.min(cx, cy))}"/>`
} else if (slideItemObj.options.angleRange) {
for (let i = 0; i < 2; i++) {
const angle = slideItemObj.options.angleRange[i]
strSlideXml += `<a:gd name="adj${i + 1}" fmla="val ${convertRotationDegrees(angle)}" />`
}
if (slideItemObj.options.arcThicknessRatio) {
strSlideXml += `<a:gd name="adj3" fmla="val ${Math.round(slideItemObj.options.arcThicknessRatio * 50000)}" />`
}
}
strSlideXml += '</a:avLst></a:prstGeom>'
// Option: FILL
strSlideXml += slideItemObj.options.fill ? genXmlColorSelection(slideItemObj.options.fill) : '<a:noFill/>'
// shape Type: LINE: line color
if (slideItemObj.options.line) {
strSlideXml += slideItemObj.options.line.width ? `<a:ln w="${valToPts(slideItemObj.options.line.width)}">` : '<a:ln>'
strSlideXml += genXmlColorSelection(slideItemObj.options.line.color)
if (slideItemObj.options.line.dashType) strSlideXml += `<a:prstDash val="${slideItemObj.options.line.dashType}"/>`
if (slideItemObj.options.line.beginArrowType) strSlideXml += `<a:headEnd type="${slideItemObj.options.line.beginArrowType}"/>`
if (slideItemObj.options.line.endArrowType) strSlideXml += `<a:tailEnd type="${slideItemObj.options.line.endArrowType}"/>`
// FUTURE: `endArrowSize` < a: headEnd type = "arrow" w = "lg" len = "lg" /> 'sm' | 'med' | 'lg'(values are 1 - 9, making a 3x3 grid of w / len possibilities)
strSlideXml += '</a:ln>'
}
// EFFECTS > SHADOW: REF: @see http://officeopenxml.com/drwSp-effects.php
if (slideItemObj.options.shadow) {
slideItemObj.options.shadow.type = slideItemObj.options.shadow.type || 'outer'
slideItemObj.options.shadow.blur = valToPts(slideItemObj.options.shadow.blur || 8)
slideItemObj.options.shadow.offset = valToPts(slideItemObj.options.shadow.offset || 4)
slideItemObj.options.shadow.angle = Math.round((slideItemObj.options.shadow.angle || 270) * 60000)
slideItemObj.options.shadow.opacity = Math.round((slideItemObj.options.shadow.opacity || 0.75) * 100000)
slideItemObj.options.shadow.color = slideItemObj.options.shadow.color || DEF_TEXT_SHADOW.color
strSlideXml += '<a:effectLst>'
strSlideXml += '<a:' + slideItemObj.options.shadow.type + 'Shdw sx="100000" sy="100000" kx="0" ky="0" '
strSlideXml += ' algn="bl" rotWithShape="0" blurRad="' + slideItemObj.options.shadow.blur + '" '
strSlideXml += ' dist="' + slideItemObj.options.shadow.offset + '" dir="' + slideItemObj.options.shadow.angle + '">'
strSlideXml += '<a:srgbClr val="' + slideItemObj.options.shadow.color + '">'
strSlideXml += '<a:alpha val="' + slideItemObj.options.shadow.opacity + '"/></a:srgbClr>'
strSlideXml += '</a:outerShdw>'
strSlideXml += '</a:effectLst>'
}
/* TODO: FUTURE: Text wrapping (copied from MS-PPTX export)
// Commented out b/c i'm not even sure this works - current code produces text that wraps in shapes and textboxes, so...
if ( slideItemObj.options.textWrap ) {
strSlideXml += '<a:extLst>'
+ '<a:ext uri="{C572A759-6A51-4108-AA02-DFA0A04FC94B}">'
+ '<ma14:wrappingTextBoxFlag xmlns:ma14="http://schemas.microsoft.com/office/mac/drawingml/2011/main" val="1"/>'
+ '</a:ext>'
+ '</a:extLst>';
}
*/
// B: Close shape Properties
strSlideXml += '</p:spPr>'
// C: Add formatted text (text body "bodyPr")
strSlideXml += genXmlTextBody(slideItemObj)
// LAST: Close SHAPE =======================================================
strSlideXml += '</p:sp>'
break
case SLIDE_OBJECT_TYPES.image:
let sizing = slideItemObj.options.sizing,
rounding = slideItemObj.options.rounding,
width = cx,
height = cy
strSlideXml += '<p:pic>'
strSlideXml += ' <p:nvPicPr>'
strSlideXml += ' <p:cNvPr id="' + (idx + 2) + '" name="Object ' + (idx + 1) + '" descr="' + encodeXmlEntities(slideItemObj.image) + '">'
if (slideItemObj.hyperlink && slideItemObj.hyperlink.url)
strSlideXml +=
'<a:hlinkClick r:id="rId' +
slideItemObj.hyperlink._rId +
'" tooltip="' +
(slideItemObj.hyperlink.tooltip ? encodeXmlEntities(slideItemObj.hyperlink.tooltip) : '') +
'"/>'
if (slideItemObj.hyperlink && slideItemObj.hyperlink.slide)
strSlideXml +=
'<a:hlinkClick r:id="rId' +
slideItemObj.hyperlink._rId +
'" tooltip="' +
(slideItemObj.hyperlink.tooltip ? encodeXmlEntities(slideItemObj.hyperlink.tooltip) : '') +
'" action="ppaction://hlinksldjump"/>'
strSlideXml += ' </p:cNvPr>'
strSlideXml += ' <p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr>'
strSlideXml += ' <p:nvPr>' + genXmlPlaceholder(placeholderObj) + '</p:nvPr>'
strSlideXml += ' </p:nvPicPr>'
strSlideXml += '<p:blipFill>'
// NOTE: This works for both cases: either `path` or `data` contains the SVG
if (
(slide._relsMedia || []).filter(rel => rel.rId === slideItemObj.imageRid)[0] &&
(slide._relsMedia || []).filter(rel => rel.rId === slideItemObj.imageRid)[0]['extn'] === 'svg'
) {
strSlideXml += '<a:blip r:embed="rId' + (slideItemObj.imageRid - 1) + '">'
strSlideXml += ' <a:extLst>'
strSlideXml += ' <a:ext uri="{96DAC541-7B7A-43D3-8B79-37D633B846F1}">'
strSlideXml += ' <asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="rId' + slideItemObj.imageRid + '"/>'
strSlideXml += ' </a:ext>'
strSlideXml += ' </a:extLst>'
strSlideXml += '</a:blip>'
} else {
strSlideXml += '<a:blip r:embed="rId' + slideItemObj.imageRid + '"/>'
}
if (sizing && sizing.type) {
let boxW = sizing.w ? getSmartParseNumber(sizing.w, 'X', slide._presLayout) : cx,
boxH = sizing.h ? getSmartParseNumber(sizing.h, 'Y', slide._presLayout) : cy,
boxX = getSmartParseNumber(sizing.x || 0, 'X', slide._presLayout),
boxY = getSmartParseNumber(sizing.y || 0, 'Y', slide._presLayout)
strSlideXml += imageSizingXml[sizing.type]({ w: width, h: height }, { w: boxW, h: boxH, x: boxX, y: boxY })
width = boxW
height = boxH
} else {
strSlideXml += ' <a:stretch><a:fillRect/></a:stretch>'
}
strSlideXml += '</p:blipFill>'
strSlideXml += '<p:spPr>'
strSlideXml += ' <a:xfrm' + locationAttr + '>'
strSlideXml += ' <a:off x="' + x + '" y="' + y + '"/>'
strSlideXml += ' <a:ext cx="' + width + '" cy="' + height + '"/>'
strSlideXml += ' </a:xfrm>'
strSlideXml += ' <a:prstGeom prst="' + (rounding ? 'ellipse' : 'rect') + '"><a:avLst/></a:prstGeom>'
strSlideXml += '</p:spPr>'
strSlideXml += '</p:pic>'
break
case SLIDE_OBJECT_TYPES.media:
if (slideItemObj.mtype === 'online') {
strSlideXml += '<p:pic>'
strSlideXml += ' <p:nvPicPr>'
// IMPORTANT: <p:cNvPr id="" value is critical - if not the same number as preview image rId, PowerPoint throws error!
strSlideXml += ' <p:cNvPr id="' + (slideItemObj.mediaRid + 2) + '" name="Picture' + (idx + 1) + '"/>'
strSlideXml += ' <p:cNvPicPr/>'
strSlideXml += ' <p:nvPr>'
strSlideXml += ' <a:videoFile r:link="rId' + slideItemObj.mediaRid + '"/>'
strSlideXml += ' </p:nvPr>'
strSlideXml += ' </p:nvPicPr>'
// NOTE: `blip` is diferent than videos; also there's no preview "p:extLst" above but exists in videos
strSlideXml += ' <p:blipFill><a:blip r:embed="rId' + (slideItemObj.mediaRid + 1) + '"/><a:stretch><a:fillRect/></a:stretch></p:blipFill>' // NOTE: Preview image is required!
strSlideXml += ' <p:spPr>'
strSlideXml += ' <a:xfrm' + locationAttr + '>'
strSlideXml += ' <a:off x="' + x + '" y="' + y + '"/>'
strSlideXml += ' <a:ext cx="' + cx + '" cy="' + cy + '"/>'
strSlideXml += ' </a:xfrm>'
strSlideXml += ' <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>'
strSlideXml += ' </p:spPr>'
strSlideXml += '</p:pic>'
} else {
strSlideXml += '<p:pic>'
strSlideXml += ' <p:nvPicPr>'
// IMPORTANT: <p:cNvPr id="" value is critical - if not the same number as preiew image rId, PowerPoint throws error!
strSlideXml +=
' <p:cNvPr id="' +
(slideItemObj.mediaRid + 2) +
'" name="' +
slideItemObj.media.split('/').pop().split('.').shift() +
'"><a:hlinkClick r:id="" action="ppaction://media"/></p:cNvPr>'
strSlideXml += ' <p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr>'
strSlideXml += ' <p:nvPr>'
strSlideXml += ' <a:videoFile r:link="rId' + slideItemObj.mediaRid + '"/>'
strSlideXml += ' <p:extLst>'
strSlideXml += ' <p:ext uri="{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}">'
strSlideXml += ' <p14:media xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" r:embed="rId' + (slideItemObj.mediaRid + 1) + '"/>'
strSlideXml += ' </p:ext>'
strSlideXml += ' </p:extLst>'
strSlideXml += ' </p:nvPr>'
strSlideXml += ' </p:nvPicPr>'
strSlideXml += ' <p:blipFill><a:blip r:embed="rId' + (slideItemObj.mediaRid + 2) + '"/><a:stretch><a:fillRect/></a:stretch></p:blipFill>' // NOTE: Preview image is required!
strSlideXml += ' <p:spPr>'
strSlideXml += ' <a:xfrm' + locationAttr + '>'
strSlideXml += ' <a:off x="' + x + '" y="' + y + '"/>'
strSlideXml += ' <a:ext cx="' + cx + '" cy="' + cy + '"/>'
strSlideXml += ' </a:xfrm>'
strSlideXml += ' <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>'
strSlideXml += ' </p:spPr>'
strSlideXml += '</p:pic>'
}
break
case SLIDE_OBJECT_TYPES.chart:
strSlideXml += '<p:graphicFrame>'
strSlideXml += ' <p:nvGraphicFramePr>'
strSlideXml += ' <p:cNvPr id="' + (idx + 2) + '" name="Chart ' + (idx + 1) + '"/>'
strSlideXml += ' <p:cNvGraphicFramePr/>'
strSlideXml += ' <p:nvPr>' + genXmlPlaceholder(placeholderObj) + '</p:nvPr>'
strSlideXml += ' </p:nvGraphicFramePr>'
strSlideXml += ' <p:xfrm>'
strSlideXml += ' <a:off x="' + x + '" y="' + y + '"/>'
strSlideXml += ' <a:ext cx="' + cx + '" cy="' + cy + '"/>'
strSlideXml += ' </p:xfrm>'
strSlideXml += ' <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">'
strSlideXml += ' <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">'
strSlideXml += ' <c:chart r:id="rId' + slideItemObj.chartRid + '" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"/>'
strSlideXml += ' </a:graphicData>'
strSlideXml += ' </a:graphic>'
strSlideXml += '</p:graphicFrame>'
break
default:
strSlideXml += ''
break
}
})
// STEP 5: Add slide numbers (if any) last
if (slide._slideNumberProps) {
strSlideXml +=
'<p:sp>' +
' <p:nvSpPr>' +
' <p:cNvPr id="25" name="Slide Number Placeholder 24"/>' +
' <p:cNvSpPr><a:spLocks noGrp="1"/></p:cNvSpPr>' +
' <p:nvPr><p:ph type="sldNum" sz="quarter" idx="4294967295"/></p:nvPr>' +
' </p:nvSpPr>' +
' <p:spPr>' +
' <a:xfrm>' +
' <a:off x="' +
getSmartParseNumber(slide._slideNumberProps.x, 'X', slide._presLayout) +
'" y="' +
getSmartParseNumber(slide._slideNumberProps.y, 'Y', slide._presLayout) +
'"/>' +
' <a:ext cx="' +
(slide._slideNumberProps.w ? getSmartParseNumber(slide._slideNumberProps.w, 'X', slide._presLayout) : 800000) +
'" cy="' +
(slide._slideNumberProps.h ? getSmartParseNumber(slide._slideNumberProps.h, 'Y', slide._presLayout) : 300000) +
'"/>' +
' </a:xfrm>' +
' <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>' +
' <a:extLst><a:ext uri="{C572A759-6A51-4108-AA02-DFA0A04FC94B}"><ma14:wrappingTextBoxFlag val="0" xmlns:ma14="http://schemas.microsoft.com/office/mac/drawingml/2011/main"/></a:ext></a:extLst>' +
' </p:spPr>'
strSlideXml += '<p:txBody>'
strSlideXml += '<a:bodyPr'
if (slide._slideNumberProps.margin && Array.isArray(slide._slideNumberProps.margin)) {
strSlideXml += ` lIns="${valToPts(slide._slideNumberProps.margin[3] || 0)}"`
strSlideXml += ` tIns="${valToPts(slide._slideNumberProps.margin[0] || 0)}"`
strSlideXml += ` rIns="${valToPts(slide._slideNumberProps.margin[1] || 0)}"`
strSlideXml += ` bIns="${valToPts(slide._slideNumberProps.margin[2] || 0)}"`
} else if (typeof slide._slideNumberProps.margin === 'number') {
strSlideXml += ` lIns="${valToPts(slide._slideNumberProps.margin || 0)}"`
strSlideXml += ` tIns="${valToPts(slide._slideNumberProps.margin || 0)}"`
strSlideXml += ` rIns="${valToPts(slide._slideNumberProps.margin || 0)}"`
strSlideXml += ` bIns="${valToPts(slide._slideNumberProps.margin || 0)}"`
}
strSlideXml += '/>'
strSlideXml += ' <a:lstStyle><a:lvl1pPr>'
if (slide._slideNumberProps.fontFace || slide._slideNumberProps.fontSize || slide._slideNumberProps.color) {
strSlideXml += `<a:defRPr sz="${Math.round((slide._slideNumberProps.fontSize || 12) * 100)}">`
if (slide._slideNumberProps.color) strSlideXml += genXmlColorSelection(slide._slideNumberProps.color)
if (slide._slideNumberProps.fontFace)
strSlideXml += `<a:latin typeface="${slide._slideNumberProps.fontFace}"/><a:ea typeface="${slide._slideNumberProps.fontFace}"/><a:cs typeface="${slide._slideNumberProps.fontFace}"/>`
strSlideXml += '</a:defRPr>'
}
strSlideXml += '</a:lvl1pPr></a:lstStyle>'
strSlideXml += `<a:p><a:fld id="${SLDNUMFLDID}" type="slidenum"><a:rPr lang="en-US"/>`
if (slide._slideNumberProps.align) strSlideXml += `<a:pPr algn="${slide._slideNumberProps.align.substring(0, 1)}"/>`
strSlideXml += `<a:t></a:t></a:fld><a:endParaRPr lang="en-US"/></a:p>`
strSlideXml += '</p:txBody></p:sp>'
}
// STEP 6: Close spTree and finalize slide XML
strSlideXml += '</p:spTree>'
strSlideXml += '</p:cSld>'
// LAST: Return
return strSlideXml
}
/**
* Transforms slide relations to XML string.
* Extra relations that are not dynamic can be passed using the 2nd arg (e.g. theme relation in master file).
* These relations use rId series that starts with 1-increased maximum of rIds used for dynamic relations.
* @param {PresSlide | SlideLayout} slide - slide object whose relations are being transformed
* @param {{ target: string; type: string }[]} defaultRels - array of default relations
* @return {string} XML
*/
function slideObjectRelationsToXml(slide: PresSlide | SlideLayout, defaultRels: { target: string; type: string }[]): string {
let lastRid = 0 // stores maximum rId used for dynamic relations
let strXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + CRLF + '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
// STEP 1: Add all rels for this Slide
slide._rels.forEach((rel: ISlideRel) => {
lastRid = Math.max(lastRid, rel.rId)
if (rel.type.toLowerCase().indexOf('hyperlink') > -1) {
if (rel.data === 'slide') {
strXml +=
'<Relationship Id="rId' +
rel.rId +
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"' +
' Target="slide' +
rel.Target +
'.xml"/>'
} else {
strXml +=
'<Relationship Id="rId' +
rel.rId +
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"' +
' Target="' +
rel.Target +
'" TargetMode="External"/>'
}
} else if (rel.type.toLowerCase().indexOf('notesSlide') > -1) {
strXml +=
'<Relationship Id="rId' + rel.rId + '" Target="' + rel.Target + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide"/>'
}
})
;(slide._relsChart || []).forEach((rel: ISlideRelChart) => {
lastRid = Math.max(lastRid, rel.rId)
strXml += '<Relationship Id="rId' + rel.rId + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="' + rel.Target + '"/>'
})
;(slide._relsMedia || []).forEach((rel: ISlideRelMedia) => {
lastRid = Math.max(lastRid, rel.rId)
if (rel.type.toLowerCase().indexOf('image') > -1) {
strXml += '<Relationship Id="rId' + rel.rId + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="' + rel.Target + '"/>'
} else if (rel.type.toLowerCase().indexOf('audio') > -1) {
// As media has *TWO* rel entries per item, check for first one, if found add second rel with alt style
if (strXml.indexOf(' Target="' + rel.Target + '"') > -1)
strXml += '<Relationship Id="rId' + rel.rId + '" Type="http://schemas.microsoft.com/office/2007/relationships/media" Target="' + rel.Target + '"/>'
else
strXml +=
'<Relationship Id="rId' + rel.rId + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/audio" Target="' + rel.Target + '"/>'
} else if (rel.type.toLowerCase().indexOf('video') > -1) {
// As media has *TWO* rel entries per item, check for first one, if found add second rel with alt style
if (strXml.indexOf(' Target="' + rel.Target + '"') > -1)
strXml += '<Relationship Id="rId' + rel.rId + '" Type="http://schemas.microsoft.com/office/2007/relationships/media" Target="' + rel.Target + '"/>'
else
strXml +=
'<Relationship Id="rId' + rel.rId + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/video" Target="' + rel.Target + '"/>'
} else if (rel.type.toLowerCase().indexOf('online') > -1) {
// As media has *TWO* rel entries per item, check for first one, if found add second rel with alt style
if (strXml.indexOf(' Target="' + rel.Target + '"') > -1)
strXml += '<Relationship Id="rId' + rel.rId + '" Type="http://schemas.microsoft.com/office/2007/relationships/image" Target="' + rel.Target + '"/>'
else
strXml +=
'<Relationship Id="rId' +
rel.rId +
'" Target="' +
rel.Target +
'" TargetMode="External" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/video"/>'
}
})
// STEP 2: Add default rels
defaultRels.forEach((rel, idx) => {
strXml += '<Relationship Id="rId' + (lastRid + idx + 1) + '" Type="' + rel.type + '" Target="' + rel.target + '"/>'
})
strXml += '</Relationships>'
return strXml
}
/**
* Generate XML Paragraph Properties
* @param {ISlideObject|TextProps} textObj - text object
* @param {boolean} isDefault - array of default relations
* @return {string} XML
*/
function genXmlParagraphProperties(textObj: ISlideObject | TextProps, isDefault: boolean): string {
let strXmlBullet = '',
strXmlLnSpc = '',
strXmlParaSpc = ''
let tag = isDefault ? 'a:lvl1pPr' : 'a:pPr'
let bulletMarL = valToPts(DEF_BULLET_MARGIN)
let paragraphPropXml = `<${tag}${textObj.options.rtlMode ? ' rtl="1" ' : ''}`
// A: Build paragraphProperties
{
// OPTION: align
if (textObj.options.align) {
switch (textObj.options.align) {
case 'left':
paragraphPropXml += ' algn="l"'
break
case 'right':
paragraphPropXml += ' algn="r"'
break
case 'center':
paragraphPropXml += ' algn="ctr"'
break
case 'justify':
paragraphPropXml += ' algn="just"'
break
default:
paragraphPropXml += ''
break
}
}
if (textObj.options.lineSpacing) strXmlLnSpc = `<a:lnSpc><a:spcPts val="${Math.round(textObj.options.lineSpacing * 100)}"/></a:lnSpc>`
// OPTION: indent
if (textObj.options.indentLevel && !isNaN(Number(textObj.options.indentLevel)) && textObj.options.indentLevel > 0) {
paragraphPropXml += ` lvl="${textObj.options.indentLevel}"`
}
// OPTION: Paragraph Spacing: Before/After
if (textObj.options.paraSpaceBefore && !isNaN(Number(textObj.options.paraSpaceBefore)) && textObj.options.paraSpaceBefore > 0) {
strXmlParaSpc += `<a:spcBef><a:spcPts val="${Math.round(textObj.options.paraSpaceBefore * 100)}"/></a:spcBef>`
}
if (textObj.options.paraSpaceAfter && !isNaN(Number(textObj.options.paraSpaceAfter)) && textObj.options.paraSpaceAfter > 0) {
strXmlParaSpc += `<a:spcAft><a:spcPts val="${Math.round(textObj.options.paraSpaceAfter * 100)}"/></a:spcAft>`
}
// OPTION: bullet
// NOTE: OOXML uses the unicode character set for Bullets
// EX: Unicode Character 'BULLET' (U+2022) ==> '<a:buChar char="•"/>'
if (typeof textObj.options.bullet === 'object') {
if (textObj && textObj.options && textObj.options.bullet && textObj.options.bullet.indent) bulletMarL = valToPts(textObj.options.bullet.indent)
if (textObj.options.bullet.type) {
if (textObj.options.bullet.type.toString().toLowerCase() === 'number') {
paragraphPropXml += ` marL="${
textObj.options.indentLevel && textObj.options.indentLevel > 0 ? bulletMarL + bulletMarL * textObj.options.indentLevel : bulletMarL
}" indent="-${bulletMarL}"`
strXmlBullet = `<a:buSzPct val="100000"/><a:buFont typeface="+mj-lt"/><a:buAutoNum type="${textObj.options.bullet.style || 'arabicPeriod'}" startAt="${
textObj.options.bullet.numberStartAt || textObj.options.bullet.startAt || '1'
}"/>`
}
} else if (textObj.options.bullet.characterCode) {
let bulletCode = `&#x${textObj.options.bullet.characterCode};`
// Check value for hex-ness (s/b 4 char hex)
if (/^[0-9A-Fa-f]{4}$/.test(textObj.options.bullet.characterCode) === false) {
console.warn('Warning: `bullet.characterCode should be a 4-digit unicode charatcer (ex: 22AB)`!')
bulletCode = BULLET_TYPES['DEFAULT']
}
paragraphPropXml += ` marL="${
textObj.options.indentLevel && textObj.options.indentLevel > 0 ? bulletMarL + bulletMarL * textObj.options.indentLevel : bulletMarL
}" indent="-${bulletMarL}"`
strXmlBullet = '<a:buSzPct val="100000"/><a:buChar char="' + bulletCode + '"/>'
} else if (textObj.options.bullet.code) {
// @deprecated `bullet.code` v3.3.0
let bulletCode = `&#x${textObj.options.bullet.code};`
// Check value for hex-ness (s/b 4 char hex)
if (/^[0-9A-Fa-f]{4}$/.test(textObj.options.bullet.code) === false) {
console.warn('Warning: `bullet.code should be a 4-digit hex code (ex: 22AB)`!')
bulletCode = BULLET_TYPES['DEFAULT']
}
paragraphPropXml += ` marL="${
textObj.options.indentLevel && textObj.options.indentLevel > 0 ? bulletMarL + bulletMarL * textObj.options.indentLevel : bulletMarL
}" indent="-${bulletMarL}"`
strXmlBullet = '<a:buSzPct val="100000"/><a:buChar char="' + bulletCode + '"/>'
} else {
paragraphPropXml += ` marL="${
textObj.options.indentLevel && textObj.options.indentLevel > 0 ? bulletMarL + bulletMarL * textObj.options.indentLevel : bulletMarL
}" indent="-${bulletMarL}"`
strXmlBullet = `<a:buSzPct val="100000"/><a:buChar char="${BULLET_TYPES['DEFAULT']}"/>`
}
} else if (textObj.options.bullet === true) {
paragraphPropXml += ` marL="${
textObj.options.indentLevel && textObj.options.indentLevel > 0 ? bulletMarL + bulletMarL * textObj.options.indentLevel : bulletMarL
}" indent="-${bulletMarL}"`
strXmlBullet = `<a:buSzPct val="100000"/><a:buChar char="${BULLET_TYPES['DEFAULT']}"/>`
} else if (textObj.options.bullet === false) {
// We only add this when the user explicitely asks for no bullet, otherwise, it can override the master defaults!
paragraphPropXml += ` indent="0" marL="0"` // FIX: ISSUE#589 - specify zero indent and marL or default will be hanging paragraph
strXmlBullet = '<a:buNone/>'
}
// B: Close Paragraph-Properties
// IMPORTANT: strXmlLnSpc, strXmlParaSpc, and strXmlBullet require strict ordering - anything out of order is ignored. (PPT-Online, PPT for Mac)
let childPropXml = strXmlLnSpc + strXmlParaSpc + strXmlBullet
if (isDefault) childPropXml += genXmlTextRunProperties(textObj.options, true)
if (childPropXml) {
paragraphPropXml += '>' + childPropXml + '</' + tag + '>'
} else {
// self-close when no child props
paragraphPropXml += '/>'
}
}
return paragraphPropXml
}
/**
* Generate XML Text Run Properties (`a:rPr`)
* @param {ObjectOptions|TextPropsOptions} opts - text options
* @param {boolean} isDefault - whether these are the default text run properties
* @return {string} XML
*/
function genXmlTextRunProperties(opts: ObjectOptions | TextPropsOptions, isDefault: boolean): string {
let runProps = ''
let runPropsTag = isDefault ? 'a:defRPr' : 'a:rPr'
// BEGIN runProperties (ex: `<a:rPr lang="en-US" sz="1600" b="1" dirty="0">`)
runProps += '<' + runPropsTag + ' lang="' + (opts.lang ? opts.lang : 'en-US') + '"' + (opts.lang ? ' altLang="en-US"' : '')
runProps += opts.fontSize ? ' sz="' + Math.round(opts.fontSize) + '00"' : '' // NOTE: Use round so sizes like '7.5' wont cause corrupt pres.
runProps += opts.hasOwnProperty('bold') ? ` b="${opts.bold ? 1 : 0}"` : ''
runProps += opts.hasOwnProperty('italic') ? ` i="${opts.italic ? 1 : 0}"` : ''
runProps += opts.hasOwnProperty('strike') ? ` strike="${opts.strike ? 'sngStrike' : 'noStrike'}"` : ''
runProps += opts.hasOwnProperty('underline') || opts.hyperlink ? ` u="${opts.underline || opts.hyperlink ? 'sng' : 'none'}"` : ''
runProps += opts.subscript ? ' baseline="-40000"' : opts.superscript ? ' baseline="30000"' : ''
runProps += opts.charSpacing ? ` spc="${Math.round(opts.charSpacing * 100)}" kern="0"` : '' // IMPORTANT: Also disable kerning; otherwise text won't actually expand
runProps += ' dirty="0">'
// Color / Font / Outline are children of <a:rPr>, so add them now before closing the runProperties tag
if (opts.color || opts.fontFace || opts.outline) {
if (opts.outline && typeof opts.outline === 'object') {
runProps += `<a:ln w="${valToPts(opts.outline.size || 0.75)}">${genXmlColorSelection(opts.outline.color || 'FFFFFF')}</a:ln>`
}
if (opts.color) runProps += genXmlColorSelection(opts.color)
if (opts.glow) runProps += `<a:effectLst>${createGlowElement(opts.glow, DEF_TEXT_GLOW)}</a:effectLst>`
if (opts.fontFace) {
// NOTE: 'cs' = Complex Script, 'ea' = East Asian (use "-120" instead of "0" - per Issue #174); ea must come first (Issue #174)
runProps += `<a:latin typeface="${opts.fontFace}" pitchFamily="34" charset="0"/><a:ea typeface="${opts.fontFace}" pitchFamily="34" charset="-122"/><a:cs typeface="${opts.fontFace}" pitchFamily="34" charset="-120"/>`
}
}
// Hyperlink support
if (opts.hyperlink) {
if (typeof opts.hyperlink !== 'object') throw new Error("ERROR: text `hyperlink` option should be an object. Ex: `hyperlink:{url:'https://github.com'}` ")
else if (!opts.hyperlink.url && !opts.hyperlink.slide) throw new Error("ERROR: 'hyperlink requires either `url` or `slide`'")
else if (opts.hyperlink.url) {
// TODO: (20170410): FUTURE-FEATURE: color (link is always blue in Keynote and PPT online, so usual text run above isnt honored for links..?)
//runProps += '<a:uFill>'+ genXmlColorSelection('0000FF') +'</a:uFill>'; // Breaks PPT2010! (Issue#74)
runProps += `<a:hlinkClick r:id="rId${opts.hyperlink._rId}" invalidUrl="" action="" tgtFrame="" tooltip="${
opts.hyperlink.tooltip ? encodeXmlEntities(opts.hyperlink.tooltip) : ''
}" history="1" highlightClick="0" endSnd="0"/>`
} else if (opts.hyperlink.slide) {
runProps += `<a:hlinkClick r:id="rId${opts.hyperlink._rId}" action="ppaction://hlinksldjump" tooltip="${
opts.hyperlink.tooltip ? encodeXmlEntities(opts.hyperlink.tooltip) : ''
}"/>`
}
}
// END runProperties
runProps += `</${runPropsTag}>`
return runProps
}
/**
* Build textBody text runs [`<a:r></a:r>`] for paragraphs [`<a:p>`]
* @param {TextProps} textObj - Text object
* @return {string} XML string
*/