<?xml version="1.0" encoding="UTF-8"?>
<alto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.loc.gov/standards/alto/ns-v4#"
	xsi:schemaLocation="http://www.loc.gov/standards/alto/ns-v4# http://www.loc.gov/standards/alto/v4/alto-4-0.xsd">
	<Description>
		<MeasurementUnit>pixel</MeasurementUnit>
		<sourceImageInformation>
			<fileName>{{ page.name }}</fileName>
		</sourceImageInformation>
		<OCRProcessing ID="OCR_0">
			<ocrProcessingStep>
				<processingSoftware>
					<softwareName>kraken</softwareName>
				</processingSoftware>
			</ocrProcessingStep>
		</OCRProcessing>
	</Description>

	<Layout>
		<Page WIDTH="{{ page.size[0] }}" HEIGHT="{{ page.size[1] }}" PHYSICAL_IMG_NR="0" ID="page_0">
			<PrintSpace HPOS="0" VPOS="0" WIDTH="{{ page.size[0] }}" HEIGHT="{{ page.size[1] }}">
				<TextBlock HPOS="0" VPOS="0" ID="textblock_0" WIDTH="{{ page.size[0] }}" HEIGHT="{{ page.size[1] }}">
					{% for line in page.lines %}
					<TextLine ID="line_{{ line.index }}"
						  HPOS="{{ line.bbox[0] }}"
						  VPOS="{{ line.bbox[1] }}" 
						  WIDTH="{{ line.bbox[2] - line.bbox[0] }}" 
						  HEIGHT="{{ line.bbox[3] - line.bbox[1] }}">
						{% for segment in line.recognition %}
							{# ALTO forbids encoding whitespace before any String/Shape tags #}
							{% if segment.text is whitespace and loop.index > 1 %}
							<SP ID="segment_{{ segment.index }}"
							    HPOS="{{ segment.bbox[0]}}" 
							    VPOS="{{ segment.bbox[1] }}"
							    WIDTH="{{ segment.bbox[2] - segment.bbox[0] }}" 
							    HEIGHT="{{ segment.bbox[3] - segment.bbox[1] }}"/>
							{% else %}
							<String ID="segment_{{ segment.index }}"
								CONTENT="{{ segment.text|e }}" 
								HPOS="{{ segment.bbox[0] }}" 
								VPOS="{{ segment.bbox[1] }}"
								WIDTH="{{ segment.bbox[2] - segment.bbox[0] }}" 
								HEIGHT="{{ segment.bbox[3] - segment.bbox[1] }}" 
								WC="{{ (segment.confidences|sum / segment.confidences|length)|round(4) }}">
								{% for char in segment.recognition %}
								<Glyph ID="char_{{ char.index }}" 
								       CONTENT="{{ char.text|e }}"
								       HPOS="{{ char.bbox[0] }}"
								       VPOS="{{ char.bbox[1] }}"
								       WIDTH="{{ char.bbox[2] - char.bbox[0] }}"
								       HEIGHT="{{ char.bbox[3] - char.bbox[1] }}"
								       GC="{{ char.confidence|round(4) }}">
								</Glyph>
								{% endfor %}
							</String>
							{% endif %}
						{% endfor %}
					</TextLine>
					{% endfor %}
				</TextBlock>
			</PrintSpace>
		</Page>
	</Layout>
</alto>
