def output_layout(layout, depth=1): if 'tag' in layout: return ( ' ' * depth + '* {tag}{n[content][size][0]}x{n[content][size][1]}' ' @ ({n[content][position][0]}, {n[content][position][1]})' '\n'.format( n=layout, tag=('<' + layout['tag'] + '> ') if 'tag' in layout else '', # text=(": '" + layout['text'] + "'") if 'text' in layout else '' ) # + ' ' * depth # + ' padding: {n[padding_box][size][0]}x{n[padding_box][size][1]}' # ' @ ({n[padding_box][position][0]}, {n[padding_box][position][1]})' # '\n'.format(n=layout) # + ' ' * depth # + ' border: {n[border_box][size][0]}x{n[border_box][size][1]}' # ' @ ({n[border_box][position][0]}, {n[border_box][position][1]})' # '\n'.format(n=layout) + ''.join( output_layout(child, depth=depth + 1) for child in layout.get('children', [])) if layout else '' + ('\n' if layout and layout.get('children', None) and depth > 1 else '')) else: return (' ' * depth + "* '{text}'\n".format(text=layout['text'].strip()))
def clean_layout(layout): if 'tag' in layout: cleaned = { key: { 'position': (layout[key]['position'][0], layout[key]['position'][1]), 'size': (layout[key]['size'][0], layout[key]['size'][1]), } for key in ['content', 'padding_box', 'border_box'] } children = [] for child in layout.get('children', []): sublayout = clean_layout(child) if sublayout: children.append(sublayout) if children: cleaned['children'] = children else: cleaned = None # # TODO - add proper handling for anonymous boxes. # cleaned = { # 'text': layout.get('text', '???') # } return cleaned