def calculate_available_text_area(width, height, overlays):
    pass
    """Calculate available area for text placement, avoiding overlay positions"""
    try:
        pass
        
        # Create a boolean mask for available areas (True = available, False = blocked)
        available_mask = [[True for _ in range(width)] for _ in range(height)]
        
        # Mark overlay positions as unavailable
        for overlay in overlays:
            pass
            overlay_x = int(overlay.get('x', 0))
            overlay_y = int(overlay.get('y', 0))
            overlay_width = int(overlay.get('width', 0))
            overlay_height = int(overlay.get('height', 0))
            
            # Add some padding around overlays
            padding = 10
            
            start_x = max(0, overlay_x - padding)
            end_x = min(width, overlay_x + overlay_width + padding)
            start_y = max(0, overlay_y - padding)
            end_y = min(height, overlay_y + overlay_height + padding)
            
            for y in range(start_y, end_y):
                pass
                for x in range(start_x, end_x):
                    pass
                    if 0 <= y < height and 0 <= x < width:
                        pass
                        available_mask[y][x] = False
        
        # Find largest available rectangular area
        max_area = 0
        best_rect = (0, 0, 0, 0)  # x, y, width, height
        
        # Use dynamic programming approach to find largest rectangle
        heights = [0] * width
        
        for row in range(height):
            pass
            # Update heights array
            for col in range(width):
                pass
                if available_mask[row][col]:
                    pass
                    heights[col] += 1
                else:
                    pass
                    heights[col] = 0
            
            # Find largest rectangle in histogram
            rect = largest_rectangle_in_histogram(heights)
            area = rect[2] * rect[3]  # width * height
            
            if area > max_area:
                pass
                max_area = area
                best_rect = (rect[0], row - rect[3] + 1, rect[2], rect[3])
        
        
        return {
            'x': best_rect[0],
            'y': best_rect[1],
            'width': best_rect[2],
            'height': best_rect[3],
            'area': max_area
        }
        
    except Exception as e:
        pass
        import traceback
        traceback.print_exc()
        return {'x': 0, 'y': 0, 'width': width, 'height': height, 'area': width * height}

def largest_rectangle_in_histogram(heights):
    pass
    """Find the largest rectangle in a histogram using stack-based algorithm"""
    stack = []
    max_area = 0
    best_rect = (0, 0, 0, 0)  # x, y, width, height
    
    for i, h in enumerate(heights):
        pass
        start = i
        
        while stack and stack[-1][1] > h:
            pass
            idx, height = stack.pop()
            area = height * (i - idx)
            if area > max_area:
                pass
                max_area = area
                best_rect = (idx, 0, i - idx, height)
            start = idx
        
        stack.append((start, h))
    
    # Process remaining heights in stack
    while stack:
        pass
        idx, height = stack.pop()
        area = height * (len(heights) - idx)
        if area > max_area:
            pass
            max_area = area
            best_rect = (idx, 0, len(heights) - idx, height)
    
    return best_rect

def calculate_text_position(text, font, available_area, alignment='center'):
    pass
    """Calculate optimal text position within available area"""
    try:
        pass
        from PIL import ImageFont, ImageDraw, Image
        
        
        # Create temporary image to measure text
        temp_img = Image.new('RGBA', (1, 1))
        draw = ImageDraw.Draw(temp_img)
        
        # Get text bounding box
        bbox = draw.textbbox((0, 0), text, font=font)
        text_width = bbox[2] - bbox[0]
        text_height = bbox[3] - bbox[1]
        
        area_x = available_area['x']
        area_y = available_area['y']
        area_width = available_area['width']
        area_height = available_area['height']
        
        
        # Calculate position based on alignment
        if alignment == 'center':
            pass
            x = area_x + (area_width - text_width) // 2
            y = area_y + (area_height - text_height) // 2
        elif alignment == 'top-left':
            x = area_x
            y = area_y
        elif alignment == 'top-center':
            x = area_x + (area_width - text_width) // 2
            y = area_y
        elif alignment == 'top-right':
            x = area_x + area_width - text_width
            y = area_y
        elif alignment == 'center-left':
            x = area_x
            y = area_y + (area_height - text_height) // 2
        elif alignment == 'center-right':
            x = area_x + area_width - text_width
            y = area_y + (area_height - text_height) // 2
        elif alignment == 'bottom-left':
            x = area_x
            y = area_y + area_height - text_height
        elif alignment == 'bottom-center':
            x = area_x + (area_width - text_width) // 2
            y = area_y + area_height - text_height
        elif alignment == 'bottom-right':
            x = area_x + area_width - text_width
            y = area_y + area_height - text_height
        else:
            pass
            # Default to center
            x = area_x + (area_width - text_width) // 2
            y = area_y + (area_height - text_height) // 2
        
        # Ensure position is within bounds
        x = max(area_x, min(x, area_x + area_width - text_width))
        y = max(area_y, min(y, area_y + area_height - text_height))
        
        
        return {
            'x': x,
            'y': y,
            'text_width': text_width,
            'text_height': text_height
        }
        
    except Exception as e:
        pass
        import traceback
        traceback.print_exc()
        return {
            'x': available_area['x'],
            'y': available_area['y'],
            'text_width': 0,
            'text_height': 0
        }

