def normalize_pdf(layout, scaler):
chars = []<EOL>mentions = []<EOL>height = scaler * layout.height<EOL>font_size_counter = collections.Counter()<EOL>pts_thres = <NUM_LIT> * scaler<EOL>segments = []<EOL>curves = []<EOL>figures = []<EOL>def processor(m):<EOL><INDENT>if isinstance(m, LTComponent):<EOL><INDENT>m.set_bbox(normalize_bbox(m.bbox, height, scaler))<EOL>if isinstance(m, LTCurve):<EOL><INDENT>m.pts = normalize_pts(m.pts, height, scaler)<EOL>if isinstance(m, LTLine) and max(m.width, m.height) > pts_thres:<EOL><INDENT>segments.append(m)<EOL>return<EOL><DEDENT>curves.append(m)<EOL>return<EOL><DEDENT>if isinstance(m, LTFigure):<EOL><INDENT>figures.append(m)<EOL>return<EOL><DEDENT>if isinstance(m, LTChar):<EOL><INDENT>chars.append(m)<EOL>font_size = _font_size_of(m)<EOL>font_size_counter[font_size] += <NUM_LIT:1><EOL>return<EOL><DEDENT>if isinstance(m, LTTextLine):<EOL><INDENT>mention_text = keep_allowed_chars(m.get_text()).strip()<EOL>if mention_text:<EOL><INDENT>m.clean_text = mention_text<EOL>m.font_name, m.font_size = _font_of_mention(m)<EOL>mentions.append(m)<EOL><DEDENT>return<EOL><DEDENT><DEDENT>if isinstance(m, LTAnno):<EOL><INDENT>chars.append(m)<EOL><DEDENT><DEDENT>traverse_layout(layout, processor)<EOL>for m in mentions:<EOL><INDENT>alphanum_c = next((c for c in m if c.get_text().isalnum()), None)<EOL>if alphanum_c:<EOL><INDENT>m.set_bbox((m.x0, alphanum_c.y0, m.x1, alphanum_c.y1))<EOL><DEDENT><DEDENT>elems = PDFElems(mentions, segments, curves, figures, layout, chars)<EOL>return elems, font_size_counter<EOL>
Normalizes pdf object coordinates (bot left) to image
conventions (top left origin).
Returns the list of chars and average char size