font
This commit is contained in:
@@ -5,6 +5,7 @@ Core functionality for generating texture images with text overlays.
|
||||
|
||||
import bpy
|
||||
from ..utils.constants import has_text_fitting
|
||||
from ..utils.system import find_default_truetype_font
|
||||
|
||||
def generate_texture_image(props, width, height):
|
||||
pass
|
||||
@@ -187,24 +188,48 @@ def generate_texture_image(props, width, height):
|
||||
layout_mode = "vertical"
|
||||
|
||||
|
||||
# Determine candidate font paths based on user configuration
|
||||
candidate_paths = []
|
||||
seen_candidates = set()
|
||||
for candidate in (
|
||||
getattr(props, 'custom_font_path', '') if getattr(props, 'use_custom_font', False) else '',
|
||||
getattr(props, 'font_path', '') if getattr(props, 'font_path', 'default') != "default" else '',
|
||||
find_default_truetype_font()
|
||||
):
|
||||
if candidate and candidate not in seen_candidates:
|
||||
candidate_paths.append(candidate)
|
||||
seen_candidates.add(candidate)
|
||||
|
||||
failed_candidates = set()
|
||||
active_font_path = None
|
||||
|
||||
def load_font_for_size(size):
|
||||
pass
|
||||
nonlocal active_font_path
|
||||
for path in candidate_paths:
|
||||
pass
|
||||
if path in failed_candidates:
|
||||
pass
|
||||
continue
|
||||
try:
|
||||
pass
|
||||
font_obj = ImageFont.truetype(path, size)
|
||||
active_font_path = path
|
||||
return font_obj
|
||||
except (OSError, IOError, AttributeError, TypeError) as font_error:
|
||||
pass
|
||||
failed_candidates.add(path)
|
||||
continue
|
||||
active_font_path = None
|
||||
return ImageFont.load_default()
|
||||
|
||||
# Determine font size (with text fitting if enabled)
|
||||
font_size = props.font_size
|
||||
if props.enable_text_fitting and has_text_fitting():
|
||||
pass
|
||||
|
||||
# Try to load font for sizing
|
||||
try:
|
||||
pass
|
||||
if props.use_custom_font and props.custom_font_path:
|
||||
pass
|
||||
test_font_path = props.custom_font_path
|
||||
elif props.font_path != "default":
|
||||
test_font_path = props.font_path
|
||||
else:
|
||||
pass
|
||||
test_font_path = None
|
||||
|
||||
# Binary search for optimal font size
|
||||
min_size = props.min_font_size
|
||||
max_size = props.max_font_size
|
||||
target_width = width * (1.0 - props.text_fitting_margin / 100.0)
|
||||
@@ -212,52 +237,30 @@ def generate_texture_image(props, width, height):
|
||||
|
||||
optimal_size = min_size
|
||||
for test_size in range(min_size, max_size + 1, 2): # Step by 2 for performance
|
||||
pass
|
||||
test_font = load_font_for_size(test_size)
|
||||
|
||||
try:
|
||||
bbox = draw.textbbox((0, 0), full_text, font=test_font)
|
||||
test_width = bbox[2] - bbox[0]
|
||||
test_height = bbox[3] - bbox[1]
|
||||
except (ValueError, AttributeError):
|
||||
# Fallback for bitmap fonts
|
||||
test_width, test_height = draw.textsize(full_text, font=test_font)
|
||||
|
||||
if test_width <= target_width and test_height <= target_height:
|
||||
pass
|
||||
if test_font_path:
|
||||
pass
|
||||
test_font = ImageFont.truetype(test_font_path, test_size)
|
||||
else:
|
||||
pass
|
||||
test_font = ImageFont.load_default()
|
||||
|
||||
try:
|
||||
bbox = draw.textbbox((0, 0), full_text, font=test_font)
|
||||
test_width = bbox[2] - bbox[0]
|
||||
test_height = bbox[3] - bbox[1]
|
||||
except (ValueError, AttributeError):
|
||||
# Fallback for bitmap fonts
|
||||
test_width, test_height = draw.textsize(full_text, font=test_font)
|
||||
|
||||
if test_width <= target_width and test_height <= target_height:
|
||||
pass
|
||||
optimal_size = test_size
|
||||
else:
|
||||
pass
|
||||
break
|
||||
except (OSError, IOError):
|
||||
optimal_size = test_size
|
||||
else:
|
||||
pass
|
||||
break
|
||||
|
||||
|
||||
font_size = optimal_size
|
||||
except Exception as e:
|
||||
pass
|
||||
font_size = props.font_size
|
||||
|
||||
# Load final font
|
||||
try:
|
||||
pass
|
||||
if props.use_custom_font and props.custom_font_path:
|
||||
pass
|
||||
font = ImageFont.truetype(props.custom_font_path, font_size)
|
||||
elif props.font_path != "default":
|
||||
font = ImageFont.truetype(props.font_path, font_size)
|
||||
else:
|
||||
pass
|
||||
font = ImageFont.load_default()
|
||||
except (OSError, IOError) as e:
|
||||
pass
|
||||
font = ImageFont.load_default()
|
||||
font = load_font_for_size(font_size)
|
||||
|
||||
# Calculate text position and draw
|
||||
if layout_mode == "vertical" and props.prepend_append_layout == 'VERTICAL':
|
||||
|
||||
Reference in New Issue
Block a user