-
Notifications
You must be signed in to change notification settings - Fork 81
feat: support TIFF images in DOCX rendering #2193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gpardhivvarma
wants to merge
8
commits into
superdoc-dev:main
Choose a base branch
from
gpardhivvarma:feat/tiff-image-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b633581
feat: support TIFF images in DOCX rendering
gpardhivvarma 5f0832d
fix: enforce pixel limit before TIFF decode to prevent DoS
gpardhivvarma 1c061d6
fix: validate TIFF dimensions before decoding and correct .tif MIME type
gpardhivvarma 498aa80
fix: read TIFF dimensions from raw IFD tags for pre-decode validation
gpardhivvarma 535b006
fix: address PR review feedback for TIFF support
gpardhivvarma 34c8784
Merge remote-tracking branch 'upstream/main' into feat/tiff-image-sup…
gpardhivvarma f538843
fix: clean up convertTiffToPng signature and add wiring test
gpardhivvarma eeafd64
Merge remote-tracking branch 'upstream/main' into feat/tiff-image-sup…
gpardhivvarma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/super-editor/src/core/super-converter/v3/handlers/wp/helpers/tiff-converter.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * TIFF to PNG Converter | ||
| * | ||
| * Converts TIFF images to PNG format using utif2 for decoding and Canvas for | ||
| * encoding. Browsers cannot natively render TIFF images, so this converts them | ||
| * at import time to a browser-friendly format. | ||
| * | ||
| * @module tiff-converter | ||
| */ | ||
|
|
||
| import * as UTIF from 'utif2'; | ||
| import { base64ToUint8Array } from '../../../../helpers.js'; | ||
|
|
||
| // Optional DOM environment provided by callers (e.g., JSDOM in Node) | ||
| let domEnvironment = null; | ||
|
|
||
| // Safety limit: reject TIFF images whose decoded RGBA buffer would exceed this | ||
| // pixel count. 100 million pixels ≈ 400 MB of RGBA data — well above any | ||
| // realistic document image while still preventing DoS from malicious dimensions. | ||
| const MAX_PIXEL_COUNT = 100_000_000; | ||
|
|
||
| /** | ||
| * Configure a DOM environment that can be used when running in Node. | ||
| * | ||
| * @param {{ mockWindow?: Window|null, window?: Window|null, mockDocument?: Document|null, document?: Document|null }|null} env | ||
| */ | ||
| export const setTiffDomEnvironment = (env) => { | ||
| domEnvironment = env || null; | ||
| }; | ||
|
|
||
| /** | ||
| * Checks if a file extension is a TIFF format. | ||
| * | ||
| * @param {string} extension - File extension to check | ||
| * @returns {boolean} True if the extension is 'tiff' or 'tif' | ||
| */ | ||
| export function isTiffExtension(extension) { | ||
| const ext = extension?.toLowerCase(); | ||
| return ext === 'tiff' || ext === 'tif'; | ||
| } | ||
|
|
||
| /** | ||
| * Get a canvas element, trying the global document first, then the domEnvironment. | ||
| * | ||
| * @returns {HTMLCanvasElement|null} | ||
| */ | ||
| function createCanvas() { | ||
| if (typeof document !== 'undefined') { | ||
| return document.createElement('canvas'); | ||
| } | ||
|
|
||
| const env = domEnvironment || {}; | ||
| const doc = env.document || env.mockDocument || env.window?.document || env.mockWindow?.document || null; | ||
| if (doc) { | ||
| return doc.createElement('canvas'); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a TIFF image to a PNG data URI. | ||
| * | ||
| * @param {string} data - Base64 encoded data or data URI of the TIFF file | ||
| * @returns {{ dataUri: string, format: string }|null} Data URI plus format, or null if conversion fails | ||
| */ | ||
| export function convertTiffToPng(data) { | ||
| try { | ||
| if (typeof data !== 'string') return null; | ||
|
|
||
| // Parse input — accept data URI or raw base64 | ||
gpardhivvarma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let base64 = data; | ||
| if (data.startsWith('data:')) { | ||
| const commaIndex = data.indexOf(','); | ||
| if (commaIndex === -1) return null; | ||
| base64 = data.substring(commaIndex + 1); | ||
| } | ||
| const bytes = base64ToUint8Array(base64); | ||
|
|
||
| const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); | ||
|
|
||
| // Decode TIFF — get Image File Directories (pages) | ||
| const ifds = UTIF.decode(buffer); | ||
| if (!ifds || ifds.length === 0) return null; | ||
|
|
||
| // Validate dimensions from raw IFD tags before decoding pixel data. | ||
| // UTIF.decode populates tag entries (t256=ImageWidth, t257=ImageLength) | ||
| // but .width/.height are only set after decodeImage. | ||
| const ifdWidth = ifds[0].t256?.[0]; | ||
| const ifdHeight = ifds[0].t257?.[0]; | ||
| if (!ifdWidth || !ifdHeight || ifdWidth * ifdHeight > MAX_PIXEL_COUNT) return null; | ||
|
|
||
| // Decode pixel data for the first page | ||
| UTIF.decodeImage(buffer, ifds[0]); | ||
| const rgba = UTIF.toRGBA8(ifds[0]); | ||
gpardhivvarma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!rgba || rgba.length === 0) return null; | ||
|
|
||
| const { width, height } = ifds[0]; | ||
|
|
||
| // Render to canvas and export as PNG | ||
| const canvas = createCanvas(); | ||
| if (!canvas) { | ||
| console.warn('TIFF conversion requires a DOM environment with canvas support'); | ||
| return null; | ||
| } | ||
|
|
||
| canvas.width = width; | ||
| canvas.height = height; | ||
|
|
||
| const ctx = canvas.getContext('2d'); | ||
| if (!ctx) return null; | ||
|
|
||
| const imageData = ctx.createImageData(width, height); | ||
gpardhivvarma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| imageData.data.set(new Uint8Array(rgba.buffer, rgba.byteOffset, rgba.byteLength)); | ||
| ctx.putImageData(imageData, 0, 0); | ||
|
|
||
| const dataUri = canvas.toDataURL('image/png'); | ||
| if (!dataUri || dataUri === 'data:,') return null; | ||
|
|
||
| return { dataUri, format: 'png' }; | ||
| } catch (error) { | ||
| console.warn('Failed to convert TIFF to PNG:', error.message); | ||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.