lordmauve / lordmauve/pgzero

"ValueError: substring not found" in textbox draw

Open
#250 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

ptext
Dominant language
Python
Stars
611
Forks
211
PR merge metrics
No merged PRs in 30d

Description

Hey,
I was recently digging around the text fitting source (ptext.py) and found an issue.

When calling `screen.draw.textbox`, if you prefix the string with a space(s), it will cause a `ValueError` exception to be thrown.

Without the space at the beginning, it works just fine.

Thank you for your time.

### Source Code
```python
import pgzrun

WIDTH = 500
HEIGHT = 500

text = " ab"

def draw():
screen.draw.textbox(text, (0, 0, 500, 500))

pgzrun.go()
```
### Stack Trace
```
File "C:\..\issue.py", line 9, in draw
screen.draw.textbox(text, (0, 0, 500, 500))
File "C:\..\lib\site-packages\pgzero\screen.py", line 66, in textbox
ptext.drawbox(*args, surf=self._surf, **kwargs)
File "C:\..\lib\site-packages\pgzero\ptext.py", line 476, in drawbox
fontsize = _fitsize(text, fontname, sysfontname, bold, italic, underline,
File "C:\..\lib\site-packages\pgzero\ptext.py", line 162, in _fitsize
if not fits(a):
File "C:\..\lib\site-packages\pgzero\ptext.py", line 153, in fits
texts = wrap(text, fontname, fontsize, sysfontname,
File "C:\..\lib\site-packages\pgzero\ptext.py", line 109, in wrap
a = text.index(" ", a) if " " in text else len(text)
ValueError: substring not found
```

### System Information
>Pygame 2.0.1
SDL 2.0.14, Python 3.9.2
Windows 10.0.19041.867

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in ptext.py, especially wrap around the reported line 109 and the _fitsize/drawbox call path. Reproduce the failure with the supplied textbox example using text=" ab"; done means the leading-space input no longer raises ValueError and the textbox draws successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
game-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.