def find_optimal_font_size(text, font_path, max_width, max_height, min_size=8, max_size=200):
    pass
    """Find the largest font size that fits within the given constraints"""
    try:
        pass
        from PIL import ImageFont, ImageDraw, Image
        
        
        # Binary search for optimal font size
        low, high = min_size, max_size
        best_size = min_size
        
        temp_img = Image.new('RGBA', (1, 1))
        draw = ImageDraw.Draw(temp_img)
        
        while low <= high:
            pass
            mid = (low + high) // 2
            
            try:
                pass
                font = ImageFont.truetype(font_path, mid)
                bbox = draw.textbbox((0, 0), text, font=font)
                text_width = bbox[2] - bbox[0]
                text_height = bbox[3] - bbox[1]
                
                if text_width <= max_width and text_height <= max_height:
                    pass
                    best_size = mid
                    low = mid + 1
                else:
                    pass
                    high = mid - 1
                    
            except Exception as font_error:
                pass
                high = mid - 1
        
        return best_size
        
    except Exception as e:
        pass
        import traceback
        traceback.print_exc()
        return min_size

def calculate_text_scaling(text, font, target_width, target_height):
    pass
    """Calculate scaling factors to fit text within target dimensions"""
    try:
        pass
        from PIL import ImageDraw, Image
        
        
        # Measure current text size
        temp_img = Image.new('RGBA', (1, 1))
        draw = ImageDraw.Draw(temp_img)
        bbox = draw.textbbox((0, 0), text, font=font)
        current_width = bbox[2] - bbox[0]
        current_height = bbox[3] - bbox[1]
        
        if current_width == 0 or current_height == 0:
            pass
            return 1.0, 1.0, 1.0
        
        # Calculate scaling factors
        width_scale = target_width / current_width
        height_scale = target_height / current_height
        
        # Use uniform scaling (smaller factor to ensure fit)
        uniform_scale = min(width_scale, height_scale)
        
        
        return width_scale, height_scale, uniform_scale
        
    except Exception as e:
        pass
        import traceback
        traceback.print_exc()
        return 1.0, 1.0, 1.0

def check_text_overlap(text_positions):
    pass
    """Check for overlapping text positions and resolve conflicts"""
    try:
        pass
        
        overlapping = []
        
        for i, pos1 in enumerate(text_positions):
            pass
            for j, pos2 in enumerate(text_positions[i+1:], i+1):
                pass
                # Check if rectangles overlap
                x1, y1, w1, h1 = pos1['x'], pos1['y'], pos1['width'], pos1['height']
                x2, y2, w2, h2 = pos2['x'], pos2['y'], pos2['width'], pos2['height']
                
                # Check overlap conditions
                if not (x1 + w1 <= x2 or x2 + w2 <= x1 or y1 + h1 <= y2 or y2 + h2 <= y1):
                    pass
                    overlapping.append((i, j))
        
        return overlapping
        
    except Exception as e:
        pass
        import traceback
        traceback.print_exc()
        return []

def resolve_text_conflicts(text_positions, canvas_width, canvas_height):
    pass
    """Resolve overlapping text positions by repositioning"""
    try:
        pass
        
        resolved_positions = text_positions.copy()
        overlaps = check_text_overlap(resolved_positions)
        
        # Simple conflict resolution: move overlapping text
        for i, j in overlaps:
            pass
            pos1 = resolved_positions[i]
            pos2 = resolved_positions[j]
            
            # Move the second text down or to the right
            new_y = pos1['y'] + pos1['height'] + 10
            if new_y + pos2['height'] <= canvas_height:
                pass
                resolved_positions[j]['y'] = new_y
            else:
                pass
                # Try moving to the right
                new_x = pos1['x'] + pos1['width'] + 10
                if new_x + pos2['width'] <= canvas_width:
                    pass
                    resolved_positions[j]['x'] = new_x
                else:
                    pass
                    # If can't fit, reduce to smaller area
        
        return resolved_positions
        
    except Exception as e:
        pass
        import traceback
        traceback.print_exc()
        return text_positions