c++textarduinoword-processor

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


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


Solution

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