This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: textwrap.wrap: add control for fonts with different character widths
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, eric.araujo, georg.brandl, parent5446, serhiy.storchaka, xi2
Priority: normal Keywords: needs review, patch

Created on 2011-07-05 03:03 by parent5446, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
textwrap.py-widthfunction-2011-07-04_22-57-49_r71219+.diff parent5446, 2011-07-05 03:03 Addition of custom width function to textwrap.py.
Pull Requests
URL Status Linked Edit
PR 28136 open xi2, 2021-09-03 08:13
Messages (6)
msg139828 - (view) Author: Tyler Romeo (parent5446) Date: 2011-07-05 03:03
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.
msg139829 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-07-05 06:10
About the patch: the function should not be passed to the constructor, it could be a regular method that can be overridden in subclasses.
msg139881 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-07-05 15:44
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.
msg139894 - (view) Author: Tyler Romeo (parent5446) Date: 2011-07-05 16:17
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__.
msg231092 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-12 20:52
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?
msg401011 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2021-09-03 18:32
A PR was opened for this.  `text_len` is used as param/attribute name.
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56708
2021-09-03 18:32:07eric.araujosetmessages: + msg401011
versions: + Python 3.11, - Python 3.5
2021-09-03 08:13:33xi2setpull_requests: + pull_request26574
2021-09-03 08:11:12xi2setnosy: + xi2
2018-07-09 08:58:24methaneunlinkissue24665 dependencies
2017-02-15 07:44:17serhiy.storchakalinkissue24665 dependencies
2014-11-12 20:52:31serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg231092
versions: + Python 3.5, - Python 3.3
2011-07-05 16:17:24parent5446setmessages: + msg139894
2011-07-05 15:44:29eric.araujosetnosy: + georg.brandl, eric.araujo
messages: + msg139881

keywords: + needs review
stage: patch review
2011-07-05 06:10:03amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg139829
2011-07-05 03:03:24parent5446create