|
| 1 | +import { getAllComps, getTextLayersByComp } from '../utils'; |
| 2 | +import { ProjectIssueType, type TextLayerIssues } from './types'; |
| 3 | + |
| 4 | +function checkTextLayers(): TextLayerIssues[] { |
| 5 | + const comps = getAllComps(app.project); |
| 6 | + const textLayers: TextLayerIssues[] = []; |
| 7 | + |
| 8 | + for (let i = 0; i < comps.length; i++) { |
| 9 | + const comp = comps[i]; |
| 10 | + const layers = getTextLayersByComp(comp); |
| 11 | + if (layers.length === 0) { |
| 12 | + continue; |
| 13 | + } |
| 14 | + |
| 15 | + for (let j = 0; j < layers.length; j++) { |
| 16 | + const layer = layers[j]; |
| 17 | + if (layer.guideLayer) { |
| 18 | + continue; |
| 19 | + } |
| 20 | + |
| 21 | + const textDocument = layer.sourceText.value; |
| 22 | + |
| 23 | + // first check allCaps for older After Effects versions |
| 24 | + // this checks only the first character of the text layer |
| 25 | + // IMPORTANT: with this method, on older versions (< 24.3), we can't fix, but we can at least report it |
| 26 | + if (textDocument.allCaps) { |
| 27 | + textLayers.push({ |
| 28 | + type: ProjectIssueType.AllCaps, |
| 29 | + layerId: layer.id.toString(), |
| 30 | + layerName: layer.name, |
| 31 | + text: true, |
| 32 | + }); |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + // if we didn't find allCaps above, check for fontCapsOption (After Effects 24.3+) |
| 37 | + // check if characterRange exists in textDocument (After Effects 24.3+) first |
| 38 | + if (typeof textDocument.characterRange !== 'function') { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + let hasCharacterAllCaps = false; |
| 43 | + const range = textDocument.characterRange(0, textDocument.text.length); |
| 44 | + |
| 45 | + // `range.fontCapsOption === undefined` this means that there are different attributes in the text per character, |
| 46 | + // thus, we can check per-character basis to find if any character is allCaps |
| 47 | + if (range.fontCapsOption === undefined) { |
| 48 | + for (let k = 0; k < textDocument.text.length; k++) { |
| 49 | + const cRange = textDocument.characterRange(k, k + 1); |
| 50 | + if ( |
| 51 | + cRange.isRangeValid && |
| 52 | + cRange.fontCapsOption === FontCapsOption.FONT_ALL_CAPS |
| 53 | + ) { |
| 54 | + hasCharacterAllCaps = true; |
| 55 | + textLayers.push({ |
| 56 | + type: ProjectIssueType.AllCaps, |
| 57 | + layerId: layer.id.toString(), |
| 58 | + layerName: layer.name, |
| 59 | + text: true, |
| 60 | + }); |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // if we already found a character with allCaps, skip further checks |
| 67 | + if (hasCharacterAllCaps) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // if the whole text has the same attribute, we can check it directly |
| 72 | + if ( |
| 73 | + range.isRangeValid && |
| 74 | + range.fontCapsOption === FontCapsOption.FONT_ALL_CAPS |
| 75 | + ) { |
| 76 | + textLayers.push({ |
| 77 | + type: ProjectIssueType.AllCaps, |
| 78 | + layerId: layer.id.toString(), |
| 79 | + layerName: layer.name, |
| 80 | + text: true, |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return textLayers; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Fixes all caps issue for a specific text layer. |
| 91 | + * |
| 92 | + * **NOTE**: Works only on `After Effects 24.3` and later due to the use of `characterRange` method and `fontCapsOption`. |
| 93 | + * |
| 94 | + * @param layerId string |
| 95 | + * @returns void |
| 96 | + */ |
| 97 | +function fixAllCapsIssue(layerId: string) { |
| 98 | + const layer = app.project.layerByID(parseInt(layerId, 10)); |
| 99 | + if (!(layer && layer instanceof TextLayer)) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const textDocument = layer.sourceText.value; |
| 104 | + |
| 105 | + // check if characterRange exists in textDocument (After Effects 24.3+) |
| 106 | + if (typeof textDocument.characterRange !== 'function') { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const newValue = textDocument; |
| 111 | + |
| 112 | + // first check if the whole text has the same attribute |
| 113 | + const range = newValue.characterRange(0, newValue.text.length); |
| 114 | + if ( |
| 115 | + range.isRangeValid && |
| 116 | + range.fontCapsOption === FontCapsOption.FONT_ALL_CAPS |
| 117 | + ) { |
| 118 | + newValue.fontCapsOption = FontCapsOption.FONT_NORMAL_CAPS; |
| 119 | + newValue.text = newValue.text.toUpperCase(); |
| 120 | + updateLayerTextDocument(layer, newValue); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // otherwise, check per-character basis |
| 125 | + for (let i = 0; i < newValue.text.length; i++) { |
| 126 | + const cRange = newValue.characterRange(i, i + 1); |
| 127 | + if ( |
| 128 | + cRange.isRangeValid && |
| 129 | + cRange.fontCapsOption === FontCapsOption.FONT_ALL_CAPS |
| 130 | + ) { |
| 131 | + newValue.characterRange(i, i + 1).fontCapsOption = |
| 132 | + FontCapsOption.FONT_NORMAL_CAPS; |
| 133 | + newValue.characterRange(i, i + 1).text = newValue |
| 134 | + .characterRange(i, i + 1) |
| 135 | + .text.toUpperCase(); |
| 136 | + } |
| 137 | + } |
| 138 | + updateLayerTextDocument(layer, newValue); |
| 139 | +} |
| 140 | + |
| 141 | +function updateLayerTextDocument(layer: TextLayer, newValue: TextDocument) { |
| 142 | + const originalLayerName = layer.name; |
| 143 | + layer.sourceText.setValue(newValue); |
| 144 | + layer.name = originalLayerName; // Preserve original layer name |
| 145 | +} |
| 146 | + |
| 147 | +export { checkTextLayers, fixAllCapsIssue }; |
0 commit comments