Skip to content

Instantly share code, notes, and snippets.

@craigmarvelley
Created August 10, 2021 16:48
Show Gist options
  • Save craigmarvelley/59777cf7fb94e507a90ff12942b4bbc0 to your computer and use it in GitHub Desktop.
Save craigmarvelley/59777cf7fb94e507a90ff12942b4bbc0 to your computer and use it in GitHub Desktop.
// 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