Skip to content

Commit

Permalink
feat: 支持文字删除线、字间距、阴影、角标
Browse files Browse the repository at this point in the history
  • Loading branch information
pipipi-pikachu committed Dec 1, 2023
1 parent f192ae8 commit 6ff50fd
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.umd.js.map

Large diffs are not rendered by default.

41 changes: 38 additions & 3 deletions src/fontStyle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getTextByPathList } from './utils'
import { getSolidFill } from './fill'

export function getFontType(node, type, warpObj) {
let typeface = getTextByPathList(node, ['a:rPr', 'a:latin', 'attrs', 'typeface'])
Expand Down Expand Up @@ -61,13 +62,47 @@ export function getFontSize(node, slideLayoutSpNode, type, slideMasterTextStyles
}

export function getFontBold(node) {
return (node['a:rPr'] && node['a:rPr']['attrs']['b'] === '1') ? 'bold' : ''
return getTextByPathList(node, ['a:rPr', 'attrs', 'b']) === '1' ? 'bold' : ''
}

export function getFontItalic(node) {
return (node['a:rPr'] && node['a:rPr']['attrs']['i'] === '1') ? 'italic' : ''
return getTextByPathList(node, ['a:rPr', 'attrs', 'i']) === '1' ? 'italic' : ''
}

export function getFontDecoration(node) {
return (node['a:rPr'] && node['a:rPr']['attrs']['u'] === 'sng') ? 'underline' : ''
const underline = getTextByPathList(node, ['a:rPr', 'attrs', 'u']) === 'sng' ? 'underline' : ''
const strike = getTextByPathList(node, ['a:rPr', 'attrs', 'strike']) === 'sngStrike' ? 'line-through' : ''

if (!underline && !strike) return ''
else if (underline && !strike) return underline
else if (!underline && strike) return strike
return `${underline} ${strike}`
}

export function getFontSpace(node, fontsizeFactor) {
const spc = getTextByPathList(node, ['a:rPr', 'attrs', 'spc'])
return spc ? (parseInt(spc) / 100 * fontsizeFactor + 'px') : ''
}

export function getFontSubscript(node) {
const baseline = getTextByPathList(node, ['a:rPr', 'attrs', 'baseline'])
if (!baseline) return ''
return parseInt(baseline) > 0 ? 'super' : 'sub'
}

export function getFontShadow(node, warpObj, slideFactor) {
const txtShadow = getTextByPathList(node, ['a:rPr', 'a:effectLst', 'a:outerShdw'])
if (txtShadow) {
const shadowClr = getSolidFill(txtShadow, undefined, undefined, warpObj)
const outerShdwAttrs = txtShadow['attrs']
const dir = (outerShdwAttrs['dir']) ? (parseInt(outerShdwAttrs['dir']) / 60000) : 0
const dist = parseInt(outerShdwAttrs['dist']) * slideFactor
const blurRad = (outerShdwAttrs['blurRad']) ? (parseInt(outerShdwAttrs['blurRad']) * slideFactor + 'px') : ''
const vx = dist * Math.sin(dir * Math.PI / 180)
const hx = dist * Math.cos(dir * Math.PI / 180)
if (!isNaN(vx) && !isNaN(hx)) {
return hx + 'px ' + vx + 'px ' + blurRad + ' #' + shadowClr
}
}
return ''
}
10 changes: 5 additions & 5 deletions src/pptxtojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ function genShape(node, slideLayoutSpNode, slideMasterSpNode, id, name, idx, typ
else txtRotate = rotate

let content = ''
if (node['p:txBody']) content = genTextBody(node['p:txBody'], slideLayoutSpNode, slideMasterSpNode, type, warpObj, FONTSIZE_FACTOR)
if (node['p:txBody']) content = genTextBody(node['p:txBody'], slideLayoutSpNode, slideMasterSpNode, type, warpObj, FONTSIZE_FACTOR, SLIDE_FACTOR)

const { borderColor, borderWidth, borderType, strokeDasharray } = getBorder(node, type, warpObj)
const fillColor = getShapeFill(node, undefined, warpObj) || ''
Expand Down Expand Up @@ -676,7 +676,7 @@ function genTable(node, warpObj) {

if (tcNodes.constructor === Array) {
for (const tcNode of tcNodes) {
const text = genTextBody(tcNode['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR)
const text = genTextBody(tcNode['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR, SLIDE_FACTOR)
const rowSpan = getTextByPathList(tcNode, ['attrs', 'rowSpan'])
const colSpan = getTextByPathList(tcNode, ['attrs', 'gridSpan'])
const vMerge = getTextByPathList(tcNode, ['attrs', 'vMerge'])
Expand All @@ -686,7 +686,7 @@ function genTable(node, warpObj) {
}
}
else {
const text = genTextBody(tcNodes['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR)
const text = genTextBody(tcNodes['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR, SLIDE_FACTOR)
tr.push({ text })
}
data.push(tr)
Expand All @@ -698,12 +698,12 @@ function genTable(node, warpObj) {

if (tcNodes.constructor === Array) {
for (const tcNode of tcNodes) {
const text = genTextBody(tcNode['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR)
const text = genTextBody(tcNode['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR, SLIDE_FACTOR)
tr.push({ text })
}
}
else {
const text = genTextBody(tcNodes['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR)
const text = genTextBody(tcNodes['a:txBody'], undefined, undefined, undefined, warpObj, FONTSIZE_FACTOR, SLIDE_FACTOR)
tr.push({ text })
}
data.push(tr)
Expand Down
18 changes: 14 additions & 4 deletions src/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
getFontBold,
getFontItalic,
getFontDecoration,
getFontSpace,
getFontSubscript,
getFontShadow,
} from './fontStyle'

export function genTextBody(textBodyNode, slideLayoutSpNode, slideMasterSpNode, type, warpObj, fontsizeFactor) {
export function genTextBody(textBodyNode, slideLayoutSpNode, slideMasterSpNode, type, warpObj, fontsizeFactor, slideFactor) {
if (!textBodyNode) return ''

let text = ''
Expand Down Expand Up @@ -68,10 +71,10 @@ export function genTextBody(textBodyNode, slideLayoutSpNode, slideMasterSpNode,
text += `<p style="text-align: ${align};">`
}

if (!rNode) text += genSpanElement(pNode, slideLayoutSpNode, type, warpObj, fontsizeFactor)
if (!rNode) text += genSpanElement(pNode, slideLayoutSpNode, type, warpObj, fontsizeFactor, slideFactor)
else {
for (const rNodeItem of rNode) {
text += genSpanElement(rNodeItem, slideLayoutSpNode, type, warpObj, fontsizeFactor)
text += genSpanElement(rNodeItem, slideLayoutSpNode, type, warpObj, fontsizeFactor, slideFactor)
}
}

Expand All @@ -91,7 +94,7 @@ export function getListType(node) {
return ''
}

export function genSpanElement(node, slideLayoutSpNode, type, warpObj, fontsizeFactor) {
export function genSpanElement(node, slideLayoutSpNode, type, warpObj, fontsizeFactor, slideFactor) {
const slideMasterTextStyles = warpObj['slideMasterTextStyles']

let text = node['a:t']
Expand All @@ -105,12 +108,19 @@ export function genSpanElement(node, slideLayoutSpNode, type, warpObj, fontsizeF
const fontBold = getFontBold(node)
const fontItalic = getFontItalic(node)
const fontDecoration = getFontDecoration(node)
const fontSpace = getFontSpace(node, fontsizeFactor)
const shadow = getFontShadow(node, warpObj, slideFactor)
const subscript = getFontSubscript(node)

if (fontColor) styleText += `color: ${fontColor};`
if (fontSize) styleText += `font-size: ${fontSize};`
if (fontType) styleText += `font-family: ${fontType};`
if (fontBold) styleText += `font-weight: ${fontBold};`
if (fontItalic) styleText += `font-style: ${fontItalic};`
if (fontDecoration) styleText += `text-decoration: ${fontDecoration};`
if (fontSpace) styleText += `letter-spacing: ${fontSpace};`
if (subscript) styleText += `vertical-align: ${subscript}; font-size: smaller;`
if (shadow) styleText += `text-shadow: ${shadow};`

const linkID = getTextByPathList(node, ['a:rPr', 'a:hlinkClick', 'attrs', 'r:id'])
if (linkID) {
Expand Down
Binary file modified test.pptx
Binary file not shown.
Binary file added test2.pptx
Binary file not shown.

0 comments on commit 6ff50fd

Please sign in to comment.