refactor ensure glyphs + alternates

This commit is contained in:
jrkb 2025-06-04 14:47:09 +02:00
parent 14d1b7a160
commit 7de8fcc5d1
3 changed files with 332 additions and 129 deletions

View file

@ -143,13 +143,40 @@ class ABC3D_glyph_properties(bpy.types.PropertyGroup):
t = butils.get_text_properties(self.text_id)
if t is not None:
butils.set_text_on_curve(t)
return None
def alternate_get_callback(self):
return self["alternate"] if "alternate" in self else 0
def alternate_set_callback(self, value):
min_value = 0
new_value = max(value, min_value)
if self.text_id >= 0:
text_properties = butils.get_text_properties(self.text_id)
max_value = (
len(
Font.get_glyphs(
text_properties.font_name,
text_properties.face_name,
self.glyph_id,
)
)
- 1
)
new_value = min(new_value, max_value)
self["alternate"] = new_value
return None
glyph_id: bpy.props.StringProperty(maxlen=1)
text_id: bpy.props.IntProperty(
default=-1,
)
alternate: bpy.props.IntProperty(
default=-1,
default=0, # also change in alternate_get_callback
get=alternate_get_callback,
set=alternate_set_callback,
update=update_callback,
)
glyph_object: bpy.props.PointerProperty(type=bpy.types.Object)
@ -267,6 +294,7 @@ class ABC3D_data(bpy.types.PropertyGroup):
available_texts: bpy.props.CollectionProperty(
type=ABC3D_text_properties, name="Available texts"
)
texts: bpy.props.CollectionProperty(type=ABC3D_text_properties, name="texts")
def active_text_index_update(self, context):
lock_depsgraph_updates()
@ -713,6 +741,22 @@ class ABC3D_PT_TextPropertiesPanel(bpy.types.Panel):
layout.label(text="props.text_object is none")
return
# TODO: put this at a better place
# here we set the font if it is not correct
# this is a fix for a UI glitch, perhaps it could be fixed
# rather where it is not set properly
# if (
# butils.get_key("font_name") in props.text_object
# and butils.get_key("face_name") in props.text_object
# ):
# font = f"{props.text_object[butils.get_key('font_name')]} {props.text_object[butils.get_key('face_name')]}"
# if font != props.font:
#
# def setfont():
# props.font = font
#
# butils.run_in_main_thread(setfont)
#
layout.label(text=f"Mom: {props.text_object.name}")
layout.row().prop(props, "font")
layout.row().prop(props, "text")
@ -729,8 +773,26 @@ class ABC3D_PT_TextPropertiesPanel(bpy.types.Panel):
if glyph_props is None:
return
box = layout.box()
box.label(text=f"{glyph_props.glyph_id}")
box.label(text=f"selected character: {glyph_props.glyph_id}")
box.row().prop(glyph_props, "letter_spacing")
# if True:
# font_name = props.font_name
# face_name = props.face_name
# glyph_id = glyph_props.glyph_id
# glyphs_n = len(Font.get_glyphs(font_name, face_name, glyph_id))
# glyph_props.alternate.hard_min = -1
# glyph_props.alternate.hard_max = glyphs_n - 1
n_alternates = len(
Font.get_glyphs(
props.font_name,
props.face_name,
glyph_props.glyph_id,
)
)
if n_alternates > 1:
box.row().prop(glyph_props, "alternate", text=f"alternate ({n_alternates})")
# if glyph_props.glyph_object.preview is not None:
# box.row().template_preview(glyph_props.glyph_object.preview.icon_id)
class ABC3D_OT_RefreshAvailableFonts(bpy.types.Operator):
@ -1586,9 +1648,10 @@ class ABC3D_OT_CreateFontFromObjects(bpy.types.Operator):
def do_autodetect_names(self, name: str):
ifxsplit = name.split("_")
if len(ifxsplit) < 4:
print(f"name could not be autodetected {name}")
print("split:")
print(ifxsplit)
print(
f"{utils.prefix()}::CreateFontFromObjects: name could not be autodetected {name}"
)
print(f"{utils.prefix()}::CreateFontFromObjects: split: {ifxsplit=}")
return self.font_name, self.face_name
detected_font_name = f"{ifxsplit[1]}_{ifxsplit[2]}"
detected_face_name = ifxsplit[3]
@ -1663,9 +1726,11 @@ class ABC3D_OT_CreateFontFromObjects(bpy.types.Operator):
row.label(text=f"{k}{Font.known_misspellings[k]}{character}")
def execute(self, context):
print(f"executing {self.bl_idname}")
print(f"{utils.prefix()}::CreateFontFromObjects: executing {self.bl_idname}")
if len(context.selected_objects) == 0:
print(f"cancelled {self.bl_idname} - no objects selected")
print(
f"{utils.prefix()}::CreateFontFromObjects: cancelled {self.bl_idname} - no objects selected"
)
return {"CANCELLED"}
global shared
scene = bpy.context.scene
@ -1682,7 +1747,7 @@ class ABC3D_OT_CreateFontFromObjects(bpy.types.Operator):
currentObjects = []
for o in context.selected_objects:
if o.name not in currentObjects:
print(f"processing {o.name}")
print(f"{utils.prefix()}::CreateFontFromObjects: processing {o.name}")
process_object = True
if self.autodetect_names:
font_name, face_name = self.do_autodetect_names(o.name)
@ -1719,7 +1784,9 @@ class ABC3D_OT_CreateFontFromObjects(bpy.types.Operator):
f.face_name = face_name
else:
print(f"import warning: did not understand glyph {name}")
print(
f"{utils.prefix()}::CreateFontFromObjects: import warning: did not understand glyph {name}"
)
self.report({"INFO"}, f"did not understand glyph {name}")
return {"FINISHED"}