fix(pdf2docx): merge touching intervals at minGap boundary (#304)

This commit is contained in:
ANIRUDDHA ADAK
2026-09-12 11:37:25 +08:00
committed by GitHub
parent 0c6fa8fb35
commit dad0cec0ec
2 changed files with 43 additions and 1 deletions
+1 -1
View File
@@ -77,7 +77,7 @@ export function mergeIntervals(intervals: readonly Interval[], minGap = 0): Inte
const out: Interval[] = []
for (const iv of sorted) {
const last = out[out.length - 1]
if (last && iv.lo - last.hi < minGap) last.hi = Math.max(last.hi, iv.hi)
if (last && iv.lo - last.hi <= minGap) last.hi = Math.max(last.hi, iv.hi)
else out.push({ ...iv })
}
return out
+42
View File
@@ -4,6 +4,7 @@ import {
coverageRatio,
intersectArea,
median,
mergeIntervals,
overlapRatio,
rectUnion,
verticalOverlapRatio,
@@ -41,6 +42,47 @@ describe('rect math', () => {
})
})
describe('mergeIntervals', () => {
it('merges touching intervals with the default gap of 0', () => {
expect(
mergeIntervals([
{ lo: 0, hi: 10 },
{ lo: 10, hi: 20 },
]),
).toEqual([{ lo: 0, hi: 20 }])
})
it('merges overlapping intervals and honors minGap boundaries', () => {
expect(
mergeIntervals([
{ lo: 0, hi: 10 },
{ lo: 5, hi: 15 },
]),
).toEqual([{ lo: 0, hi: 15 }])
expect(
mergeIntervals(
[
{ lo: 0, hi: 10 },
{ lo: 12, hi: 20 },
],
2,
),
).toEqual([{ lo: 0, hi: 20 }])
expect(
mergeIntervals(
[
{ lo: 0, hi: 10 },
{ lo: 13, hi: 20 },
],
2,
),
).toEqual([
{ lo: 0, hi: 10 },
{ lo: 13, hi: 20 },
])
})
})
describe('coverageRatio (P29 E)', () => {
it('full-page box covers everything', () => {
expect(coverageRatio([{ x0: 0, y0: 0, x1: 600, y1: 800 }], 600, 800)).toBe(1)