Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 62 additions & 4 deletions src/engineData2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// Engine data 2 experiments
// /test/engineData2.json:1109 is character codes

import { EngineData } from './text';

interface KeysDict {
[key: string]: {
name?: string;
Expand Down Expand Up @@ -127,7 +129,7 @@ const keysRoot: KeysDict = {
},
},
},
}
},
},
},
},
Expand Down Expand Up @@ -217,8 +219,64 @@ const keysRoot: KeysDict = {
},
},
'8': {
name: '8',
children: {},
name: 'TextFrameSet',
children: {
'0': {
uproot: true,
children: {
'0': {
name: 'TextPath',
children: {
'0': { name: 'Name' },
'1': {
name: 'BezierCurve',
children: {
'0': { name: 'ControlPoints' },
},
},
'2': {
name: 'Data',
children: {
'0': { name: 'Type' },
'1': { name: 'Orientation' },
'2': { name: 'FrameMatrix' },
'4': { name: '4' },
'6': { name: 'TextRange' },
'7': { name: 'RowGutter' },
'8': { name: 'ColumnGutter' },
'9': { name: '9' },
'10': {
name: 'BaselineAlignment',
children: {
'0': { name: 'Flag' },
'1': { name: 'Min' },
},
},
'11': {
name: 'PathData',
children: {
'1': { name: '1' },
'0': { name: 'Reversed' },
'2': { name: '2' },
'3': { name: '3' },
'4': { name: 'Spacing' },
'5': { name: '5' },
'6': { name: '6' },
'7': { name: '7' },
'18': { name: '18' },
},
},
'12': { name: '12' },
'13': { name: '13' },
},
},
'3': { name: '3' },
'97': { name: 'UUID' },
},
},
},
},
},
},
'9': {
name: 'Predefined',
Expand Down Expand Up @@ -343,7 +401,7 @@ const keysRoot: KeysDict = {
},
};

function decodeObj(obj: any, keys: KeysDict): any {
function decodeObj(obj: EngineData, keys: KeysDict): any {
if (obj === null) return obj;
if (Array.isArray(obj)) return obj.map(x => decodeObj(x, keys));
if (typeof obj !== 'object') return obj;
Expand Down
12 changes: 12 additions & 0 deletions src/psd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ export interface LayerTextData {
boxBounds?: number[];
bounds?: UnitsBounds;
boundingBox?: UnitsBounds;

textPath?: {
BezierCurve?: {
/**the length number should be multiples of 8, which is present a bezier curve */
ControlPoints: number[];
},
Data: {
Type: number;
FrameMatrix: number[];
TextRange: number[];
}
}
}

export interface PatternInfo {
Expand Down
37 changes: 37 additions & 0 deletions src/psdReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { Psd, Layer, ColorMode, SectionDividerType, LayerAdditionalInfo, ReadOpt
import { resetImageData, offsetForChannel, decodeBitmap, createImageData, toBlendMode, ChannelID, Compression, LayerMaskFlags, MaskParams, ColorSpace, RAW_IMAGE_DATA, largeAdditionalInfoKeys, imageDataToCanvas } from './helpers';
import { infoHandlersMap } from './additionalInfo';
import { resourceHandlersMap } from './imageResources';
import { parseEngineData } from './engineData';
import { toByteArray } from 'base64-js';
import { decodeEngineData2 } from './engineData2';
import type { EngineData } from './text';

interface ChannelInfo {
id: ChannelID;
Expand Down Expand Up @@ -345,6 +349,14 @@ export function readPsd(reader: PsdReader, readOptions: ReadOptions = {}) {
// but add option to preserve file color mode (need to return image data instead of canvas in that case)
// psd.colorMode = ColorMode.RGB; // we convert all color modes to RGB

if(psd.engineData){
const byteArray = toByteArray(psd.engineData);
const engineData = parseEngineData(byteArray);
const parsedEngineData = decodeEngineData2(engineData);

assignGlobalEngineData(psd.children, parsedEngineData);
}

return psd;
}

Expand Down Expand Up @@ -705,6 +717,31 @@ export function readAdditionalLayerInfo(reader: PsdReader, target: LayerAddition
}, false, u64);
}

/**
* There is a Global text engine data outside text element.
* So, we need to pick global engine data and set to per text element.
*/
function assignGlobalEngineData(layers: Layer[] | undefined, globalEngineData: EngineData) {
const resources = globalEngineData?.ResourceDict?.TextFrameSet;

if (!resources || Object.keys(resources).length === 0 || !layers?.length) {
return;
}
const resourceLength = Object.keys(resources).length;
let textIndex = 0;
layers.forEach((layer) => {
const isText = !!layer.text;
if (isText) {
if (textIndex < resourceLength) {
const resource = resources[textIndex++];
layer.text!.textPath = resource.TextPath;
} else {
console.warn('Not enough resources for all text layers');
}
}
});
}

export function createImageDataBitDepth(width: number, height: number, bitDepth: number, channels = 4): PixelData {
if (bitDepth === 1 || bitDepth === 8) {
if (channels === 4) {
Expand Down
3 changes: 2 additions & 1 deletion src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ interface ResourceDict {
SubscriptSize: number;
SubscriptPosition: number;
SmallCapSize: number;
TextFrameSet?: any[];
}

interface ParagraphRun {
Expand All @@ -120,7 +121,7 @@ interface PhotoshopNode {
};
}

interface EngineData {
export interface EngineData {
EngineDict: {
Editor: { Text: string; };
ParagraphRun: {
Expand Down