Skip to content

Commit 70d0033

Browse files
authored
Merge pull request #1433 from MrRio/feature/custom-fonts
[WIP] Feature/custom fonts
2 parents 088da5e + 45dd407 commit 70d0033

31 files changed

+3690
-648
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- "8.2.1"
3+
- "8.9.4"
44
install:
55
- npm -g install yarn
66
- yarn

build.js

+26-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,32 @@ bundle({
1212
})
1313

1414
// Monkey patching adler32 and filesaver
15-
function monkeyPatch () {
15+
function monkeyPatch() {
1616
return {
1717
transform: (code, id) => {
1818
var file = id.split('/').pop()
19+
1920
if (file === 'adler32cs.js') {
21+
// Make library assign itself to the jsPDF instance instead
22+
// of window to avoid polluting global scope
2023
code = code.replace(/this, function/g, 'jsPDF, function')
24+
25+
// Remove call to require('buffer') since it is not used and
26+
// might import a large arbitrary buffer library if a user is
27+
// using a commonjs module bundler
2128
code = code.replace(/require\('buffer'\)/g, '{}')
2229
}
30+
31+
// Only one define call per module is allowed by requirejs so
32+
// we have to remove calls that other libraries make
33+
if (file === 'adler32cs.js') {
34+
code = code.replace(/typeof define/g, '0')
35+
} else if (file === 'FileSaver.js') {
36+
code = code.replace(/define !== null\) && \(define.amd != null/g, '0')
37+
} else if (file === 'html2canvas.js') {
38+
code = code.replace(/&&\s+define.amd/g, '&& define.amd && false')
39+
}
40+
2341
return code
2442
}
2543
}
@@ -29,7 +47,7 @@ function monkeyPatch () {
2947
// This plugin makes sure specified local variables are preserved
3048
// and kept local. This plugin wouldn't be necessary if es2015
3149
// modules would be used.
32-
function rawjs (opts) {
50+
function rawjs(opts) {
3351
opts = opts || {}
3452
return {
3553
transform: (code, id) => {
@@ -51,9 +69,10 @@ function rawjs (opts) {
5169
}
5270
}
5371

54-
function bundle (paths) {
72+
function bundle(paths) {
5573
rollup.rollup({
56-
entry: './main.js',
74+
input: './main.js',
75+
context: 'window',
5776
plugins: [
5877
monkeyPatch(),
5978
rawjs({
@@ -72,7 +91,7 @@ function bundle (paths) {
7291
}).then((bundle) => {
7392
return bundle.generate({
7493
format: 'umd',
75-
moduleName: 'jspdf'
94+
name: 'jsPDF'
7695
})
7796
}).then(output => {
7897
let code = output.code
@@ -97,7 +116,7 @@ function bundle (paths) {
97116
})
98117
}
99118

100-
function renew (code) {
119+
function renew(code) {
101120
var date = new Date().toISOString()
102121
var version = require('./package.json').version
103122
var whoami = execSync('whoami').toString().trim()
@@ -108,4 +127,4 @@ function renew (code) {
108127
code = code.replace(/1\.0\.0-trunk/, version + ' ' + date + ':' + whoami)
109128

110129
return code
111-
}
130+
}

examples/basic.html

+25-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<script>
1616
$(function() {
17-
$("#accordion-basic, #accordion-text, #accordion-graphic").accordion({
17+
$("#accordion-basic, #accordion-text, #accordion-graphic, #accordion-font").accordion({
1818
autoHeight: false,
1919
navigation: true
2020
});
@@ -39,6 +39,7 @@ <h1>jsPDF Demos</h1>
3939
<li><a href="#tabs-basic">Basic elements</a></li>
4040
<li><a href="#tabs-text">Text elements</a></li>
4141
<li><a href="#tabs-graphic">Graphic elements</a></li>
42+
<li><a href="#tabs-font">Using Fonts</a></li>
4243
</ul>
4344

4445
<div id="tabs-basic">
@@ -514,6 +515,29 @@ <h2><a href="#">Draw example: Images</a></h2>
514515
<!-- I'm lazy, so using eval() directly, sorry ;) [diegocr] -->
515516
<a href="javascript:void(0);" onclick="eval($(this).prev().text())" class="button">Run Code</a>
516517
</p></div>
518+
</div>
519+
</div>
520+
521+
<div id="tabs-font">
522+
<div id="accordion-font">
523+
<h2><a href="#">Using a base64-encoded TTF</a></h2>
524+
<div><p>
525+
526+
<pre>
527+
//https://fonts.google.com/specimen/PT+Sans
528+
var PTSans = "AAEAAAAUAQA..."; //shortened. see demoUsingTTFFont in examples/js/basic.js for full base64 encoded ttffile
529+
var doc = new jsPDF();
530+
531+
doc.addFileToVFS("PTSans.ttf", PTSans);
532+
doc.addFont('PTSans.ttf', 'PTSans', 'normal');
533+
534+
doc.setFont('PTSans'); // set font
535+
doc.setFontSize(10);
536+
doc.text("А ну чики брики и в дамки!", 10, 10);
537+
538+
doc.save('test.pdf');</pre>
539+
<a href="javascript:demoUsingTTFFont()" class="button">Run Code</a></p></div>
540+
517541
</div>
518542
</div>
519543
</div>

examples/js/basic.js

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)