feat: add text overflow detection to TextFrame by saggl · Pull Request #1114 · scanny/python-pptx

and others added 4 commits

March 21, 2026 10:21
Add new methods to detect and analyze text overflow before it occurs:

- TextFrame.will_overflow(): Simple boolean check for overflow
- TextFrame.overflow_info(): Detailed overflow metrics with OverflowInfo dataclass
- TextFitter.will_fit(): Core overflow detection logic
- TextFitter.measure_text_height(): Calculate required text height
- TextFitter.calculate_overflow_metrics(): Detailed overflow analysis

The OverflowInfo dataclass provides:
- will_overflow: Boolean indicator
- required_height/available_height: Size metrics
- overflow_height/overflow_percentage: Overflow amounts
- estimated_lines: Line count estimate
- fits_at_font_size: Recommended font size to fit

This enables proactive content validation to prevent text from being
cut off at the bottom of text frames, addressing the issue where users
don't know content will overflow until they open the presentation.

Includes comprehensive unit tests, feature tests, and examples.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extends existing vertical overflow detection to support horizontal
overflow, providing comprehensive text overflow analysis in both
dimensions.

Key additions:
- TextFitter.will_fit_width() - Check if text fits horizontally
- TextFitter.measure_text_width() - Calculate max width and identify widest element
- TextFitter.calculate_horizontal_overflow_metrics() - Detailed horizontal metrics
- Added 'direction' parameter to TextFrame.will_overflow() and overflow_info()
  - Supports 'vertical' (default), 'horizontal', or 'both'
- Extended OverflowInfo dataclass with horizontal overflow fields
- Added comprehensive example and documentation

Features:
- Detects when individual words are too wide (word_wrap=True)
- Detects when entire lines are too wide (word_wrap=False)
- Backward compatible: default direction='vertical' maintains existing behavior
- Identifies specific problematic text elements
- Provides overflow percentages and font size suggestions

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Performance improvements:
- Replace O(n) linear search with O(log n) binary search for horizontal best fit
- Add short-circuit evaluation in will_overflow() when direction='both'
- Reduce from 72 to ~7 iterations for large font sizes

Code quality improvements:
- Extract _calculate_overflow() helper to eliminate duplication
- Consolidate overflow percentage calculation logic
- Remove implementation documentation file

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Address issues identified in PR scanny#1114 review:
- Fix broken unit tests using positional args instead of keyword args
- Guard _rendered_size against empty strings (Pillow 10+ compat)
- Filter empty paragraphs in horizontal width methods
- Fix _calculate_overflow division by zero when available <= 0
- Make OverflowInfo frozen dataclass with Literal type for direction
- Fix dead conditional for widest_element, fix docstrings
- Add cross-platform font paths in examples
- Remove uv.lock and add to .gitignore
- Add unit tests for horizontal/both directions and invalid direction
- Add BDD scenarios for horizontal overflow detection

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>