Skip to content

Canvas: deprecate write_text, add fill_text & stroke_text#4432

Draft
HalfWhitt wants to merge 6 commits into
beeware:mainfrom
HalfWhitt:canvas-text
Draft

Canvas: deprecate write_text, add fill_text & stroke_text#4432
HalfWhitt wants to merge 6 commits into
beeware:mainfrom
HalfWhitt:canvas-text

Conversation

@HalfWhitt
Copy link
Copy Markdown
Member

@HalfWhitt HalfWhitt commented May 30, 2026

Part of #3994

While there are still various HTML canvas context methods and attributes we haven't implemented, I believe this fixes the last one that we have but is significantly different than its inspiration.

I've put deprecation warnings in both the write_text method and the WriteText drawing action; calling the former internally suppresses the warning from the latter.

Also converted a number ifs/elifs to match/case, because I have a fever, and the only cure is more pattern matching.

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool

self.interface.style == other.interface.style,
]
)
except AttributeError:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Never occurred to me that Dummy has its own font implementation that never got beeware/travertino#233.

# percentage to get both stroke and fill.
# Stroke width is expressed as a percentage of the font size
stroke_width = line_width / font.native.pointSize * 100
if fill_style:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This can't happen anymore, since it'll always be either a stroke or a fill, but not both.

@HalfWhitt HalfWhitt marked this pull request as ready for review May 30, 2026 04:26
@HalfWhitt HalfWhitt requested a review from freakboy3742 May 30, 2026 04:26
@HalfWhitt HalfWhitt requested a review from corranwebster May 30, 2026 15:04
Copy link
Copy Markdown
Contributor

@corranwebster corranwebster left a comment

Choose a reason for hiding this comment

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

Overall looks good and the changes make sense. The new API still doesn't quite match the HTML Canvas API in that font, baseline, etc. aren't properties of the current state, like color, etc. I understand if that's too big a change for this PR, but we probably want to make that change eventually.

One change that is needed before merging is that I think there are some uses of write_text in the examples that still need conversion.

(
self.font._impl
if self.font is not None
else Font(family=SYSTEM, size=SYSTEM_DEFAULT_FONT_SIZE)._impl
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not for this PR, but it seems like there ought to be a simpler way of saying "default system font".

@HalfWhitt
Copy link
Copy Markdown
Member Author

The new API still doesn't quite match the HTML Canvas API in that font, baseline, etc. aren't properties of the current state, like color, etc. I understand if that's too big a change for this PR, but we probably want to make that change eventually.

D'oh, thank you for reminding me. I do think I'll add the actual attributes in a subsequent PR, but I should go ahead and make the non-HTML-matching parameters keyword-only from the start.

One change that is needed before merging is that I think there are some uses of write_text in the examples that still need conversion.

I always forget about these... we really do need a way to automatically test them.

@HalfWhitt HalfWhitt marked this pull request as draft June 3, 2026 03:48
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.

2 participants