@@ -3,6 +3,10 @@ import { Psd, Layer, ColorMode, SectionDividerType, LayerAdditionalInfo, ReadOpt
33import { resetImageData , offsetForChannel , decodeBitmap , createImageData , toBlendMode , ChannelID , Compression , LayerMaskFlags , MaskParams , ColorSpace , RAW_IMAGE_DATA , largeAdditionalInfoKeys , imageDataToCanvas } from './helpers' ;
44import { infoHandlersMap } from './additionalInfo' ;
55import { resourceHandlersMap } from './imageResources' ;
6+ import { parseEngineData } from './engineData' ;
7+ import { toByteArray } from 'base64-js' ;
8+ import { decodeEngineData2 } from './engineData2' ;
9+ import type { EngineData } from './text' ;
610
711interface ChannelInfo {
812 id : ChannelID ;
@@ -345,6 +349,14 @@ export function readPsd(reader: PsdReader, readOptions: ReadOptions = {}) {
345349 // but add option to preserve file color mode (need to return image data instead of canvas in that case)
346350 // psd.colorMode = ColorMode.RGB; // we convert all color modes to RGB
347351
352+ if ( psd . engineData ) {
353+ const byteArray = toByteArray ( psd . engineData ) ;
354+ const engineData = parseEngineData ( byteArray ) ;
355+ const parsedEngineData = decodeEngineData2 ( engineData ) ;
356+
357+ assignGlobalEngineData ( psd . children , parsedEngineData ) ;
358+ }
359+
348360 return psd ;
349361}
350362
@@ -705,6 +717,31 @@ export function readAdditionalLayerInfo(reader: PsdReader, target: LayerAddition
705717 } , false , u64 ) ;
706718}
707719
720+ /**
721+ * There is a Global text engine data outside text element.
722+ * So, we need to pick global engine data and set to per text element.
723+ */
724+ function assignGlobalEngineData ( layers : Layer [ ] | undefined , globalEngineData : EngineData ) {
725+ const resources = globalEngineData ?. ResourceDict ?. TextFrameSet ;
726+
727+ if ( ! resources || Object . keys ( resources ) . length === 0 || ! layers ?. length ) {
728+ return ;
729+ }
730+ const resourceLength = Object . keys ( resources ) . length ;
731+ let textIndex = 0 ;
732+ layers . forEach ( ( layer ) => {
733+ const isText = ! ! layer . text ;
734+ if ( isText ) {
735+ if ( textIndex < resourceLength ) {
736+ const resource = resources [ textIndex ++ ] ;
737+ layer . text ! . textPath = resource . TextPath ;
738+ } else {
739+ console . warn ( 'Not enough resources for all text layers' ) ;
740+ }
741+ }
742+ } ) ;
743+ }
744+
708745export function createImageDataBitDepth ( width : number , height : number , bitDepth : number , channels = 4 ) : PixelData {
709746 if ( bitDepth === 1 || bitDepth === 8 ) {
710747 if ( channels === 4 ) {
0 commit comments