Skip to content

Commit

Permalink
Merge pull request #1054 from Prevole/leading-indent
Browse files Browse the repository at this point in the history
Added leading indent
  • Loading branch information
liborm85 committed Jul 1, 2017
2 parents 6f287d5 + 21936ce commit 6865895
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/styleContextStack.js
Expand Up @@ -99,7 +99,8 @@ StyleContextStack.prototype.autopush = function (item) {
'lineHeight',
'characterSpacing',
'noWrap',
'markerColor'
'markerColor',
'leadingIndent'
//'tableCellPadding'
// 'cellBorder',
// 'headerCellBorder',
Expand Down
20 changes: 16 additions & 4 deletions src/textTools.js
Expand Up @@ -214,6 +214,15 @@ function getStyleProperty(item, styleContextStack, property, defaultValue) {
function measure(fontProvider, textArray, styleContextStack) {
var normalized = normalizeTextArray(textArray, styleContextStack);

if (normalized.length) {
var leadingIndent = getStyleProperty(normalized[0], styleContextStack, 'leadingIndent', 0);

if (leadingIndent) {
normalized[0].leadingCut = -leadingIndent;
normalized[0].leadingIndent = leadingIndent;
}
}

normalized.forEach(function (item) {
var fontName = getStyleProperty(item, styleContextStack, 'font', 'Roboto');
var fontSize = getStyleProperty(item, styleContextStack, 'fontSize', 12);
Expand All @@ -236,13 +245,16 @@ function measure(fontProvider, textArray, styleContextStack) {
item.height = font.lineHeight(fontSize) * lineHeight;

var leadingSpaces = item.text.match(LEADING);
var trailingSpaces = item.text.match(TRAILING);
if (leadingSpaces) {
item.leadingCut = widthOfString(leadingSpaces[0], font, fontSize, characterSpacing);
} else {

if (!item.leadingCut) {
item.leadingCut = 0;
}

if (leadingSpaces) {
item.leadingCut += widthOfString(leadingSpaces[0], font, fontSize, characterSpacing);
}

var trailingSpaces = item.text.match(TRAILING);
if (trailingSpaces) {
item.trailingCut = widthOfString(trailingSpaces[0], font, fontSize, characterSpacing);
} else {
Expand Down
18 changes: 18 additions & 0 deletions tests/textTools.js
Expand Up @@ -90,6 +90,17 @@ describe('TextTools', function () {
' text having two lines '
];

var textWithLeadingIndent = {
text:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut porttitor risus sapien, at commodo eros suscipit ' +
'nec. Pellentesque pretium, justo eleifend pulvinar malesuada, lorem arcu pellentesque ex, ac congue arcu erat ' +
'id nunc. Morbi facilisis pulvinar nunc, quis laoreet ligula rutrum ut. Mauris at ante imperdiet, vestibulum ' +
'libero nec, iaculis justo. Mauris aliquam congue ligula vel convallis. Duis iaculis ornare nulla, id finibus ' +
'sapien commodo quis. Sed semper sagittis urna. Nunc aliquam aliquet placerat. Maecenas ac arcu auctor, ' +
'bibendum nisl non, bibendum odio. Proin semper lacus faucibus, pretium neque nec, viverra sem. ',
leadingIndent: 10
};

var mixedTextArrayWithVariousTypes = [
{text: ''},
{text: null},
Expand Down Expand Up @@ -377,6 +388,13 @@ describe('TextTools', function () {
assert.equal(inlines.maxWidth, 52 * 12);
});

it('should set set the leading indent only to the first line of a paragraph', function() {
var inlines = textTools.buildInlines(textWithLeadingIndent);
assert.equal(inlines.items[0].leadingCut, -10);
var countLeadingCut = 0;
inlines.items.forEach(function(item) { countLeadingCut += item.leadingCut; });
assert.equal(countLeadingCut, -10);
});
});

describe('sizeOfString', function () {
Expand Down

0 comments on commit 6865895

Please sign in to comment.