Skip to content

Commit d73a388

Browse files
committed
build lib
1 parent 3d2807f commit d73a388

File tree

8 files changed

+209
-29
lines changed

8 files changed

+209
-29
lines changed

demos/browser/js/pptxgen.bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demos/browser/js/pptxgen.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pptxgen.bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pptxgen.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pptxgen.cjs.js

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* PptxGenJS 3.11.0-beta @ 2022-08-12T09:49:47.957Z */
1+
/* PptxGenJS 3.11.0-beta @ 2022-08-25T07:49:20.773Z */
22
'use strict';
33

44
var JSZip = require('jszip');
@@ -4947,6 +4947,38 @@ function createGridLineElement(glOpts) {
49474947
return strXml;
49484948
}
49494949

4950+
var encodePresFontRels = function (fonts) {
4951+
return fonts
4952+
.map(function (f) { return f.styles; })
4953+
.flat()
4954+
.filter(function (v) { return !v.data && v.path; })
4955+
.map(loadFontStyle);
4956+
};
4957+
var loadFontStyle = function (style) {
4958+
return new Promise(function (resolve, reject) {
4959+
var xhr = new XMLHttpRequest();
4960+
xhr.onload = function () {
4961+
var reader = new FileReader();
4962+
reader.onloadend = function () {
4963+
style.data = reader.result;
4964+
resolve('done');
4965+
};
4966+
reader.readAsDataURL(xhr.response);
4967+
};
4968+
xhr.onerror = function (ex) {
4969+
reject("ERROR! Unable to load font (xhr.onerror): ".concat(style.path));
4970+
};
4971+
xhr.open('GET', style.path);
4972+
xhr.responseType = 'blob';
4973+
xhr.send();
4974+
});
4975+
};
4976+
var validStyles = ['regular', 'bold', 'italic', 'boldItalic'];
4977+
var isValidStyle = function (style) { return typeof style === 'object' && validStyles.includes(style.name) && (style.path || style.data); };
4978+
var isValidStyles = function (styles) { return Array.isArray(styles) && styles.every(isValidStyle); };
4979+
var isValidFont = function (font) { return typeof font === 'object' && font.name !== '' && isValidStyles(font.styles); };
4980+
var isValidFonts = function (fonts) { return Array.isArray(fonts) && fonts.every(isValidFont); };
4981+
49504982
/**
49514983
* PptxGenJS: Media Methods
49524984
*/
@@ -6409,6 +6441,7 @@ function makeXmlContTypes(slides, slideLayouts, masterSlide) {
64096441
strXml += '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>';
64106442
strXml += '<Default Extension="jpeg" ContentType="image/jpeg"/>';
64116443
strXml += '<Default Extension="jpg" ContentType="image/jpg"/>';
6444+
strXml += '<Default Extension="fntdata" ContentType="application/x-fontdata"/>';
64126445
// STEP 1: Add standard/any media types used in Presentation
64136446
strXml += '<Default Extension="png" ContentType="image/png"/>';
64146447
strXml += '<Default Extension="gif" ContentType="image/gif"/>';
@@ -6505,7 +6538,7 @@ function makeXmlCore(title, subject, author, revision) {
65056538
* @param {PresSlide[]} slides - Presenation Slides
65066539
* @returns XML
65076540
*/
6508-
function makeXmlPresentationRels(slides) {
6541+
function makeXmlPresentationRels(slides, fonts) {
65096542
var intRelNum = 1;
65106543
var strXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + CRLF;
65116544
strXml += '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">';
@@ -6517,21 +6550,27 @@ function makeXmlPresentationRels(slides) {
65176550
intRelNum++;
65186551
strXml +=
65196552
'<Relationship Id="rId' +
6520-
intRelNum +
6553+
intRelNum++ +
65216554
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster" Target="notesMasters/notesMaster1.xml"/>' +
65226555
'<Relationship Id="rId' +
6523-
(intRelNum + 1) +
6556+
intRelNum++ +
65246557
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps" Target="presProps.xml"/>' +
65256558
'<Relationship Id="rId' +
6526-
(intRelNum + 2) +
6559+
intRelNum++ +
65276560
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps" Target="viewProps.xml"/>' +
65286561
'<Relationship Id="rId' +
6529-
(intRelNum + 3) +
6562+
intRelNum++ +
65306563
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>' +
65316564
'<Relationship Id="rId' +
6532-
(intRelNum + 4) +
6533-
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles" Target="tableStyles.xml"/>' +
6534-
'</Relationships>';
6565+
intRelNum++ +
6566+
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles" Target="tableStyles.xml"/>';
6567+
fonts.forEach(function (font) {
6568+
font.styles.forEach(function (style) {
6569+
style.rel.rId = "rId".concat(intRelNum); // populate rel id
6570+
strXml += "<Relationship Id=\"rId".concat(intRelNum++, "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/font\" Target=\"").concat(style.rel.Target, "\"/>");
6571+
});
6572+
});
6573+
strXml += '</Relationships>';
65356574
return strXml;
65366575
}
65376576
// XML-GEN: Functions that run 1-N times (once for each Slide)
@@ -6774,6 +6813,19 @@ function makeXmlPresentation(pres) {
67746813
// STEP 4: Add sizes
67756814
strXml += "<p:sldSz cx=\"".concat(pres.presLayout.width, "\" cy=\"").concat(pres.presLayout.height, "\"/>");
67766815
strXml += "<p:notesSz cx=\"".concat(pres.presLayout.height, "\" cy=\"").concat(pres.presLayout.width, "\"/>");
6816+
// STEP: Add embedded fonts
6817+
if (pres.fonts.length > 0) {
6818+
strXml += '<p:embeddedFontLst>';
6819+
pres.fonts.forEach(function (font) {
6820+
strXml += '<p:embeddedFont>';
6821+
strXml += "<p:font typeface=\"".concat(font.name, "\"/>");
6822+
font.styles.forEach(function (style) {
6823+
strXml += "<p:".concat(style.name, " r:id=\"").concat(style.rel.rId, "\"/>");
6824+
});
6825+
strXml += '</p:embeddedFont>';
6826+
});
6827+
strXml += '</p:embeddedFontLst>';
6828+
}
67776829
// STEP 5: Add text styles
67786830
strXml += '<p:defaultTextStyle>';
67796831
for (var idy = 1; idy < 10; idy++) {
@@ -6936,6 +6988,23 @@ var PptxGenJS = /** @class */ (function () {
69366988
}
69376989
});
69386990
};
6991+
/**
6992+
* Create font rels for this Presentation
6993+
* @param {JSZip} zip - JSZip instance
6994+
* @param {PresFont[]} fonts - presentation fonts
6995+
*/
6996+
this.createFontRels = function (zip, fonts) {
6997+
fonts
6998+
.map(function (font) { return font.styles; })
6999+
.flat()
7000+
.forEach(function (style) {
7001+
var opts = { base64: true };
7002+
var loc = 'ppt/' + style.rel.Target;
7003+
var base64 = typeof style.data === 'string' ? style.data : '';
7004+
var data = base64.split(',').pop();
7005+
zip.file(loc, data, opts);
7006+
});
7007+
};
69397008
/**
69407009
* Create and export the .pptx file
69417010
* @param {string} exportName - output file type
@@ -6981,6 +7050,7 @@ var PptxGenJS = /** @class */ (function () {
69817050
arrMediaPromises = arrMediaPromises.concat(encodeSlideMediaRels(layout));
69827051
});
69837052
arrMediaPromises = arrMediaPromises.concat(encodeSlideMediaRels(_this.masterSlide));
7053+
arrMediaPromises = arrMediaPromises.concat(encodePresFontRels(_this.fonts));
69847054
// STEP 2: Wait for Promises (if any) then generate the PPTX file
69857055
return Promise.all(arrMediaPromises).then(function () {
69867056
// A: Add empty placeholder objects to slides that don't already have them
@@ -7005,7 +7075,7 @@ var PptxGenJS = /** @class */ (function () {
70057075
zip.file('_rels/.rels', makeXmlRootRels());
70067076
zip.file('docProps/app.xml', makeXmlApp(_this.slides, _this.company)); // TODO: pass only `this` like below! 20200206
70077077
zip.file('docProps/core.xml', makeXmlCore(_this.title, _this.subject, _this.author, _this.revision)); // TODO: pass only `this` like below! 20200206
7008-
zip.file('ppt/_rels/presentation.xml.rels', makeXmlPresentationRels(_this.slides));
7078+
zip.file('ppt/_rels/presentation.xml.rels', makeXmlPresentationRels(_this.slides, _this.fonts));
70097079
zip.file('ppt/theme/theme1.xml', makeXmlTheme());
70107080
zip.file('ppt/presentation.xml', makeXmlPresentation(_this));
70117081
zip.file('ppt/presProps.xml', makeXmlPresProps());
@@ -7035,6 +7105,7 @@ var PptxGenJS = /** @class */ (function () {
70357105
_this.createChartMediaRels(slide, zip, arrChartPromises);
70367106
});
70377107
_this.createChartMediaRels(_this.masterSlide, zip, arrChartPromises);
7108+
_this.createFontRels(zip, _this.fonts);
70387109
// E: Wait for Promises (if any) then generate the PPTX file
70397110
// @ts-ignore
70407111
return Promise.all(arrChartPromises).then(function () {
@@ -7075,6 +7146,7 @@ var PptxGenJS = /** @class */ (function () {
70757146
height: this.LAYOUTS[DEF_PRES_LAYOUT].height,
70767147
};
70777148
this._rtlMode = false;
7149+
this._fonts = [];
70787150
//
70797151
this._slideLayouts = [
70807152
{
@@ -7226,6 +7298,13 @@ var PptxGenJS = /** @class */ (function () {
72267298
enumerable: false,
72277299
configurable: true
72287300
});
7301+
Object.defineProperty(PptxGenJS.prototype, "fonts", {
7302+
get: function () {
7303+
return this._fonts;
7304+
},
7305+
enumerable: false,
7306+
configurable: true
7307+
});
72297308
Object.defineProperty(PptxGenJS.prototype, "AlignH", {
72307309
get: function () {
72317310
return this._alignH;
@@ -7439,6 +7518,17 @@ var PptxGenJS = /** @class */ (function () {
74397518
}
74407519
return newSlide;
74417520
};
7521+
PptxGenJS.prototype.addFonts = function (fonts) {
7522+
if (!isValidFonts(fonts)) {
7523+
console.warn('addFonts: received invalid fonts:', fonts);
7524+
console.warn("\n\t\t\t\tUsage example:\n\n\t\t\t\tpptx.addFonts([\n\t\t\t\t\t{\n\t\t\t\t\t\tname: \"Roboto\",\n\t\t\t\t\t\tstyles: [\n\t\t\t\t\t\t\t{ name: \"regular\", path: \"http://path.to.font/roboto-regular.fntdata\" },\n\t\t\t\t\t\t\t{ name: \"italic\", path: \"http://path.to.font/roboto-italic.fntdata\" },\n\t\t\t\t\t\t\t{ name: \"bold\", path: \"http://path.to.font/roboto-bold.fntdata\" },\n\t\t\t\t\t\t\t{ name: \"boldItalic\", path: \"http://path.to.font/roboto-bold-italic.fntdata\" }\n\t\t\t\t\t\t]\n\t\t\t\t }\n\t\t\t\t])\n\t\t\t");
7525+
return;
7526+
}
7527+
this._fonts = fonts.map(function (font) { return (__assign(__assign({}, font), { styles: font.styles.map(function (style) { return (__assign(__assign({}, style), { rel: {
7528+
rId: '',
7529+
Target: "fonts/".concat(font.name, "-").concat(style.name, ".fntdata"),
7530+
} })); }) })); });
7531+
};
74427532
/**
74437533
* Create a custom Slide Layout in any size
74447534
* @param {PresLayout} layout - layout properties

0 commit comments

Comments
 (0)