word wrap - part 1 and 2

This commit is contained in:
Fournier Gabriel
2020-11-21 19:29:46 +01:00
parent 5b21c57e4c
commit 3e2b5178ed
20 changed files with 254 additions and 15 deletions

View File

@@ -5,6 +5,8 @@ app_sreader_src = $(addprefix apps/reader/,\
app.cpp \
list_book_controller.cpp \
utility.cpp \
read_book_controller \
word_wrap_view.cpp \
)
apps_src += $(app_sreader_src)

View File

@@ -30,8 +30,9 @@ App::Descriptor * App::Snapshot::descriptor() {
App::App(Snapshot * snapshot) :
::App(snapshot, &m_listBookController),
m_listBookController(nullptr)
::App(snapshot, &m_stackViewController),
m_listBookController(&m_stackViewController),
m_stackViewController(nullptr, &m_listBookController)
{
}

View File

@@ -22,6 +22,7 @@ public:
private:
App(Snapshot * snapshot);
ListBookController m_listBookController;
StackViewController m_stackViewController;
};
}

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay = "Keine Dateien zum Anzeigen!"

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay = "No file to display!"

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay ="No hay archivos para mostrar!"

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay = "Aucun fichier à afficher!"

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay ="Nincs megjeleníthető fájl"

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay ="essun file da visualizzare"

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay ="Geen bestanden om weer te geven"

View File

@@ -1,2 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
ReaderAppCapital = "READER"
NoFileToDisplay ="Nenhum arquivo para exibir"

View File

@@ -1,5 +1,7 @@
#include "list_book_controller.h"
#include "utility.h"
#include "apps/i18n.h"
namespace reader
{
@@ -10,7 +12,8 @@ View* ListBookController::view()
ListBookController::ListBookController(Responder * parentResponder):
ViewController(parentResponder),
m_tableView(this, this)
m_tableView(this, this, this),
m_readBookController(this)
{
m_nbFiles = filesWithExtension(".txt", m_files, NB_FILES);
}
@@ -43,4 +46,30 @@ void ListBookController::willDisplayCellForIndex(HighlightCell * cell, int index
myTextCell->setMessageFont(KDFont::LargeFont);
}
void ListBookController::didBecomeFirstResponder()
{
if (selectedRow() < 0) {
selectCellAtLocation(0, 0);
}
Container::activeApp()->setFirstResponder(&m_tableView);
if(m_nbFiles == 0)
{
Container::activeApp()->displayWarning(I18n::Message::NoFileToDisplay);
}
}
bool ListBookController::handleEvent(Ion::Events::Event event)
{
if (event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right)
{
m_readBookController.setBook(m_files[selectedRow()]);
static_cast<StackViewController*>(parentResponder())->push(&m_readBookController);
Container::activeApp()->setFirstResponder(&m_readBookController);
return true;
}
return false;
}
}

View File

@@ -4,10 +4,12 @@
#include <escher.h>
#include <apps/external/archive.h>
#include "read_book_controller.h"
namespace reader
{
class ListBookController : public ViewController, public SimpleListViewDataSource, public ScrollViewDataSource
class ListBookController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource
{
public:
ListBookController(Responder * parentResponder);
@@ -18,8 +20,10 @@ public:
HighlightCell * reusableCell(int index) override;
int reusableCellCount() const override;
void willDisplayCellForIndex(HighlightCell * cell, int index) override;
void didBecomeFirstResponder() override;
bool handleEvent(Ion::Events::Event event) override;
private:
TableView m_tableView;
SelectableTableView m_tableView;
static const int NB_FILES = 20;
External::Archive::File m_files[NB_FILES];
@@ -27,6 +31,8 @@ private:
static const int NB_CELLS = 6;
MessageTableCell m_cells[NB_CELLS];
ReadBookController m_readBookController;
};
}

18
apps/reader/normalize.py Normal file
View File

@@ -0,0 +1,18 @@
import sys
import unicodedata
import argparse
import io
import shutil
filename = sys.argv[1]
print("Normalization of "+filename)
output = open(filename+".tmp", "wb")
with io.open(filename, "r", encoding='utf-8') as file:
for line in file:
unicodeLine = unicodedata.normalize("NFKD", line)
output.write(unicodeLine.encode("UTF-8"))
output.close()
shutil.move(filename+".tmp",filename)

View File

@@ -0,0 +1,21 @@
#include "read_book_controller.h"
namespace reader
{
ReadBookController::ReadBookController(Responder * parentResponder) :
ViewController(parentResponder)
{
}
View * ReadBookController::view()
{
return &m_readerView;
}
void ReadBookController::setBook(const External::Archive::File& file)
{
m_readerView.setText(reinterpret_cast<const char*>(file.data));
}
}

