Reputation: 23
I'm using the Adafruit_ST7735 (with the Adafruit_GFX) library to display stuff on my 1.8 TFT display. When I set the tft.setTextWrap(false); it does wrap the text but it doesn't care about words. For example, it wraps it like this:
I like to play baske
tball and I really lik
e to play compute
r games
And I need to make it look like this:
I like to play bask-
etball and I really
like to play comp-
uter games
Short words put on the next line but longer words split into two lines connected with a - would allow me to display much more text than putting each word on a new line. My main struggle with this is that the characters are coming one by one in an SD manner like this:
File myFile = SD.open(file_name);
if (myFile) {
while (myFile.available() > myFile.size() - 300) {
tft.write(myFile.read());
}
myFile.close();
} else {
tft.print("Error opening file.");
}
How would I go about writing such a word processor for the incoming characters so short words (i.e containing less or equal to 5 characters) get transferred on the next line and longer words (i.e containing more than 5 characters) get cut with a - and one part is on one line and the other is on the next line (like the last example)?
Upvotes: 0
Views: 578
Reputation: 1
The simplest hyphenation algorithm (assuming school English) is to break a word at the last consonant* which will fit on the line. But that requires multiple steps:
I have denoted lists with asterisks* above. Punctuation: space, hyphen, apostrophe, quote (can you escape quotes cleanly?), general punctuation Consonant: easily listed
The quick br-
own fox jump-
ed over the sl-
ow lazy dog.
Four sco-
re and s-
even y-
ears ago
our for-
efathers
set
forth on
this new
contin-
ent
Upvotes: 0
Reputation: 1974
You would change
tft.write(myFile.read());
to read into a intermediate string, where you do the hyphenation.
If the memory on your Arduino is not enough for a full hyphenation libary
Upvotes: 0
Reputation: 9422
This problem is called hyphenation and it is not trivial. Text editors like MS Word also have this feature, see https://practicaltypography.com/hyphenation.html
There exist algorithms for hyphenation (https://en.wikipedia.org/wiki/Hyphenation_algorithm), e.g. Knuth-Liang algorithm, Donald Knuth wrote the famous TeX (https://en.wikipedia.org/wiki/TeX)
There is libraries in C like https://github.com/hunspell/hyphen that are also very complex. Possibly search the net with search word 'hyphenation' if you find an easier solution. The problem in general is complex (syllable detection,...)
i think the easiest solution is to skip hyphenation completely and use whitespaces to separate the words (in a sentence between any words are whitespaces...)
Upvotes: 1