Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Lists use the following additional options:
* `bulletRadius`
* `textIndent`
* `bulletIndent`
* `listType` - `bullet`, `numbered`, `roman`, `roman-lower`, `lettered` (`bullet` by default)

## Rich Text

Expand Down
17 changes: 17 additions & 0 deletions lib/mixins/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,25 @@ export default {

flatten(list);

const toRoman = function (n, lowerCase = false) {
var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},roman = '',i;
for (i in lookup) {
while (n >= lookup[i]) {
roman += i;
n -= lookup[i];
}
}
return lowerCase ? roman.toLowerCase() : roman;
};

const label = function (n) {
switch (listType) {
case 'numbered':
return `${n}.`;
case 'roman':
return `${toRoman(n)}.`;
case 'roman-lower':
return `${toRoman(n, true)}.`;
case 'lettered':
var letter = String.fromCharCode(((n - 1) % 26) + 65);
var times = Math.floor((n - 1) / 26 + 1);
Expand Down Expand Up @@ -344,6 +359,8 @@ export default {
this.fill();
break;
case 'numbered':
case 'roman':
case 'roman-lower':
case 'lettered':
var text = label(numbers[i - 1]);
this._fragment(text, this.x - indent, this.y, options);
Expand Down
18 changes: 18 additions & 0 deletions tests/visual/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ describe('text', function () {
});
});

test('list (roman numerals)', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc
.fillColor('#000')
.list(['One', 'Two', 'Three'], 100, 150, { listType: 'roman' });
});
});

test('list (roman numerals lowercase)', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc
.fillColor('#000')
.list(['One', 'Two', 'Three'], 100, 150, { listType: 'roman-lower' });
});
});

test('list (lettered)', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
Expand Down