Skip to content

Commit f7ca60f

Browse files
committed
Add support for inline images
1 parent dde18e3 commit f7ca60f

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

src/docMeasure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var qrEncoder = require('./qrEnc.js');
1717
* @private
1818
*/
1919
function DocMeasure(fontProvider, styleDictionary, defaultStyle, imageMeasure, tableLayouts, images) {
20-
this.textTools = new TextTools(fontProvider);
20+
this.textTools = new TextTools(fontProvider, this);
2121
this.styleStack = new StyleContextStack(styleDictionary, defaultStyle);
2222
this.imageMeasure = imageMeasure;
2323
this.tableLayouts = tableLayouts;

src/layoutBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ LayoutBuilder.prototype.buildNextLine = function (textNode) {
657657
while (textNode._inlines && textNode._inlines.length > 0 && line.hasEnoughSpaceForInline(textNode._inlines[0])) {
658658
var inline = textNode._inlines.shift();
659659

660-
if (!inline.noWrap && inline.text.length > 1 && inline.width > line.maxWidth) {
660+
if (!inline.noWrap && (!inline.text || inline.text.length > 1) && inline.width > line.maxWidth) {
661661
var widthPerChar = inline.width / inline.text.length;
662662
var maxChars = Math.floor(line.maxWidth / widthPerChar);
663663
if (maxChars < 1) {

src/line.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Line.prototype.getAscenderHeight = function () {
1919
var y = 0;
2020

2121
this.inlines.forEach(function (inline) {
22-
y = Math.max(y, inline.font.ascender / 1000 * inline.fontSize);
22+
y = Math.max(y, inline.font.ascender / 1000 * (inline.image ? inline._height : inline.fontSize));
2323
});
2424
return y;
2525
};
@@ -32,7 +32,7 @@ Line.prototype.hasEnoughSpaceForInline = function (inline) {
3232
return false;
3333
}
3434

35-
return this.inlineWidths + inline.width - this.leadingCut - (inline.trailingCut || 0) <= this.maxWidth;
35+
return this.inlineWidths + (inline._width || inline.width) - this.leadingCut - (inline.trailingCut || 0) <= this.maxWidth;
3636
};
3737

3838
Line.prototype.addInline = function (inline) {
@@ -44,7 +44,7 @@ Line.prototype.addInline = function (inline) {
4444
inline.x = this.inlineWidths - this.leadingCut;
4545

4646
this.inlines.push(inline);
47-
this.inlineWidths += inline.width;
47+
this.inlineWidths += (inline._width || inline.width);
4848

4949
if (inline.lineEnd) {
5050
this.newLineForced = true;
@@ -63,7 +63,7 @@ Line.prototype.getHeight = function () {
6363
var max = 0;
6464

6565
this.inlines.forEach(function (item) {
66-
max = Math.max(max, item.height || 0);
66+
max = Math.max(max, item.height || item._height || 0);
6767
});
6868

6969
return max;

src/printer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ function renderLine(line, x, y, pdfKitDoc) {
402402

403403
pdfKitDoc._font = inline.font;
404404
pdfKitDoc.fontSize(inline.fontSize);
405-
pdfKitDoc.text(inline.text, x + inline.x, y + shiftToBaseline, options);
405+
if (inline.image) {
406+
pdfKitDoc.image(inline.image, x + inline.x, y, {width: inline.width});
407+
} else {
408+
pdfKitDoc.text(inline.text, x + inline.x, y + shiftToBaseline, options);
409+
}
406410

407411
if (inline.linkToPage) {
408412
var _ref = pdfKitDoc.ref({Type: 'Action', S: 'GoTo', D: [inline.linkToPage, 0, 0]}).end();

src/tableProcessor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ TableProcessor.prototype.drawVerticalLine = function (x, y0, y1, vLineIndex, wri
211211
if (width === 0) {
212212
return;
213213
}
214+
var cx = x + width / 2;
214215
writer.addVector({
215216
type: 'line',
216-
x1: x + width / 2,
217-
x2: x + width / 2,
217+
x1: cx,
218+
x2: cx,
218219
y1: y0,
219220
y2: y1,
220221
lineWidth: width,

src/textTools.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ var TRAILING = /(\s)+$/g;
1515
* @constructor
1616
* @param {FontProvider} fontProvider
1717
*/
18-
function TextTools(fontProvider) {
18+
function TextTools(fontProvider, docMeasure) {
1919
this.fontProvider = fontProvider;
20+
this.docMeasure = docMeasure;
2021
}
2122

2223
/**
@@ -28,20 +29,20 @@ function TextTools(fontProvider) {
2829
* @return {Object} collection of inlines, minWidth, maxWidth
2930
*/
3031
TextTools.prototype.buildInlines = function (textArray, styleContextStack) {
31-
var measured = measure(this.fontProvider, textArray, styleContextStack);
32+
var measured = measure(this.fontProvider, textArray, styleContextStack, this.docMeasure);
3233

3334
var minWidth = 0,
3435
maxWidth = 0,
3536
currentLineWidth;
3637

3738
measured.forEach(function (inline) {
38-
minWidth = Math.max(minWidth, inline.width - inline.leadingCut - inline.trailingCut);
39+
minWidth = Math.max(minWidth, (inline.width || inline._width) - inline.leadingCut - inline.trailingCut);
3940

4041
if (!currentLineWidth) {
4142
currentLineWidth = {width: 0, leadingCut: inline.leadingCut, trailingCut: 0};
4243
}
4344

44-
currentLineWidth.width += inline.width;
45+
currentLineWidth.width += inline.width || inline._width;
4546
currentLineWidth.trailingCut = inline.trailingCut;
4647

4748
maxWidth = Math.max(maxWidth, getTrimmedWidth(currentLineWidth));
@@ -62,7 +63,7 @@ TextTools.prototype.buildInlines = function (textArray, styleContextStack) {
6263
};
6364

6465
function getTrimmedWidth(item) {
65-
return Math.max(0, item.width - item.leadingCut - item.trailingCut);
66+
return Math.max(0, (item.width || item._width) - item.leadingCut - item.trailingCut);
6667
}
6768
};
6869

@@ -164,6 +165,11 @@ function normalizeTextArray(array, styleContextStack) {
164165
var style = null;
165166
var words;
166167

168+
if (item.image) {
169+
results.push(item)
170+
continue
171+
}
172+
167173
var noWrap = getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
168174
if (isObject(item)) {
169175
words = splitWords(normalizeString(item.text), noWrap);
@@ -225,7 +231,7 @@ function getStyleProperty(item, styleContextStack, property, defaultValue) {
225231
}
226232
}
227233

228-
function measure(fontProvider, textArray, styleContextStack) {
234+
function measure(fontProvider, textArray, styleContextStack, docMeasure) {
229235
var normalized = normalizeTextArray(textArray, styleContextStack);
230236

231237
if (normalized.length) {
@@ -257,10 +263,14 @@ function measure(fontProvider, textArray, styleContextStack) {
257263

258264
var font = fontProvider.provideFont(fontName, bold, italics);
259265

260-
item.width = widthOfString(item.text, font, fontSize, characterSpacing, fontFeatures);
261-
item.height = font.lineHeight(fontSize) * lineHeight;
266+
if (item.image) {
267+
docMeasure.measureImage(item)
268+
} else {
269+
item.width = widthOfString(item.text, font, fontSize, characterSpacing, fontFeatures);
270+
item.height = font.lineHeight(fontSize) * lineHeight;
271+
}
262272

263-
var leadingSpaces = item.text.match(LEADING);
273+
var leadingSpaces = item.text ? item.text.match(LEADING) : [' '];
264274

265275
if (!item.leadingCut) {
266276
item.leadingCut = 0;
@@ -270,7 +280,7 @@ function measure(fontProvider, textArray, styleContextStack) {
270280
item.leadingCut += widthOfString(leadingSpaces[0], font, fontSize, characterSpacing, fontFeatures);
271281
}
272282

273-
var trailingSpaces = item.text.match(TRAILING);
283+
var trailingSpaces = item.text ? item.text.match(TRAILING) : [' '];
274284
if (trailingSpaces) {
275285
item.trailingCut = widthOfString(trailingSpaces[0], font, fontSize, characterSpacing, fontFeatures);
276286
} else {

0 commit comments

Comments
 (0)