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 Mar 1, 2018
1 parent 2edc027 commit b1df758
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 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
2 changes: 1 addition & 1 deletion 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 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
24 changes: 17 additions & 7 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,7 +29,7 @@ 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,
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 b1df758

Please sign in to comment.