View File

@@ -0,0 +1,23 @@
#ifndef _READ_BOOK_CONTROLLER_H_
#define _READ_BOOK_CONTROLLER_H_
#include <escher.h>
#include "apps/external/archive.h"
#include "word_wrap_view.h"
namespace reader {
class ReadBookController : public ViewController {
public:
ReadBookController(Responder * parentResponder);
View * view() override;
void setBook(const External::Archive::File& file);
private:
WordWrapTextView m_readerView;
};
}
#endif

View File

@@ -31,6 +31,19 @@ bool stringEndsWith(const char* str, const char* pattern)
return false;
}
void stringNCopy(char* dest, int max, const char* src, int len)
{
while(len>0 && max >1 && *src)
{
*dest = *src;
dest++;
src++;
len--;
max--;
}
*dest=0;
}
#ifdef DEVICE
@@ -54,6 +67,34 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in
}
#else
static void fillFileData(External::Archive::File& file)
{
file.data = nullptr;
file.dataLength = 0;
struct stat info;
if (stat(file.name, &info) != 0)
{
return;
}
unsigned char* content = new unsigned char[info.st_size];
if (content == NULL)
{
return ;
}
FILE *fp = fopen(file.name, "rb");
if (fp == NULL)
{
return ;
}
fread(content, info.st_size, 1, fp);
fclose(fp);
file.data = content;
file.dataLength = info.st_size;
}
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize)
{
dirent *file;
@@ -63,9 +104,10 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in
{
while ((file = readdir(d)) != NULL)
{
if(stringEndsWith(dir->d_name, extension))
if(stringEndsWith(file->d_name, extension))
{
files[nb].name = strdup(file->d_name);//will probably leak
fillFileData(files[nb]);
nb++;
if(nb == filesSize)
break;
@@ -75,7 +117,6 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in
}
return nb;
}
#endif
}

View File

@@ -8,6 +8,7 @@ namespace reader
bool stringEndsWith(const char* str, const char* end);
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize) ;
void stringNCopy(char* dest, int max, const char* src, int len);
}
#endif

View File

@@ -0,0 +1,68 @@
#include "word_wrap_view.h"
#include "utility.h"
#include <iostream>
namespace reader
{
void WordWrapTextView::drawRect(KDContext * ctx, KDRect rect) const
{
ctx->fillRect(KDRect(0, 0, bounds().width(), bounds().height()), m_backgroundColor);
const char * current = text();
const char * startOfWord = current;
const char * endOfWord = UTF8Helper::EndOfWord(startOfWord);
KDPoint textPosition(0, 0);
const int wordMaxLength = 128;
char word[wordMaxLength];
const int spaceWidth = m_font->stringSize(" ").width();
while(*startOfWord != 0)
{
KDSize textSize = m_font->stringSizeUntil(startOfWord, endOfWord);
KDPoint nextTextPosition = KDPoint(textPosition.x()+textSize.width(), textPosition.y());
if(nextTextPosition.x() > m_frame.width())
{
textPosition = KDPoint(0, textPosition.y() + textSize.height());
nextTextPosition = KDPoint(textSize.width(), textPosition.y());
}
stringNCopy(word, wordMaxLength, startOfWord, endOfWord-startOfWord);
ctx->drawString(word, textPosition, m_font, m_textColor, m_backgroundColor);
while(*endOfWord == ' ')
{
nextTextPosition = KDPoint(nextTextPosition.x() + spaceWidth, nextTextPosition.y());
++endOfWord;
}
if(nextTextPosition.x() > m_frame.width())
{
nextTextPosition = KDPoint(0, nextTextPosition.y() + textSize.height());
}
while(*endOfWord == '\n')
{
nextTextPosition = KDPoint(0, nextTextPosition.y() + textSize.height());
++endOfWord;
}
if(nextTextPosition.y() + textSize.height() > m_frame.height())
{
break;
}
textPosition = nextTextPosition;
startOfWord = endOfWord;
endOfWord = UTF8Helper::EndOfWord(startOfWord);
std::cout<<startOfWord<<std::endl;
}
}
}

View File

@@ -0,0 +1,20 @@
#ifndef _WORD_WRAP_VIEW_H_
#define _WORD_WRAP_VIEW_H_
#include <escher.h>
namespace reader
{
class WordWrapTextView : public PointerTextView {
public:
void drawRect(KDContext * ctx, KDRect rect) const override;
protected:
};
}
#endif