Skip to curriculum
Jump to the editor

23.7. Sort page blocks into reading order

Hard
Topics
30–50 min

Text recognition on a scanned page, a PDF text layer, or the boxes of a page builder all give you the same thing: a set of rectangles with text, in no useful order. Before you can read the page aloud, copy it, or search it, you need the order a person would read it in. Sorting by y and then x fails at once: a word 2 pixels higher than its neighbour jumps ahead of it, and two columns get read line by line across the gutter.

A classic answer is to cut the page at its empty gaps. A heading separated from the text below by white space is cut off first; a gutter splits two columns; each part is then cut again, down to single words.

Write readingOrder(blocks) in reading-order.js. It returns the blocks' ids in reading order, following the rule below.

Signature

JAVASCRIPT
function readingOrder(blocks) // returns an array of ids
  • blocks: an array of { id, x, y, width, height }. y grows downward. A block covers x up to x + width and y up to y + height. Other fields are ignored.

The rule

To order a group of two or more blocks:

  1. Horizontal gaps. Sort the group by y (ties: input order). Walking in that order, a block whose y is at least the lowest bottom edge (y + height) seen so far starts after a horizontal gap, whose width is the difference. A gap of width 0 (blocks that touch) counts.
  2. Vertical gaps. The same along x, with x and x + width.
  3. Cut. Take the widest gap found in either direction; when the widest horizontal and vertical gaps are equal, the horizontal one wins. Cut the group at every gap of that width in that direction. The parts are read top to bottom (or left to right), and each part is ordered by the same rule.
  4. No gap. If the group has no gap in either direction (its blocks overlap), order it by y, then x, then input order.

A group of one block is just that block.

Requirements

  1. Words on one line are read left to right, even when their y differs by a few pixels.
  2. Lines are read top to bottom, and columns one after the other, left to right.
  3. A heading or footer across several columns is read before or after them.
  4. The widest gap decides the direction, horizontal wins ties, and all gaps of that width are cut at once.
  5. Blocks that touch are separated by a gap of width 0.
  6. Overlapping blocks are ordered by y, then x, then input order.
  7. The ids are returned as they are (any value). blocks and its objects are never changed.
  8. A TypeError is thrown when x, y, width or height of any block is not a finite number, and a RangeError when width or height is negative. Zero is allowed.
  9. A page of 2,000 blocks is ordered in well under a second.

Examples

JAVASCRIPT
readingOrder([
  { id: "w2", x: 50, y: 0, width: 40, height: 20 },
  { id: "w1", x: 0, y: 2, width: 40, height: 16 },
  { id: "w3", x: 100, y: 3, width: 40, height: 15 },
]);
// ["w1", "w2", "w3"]: one line, so only vertical gaps
JAVASCRIPT
readingOrder([
  { id: "H", x: 0, y: 0, width: 450, height: 30 },     // heading over both columns
  { id: "R1", x: 250, y: 50, width: 200, height: 20 },
  { id: "L1", x: 0, y: 50, width: 200, height: 20 },
  { id: "L2", x: 0, y: 80, width: 200, height: 20 },
  { id: "R2", x: 250, y: 80, width: 200, height: 20 },
]);
// ["H", "L1", "L2", "R1", "R2"]
// The heading crosses the gutter, so the page has only horizontal gaps: 20 below H, 10 between lines.
// The 20 gap is cut. Below it, the gutter (50) is wider than the line gap (10): columns.
JAVASCRIPT
readingOrder([
  { id: "A", x: 0, y: 0, width: 100, height: 20 },
  { id: "B", x: 110, y: 0, width: 100, height: 20 },
  { id: "C", x: 0, y: 60, width: 100, height: 20 },
  { id: "D", x: 110, y: 60, width: 100, height: 20 },
]);
// ["A", "B", "C", "D"]: rows are 40 apart, columns only 10

Environment

  • Plain JavaScript in a browser page; the tests call the global readingOrder from reading-order.js.
  • Right-to-left scripts, vertical writing, rotated boxes and guessing which blocks are captions are not part of this problem.
  • Any built-in is allowed.

Hints

  1. After sorting by y, what single number tells you whether the next block starts below everything before it?
  2. Each part after a cut is a smaller page. What does that suggest about the shape of your function?
  3. Why does sorting with one comparator (a.y - b.y || a.x - b.x) never give columns?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…