Skip to content

Commit

Permalink
Add support for inline images
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondama committed Feb 23, 2018
1 parent 19a63f5 commit 5eba4ce
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/docMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var qrEncoder = require('./qrEnc.js');
* @private
*/
function DocMeasure(fontProvider, styleDictionary, defaultStyle, imageMeasure, tableLayouts, images) {
this.textTools = new TextTools(fontProvider);
this.textTools = new TextTools(fontProvider, this);
this.styleStack = new StyleContextStack(styleDictionary, defaultStyle);
this.imageMeasure = imageMeasure;
this.tableLayouts = tableLayouts;
Expand Down
2 changes: 1 addition & 1 deletion src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ LayoutBuilder.prototype.buildNextLine = function (textNode) {
while (textNode._inlines && textNode._inlines.length > 0 && line.hasEnoughSpaceForInline(textNode._inlines[0])) {
var inline = textNode._inlines.shift();

if (!inline.noWrap && inline.text.length > 1 && inline.width > line.maxWidth) {
if (!inline.noWrap && (!inline.text || inline.text.length > 1) && inline.width > line.maxWidth) {
var widthPerChar = inline.width / inline.text.length;
var maxChars = Math.floor(line.maxWidth / widthPerChar);
if (maxChars < 1) {
Expand Down
8 changes: 4 additions & 4 deletions src/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Line.prototype.getAscenderHeight = function () {
var y = 0;

this.inlines.forEach(function (inline) {
y = Math.max(y, inline.font.ascender / 1000 * inline.fontSize);
y = Math.max(y, inline.font.ascender / 1000 * (inline.image ? inline._height : inline.fontSize));
});
return y;
};
Expand All @@ -32,7 +32,7 @@ Line.prototype.hasEnoughSpaceForInline = function (inline) {
return false;
}

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

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

this.inlines.push(inline);
this.inlineWidths += inline.width;
this.inlineWidths += (inline._width || inline.width);

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

this.inlines.forEach(function (item) {
max = Math.max(max, item.height || 0);
max = Math.max(max, item.height || item._height || 0);
});

return max;
Expand Down
6 changes: 5 additions & 1 deletion src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ function renderLine(line, x, y, pdfKitDoc) {

pdfKitDoc._font = inline.font;
pdfKitDoc.fontSize(inline.fontSize);
pdfKitDoc.text(inline.text, x + inline.x, y + shiftToBaseline, options);
if (inline.image) {
pdfKitDoc.image(inline.image, x + inline.x, y, {width: inline.width});
} else {
pdfKitDoc.text(inline.text, x + inline.x, y + shiftToBaseline, options);
}

if (inline.linkToPage) {
var _ref = pdfKitDoc.ref({Type: 'Action', S: 'GoTo', D: [inline.linkToPage, 0, 0]}).end();
Expand Down
5 changes: 3 additions & 2 deletions src/tableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ TableProcessor.prototype.drawVerticalLine = function (x, y0, y1, vLineIndex, wri
if (width === 0) {
return;
}
var cx = x + width / 2;
writer.addVector({
type: 'line',
x1: x + width / 2,
x2: x + width / 2,
x1: cx,
x2: cx,
y1: y0,
y2: y1,
lineWidth: width,
Expand Down
30 changes: 20 additions & 10 deletions src/textTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ var TRAILING = /(\s)+$/g;
* @constructor
* @param {FontProvider} fontProvider
*/
function TextTools(fontProvider) {
function TextTools(fontProvider, docMeasure) {
this.fontProvider = fontProvider;
this.docMeasure = docMeasure;
}

/**
Expand All @@ -28,20 +29,20 @@ function TextTools(fontProvider) {
* @return {Object} collection of inlines, minWidth, maxWidth
*/
TextTools.prototype.buildInlines = function (textArray, styleContextStack) {
var measured = measure(this.fontProvider, textArray, styleContextStack);
var measured = measure(this.fontProvider, textArray, styleContextStack, this.docMeasure);

var minWidth = 0,
maxWidth = 0,
currentLineWidth;

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

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

currentLineWidth.width += inline.width;
currentLineWidth.width += inline.width || inline._width;
currentLineWidth.trailingCut = inline.trailingCut;

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

function getTrimmedWidth(item) {
return Math.max(0, item.width - item.leadingCut - item.trailingCut);
return Math.max(0, (item.width || item._width) - item.leadingCut - item.trailingCut);
}
};

Expand Down Expand Up @@ -164,6 +165,11 @@ function normalizeTextArray(array, styleContextStack) {
var style = null;
var words;

if (item.image) {
results.push(item)
continue
}

var noWrap = getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
if (isObject(item)) {
words = splitWords(normalizeString(item.text), noWrap);
Expand Down Expand Up @@ -225,7 +231,7 @@ function getStyleProperty(item, styleContextStack, property, defaultValue) {
}
}

function measure(fontProvider, textArray, styleContextStack) {
function measure(fontProvider, textArray, styleContextStack, docMeasure) {
var normalized = normalizeTextArray(textArray, styleContextStack);

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

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

item.width = widthOfString(item.text, font, fontSize, characterSpacing, fontFeatures);
item.height = font.lineHeight(fontSize) * lineHeight;
if (item.image) {
docMeasure.measureImage(item)
} else {
item.width = widthOfString(item.text, font, fontSize, characterSpacing, fontFeatures);
item.height = font.lineHeight(fontSize) * lineHeight;
}

var leadingSpaces = item.text.match(LEADING);
var leadingSpaces = item.text ? item.text.match(LEADING) : [' '];

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

var trailingSpaces = item.text.match(TRAILING);
var trailingSpaces = item.text ? item.text.match(TRAILING) : [' '];
if (trailingSpaces) {
item.trailingCut = widthOfString(trailingSpaces[0], font, fontSize, characterSpacing, fontFeatures);
} else {
Expand Down

0 comments on commit 5eba4ce

Please sign in to comment.