diff --git a/klausur-service/backend/cv_vocab_pipeline.py b/klausur-service/backend/cv_vocab_pipeline.py index 33a5a3a..a10a0c7 100644 --- a/klausur-service/backend/cv_vocab_pipeline.py +++ b/klausur-service/backend/cv_vocab_pipeline.py @@ -1976,6 +1976,41 @@ def expand_narrow_columns( "ExpandNarrowCols: col %d (%.1f%% → %.1f%%) x=%d w=%d words=%d", i, orig_pct, g.width / content_w * 100, g.x, g.width, g.word_count) + # --- Shrink overlapping neighbors to match new boundaries --- + # Left neighbor: its right edge must not exceed our new left edge + if i > 0: + left_nb = geometries[i - 1] + nb_right = left_nb.x + left_nb.width + if nb_right > g.x: + left_nb.width = g.x - left_nb.x + if left_nb.width < 0: + left_nb.width = 0 + left_nb.width_ratio = left_nb.width / content_w if content_w > 0 else 0.0 + # Re-assign words + nb_left_rel = left_nb.x - left_x + nb_right_rel = nb_left_rel + left_nb.width + left_nb.words = [wd for wd in word_dicts + if nb_left_rel <= wd['left'] < nb_right_rel] + left_nb.word_count = len(left_nb.words) + + # Right neighbor: its left edge must not be before our new right edge + if i + 1 < len(geometries): + right_nb = geometries[i + 1] + my_right = g.x + g.width + if right_nb.x < my_right: + old_right_edge = right_nb.x + right_nb.width + right_nb.x = my_right + right_nb.width = old_right_edge - right_nb.x + if right_nb.width < 0: + right_nb.width = 0 + right_nb.width_ratio = right_nb.width / content_w if content_w > 0 else 0.0 + # Re-assign words + nb_left_rel = right_nb.x - left_x + nb_right_rel = nb_left_rel + right_nb.width + right_nb.words = [wd for wd in word_dicts + if nb_left_rel <= wd['left'] < nb_right_rel] + right_nb.word_count = len(right_nb.words) + return geometries