|
| 1 | +import PDFStructureElement from '../structure_element'; |
| 2 | +import PDFDocument from '../document'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Add accessibility to a table |
| 6 | + * |
| 7 | + * @this PDFTable |
| 8 | + * @memberOf PDFTable |
| 9 | + * @private |
| 10 | + */ |
| 11 | +export function accommodateTable() { |
| 12 | + const structParent = this.opts.structParent; |
| 13 | + if (structParent) { |
| 14 | + this._tableStruct = this.document.struct('Table'); |
| 15 | + this._tableStruct.dictionary.data.ID = this._id; |
| 16 | + if (structParent instanceof PDFStructureElement) { |
| 17 | + structParent.add(this._tableStruct); |
| 18 | + } else if (structParent instanceof PDFDocument) { |
| 19 | + structParent.addStructure(this._tableStruct); |
| 20 | + } |
| 21 | + this._headerRowLookup = {}; |
| 22 | + this._headerColumnLookup = {}; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Cleanup accessibility on a table |
| 28 | + * |
| 29 | + * @this PDFTable |
| 30 | + * @memberOf PDFTable |
| 31 | + * @private |
| 32 | + */ |
| 33 | +export function accommodateCleanup() { |
| 34 | + if (this._tableStruct) this._tableStruct.end(); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Render a row with all its accessibility features |
| 39 | + * |
| 40 | + * @this PDFTable |
| 41 | + * @memberOf PDFTable |
| 42 | + * @param {SizedNormalizedTableCellStyle[]} row |
| 43 | + * @param {number} rowIndex |
| 44 | + * @param {Function} renderCell |
| 45 | + * @private |
| 46 | + */ |
| 47 | +export function accessibleRow(row, rowIndex, renderCell) { |
| 48 | + const rowStruct = this.document.struct('TR'); |
| 49 | + rowStruct.dictionary.data.ID = new String(`${this._id}-${rowIndex}`); |
| 50 | + this._tableStruct.add(rowStruct); |
| 51 | + row.forEach((cell) => renderCell(cell, rowStruct)); |
| 52 | + rowStruct.end(); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Render a cell with all its accessibility features |
| 57 | + * |
| 58 | + * @this PDFTable |
| 59 | + * @memberOf PDFTable |
| 60 | + * @param {SizedNormalizedTableCellStyle} cell |
| 61 | + * @param {PDFStructureElement} rowStruct |
| 62 | + * @param {Function} callback |
| 63 | + * @private |
| 64 | + */ |
| 65 | +export function accessibleCell(cell, rowStruct, callback) { |
| 66 | + const doc = this.document; |
| 67 | + |
| 68 | + const cellStruct = doc.struct(cell.type, { title: cell.title }); |
| 69 | + cellStruct.dictionary.data.ID = cell.id; |
| 70 | + |
| 71 | + rowStruct.add(cellStruct); |
| 72 | + |
| 73 | + const padding = cell.padding; |
| 74 | + const border = cell.border; |
| 75 | + const attributes = { |
| 76 | + O: 'Table', |
| 77 | + Width: cell.width, |
| 78 | + Height: cell.height, |
| 79 | + Padding: [padding.top, padding.bottom, padding.left, padding.right], |
| 80 | + RowSpan: cell.rowSpan > 1 ? cell.rowSpan : undefined, |
| 81 | + ColSpan: cell.colSpan > 1 ? cell.colSpan : undefined, |
| 82 | + BorderThickness: [border.top, border.bottom, border.left, border.right], |
| 83 | + }; |
| 84 | + |
| 85 | + // Claim row Headers |
| 86 | + if (cell.type === 'TH') { |
| 87 | + if (cell.scope === 'Row' || cell.scope === 'Both') { |
| 88 | + for (let i = 0; i < cell.rowSpan; i++) { |
| 89 | + if (!this._headerRowLookup[cell.rowIndex + i]) { |
| 90 | + this._headerRowLookup[cell.rowIndex + i] = []; |
| 91 | + } |
| 92 | + this._headerRowLookup[cell.rowIndex + i].push(cell.id); |
| 93 | + } |
| 94 | + attributes.Scope = cell.scope; |
| 95 | + } |
| 96 | + if (cell.scope === 'Column' || cell.scope === 'Both') { |
| 97 | + for (let i = 0; i < cell.colSpan; i++) { |
| 98 | + if (!this._headerColumnLookup[cell.colIndex + i]) { |
| 99 | + this._headerColumnLookup[cell.colIndex + i] = []; |
| 100 | + } |
| 101 | + this._headerColumnLookup[cell.colIndex + i].push(cell.id); |
| 102 | + } |
| 103 | + attributes.Scope = cell.scope; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Find any cells which are marked as headers for this cell |
| 108 | + const Headers = new Set( |
| 109 | + [ |
| 110 | + ...Array.from( |
| 111 | + { length: cell.colSpan }, |
| 112 | + (_, i) => this._headerColumnLookup[cell.colIndex + i], |
| 113 | + ).flat(), |
| 114 | + ...Array.from( |
| 115 | + { length: cell.rowSpan }, |
| 116 | + (_, i) => this._headerRowLookup[cell.rowIndex + i], |
| 117 | + ).flat(), |
| 118 | + ].filter(Boolean), |
| 119 | + ); |
| 120 | + if (Headers.size) attributes.Headers = Array.from(Headers); |
| 121 | + |
| 122 | + const normalizeColor = doc._normalizeColor; |
| 123 | + if (cell.backgroundColor != null) { |
| 124 | + attributes.BackgroundColor = normalizeColor(cell.backgroundColor); |
| 125 | + } |
| 126 | + const hasBorder = [border.top, border.bottom, border.left, border.right]; |
| 127 | + if (hasBorder.some((x) => x)) { |
| 128 | + const borderColor = cell.borderColor; |
| 129 | + attributes.BorderColor = [ |
| 130 | + hasBorder[0] ? normalizeColor(borderColor.top) : null, |
| 131 | + hasBorder[1] ? normalizeColor(borderColor.bottom) : null, |
| 132 | + hasBorder[2] ? normalizeColor(borderColor.left) : null, |
| 133 | + hasBorder[3] ? normalizeColor(borderColor.right) : null, |
| 134 | + ]; |
| 135 | + } |
| 136 | + |
| 137 | + // Remove any undefined attributes |
| 138 | + Object.keys(attributes).forEach( |
| 139 | + (key) => attributes[key] === undefined && delete attributes[key], |
| 140 | + ); |
| 141 | + cellStruct.dictionary.data.A = doc.ref(attributes); |
| 142 | + cellStruct.add(callback); |
| 143 | + cellStruct.end(); |
| 144 | + cellStruct.dictionary.data.A.end(); |
| 145 | +} |
0 commit comments