diff --git a/docs/text.md b/docs/text.md index 411505ae..60bdf741 100644 --- a/docs/text.md +++ b/docs/text.md @@ -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 diff --git a/lib/mixins/text.js b/lib/mixins/text.js index 168d390f..8b8f0c6c 100644 --- a/lib/mixins/text.js +++ b/lib/mixins/text.js @@ -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); @@ -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); diff --git a/tests/visual/text.spec.js b/tests/visual/text.spec.js index 8fb3150e..c8bf5226 100644 --- a/tests/visual/text.spec.js +++ b/tests/visual/text.spec.js @@ -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');