Issue 12499: textwrap.wrap: add control for fonts with different character widths (original) (raw)

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:19 admin set github: 56708
2021-09-03 18:32:07 eric.araujo set messages: + versions: + Python 3.11, - Python 3.5
2021-09-03 08:13:33 xi2 set pull_requests: + <pull%5Frequest26574>
2021-09-03 08:11:12 xi2 set nosy: + xi2
2018-07-09 08:58:24 methane unlink issue24665 dependencies
2017-02-15 07:44:17 serhiy.storchaka link issue24665 dependencies
2014-11-12 20:52:31 serhiy.storchaka set nosy: + serhiy.storchakamessages: + versions: + Python 3.5, - Python 3.3
2011-07-05 16:17:24 parent5446 set messages: +
2011-07-05 15:44:29 eric.araujo set nosy: + georg.brandl, eric.araujomessages: + keywords: + needs reviewstage: patch review
2011-07-05 06:10:03 amaury.forgeotdarc set nosy: + amaury.forgeotdarcmessages: +
2011-07-05 03:03:24 parent5446 create