Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve text alignment #1455

Merged
merged 8 commits into from
Jan 2, 2024
Merged

Conversation

egrim
Copy link
Contributor

@egrim egrim commented Dec 3, 2023

I found some unexpected text alignment results when cutting text into the faces of a cube and took a swing at improving the results. Here's some code the shows the issue by creating a box and cutting a hole in the center of the >Z face for a reference point. It then cuts the letter "I" into that same face with various alignment options:

import cadquery as cq
from cadquery import exporters

face = cq.Workplane().box(20, 20, 20).faces(">Z").hole(.5).faces(">Z").workplane()

bottom_left = face.text("I", 20, -2, halign="left", valign="bottom")
centered = face.text("I", 20, -2, halign="center", valign="center")
top_right = face.text("I", 20, -2, halign="right", valign="top")

exporters.export(bottom_left, "bottom_left.svg", opt=dict(projectionDir=(0, 0, 1)))
exporters.export(centered, "centered.svg", opt=dict(projectionDir=(0, 0, 1)))
exporters.export(top_right, "top_right.svg", opt=dict(projectionDir=(0, 0, 1)))

Here is the output before and after the proposed change in this PR.

bottom_left before:
bottom_left_before
bottom_left after:
bottom_left_after

centered before:
centered_before
centered after:
centered_after

top_right before:
top_right_before
top_right after:
top_right_after

@egrim
Copy link
Contributor Author

egrim commented Dec 3, 2023

Oh man, those SVGs really don't work with dark mode. Please let me know if I need to convert them to JPGs.

@lorenzncode
Copy link
Member

The spacing is defined by the font. Shouldn't the font spacing be respected?

import cadquery as cq

font1 = "/usr/share/fonts/liberation-mono/LiberationMono-Regular.ttf"
font2 = "/usr/share/fonts/google-droid-sans-fonts/DroidSans.ttf"

a = cq.Workplane().text("A", 20, 2, halign="left", valign="bottom", fontPath=font1)
i = cq.Workplane().text("I", 20, 2, halign="left", valign="bottom", fontPath=font2)

This results in:
image

FontForge:
image

Oh man, those SVGs really don't work with dark mode. Please let me know if I need to convert them to JPGs.

I think it's OK. SVG export provides strokeColor. Perhaps missing is a background color option.

@egrim
Copy link
Contributor Author

egrim commented Dec 4, 2023

The spacing is defined by the font. Shouldn't the font spacing be respected?

Perhaps! I think there's definitely a case to be made that left aligning should respect that spacing, but it was unexpected when I wanted to center a letter on a face and it wasn't actually centered.

Copy link

codecov bot commented Dec 4, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6869dc6) 94.35% compared to head (1dc1555) 94.37%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1455      +/-   ##
==========================================
+ Coverage   94.35%   94.37%   +0.01%     
==========================================
  Files          28       28              
  Lines        5809     5809              
  Branches      993      993              
==========================================
+ Hits         5481     5482       +1     
  Misses        197      197              
+ Partials      131      130       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lorenzncode
Copy link
Member

Changes for halign="center" look good to me and agree with a couple of other tools that I tested.

The PR halign left, right behavior does not match with other tools. I would suggest to revert the changes for left, right.

Regarding valign, I would suggest not remove the current bottom (it might be renamed say "bottom-base" then add the new bottom change). The valign="bottom" CQ current behavior matches for example Blender "Bottom Baseline".

image

valign="top" - Perhaps add as a new valign option? I don't know that the existing top behavior should be completely replaced.

The complexity of the testTextAlignment test has been significantly reduced. There's no longer need to separately calculate bounding boxes, leading to more readable and maintainable code. Added assertions validate alignment based on bounding box coordinates and a defined tolerance level.
The changes refactor the text alignment code in the shapes module of CADquery, by leveraging the Graphic3d attributes from the OCC library. This approach eliminates the need for explicit bounding box calculations, thereby simplifying the code and increasing maintainability. The new code delegates the alignment calculations to the underlying OCC library, providing a more efficient solution.
@egrim
Copy link
Contributor Author

egrim commented Dec 11, 2023

Good news! We can rely on OCCP to do the alignment for us instead of using a bounding box based method. I've just pushed a couple of commits that should do what's needed.

@egrim egrim marked this pull request as draft December 11, 2023 20:00
@egrim
Copy link
Contributor Author

egrim commented Dec 11, 2023

Marking as draft while I work on the test failure that popped up.

This commit simplifies the testTextAlignment method in the test_cadquery module. It focuses on making the assertions clearer and more direct. Also, it removes the usage of ill-defined tolerances and switches to using the assertAlmostEqual method.
This commit updates the expected volume value in the exporter test within the "test_exporters.py" file. This change aligns the test result with the accurate calculation, ensuring the test's validity and improving its reliability.
@egrim egrim marked this pull request as ready for review December 12, 2023 01:44
@egrim
Copy link
Contributor Author

egrim commented Dec 12, 2023

Good to go now.

@lorenzncode
Copy link
Member

LGTM. I confirm alignment is improved compared to what I might expect with other tools.

Example with halign="right", valign="center":

CQ 2.3.1:

image

PR, blender:

image

@jmwright
Copy link
Member

jmwright commented Jan 2, 2024

@adam-urbanczyk @lorenzncode It sounds like this is ready for formal review, correct?

Copy link
Member

@adam-urbanczyk adam-urbanczyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice that the solution is based on what OCCT provides!

@jmwright
Copy link
Member

jmwright commented Jan 2, 2024

Since @lorenzncode has previously stated approval of this PR, I'll go ahead and improve it and get it merged.

Copy link
Member

@jmwright jmwright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @egrim

@jmwright jmwright merged commit eea5cd6 into CadQuery:master Jan 2, 2024
5 checks passed
@egrim egrim deleted the improve-text-alignment branch January 18, 2024 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants