better newline logic when fixing curvature

This commit is contained in:
themancalledjakob 2024-08-23 13:54:28 +02:00
parent 2b442c09e4
commit 76c05863b5
2 changed files with 45 additions and 15 deletions

View file

@ -244,6 +244,7 @@ class ABC3D_text_properties(bpy.types.PropertyGroup):
compensate_curvature: bpy.props.BoolProperty( compensate_curvature: bpy.props.BoolProperty(
update=update_callback, update=update_callback,
name="Compensate Curvature", name="Compensate Curvature",
description="Fixes curvature spacing issues for simple curves, don't use on curve with tiny loops.",
default=True, default=True,
) )
ignore_orientation: bpy.props.BoolProperty( ignore_orientation: bpy.props.BoolProperty(

View file

@ -245,6 +245,7 @@ def calc_point_on_bezier_spline(bezier_spline_obj,
def calc_point_on_bezier_curve(bezier_curve_obj, def calc_point_on_bezier_curve(bezier_curve_obj,
distance, distance,
output_tangent = False, output_tangent = False,
output_spline_index = False,
resolution_factor = 1.0): resolution_factor = 1.0):
curve = bezier_curve_obj.data curve = bezier_curve_obj.data
@ -254,6 +255,21 @@ def calc_point_on_bezier_curve(bezier_curve_obj,
resolution = int(spline.resolution_u * resolution_factor) resolution = int(spline.resolution_u * resolution_factor)
length = spline.calc_length(resolution=resolution) length = spline.calc_length(resolution=resolution)
if total_length + length > distance or i == len(curve.splines) - 1: if total_length + length > distance or i == len(curve.splines) - 1:
if output_spline_index and output_tangent:
# return value from c_p_o_b_s is a tuple
# so we need to append tuple + tuple
return calc_point_on_bezier_spline(spline,
(distance - total_length),
output_tangent,
resolution_factor) + (i,)
if output_spline_index and not output_tangent:
# return value from c_p_o_b_s is a location vector
# so we need to append with a comma
return calc_point_on_bezier_spline(spline,
(distance - total_length),
output_tangent,
resolution_factor), i
else:
return calc_point_on_bezier_spline(spline, return calc_point_on_bezier_spline(spline,
(distance - total_length), (distance - total_length),
output_tangent, output_tangent,
@ -677,18 +693,21 @@ def set_text_on_curve(text_properties, recursive=True):
advance = text_properties.offset advance = text_properties.offset
glyph_advance = 0 glyph_advance = 0
is_command = False is_command = False
previous_spline_index = -1
for i, c in enumerate(text_properties.text): for i, c in enumerate(text_properties.text):
if c == '\\': if c == '\\':
is_command = True is_command = True
continue continue
is_newline = False
if is_command: if is_command:
if c == 'n': if c == 'n':
is_newline = True
next_line_advance = get_next_line_advance(mom, advance, glyph_advance) next_line_advance = get_next_line_advance(mom, advance, glyph_advance)
if advance == next_line_advance: if advance == next_line_advance:
# self.report({'INFO'}, f"would like to add new line for {text_properties.text} please") # self.report({'INFO'}, f"would like to add new line for {text_properties.text} please")
print(f"would like to add new line for {text_properties.text} please") print(f"would like to add new line for {text_properties.text} please")
# TODO: add a new line # TODO: add a new line
advance = next_line_advance advance = next_line_advance + text_properties.offset
continue continue
is_command = False is_command = False
glyph_id = c glyph_id = c
@ -721,7 +740,9 @@ def set_text_on_curve(text_properties, recursive=True):
ob.constraints["Follow Path"].forward_axis = "FORWARD_X" ob.constraints["Follow Path"].forward_axis = "FORWARD_X"
ob.constraints["Follow Path"].up_axis = "UP_Y" ob.constraints["Follow Path"].up_axis = "UP_Y"
elif distribution_type == 'CALCULATE': elif distribution_type == 'CALCULATE':
location, tangent = calc_point_on_bezier_curve(mom, advance, True) location, tangent, spline_index = calc_point_on_bezier_curve(mom, advance, True, True)
if spline_index != previous_spline_index:
is_newline = True
ob.location = mom.matrix_world @ (location + text_properties.translation) ob.location = mom.matrix_world @ (location + text_properties.translation)
if not text_properties.ignore_orientation: if not text_properties.ignore_orientation:
mask = [0] mask = [0]
@ -754,20 +775,28 @@ def set_text_on_curve(text_properties, recursive=True):
# otherwise letters will be closer together the curvier the bezier is # otherwise letters will be closer together the curvier the bezier is
# this could be done more efficiently, but whatever # this could be done more efficiently, but whatever
curve_compensation = 0 curve_compensation = 0
if distribution_type == 'CALCULATE': if distribution_type == 'CALCULATE' and not is_newline:
if text_properties.compensate_curvature and glyph_advance > 0: if text_properties.compensate_curvature and glyph_advance > 0:
previous_location = calc_point_on_bezier_curve(mom, advance, False) previous_location, psi = calc_point_on_bezier_curve(mom, advance, False, True)
new_location = calc_point_on_bezier_curve(mom, advance + glyph_advance, False) new_location, si = calc_point_on_bezier_curve(mom, advance + glyph_advance, False, True)
while (previous_location - new_location).length > glyph_advance: if psi == si:
while (previous_location - new_location).length > glyph_advance and psi == si:
curve_compensation = curve_compensation - glyph_advance * 0.01 curve_compensation = curve_compensation - glyph_advance * 0.01
new_location = calc_point_on_bezier_curve(mom, advance + glyph_advance + curve_compensation, False) new_location, si = calc_point_on_bezier_curve(mom,
while (previous_location - new_location).length < glyph_advance: advance + glyph_advance + curve_compensation,
output_tangent=False,
output_spline_index=True)
while (previous_location - new_location).length < glyph_advance and psi == si:
curve_compensation = curve_compensation + glyph_advance * 0.01 curve_compensation = curve_compensation + glyph_advance * 0.01
new_location = calc_point_on_bezier_curve(mom, advance + glyph_advance + curve_compensation, False) new_location, si = calc_point_on_bezier_curve(mom,
advance + glyph_advance + curve_compensation,
output_tangent=False,
output_spline_index=True)
ob.scale = (scalor, scalor, scalor) ob.scale = (scalor, scalor, scalor)
advance = advance + glyph_advance + curve_compensation advance = advance + glyph_advance + curve_compensation
previous_spline_index = spline_index
if regenerate: if regenerate:
mom.users_collection[0].objects.link(ob) mom.users_collection[0].objects.link(ob)