Work out orientation correct for each page.

    We ask Ghostscript to draw a preview page, which will rasterize with the
    current /Rotate applied, and then ask Tesseract which way the page is
    oriented. If the value of /Rotate is correct (e.g., a user already
    manually fixed rotation), then Tesseract will say the page is pointing
    up and the correction is zero. Otherwise, the orientation found by
    Tesseract represents the clockwise rotation, or the counterclockwise
    correction to rotation.

    When we draw the real page for OCR, we rotate it by the CCW correction,
    which points it (hopefully) upright. _weave.py takes care of the orienting
    the image and text layers.

def orient_page(infiles, output_file, log, context):
    """
    Work out orientation correct for each page.

    We ask Ghostscript to draw a preview page, which will rasterize with the
    current /Rotate applied, and then ask Tesseract which way the page is
    oriented. If the value of /Rotate is correct (e.g., a user already
    manually fixed rotation), then Tesseract will say the page is pointing
    up and the correction is zero. Otherwise, the orientation found by
    Tesseract represents the clockwise rotation, or the counterclockwise
    correction to rotation.

    When we draw the real page for OCR, we rotate it by the CCW correction,
    which points it (hopefully) upright. _weave.py takes care of the orienting
    the image and text layers.

    """

    options = context.get_options()
    page_pdf = next(ii for ii in infiles if ii.endswith('.page.pdf'))

    if not options.rotate_pages:
        re_symlink(page_pdf, output_file, log)
        return
    preview = next(ii for ii in infiles if ii.endswith('.preview.jpg'))

    orient_conf = tesseract.get_orientation(
        preview,
        engine_mode=options.tesseract_oem,
        timeout=options.tesseract_timeout,
        log=log,
    )

    direction = {0: '⇧', 90: '⇨', 180: '⇩', 270: '⇦'}

    pageno = page_number(page_pdf) - 1
    pdfinfo = context.get_pdfinfo()
    existing_rotation = pdfinfo[pageno].rotation

    correction = orient_conf.angle % 360

    apply_correction = False
    action = ''
    if orient_conf.confidence >= options.rotate_pages_threshold:
        if correction != 0:
            apply_correction = True
            action = ' - will rotate'
        else:
            action = ' - rotation appears correct'
    else:
        if correction != 0:
            action = ' - confidence too low to rotate'
        else:
            action = ' - no change'

    facing = ''
    if existing_rotation != 0:
        facing = 'with existing rotation {}, '.format(
            direction.get(existing_rotation, '?')
        )
    facing += 'page is facing {}'.format(direction.get(orient_conf.angle, '?'))

    log.info(
        '{pagenum:4d}: {facing}, confidence {conf:.2f}{action}'.format(
            pagenum=page_number(preview),
            facing=facing,
            conf=orient_conf.confidence,
            action=action,
        )
    )

    re_symlink(page_pdf, output_file, log)
    if apply_correction:
        context.set_rotation(pageno, correction)