Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2011-07-05.03:03:24.379>labels= ['type-feature', 'library', '3.11']
title='textwrap.wrap: add control for fonts with different character widths'updated_at=<Date2021-09-03.18:32:07.776>user='https://bugs.python.org/parent5446'
The textwrap modules uses len to determine the length of text, but in many (if not most) fonts, the width of a character differs depending on the letter, so it would be useful to have an option to pass a custom function that returns the width of a given string of text.
Amaury, do you think it’s more common to subclass TextWrapper than just instantiate it? I find the proposed API (an argument to __init__) very intuitive.
parent5446 commented on Jul 5, 2011
parent5446mannequin
MannequinAuthor
More actions
Normally I would have just added it as a function to be overloaded, but because of the nature of the textwrap.wrap function (all kwargs are passed to the TextWrapper constructor) I thought it made a lot more sense to keep it as an argument to __init__.
Both approaches can be combined. It could be a regular overridable method, and it could be overridded for an instance if corresponding argument is specified. See the default method and parameter of JSON encoder.
I slightly hesitate about the name. Is "width_func" the best of names?
Activity
parent5446 commented on Jul 5, 2011
Originally from http://bugs.python.org/issue12485 but separated.
The textwrap modules uses len to determine the length of text, but in many (if not most) fonts, the width of a character differs depending on the letter, so it would be useful to have an option to pass a custom function that returns the width of a given string of text.
amauryfa commented on Jul 5, 2011
About the patch: the function should not be passed to the constructor, it could be a regular method that can be overridden in subclasses.
merwok commented on Jul 5, 2011
Amaury, do you think it’s more common to subclass TextWrapper than just instantiate it? I find the proposed API (an argument to __init__) very intuitive.
parent5446 commented on Jul 5, 2011
Normally I would have just added it as a function to be overloaded, but because of the nature of the textwrap.wrap function (all kwargs are passed to the TextWrapper constructor) I thought it made a lot more sense to keep it as an argument to __init__.
serhiy-storchaka commented on Nov 12, 2014
Both approaches can be combined. It could be a regular overridable method, and it could be overridded for an instance if corresponding argument is specified. See the default method and parameter of JSON encoder.
I slightly hesitate about the name. Is "width_func" the best of names?
merwok commented on Sep 3, 2021
A PR was opened for this. text_len is used as param/attribute name.
kdeldycke commented on Jun 30, 2026
I just proposed a PR for this issue at: #152702