from psd_tools import PSDImage

psd = PSDImage.open('./sample/dic_02_000000_홍길동.psd')

def print_layer_info(layer, indent=0):
    prefix = ' ' * indent
    # print(f"{prefix}Layer: {layer.name}")
    # print(f"{prefix}  Type: {layer.kind}")
    # print(f"{prefix}  Visible: {layer.visible}")
    # print(f"{prefix}  Opacity: {layer.opacity}")
    # print(f"{prefix}  Blending Mode: {layer.blending_mode}")
    # print(f"{prefix}  Bounds: {layer.bbox}")

    if layer.kind == 'pixel':
        print(f"{prefix}  Pixel Data: {layer.topil()}")
    elif layer.kind == 'type':
        # print(layer.text)
        # print(layer.engine_dict['StyleRun'])

        # # Extract font for each substring in the text.
        text = layer.engine_dict['Editor']['Text'].value
        print(f"text: {text}")
        fontset = layer.resource_dict['FontSet']
        print(fontset)
        runlength = layer.engine_dict['StyleRun']['RunLengthArray']        
        rundata = layer.engine_dict['StyleRun']['RunArray']
        print(rundata)
        
        
        
        # index = 0
        # for length, style in zip(runlength, rundata):
        #     substring = text[index:index + length]
        #     stylesheet = style['StyleSheet']['StyleSheetData']
        #     font = fontset[stylesheet['Font']]
        #     # print('%r gets %s' % (substring, font))
        #     index += length

for layer in psd:
    print_layer_info(layer)
    if layer.is_group():
        for child in layer:
            print_layer_info(child, indent=2)