From 7cf9de2c173ec67f2139cae64e2a4c954012fc1b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 31 Jan 2016 15:35:57 +0100 Subject: [PATCH] [api-minor] Change `getOutline` to actually return the RGB color of outline items Currently the `C` entry in an outline item is returned as is, which is neither particularly useful nor what the API documentation claims. This patch also adds unit-tests for both the color handling, and the `F` entry (bold/italic flags). --- src/core/obj.js | 25 +++++++++++++++++-------- src/display/api.js | 2 +- test/unit/api_spec.js | 16 +++++++++++++--- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/core/obj.js b/src/core/obj.js index a52ffe225..5f41e37ec 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -19,18 +19,18 @@ if (typeof define === 'function' && define.amd) { define('pdfjs/core/obj', ['exports', 'pdfjs/shared/util', 'pdfjs/core/primitives', 'pdfjs/core/crypto', 'pdfjs/core/parser', - 'pdfjs/core/chunked_stream'], factory); + 'pdfjs/core/chunked_stream', 'pdfjs/core/colorspace'], factory); } else if (typeof exports !== 'undefined') { factory(exports, require('../shared/util.js'), require('./primitives.js'), require('./crypto.js'), require('./parser.js'), - require('./chunked_stream.js')); + require('./chunked_stream.js'), require('./colorspace.js')); } else { factory((root.pdfjsCoreObj = {}), root.pdfjsSharedUtil, root.pdfjsCorePrimitives, root.pdfjsCoreCrypto, root.pdfjsCoreParser, - root.pdfjsCoreChunkedStream); + root.pdfjsCoreChunkedStream, root.pdfjsCoreColorSpace); } }(this, function (exports, sharedUtil, corePrimitives, coreCrypto, coreParser, - coreChunkedStream) { + coreChunkedStream, coreColorSpace) { var InvalidPDFException = sharedUtil.InvalidPDFException; var MissingDataException = sharedUtil.MissingDataException; @@ -61,6 +61,7 @@ var CipherTransformFactory = coreCrypto.CipherTransformFactory; var Lexer = coreParser.Lexer; var Parser = coreParser.Parser; var ChunkedStream = coreChunkedStream.ChunkedStream; +var ColorSpace = coreColorSpace.ColorSpace; var Catalog = (function CatalogClosure() { function Catalog(pdfManager, xref, pageFactory) { @@ -141,7 +142,7 @@ var Catalog = (function CatalogClosure() { // To avoid recursion, keep track of the already processed items. var processed = new RefSet(); processed.put(obj); - var xref = this.xref; + var xref = this.xref, blackColor = new Uint8Array(3); while (queue.length > 0) { var i = queue.shift(); @@ -169,14 +170,22 @@ var Catalog = (function CatalogClosure() { } } var title = outlineDict.get('Title'); + var flags = outlineDict.get('F') || 0; + + var color = outlineDict.get('C'), rgbColor = blackColor; + // We only need to parse the color when it's valid, and non-default. + if (isArray(color) && color.length === 3 && + (color[0] !== 0 || color[1] !== 0 || color[2] !== 0)) { + rgbColor = ColorSpace.singletons.rgb.getRgb(color, 0); + } var outlineItem = { dest: dest, url: url, title: stringToPDFString(title), - color: outlineDict.get('C') || [0, 0, 0], + color: rgbColor, count: outlineDict.get('Count'), - bold: !!(outlineDict.get('F') & 2), - italic: !!(outlineDict.get('F') & 1), + bold: !!(flags & 2), + italic: !!(flags & 1), items: [] }; i.parent.items.push(outlineItem); diff --git a/src/display/api.js b/src/display/api.js index 246bdf038..a9b243256 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -721,7 +721,7 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() { * title: string, * bold: boolean, * italic: boolean, - * color: rgb array, + * color: rgb Uint8Array, * dest: dest obj, * url: string, * items: array of more items like this diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 789f588fc..94f8e270c 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -455,6 +455,10 @@ describe('api', function() { expect(outlineItem.dest instanceof Array).toEqual(true); expect(outlineItem.url).toEqual(null); + expect(outlineItem.bold).toEqual(true); + expect(outlineItem.italic).toEqual(false); + expect(outlineItem.color).toEqual(new Uint8Array([0, 64, 128])); + expect(outlineItem.items.length).toEqual(1); expect(outlineItem.items[0].title).toEqual('Paragraph 1.1'); }); @@ -468,9 +472,15 @@ describe('api', function() { expect(outline instanceof Array).toEqual(true); expect(outline.length).toEqual(5); - var outlineItem = outline[2]; - expect(outlineItem.dest).toEqual(null); - expect(outlineItem.url).toEqual('http://google.com'); + var outlineItemTwo = outline[2]; + expect(typeof outlineItemTwo.title).toEqual('string'); + expect(outlineItemTwo.dest).toEqual(null); + expect(outlineItemTwo.url).toEqual('http://google.com'); + + var outlineItemOne = outline[1]; + expect(outlineItemOne.bold).toEqual(false); + expect(outlineItemOne.italic).toEqual(true); + expect(outlineItemOne.color).toEqual(new Uint8Array([0, 0, 0])); loadingTask.destroy(); // Cleanup the worker. });