pptxtojson 2.0.2 → 2.0.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pptxtojson",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "A javascript tool for parsing .pptx file",
5
5
  "type": "module",
6
6
  "main": "./dist/index.umd.js",
package/src/pptxtojson.js CHANGED
@@ -4,6 +4,7 @@ import { getBorder } from './border'
4
4
  import { getSlideBackgroundFill, getShapeFill, getSolidFill, getPicFill, getPicFilters, getImageData, getVideoData, getAudioData } from './fill'
5
5
  import { getChartInfo } from './chart'
6
6
  import { getVerticalAlign, getTextAutoFit } from './paragraph'
7
+ import { getTextInsets } from './textInsets'
7
8
  import { getPosition, getSize } from './position'
8
9
  import { genTextBody, getTextNodeValue } from './text'
9
10
  import { getCustomShapePath, identifyShape } from './shape'
@@ -640,13 +641,13 @@ async function processGroupSpNode(node, warpObj, source, parentGroupHierarchy =
640
641
  if (depth > 10) return elements
641
642
 
642
643
  return elements.map(element => {
643
- const processed = {
644
- ...element,
645
- left: numberToFixed(element.left * ws),
646
- top: numberToFixed(element.top * hs),
647
- width: numberToFixed(element.width * ws),
648
- height: numberToFixed(element.height * hs),
649
- }
644
+ const processed = {
645
+ ...element,
646
+ left: numberToFixed(element.left * ws),
647
+ top: numberToFixed(element.top * hs),
648
+ width: numberToFixed(element.width * ws),
649
+ height: numberToFixed(element.height * hs),
650
+ }
650
651
  if (element.type === 'group' && element.elements) {
651
652
  processed.elements = processNestedGroupElements(element.elements, ws, hs, depth + 1)
652
653
  }
@@ -698,11 +699,11 @@ async function processSpNode(node, warpObj, source, groupHierarchy = []) {
698
699
  const txBoxVal = getTextByPathList(node, ['p:nvSpPr', 'p:cNvSpPr', 'attrs', 'txBox'])
699
700
  if (txBoxVal === '1') type = 'text'
700
701
  }
701
- if (!type) type = getTextByPathList(slideLayoutSpNode, ['p:nvSpPr', 'p:nvPr', 'p:ph', 'attrs', 'type'])
702
- if (!type) type = getTextByPathList(slideMasterSpNode, ['p:nvSpPr', 'p:nvPr', 'p:ph', 'attrs', 'type'])
703
- if (!slideMasterSpNode && type === 'ctrTitle') slideMasterSpNode = warpObj['slideMasterTables']['typeTable']['title']
704
-
705
- if (!type) {
702
+ if (!type) type = getTextByPathList(slideLayoutSpNode, ['p:nvSpPr', 'p:nvPr', 'p:ph', 'attrs', 'type'])
703
+ if (!type) type = getTextByPathList(slideMasterSpNode, ['p:nvSpPr', 'p:nvPr', 'p:ph', 'attrs', 'type'])
704
+ if (!slideMasterSpNode && type === 'ctrTitle') slideMasterSpNode = warpObj['slideMasterTables']['typeTable']['title']
705
+
706
+ if (!type) {
706
707
  if (source === 'diagramBg') type = 'diagram'
707
708
  else type = 'obj'
708
709
  }
@@ -775,6 +776,7 @@ async function genShape(node, slideLayoutSpNode, slideMasterSpNode, name, type,
775
776
  const vAlign = getVerticalAlign(node, slideLayoutSpNode, slideMasterSpNode, type)
776
777
  const isVertical = getTextByPathList(node, ['p:txBody', 'a:bodyPr', 'attrs', 'vert']) === 'eaVert'
777
778
  const autoFit = getTextAutoFit(node, slideLayoutSpNode, slideMasterSpNode)
779
+ const textInset = getTextInsets(node, slideLayoutSpNode, slideMasterSpNode)
778
780
 
779
781
  const data = {
780
782
  left,
@@ -798,6 +800,7 @@ async function genShape(node, slideLayoutSpNode, slideMasterSpNode, name, type,
798
800
  if (shadow) data.shadow = shadow
799
801
  if (autoFit) data.autoFit = autoFit
800
802
  if (link) data.link = link
803
+ if (textInset) data.textInset = textInset
801
804
 
802
805
  const isHasValidText = data.content && hasValidText(data.content)
803
806
 
@@ -1188,11 +1191,11 @@ async function genTable(node, warpObj) {
1188
1191
  if (cell.rowSpan) td.rowSpan = cell.rowSpan
1189
1192
  if (cell.colSpan) td.colSpan = cell.colSpan
1190
1193
  if (cell.vMerge) td.vMerge = cell.vMerge
1191
- if (cell.hMerge) td.hMerge = cell.hMerge
1192
- if (cell.vAlign) td.vAlign = cell.vAlign
1193
- if (cell.fontBold || fontBold) td.fontBold = cell.fontBold || fontBold
1194
- if (cell.fontColor || fontColor) td.fontColor = cell.fontColor || fontColor
1195
- if (cell.fillColor || fillColor || tbl_bgcolor) td.fillColor = cell.fillColor || fillColor || tbl_bgcolor
1194
+ if (cell.hMerge) td.hMerge = cell.hMerge
1195
+ if (cell.vAlign) td.vAlign = cell.vAlign
1196
+ if (cell.fontBold || fontBold) td.fontBold = cell.fontBold || fontBold
1197
+ if (cell.fontColor || fontColor) td.fontColor = cell.fontColor || fontColor
1198
+ if (cell.fillColor || fillColor || tbl_bgcolor) td.fillColor = cell.fillColor || fillColor || tbl_bgcolor
1196
1199
  if (cell.borders) td.borders = cell.borders
1197
1200
 
1198
1201
  tr.push(td)
@@ -1221,11 +1224,11 @@ async function genTable(node, warpObj) {
1221
1224
  if (cell.rowSpan) td.rowSpan = cell.rowSpan
1222
1225
  if (cell.colSpan) td.colSpan = cell.colSpan
1223
1226
  if (cell.vMerge) td.vMerge = cell.vMerge
1224
- if (cell.hMerge) td.hMerge = cell.hMerge
1225
- if (cell.vAlign) td.vAlign = cell.vAlign
1226
- if (cell.fontBold || fontBold) td.fontBold = cell.fontBold || fontBold
1227
- if (cell.fontColor || fontColor) td.fontColor = cell.fontColor || fontColor
1228
- if (cell.fillColor || fillColor || tbl_bgcolor) td.fillColor = cell.fillColor || fillColor || tbl_bgcolor
1227
+ if (cell.hMerge) td.hMerge = cell.hMerge
1228
+ if (cell.vAlign) td.vAlign = cell.vAlign
1229
+ if (cell.fontBold || fontBold) td.fontBold = cell.fontBold || fontBold
1230
+ if (cell.fontColor || fontColor) td.fontColor = cell.fontColor || fontColor
1231
+ if (cell.fillColor || fillColor || tbl_bgcolor) td.fillColor = cell.fillColor || fillColor || tbl_bgcolor
1229
1232
  if (cell.borders) td.borders = cell.borders
1230
1233
 
1231
1234
  tr.push(td)
@@ -0,0 +1,64 @@
1
+ import { getTextByPathList, numberToFixed } from './utils'
2
+ import { RATIO_EMUs_Points } from './constants'
3
+
4
+ const DEFAULT_INSET_EMU = {
5
+ lIns: 91440, // 0.1 in
6
+ rIns: 91440, // 0.1 in
7
+ tIns: 45720, // 0.05 in
8
+ bIns: 45720, // 0.05 in
9
+ }
10
+
11
+ function getInsetAttr(slideNode, layoutNode, masterNode, attrName) {
12
+ let v = getTextByPathList(slideNode, ['p:txBody', 'a:bodyPr', 'attrs', attrName])
13
+ if (v !== undefined && v !== null && v !== '') return v
14
+
15
+ v = getTextByPathList(layoutNode, ['p:txBody', 'a:bodyPr', 'attrs', attrName])
16
+ if (v !== undefined && v !== null && v !== '') return v
17
+
18
+ return getTextByPathList(masterNode, ['p:txBody', 'a:bodyPr', 'attrs', attrName])
19
+ }
20
+
21
+ function emuToPt(emuStr) {
22
+ if (emuStr === undefined || emuStr === null || emuStr === '') return null
23
+ const v = parseInt(emuStr, 10)
24
+ if (!Number.isFinite(v)) return null
25
+ return numberToFixed(v * RATIO_EMUs_Points)
26
+ }
27
+
28
+ export function getTextInsets(node, slideLayoutSpNode, slideMasterSpNode) {
29
+ const nodeBodyPr = getTextByPathList(node, ['p:txBody', 'a:bodyPr'])
30
+ const layoutBodyPr = getTextByPathList(slideLayoutSpNode, ['p:txBody', 'a:bodyPr'])
31
+ const masterBodyPr = getTextByPathList(slideMasterSpNode, ['p:txBody', 'a:bodyPr'])
32
+
33
+ if (!nodeBodyPr) {
34
+ if (!layoutBodyPr) {
35
+ if (!masterBodyPr) return null
36
+ }
37
+ }
38
+
39
+ let li = getInsetAttr(node, slideLayoutSpNode, slideMasterSpNode, 'lIns')
40
+ if (li === undefined || li === null || li === '') li = DEFAULT_INSET_EMU.lIns
41
+
42
+ let ti = getInsetAttr(node, slideLayoutSpNode, slideMasterSpNode, 'tIns')
43
+ if (ti === undefined || ti === null || ti === '') ti = DEFAULT_INSET_EMU.tIns
44
+
45
+ let ri = getInsetAttr(node, slideLayoutSpNode, slideMasterSpNode, 'rIns')
46
+ if (ri === undefined || ri === null || ri === '') ri = DEFAULT_INSET_EMU.rIns
47
+
48
+ let bi = getInsetAttr(node, slideLayoutSpNode, slideMasterSpNode, 'bIns')
49
+ if (bi === undefined || bi === null || bi === '') bi = DEFAULT_INSET_EMU.bIns
50
+
51
+ let l = emuToPt(li)
52
+ if (l === null) l = 0
53
+
54
+ let t = emuToPt(ti)
55
+ if (t === null) t = 0
56
+
57
+ let r = emuToPt(ri)
58
+ if (r === null) r = 0
59
+
60
+ let b = emuToPt(bi)
61
+ if (b === null) b = 0
62
+
63
+ return { l, t, r, b }
64
+ }