diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eae7517d..4c18c9cf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Fix `heightOfString` drawing the measured text into the page when it is called after a `text` call with `continued: true`, because it measured with the line wrapper that call had left behind, which draws its lines. A measurement that passes `continued: true` itself still uses that wrapper and draws as before. Fixes #1284 - Render SVG quadratic curves with equivalent cubic control points, so reversing a path preserves its shape. Fixes #550 - Fix the visual test renderer rejecting its standard-font directory on Windows because its path ends in a backslash. +- Fix PNG images with a palette of fewer than 8 bits per index and transparency in a `tRNS` chunk coming out mostly transparent: their soft mask was built from the packed bytes of the indices, one alpha value per byte instead of per pixel, so most pixels got the alpha of a wrong index or none - Fix the underline, strike-through, link and `goTo` rectangle of text drawn with a `baseline` other than the default sitting an ascent too low, because they were placed from the top of the line while the glyphs had moved up with the baseline. Fixes #994, #1245 - Fix links to a named destination going nowhere in readers that look the name up in sorted order, such as Chrome (PDFium) and qpdf, because name tree keys were sorted by locale with `localeCompare` instead of by their bytes: with destinations named `appendix` and `Chapter1`, `Chapter1` was written second and could not be found. The keys of the `EmbeddedFiles` and `JavaScript` name trees are sorted the same way - Escape document information in XMP metadata and omit characters XML 1.0 cannot represent, without changing the PDF Info dictionary. Fixes #1817 diff --git a/lib/image/png.js b/lib/image/png.js index c780d6870..5c7f27cb4 100644 --- a/lib/image/png.js +++ b/lib/image/png.js @@ -1,6 +1,17 @@ import zlib from '#zlib'; import PNG from 'png-js'; +function paeth(left, up, upLeft) { + const p = left + up - upLeft; + const pLeft = Math.abs(p - left); + const pUp = Math.abs(p - up); + const pUpLeft = Math.abs(p - upLeft); + if (pLeft <= pUp && pLeft <= pUpLeft) { + return left; + } + return pUp <= pUpLeft ? up : upLeft; +} + class PNGImage { constructor(data, label) { this.label = label; @@ -148,6 +159,15 @@ class PNGImage { loadIndexedAlphaChannel() { const transparency = this.image.transparency.indexed; const isInterlaced = this.image.interlaceMethod === 1; + if (this.image.bits < 8 && !isInterlaced) { + const indices = this.decodePackedIndices(); + const alphaChannel = new Uint8Array(indices.length); + for (let i = 0; i < indices.length; i++) { + alphaChannel[i] = transparency[indices[i]]; + } + this.alphaChannel = zlib.deflateSync(alphaChannel); + return; + } const pixels = this.image.decodePixelsSync(); const alphaChannel = new Uint8Array(this.width * this.height); @@ -165,6 +185,47 @@ class PNGImage { this.alphaChannel = zlib.deflateSync(alphaChannel); } + // Palette indices of fewer than 8 bits are packed into bytes, which png-js + // leaves as they are, and it unfilters their rows in steps of less than a + // byte. PNG filters such rows a byte at a time, so unfilter and unpack the + // indices here. + decodePackedIndices() { + const { width, height } = this; + const { bits } = this.image; + const rowLength = Math.ceil((width * bits) / 8); + const data = zlib.inflateSync(new Uint8Array(this.imgData)); + const indices = new Uint8Array(width * height); + let previous = new Uint8Array(rowLength); + let row = new Uint8Array(rowLength); + let pos = 0; + for (let y = 0; y < height; y++) { + const filter = data[pos++]; + for (let x = 0; x < rowLength; x++) { + const left = x > 0 ? row[x - 1] : 0; + const up = previous[x]; + const upLeft = x > 0 ? previous[x - 1] : 0; + let predictor = 0; + if (filter === 1) { + predictor = left; + } else if (filter === 2) { + predictor = up; + } else if (filter === 3) { + predictor = (left + up) >> 1; + } else if (filter === 4) { + predictor = paeth(left, up, upLeft); + } + row[x] = (data[pos++] + predictor) & 0xff; + } + for (let x = 0; x < width; x++) { + const bit = x * bits; + indices[y * width + x] = + (row[bit >> 3] >> (8 - bits - (bit & 7))) & ((1 << bits) - 1); + } + [previous, row] = [row, previous]; + } + return indices; + } + decodeData() { const pixels = this.image.decodePixelsSync(); this.imgData = zlib.deflateSync(pixels); diff --git a/lib/zlib/browser.js b/lib/zlib/browser.js index bb93cbb50..15c9d3c16 100644 --- a/lib/zlib/browser.js +++ b/lib/zlib/browser.js @@ -1,5 +1,6 @@ -import { zlibSync } from 'fflate'; +import { unzlibSync, zlibSync } from 'fflate'; export default { deflateSync: (data) => zlibSync(data), + inflateSync: (data) => unzlibSync(data), }; diff --git a/lib/zlib/node.js b/lib/zlib/node.js index 2b1319281..579e25901 100644 --- a/lib/zlib/node.js +++ b/lib/zlib/node.js @@ -2,4 +2,5 @@ import zlib from 'zlib'; export default { deflateSync: (data) => zlib.deflateSync(data), + inflateSync: (data) => zlib.inflateSync(data), }; diff --git a/tests/unit/png.spec.js b/tests/unit/png.spec.js index 31c2d1848..440cf8e04 100644 --- a/tests/unit/png.spec.js +++ b/tests/unit/png.spec.js @@ -3,6 +3,7 @@ import PDFDocument from '../../lib/document'; import PDFReference from '../../lib/reference'; import PNGImage from '../../lib/image/png'; import fs from 'fs'; +import zlib from 'zlib'; describe('PNGImage', () => { let document; @@ -305,6 +306,21 @@ describe('PNGImage', () => { }); }); + test('Pallete indexed transparency 1bit alpha channel', () => { + const img = new PNGImage( + fs.readFileSync('./tests/images/pallete-transparent-white-1bit.png'), + 'I1', + ); + img.embed(document); + + // One alpha value per pixel; the pixels of the transparent palette entry + // are transparent, and only those + const alpha = zlib.inflateSync(img.alphaChannel); + expect(alpha.length).toBe(290 * 50); + expect(alpha.filter((value) => value === 0).length).toBe(6900); + expect(alpha.filter((value) => value === 255).length).toBe(7600); + }); + test('Grayscale', () => { // ImageWidth = 428 // ImageHeight = 320