The next problem was that the input could be varied in length. There’s maybe 80 characters or 200.
e) I want to display my text properly when it’s too long to fit into one line
I thought about the problem and thought about different design decisions. I could either shrink my font size or break my text into lines. I liked the latter better. So I looked up solutions. Gladly, Python provides textwrap which wraps text for you given a maximum length. Problem solved.
It worked so far however, it was rather uninteresting because the text was in the same size and same location. The next step was to write a function which varies the font size (easy) and finds locations which are interesting.
f) I want interesting locations for the text
My first attempt was to use the golden ratio. Still only one location. So I started to do a bit more. I thought about it and thought that it could vary by a certain degree but not too much. I wrote something like
>>> randint(2,4) / 9.0
After I read it, it reminded me of bootstrap’s grid layout. So I basically created a simple grid for the inspirational quotes.
Now a new problem appeared.
g) Text shouldn’t appear outside of the picture
When text was long or my available grid was pretty small the text would leave the bounds. Not ideal. A good thing about the text is that I works from left to right and from up to down, i.e. I only have to check the most far right text and the most far down text against the image dimensions.
Here I made a bit lazy but imho interesting solution. In case the text gets out-of-bounds I just let the function generate a new one. It basically is a function like that:
image = generate()
if not out_of_bounds():
The use of recursion here is pretty neat because it makes the function so simple and in my opinion quite logical. What can happen of course that it runs forever. In my test cases (even pretty long text) it worked pretty fast but that’s a real possibility. However, it was a problem at first.
Part 3 tomorrow