```python:inference/core/workflows/core_steps/visualizations/text_display/utils.py
from __future__ import annotations

from typing import Tuple


def clamp_box(
    box_x: int, box_y: int, box_w: int, box_h: int, img_w: int, img_h: int
) -> Tuple[int, int]:
    """Clamp box position to image bounds."""
    if box_w > img_w:
        box_x = 0
    elif box_x < 0:
        box_x = 0
    elif box_x > img_w - box_w:
        box_x = img_w - box_w

    if box_h > img_h:
        box_y = 0
    elif box_y < 0:
        box_y = 0
    elif box_y > img_h - box_h:
        box_y = img_h - box_h

    return box_x, box_y
```