How to make a word processor in Arduino (C/C++) where the text is coming character by character?

530 views Asked by At

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)?

3

There are 3 answers

0
ralf htp On BEST ANSWER

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)

Hyphenation in c#

Best word wrap algorithm?

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...)

0
Sebastian On

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

  • Depending on where the text is coming from, and if it is always shown in the same columnns you could do the hyphenation manually in the text file (and fill the lines with spaces) or
  • you could half-automatically mark all positions beforehand, where hyphenation is possible. An algorithm just deletes these markers in the intermediate string or replaces them with newlines
  • there are abridged hyphenation libraries that find not all hyphenation positions, but are much better than no hyphenation
0
AnAppleSnail On

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:

  1. Put letters on line one word (between punctuation*) at a time
  2. Detect character limit of line
  3. Step backwards to either a consonant or punctuation.

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