| |
| |
| |
|
|
| import { EMU, REGEX_HEX_COLOR, DEF_FONT_COLOR, ONEPT, SchemeColor, SCHEME_COLORS } from './core-enums' |
| import { PresLayout, TextGlowProps, PresSlide, ShapeFillProps, Color, ShapeLineProps, Coord, ShadowProps } from './core-interfaces' |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function getSmartParseNumber (size: Coord, xyDir: 'X' | 'Y', layout: PresLayout): number { |
| |
| if (typeof size === 'string' && !isNaN(Number(size))) size = Number(size) |
|
|
| |
| |
| if (typeof size === 'number' && size < 100) return inch2Emu(size) |
|
|
| |
| |
| if (typeof size === 'number' && size >= 100) return size |
|
|
| |
| if (typeof size === 'string' && size.includes('%')) { |
| if (xyDir && xyDir === 'X') return Math.round((parseFloat(size) / 100) * layout.width) |
| if (xyDir && xyDir === 'Y') return Math.round((parseFloat(size) / 100) * layout.height) |
|
|
| |
| return Math.round((parseFloat(size) / 100) * layout.width) |
| } |
|
|
| |
| return 0 |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function getUuid (uuidFormat: string): string { |
| return uuidFormat.replace(/[xy]/g, function (c) { |
| const r = (Math.random() * 16) | 0 |
| const v = c === 'x' ? r : (r & 0x3) | 0x8 |
| return v.toString(16) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| export function encodeXmlEntities (xml: string): string { |
| |
| if (typeof xml === 'undefined' || xml == null) return '' |
| return xml.toString().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') |
| } |
|
|
| |
| |
| |
| |
| |
| export function inch2Emu (inches: number | string): number { |
| |
| |
| if (typeof inches === 'number' && inches > 100) return inches |
| if (typeof inches === 'string') inches = Number(inches.replace(/in*/gi, '')) |
| return Math.round(EMU * inches) |
| } |
|
|
| |
| |
| |
| |
| |
| export function valToPts (pt: number | string): number { |
| const points = Number(pt) || 0 |
| return isNaN(points) ? 0 : Math.round(points * ONEPT) |
| } |
|
|
| |
| |
| |
| |
| |
| export function convertRotationDegrees (d: number): number { |
| d = d || 0 |
| return Math.round((d > 360 ? d - 360 : d) * 60000) |
| } |
|
|
| |
| |
| |
| |
| |
| export function componentToHex (c: number): string { |
| const hex = c.toString(16) |
| return hex.length === 1 ? '0' + hex : hex |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function rgbToHex (r: number, g: number, b: number): string { |
| return (componentToHex(r) + componentToHex(g) + componentToHex(b)).toUpperCase() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function createColorElement (colorStr: string | SCHEME_COLORS, innerElements?: string): string { |
| let colorVal = (colorStr || '').replace('#', '') |
|
|
| if ( |
| !REGEX_HEX_COLOR.test(colorVal) && |
| colorVal !== SchemeColor.background1 && |
| colorVal !== SchemeColor.background2 && |
| colorVal !== SchemeColor.text1 && |
| colorVal !== SchemeColor.text2 && |
| colorVal !== SchemeColor.accent1 && |
| colorVal !== SchemeColor.accent2 && |
| colorVal !== SchemeColor.accent3 && |
| colorVal !== SchemeColor.accent4 && |
| colorVal !== SchemeColor.accent5 && |
| colorVal !== SchemeColor.accent6 |
| ) { |
| console.warn(`"${colorVal}" is not a valid scheme color or hex RGB! "${DEF_FONT_COLOR}" used instead. Only provide 6-digit RGB or 'pptx.SchemeColor' values!`) |
| colorVal = DEF_FONT_COLOR |
| } |
|
|
| const tagName = REGEX_HEX_COLOR.test(colorVal) ? 'srgbClr' : 'schemeClr' |
| const colorAttr = 'val="' + (REGEX_HEX_COLOR.test(colorVal) ? colorVal.toUpperCase() : colorVal) + '"' |
|
|
| return innerElements ? `<a:${tagName} ${colorAttr}>${innerElements}</a:${tagName}>` : `<a:${tagName} ${colorAttr}/>` |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function createGlowElement (options: TextGlowProps, defaults: TextGlowProps): string { |
| let strXml = '' |
| const opts = { ...defaults, ...options } |
| const size = Math.round(opts.size * ONEPT) |
| const color = opts.color |
| const opacity = Math.round(opts.opacity * 100000) |
|
|
| strXml += `<a:glow rad="${size}">` |
| strXml += createColorElement(color, `<a:alpha val="${opacity}"/>`) |
| strXml += '</a:glow>' |
|
|
| return strXml |
| } |
|
|
| |
| |
| |
| |
| |
| export function genXmlColorSelection (props: Color | ShapeFillProps | ShapeLineProps): string { |
| let fillType = 'solid' |
| let colorVal = '' |
| let internalElements = '' |
| let outText = '' |
|
|
| if (props) { |
| if (typeof props === 'string') colorVal = props |
| else { |
| if (props.type) fillType = props.type |
| if (props.color) colorVal = props.color |
| if (props.alpha) internalElements += `<a:alpha val="${Math.round((100 - props.alpha) * 1000)}"/>` |
| if (props.transparency) internalElements += `<a:alpha val="${Math.round((100 - props.transparency) * 1000)}"/>` |
| } |
|
|
| switch (fillType) { |
| case 'solid': |
| outText += `<a:solidFill>${createColorElement(colorVal, internalElements)}</a:solidFill>` |
| break |
| default: |
| outText += '' |
| break |
| } |
| } |
|
|
| return outText |
| } |
|
|
| |
| |
| |
| |
| |
| export function getNewRelId (target: PresSlide): number { |
| return target._rels.length + target._relsChart.length + target._relsMedia.length + 1 |
| } |
|
|
| |
| |
| |
| |
| export function correctShadowOptions (ShadowProps: ShadowProps): ShadowProps | undefined { |
| if (!ShadowProps || typeof ShadowProps !== 'object') { |
| |
| return |
| } |
|
|
| |
| if (ShadowProps.type !== 'outer' && ShadowProps.type !== 'inner' && ShadowProps.type !== 'none') { |
| console.warn('Warning: shadow.type options are `outer`, `inner` or `none`.') |
| ShadowProps.type = 'outer' |
| } |
|
|
| |
| if (ShadowProps.angle) { |
| |
| if (isNaN(Number(ShadowProps.angle)) || ShadowProps.angle < 0 || ShadowProps.angle > 359) { |
| console.warn('Warning: shadow.angle can only be 0-359') |
| ShadowProps.angle = 270 |
| } |
|
|
| |
| ShadowProps.angle = Math.round(Number(ShadowProps.angle)) |
| } |
|
|
| |
| if (ShadowProps.opacity) { |
| |
| if (isNaN(Number(ShadowProps.opacity)) || ShadowProps.opacity < 0 || ShadowProps.opacity > 1) { |
| console.warn('Warning: shadow.opacity can only be 0-1') |
| ShadowProps.opacity = 0.75 |
| } |
|
|
| |
| ShadowProps.opacity = Number(ShadowProps.opacity) |
| } |
|
|
| |
| if (ShadowProps.color) { |
| |
| if (ShadowProps.color.startsWith('#')) { |
| console.warn('Warning: shadow.color should not include hash (#) character, , e.g. "FF0000"') |
| ShadowProps.color = ShadowProps.color.replace('#', '') |
| } |
| } |
|
|
| return ShadowProps |
| } |
|
|