00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "hyphenator.h"
00024
00025 #include "hyask.h"
00026
00027 #include "scconfig.h"
00028
00029 #include <QRegExp>
00030 #include <QCursor>
00031 #include <QDir>
00032 #include <QCheckBox>
00033
00034 #include <QByteArray>
00035 #include <cstdlib>
00036 #include <string>
00037 #include "scpaths.h"
00038 #include "scribuscore.h"
00039 #include "prefsfile.h"
00040 #include "prefsmanager.h"
00041 #include "text/nlsconfig.h"
00042
00043 Hyphenator::Hyphenator(QWidget* parent, ScribusDoc *dok) : QObject( parent ),
00044 doc(dok),
00045 hdict(0),
00046 useAble(false),
00047 codec(0),
00048 MinWordLen(doc->MinWordLen),
00049 HyCount(doc->HyCount),
00050 Automatic(doc->Automatic),
00051 AutoCheck(doc->AutoCheck)
00052 {
00053
00054 QString pfad = ScPaths::instance().dictDir();
00055 if (ScCore->primaryMainWindow()->Sprachen.contains(doc->Language))
00056 Language = doc->Language;
00057 else
00058 {
00059 Language = PrefsManager::instance()->appPrefs.Language;
00060 doc->Language = Language;
00061 }
00062 pfad += ScCore->primaryMainWindow()->Sprachen[Language];
00063 QFile f(pfad);
00064 if (f.open(QIODevice::ReadOnly))
00065 {
00066 QTextStream st(&f);
00067 QString line;
00068 line = st.readLine();
00069 codec = QTextCodec::codecForName(line.toUtf8());
00070 f.close();
00071 }
00072 else
00073 {
00074 useAble = false;
00075 hdict = NULL;
00076 return;
00077 }
00078 QByteArray fn = pfad.toLocal8Bit();
00079 const char * filename = fn.data();
00080 hdict = hnj_hyphen_load(filename);
00081 useAble = hdict == NULL ? false : true;
00082 rememberedWords.clear();
00083
00084 ignoredWords.clear();
00085 specialWords.clear();
00086 }
00087
00088 Hyphenator::~Hyphenator()
00089 {
00090 hnj_hyphen_free(hdict);
00091 }
00092
00093 void Hyphenator::NewDict(const QString& name)
00094 {
00095 if (!ScCore->primaryMainWindow()->Sprachen.contains(name))
00096 return;
00097
00098 if (Language != name)
00099 {
00100 Language = name;
00101
00102 char *filename = NULL;
00103 QString pfad = ScPaths::instance().dictDir();
00104
00105 if (hdict != NULL)
00106 hnj_hyphen_free(hdict);
00107
00108 pfad += ScCore->primaryMainWindow()->Sprachen[Language];
00109 QFile f(pfad);
00110 if (f.open(QIODevice::ReadOnly))
00111 {
00112 QTextStream st(&f);
00113 QString line;
00114 line = st.readLine();
00115 codec = QTextCodec::codecForName(line.toUtf8());
00116 f.close();
00117 }
00118 else
00119 {
00120 useAble = false;
00121 hdict = NULL;
00122 return;
00123 }
00124 QByteArray fn = pfad.toLocal8Bit();
00125 filename = fn.data();
00126 hdict = hnj_hyphen_load(filename);
00127 useAble = hdict == NULL ? false : true;
00128 }
00129 }
00130
00131 void Hyphenator::slotNewSettings(int Wordlen, bool Autom, bool ACheck, int Num)
00132 {
00133 MinWordLen = Wordlen;
00134 Automatic = Autom;
00135 AutoCheck = ACheck;
00136 HyCount = Num;
00137 doc->MinWordLen = Wordlen;
00138 doc->Automatic = Autom;
00139 doc->AutoCheck = AutoCheck;
00140 doc->HyCount = Num;
00141 }
00142
00143 void Hyphenator::slotHyphenateWord(PageItem* it, const QString& text, int firstC)
00144 {
00145 if ((!useAble))
00146 return;
00147 const char *word;
00148 char *buffer;
00149 const int BORDER = 2;
00150 QByteArray te;
00151
00152
00153 QString found = text;
00154 if (found.contains(SpecialChars::SHYPHEN))
00155 return;
00156
00157 else if (signed(found.length()) >= MinWordLen)
00158 {
00159 NewDict(it->itemText.charStyle(firstC).language());
00160 te = codec->fromUnicode( found );
00161 word = te.data();
00162 buffer = static_cast<char*>(malloc(strlen(word)+BORDER+3));
00163 if (buffer == NULL)
00164 return;
00165 if (!hnj_hyphen_hyphenate(hdict, word, strlen(word), buffer))
00166 {
00167
00168 buffer[strlen(word)] = '\0';
00169 it->itemText.hyphenateWord(firstC, found.length(), buffer);
00170 }
00171 free(buffer);
00172 }
00173 }
00174
00175 void Hyphenator::slotHyphenate(PageItem* it)
00176 {
00177 if ((!useAble) || !(it->asTextFrame()) || (it->itemText.length() == 0))
00178 return;
00179
00180 doc->DoDrawing = false;
00181
00182 const char *word;
00183 char *buffer;
00184 const int BORDER = 2;
00185 QString text = "";
00186 QString buf;
00187 QByteArray te;
00188
00189 int startC = 0;
00190 if (it->HasSel)
00191 {
00192 startC = it->itemText.startOfSelection();
00193 text = it->itemText.text(startC, it->itemText.lengthOfSelection());
00194 }
00195 else {
00196 text = it->itemText.text(0, it->itemText.length());
00197 }
00198
00199 int firstC = 0;
00200 int lastC = 0;
00201 int Ccount = 0;
00202 QString found = "";
00203 QString found2 = "";
00204 rememberedWords.clear();
00205
00206 qApp->changeOverrideCursor(QCursor(Qt::WaitCursor));
00207 QRegExp wordBoundary("\\w");
00208 QRegExp whiteSpace("\\s|\\W|\\d|\\n|\\r|\\t");
00209 while ((firstC+Ccount < signed(text.length())) && (firstC != -1) &&
00210 (lastC < signed(text.length())))
00211 {
00212 firstC = text.indexOf(wordBoundary, firstC+Ccount);
00213 if (firstC < 0)
00214 break;
00215 if (firstC > 0 && text.at(firstC-1) == SpecialChars::SHYPHEN)
00216 {
00217 Ccount = 1;
00218 continue;
00219 }
00220 lastC = text.indexOf(whiteSpace, firstC);
00221 if (lastC < 0)
00222 lastC = signed(text.length());
00223 Ccount = lastC - firstC;
00224 if (lastC < signed(text.length()) && text.at(lastC) == SpecialChars::SHYPHEN)
00225 {
00226 ++Ccount;
00227 continue;
00228 }
00229 if (Ccount > MinWordLen-1)
00230 {
00231 found = text.mid(firstC, Ccount).toLower();
00232 found2 = text.mid(firstC, Ccount);
00233 if (found.contains(SpecialChars::SHYPHEN))
00234 break;
00235
00236 NewDict(it->itemText.charStyle(firstC).language());
00237
00238 te = codec->fromUnicode( found );
00239 word = te.data();
00240 buffer = static_cast<char*>(malloc(strlen(word)+BORDER+3));
00241 if (buffer == NULL)
00242 break;
00243 if (!hnj_hyphen_hyphenate(hdict, word, strlen(word), buffer))
00244 {
00245 int i = 0;
00246 buffer[strlen(word)] = '\0';
00247 bool hasHyphen = false;
00248 for (i = 1; i < found.length()-1; ++i)
00249 {
00250 if(buffer[i] & 1)
00251 {
00252 hasHyphen = true;
00253 break;
00254 }
00255 }
00256 QString outs = "";
00257 QString input = "";
00258 outs += found2[0];
00259 for (i = 1; i < found.length()-1; ++i)
00260 {
00261 outs += found2[i];
00262 if(buffer[i] & 1)
00263 outs += "-";
00264 }
00265 outs += found2.right(1);
00266 input = outs;
00267 if (!ignoredWords.contains(found2))
00268 {
00269 if (!hasHyphen)
00270 it->itemText.hyphenateWord(startC + firstC, found.length(), NULL);
00271 else if (Automatic)
00272 it->itemText.hyphenateWord(startC + firstC, found.length(), buffer);
00273 else
00274 {
00275 if (specialWords.contains(found2))
00276 {
00277 outs = specialWords.value(found2);
00278 uint ii = 0;
00279 for (i = 1; i < outs.length()-1; ++i)
00280 {
00281 QChar cht = outs[i];
00282 if (cht == '-')
00283 buffer[ii] = 1;
00284 else
00285 buffer[ii] = 0;
00286 ii++;
00287 }
00288 }
00289 if (rememberedWords.contains(input))
00290 {
00291 outs = rememberedWords.value(input);
00292 uint ii = 0;
00293 for (i = 1; i < outs.length()-1; ++i)
00294 {
00295 QChar cht = outs[i];
00296 if (cht == '-')
00297 buffer[ii] = 1;
00298 else
00299 buffer[ii] = 0;
00300 ii++;
00301 }
00302 it->itemText.hyphenateWord(firstC, found.length(), buffer);
00303 }
00304 else
00305 {
00306 qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
00307 PrefsContext* prefs = PrefsManager::instance()->prefsFile->getContext("hyhpen_options");
00308 int xpos = prefs->getInt("Xposition", -9999);
00309 int ypos = prefs->getInt("Yposition", -9999);
00310 HyAsk *dia = new HyAsk((QWidget*)parent(), outs);
00311 if ((xpos != -9999) && (ypos != -9999))
00312 dia->move(xpos, ypos);
00313 qApp->processEvents();
00314 if (dia->exec())
00315 {
00316 outs = dia->Wort->text();
00317 uint ii = 0;
00318 for (i = 1; i < outs.length()-1; ++i)
00319 {
00320 QChar cht = outs[i];
00321 if (cht == '-')
00322 buffer[ii] = 1;
00323 else
00324 buffer[ii] = 0;
00325 ii++;
00326 }
00327 if (!rememberedWords.contains(input))
00328 rememberedWords.insert(input, outs);
00329 if (dia->addToIgnoreList->isChecked())
00330 {
00331 if (!ignoredWords.contains(found2))
00332 ignoredWords.insert(found2);
00333 }
00334 if (dia->addToExceptionList->isChecked())
00335 {
00336 if (!specialWords.contains(found2))
00337 specialWords.insert(found2, outs);
00338 }
00339 it->itemText.hyphenateWord(firstC, found.length(), buffer);
00340 }
00341 else
00342 {
00343 free(buffer);
00344 buffer = NULL;
00345 prefs->set("Xposition", dia->xpos);
00346 prefs->set("Yposition", dia->ypos);
00347 delete dia;
00348 break;
00349 }
00350 prefs->set("Xposition", dia->xpos);
00351 prefs->set("Yposition", dia->ypos);
00352 delete dia;
00353 qApp->changeOverrideCursor(QCursor(Qt::WaitCursor));
00354 }
00355 }
00356 }
00357 }
00358 free(buffer);
00359 buffer = NULL;
00360 }
00361 if (Ccount == 0)
00362 Ccount++;
00363 }
00364 qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
00365 doc->DoDrawing = true;
00366 rememberedWords.clear();
00367 }
00368
00369 void Hyphenator::slotDeHyphenate(PageItem* it)
00370 {
00371 if (!(it->asTextFrame()) || (it ->itemText.length() == 0))
00372 return;
00373
00374 uint a = it->itemText.length();
00375 it->itemText.hyphenateWord(0, a, NULL);
00376 qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
00377 doc->DoDrawing = true;
00378 }