Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Pseudocode for checking if there is a large gap between the
// current and next candidate's bounding box.
var isNextItemOnNewParagraph: Bool {
let avgHeight = (currentObservationCandidateHeight + currentObservationCandidateHeight / 2)
let currentItemHeight = currentObservationCandidateHeight
let nextItemHeight = currentObservationCandidateHeight
let isSeparationLargerThanCurrentItemHeight = avgHeight >= currentItemHeight
let isSeparationLargerThanNextItemHeight = avgHeight >= nextItemHeight
return isSeparationLargerThanCurrentItemHeight || isSeparationLargerThanNextItemHeight
}
// Pseudocode for checking if next candidate is on a new line and is there
// an indentation for the next candidate.
let isNextItemOnNewLine = avgBottomYOfNextItem > avgBottomYOfCurrentItem
if isNextItemOnNewParagraph {
addNewLineToText()
resetMinimumXPositionOfAParagraphs()
} else if isNextItemOnNewLine {
let nextItemTopLeftX = nextItemTopLeftXPosition
let minimumParagraphXposition = aParagraphMinX
let isNextItemPartOfCurrentParagraph = nextItemTopLeftX <= minimumParagraphXposition
// Assumption: if there is no sufficient gap between paragraphs then new paragraphs
// are started with an indentation
if isNextItemPartOfCurrentParagraph {
addSpacingAndAppendThenNextCandidateString()
updateMinimumXPositionOfAParagraphs(with: nextItemTopLeftX)
} else {
addNewLineToText()
resetMinimumXPositionOfAParagraphs()
}
} else {
addSpacingAndAppendThenNextCandidateString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment