'Convert absolute position to relative for texts on page

I can't correctly formulate a question for Google on the following task, so I'm looking for solution or problema title to search further.

The problem is:

I have page dimensions and list of rectangles (text blocks with known bounds). All rectangles are defined with absolute coordinates. I want to convert it to relative positioned texts. For now the task is to group rectangles to islands to make column-row layout. Rectangles may overlaps, but it's not a issue now. Actual page may contains paragraphs together with charts, be multi-column or single column, so it's generic document page.

Example Real example (magenta rectangles are text bounds):

input

Desired output (green areas are calculated text flows):

ouput



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source