Back to index

scribus-ng  1.3.4.dfsg+svn20071115
hyphenator.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008                           hyphenator.cpp  -  description
00009                              -------------------
00010     begin                : Sat Nov 02 08:00:00 CEST 2002
00011     copyright            : (C) 2002 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 #include "hyphenator.h"
00024 #include "hyphenator.moc"
00025 #include "hyask.h"
00026 
00027 #include "scconfig.h"
00028 
00029 #include <qregexp.h>
00030 #include <qcursor.h>
00031 #include <qdir.h>
00032 #include <cstdlib>
00033 #include <string>
00034 #include "scpaths.h"
00035 #include "scribuscore.h"
00036 #include "prefsfile.h"
00037 #include "prefsmanager.h"
00038 #include "text/nlsconfig.h"
00039 
00040 Hyphenator::Hyphenator(QWidget* parent, ScribusDoc *dok)
00041        : QObject( parent, "bu"),
00042        doc(dok),
00043        hdict(0),
00044        useAble(false),
00045        codec(0),
00046        MinWordLen(doc->MinWordLen),
00047        HyCount(doc->HyCount),
00048        Automatic(doc->Automatic),
00049        AutoCheck(doc->AutoCheck)
00050 {
00051               //FIXME:av pick up language from charstyle
00052        QString pfad = ScPaths::instance().libDir();
00053        if (ScCore->primaryMainWindow()->Sprachen.contains(doc->Language))
00054               Language = doc->Language;
00055        else
00056        {
00057               Language = PrefsManager::instance()->appPrefs.Language;
00058               doc->Language = Language;
00059        }
00060        pfad += "dicts/" + ScCore->primaryMainWindow()->Sprachen[Language];
00061        QFile f(pfad);
00062        if (f.open(IO_ReadOnly))
00063        {
00064               QTextStream st(&f);
00065        QString line;
00066        line = st.readLine();
00067               codec = QTextCodec::codecForName(line);
00068               f.close();
00069        }
00070        else
00071        {
00072               useAble = false;
00073               hdict = NULL;
00074               return;
00075        }
00076        QCString fn = pfad.latin1();
00077        const char * filename = fn.data();
00078        hdict = hnj_hyphen_load(filename);
00079        useAble = hdict == NULL ? false : true;
00080 }
00081 
00082 Hyphenator::~Hyphenator()
00083 {
00084        hnj_hyphen_free(hdict);
00085 }
00086 
00087 void Hyphenator::NewDict(const QString& name)
00088 {
00089        if (!ScCore->primaryMainWindow()->Sprachen.contains(name))
00090               return;
00091 
00092        if (Language != name) 
00093        {
00094               Language = name;
00095 
00096               char *filename = NULL;
00097               QString pfad = ScPaths::instance().libDir();
00098               
00099               if (hdict != NULL)
00100                      hnj_hyphen_free(hdict);
00101 
00102               pfad += "dicts/" + ScCore->primaryMainWindow()->Sprachen[Language];
00103               QFile f(pfad);
00104               if (f.open(IO_ReadOnly))
00105               {
00106                      QTextStream st(&f);
00107                      QString line;
00108                      line = st.readLine();
00109                      codec = QTextCodec::codecForName(line);
00110                      f.close();
00111               }
00112               else
00113               {
00114                      useAble = false;
00115                      hdict = NULL;
00116                      return;
00117               }
00118               QCString fn = pfad.latin1();
00119               filename = fn.data();
00120               hdict = hnj_hyphen_load(filename);
00121               useAble = hdict == NULL ? false : true;
00122        }
00123 }
00124 
00125 void Hyphenator::slotNewSettings(int Wordlen, bool Autom, bool ACheck, int Num)
00126 {
00127        MinWordLen = Wordlen;
00128        Automatic = Autom;
00129        AutoCheck = ACheck;
00130        HyCount = Num;
00131        doc->MinWordLen = Wordlen;
00132        doc->Automatic = Autom;
00133        doc->AutoCheck = AutoCheck;
00134        doc->HyCount = Num;
00135 }
00136 
00137 void Hyphenator::slotHyphenateWord(PageItem* it, const QString& text, int firstC)
00138 {
00139        if ((!useAble))//FIXME:av || (!ScMW->Sprachen.contains(it->Language)))
00140               return;
00141        const char *word;
00142        char *buffer;
00143        const int BORDER = 2;
00144        QCString te;
00145 
00146        //uint maxC = it->itemText.length() - 1;
00147        QString found = text;
00148        if (found.contains(SpecialChars::SHYPHEN))
00149               return;
00150        // else if (findException(found, &buffer) it->itemText.hyphenateWord(firstC, found.length(), buffer);
00151        else if (signed(found.length()) >= MinWordLen)
00152        {
00153               NewDict(it->itemText.charStyle(firstC).language());
00154               te = codec->fromUnicode( found );
00155               word = te.data();
00156               buffer = static_cast<char*>(malloc(strlen(word)+BORDER+3));
00157               if (buffer == NULL)
00158                      return;
00159               if (!hnj_hyphen_hyphenate(hdict, word, strlen(word), buffer))
00160               {
00161                      //uint i = 0;
00162                      buffer[strlen(word)] = '\0';
00163                      it->itemText.hyphenateWord(firstC, found.length(), buffer); 
00164               }
00165               free(buffer);
00166        }
00167 }
00168 
00169 void Hyphenator::slotHyphenate(PageItem* it)
00170 {
00171        if ((!useAble) || !(it->asTextFrame()) || (it->itemText.length() == 0))
00172               return;
00173 
00174        doc->DoDrawing = false;
00175 
00176        const char *word;
00177        char *buffer;
00178        const int BORDER = 2;
00179        QString text = "";
00180        QString buf;
00181        QCString te;
00182 
00183        int startC = 0;
00184        if (it->HasSel)
00185        {
00186               startC = it->itemText.startOfSelection();
00187               text = it->itemText.text(startC, it->itemText.lengthOfSelection());
00188        }
00189        else {
00190               text = it->itemText.text(0, it->itemText.length());
00191        }
00192        
00193        int firstC = 0;
00194        int lastC = 0;
00195        int Ccount = 0;
00196        QString found = "";
00197        QString found2 = "";
00198        //uint maxC = it->itemText.length() - 1;
00199        qApp->setOverrideCursor(QCursor(waitCursor), true);
00200        QRegExp wordBoundary("\\w");
00201        QRegExp whiteSpace("\\s|\\W|\\d|\\n|\\r|\\t");
00202        while ((firstC+Ccount < signed(text.length())) && (firstC != -1) && 
00203                      (lastC < signed(text.length())))
00204        {
00205               firstC = text.find(wordBoundary, firstC+Ccount);
00206               if (firstC < 0)
00207                      break;
00208               if (firstC > 0 && text.at(firstC-1) == SpecialChars::SHYPHEN)
00209               {
00210                      Ccount = 1;
00211                      continue;
00212               }
00213               lastC = text.find(whiteSpace, firstC);
00214               if (lastC < 0)
00215                      lastC = signed(text.length());
00216               Ccount = lastC - firstC;
00217               if (lastC < signed(text.length()) && text.at(lastC) == SpecialChars::SHYPHEN)
00218               {
00219                      ++Ccount;
00220                      continue;
00221               }
00222               if (Ccount > MinWordLen-1)
00223               {
00224                      found = text.mid(firstC, Ccount).lower();
00225                      found2 = text.mid(firstC, Ccount);
00226                      if (found.contains(SpecialChars::SHYPHEN))
00227                             break;
00228 
00229                      NewDict(it->itemText.charStyle(firstC).language());
00230 
00231                      te = codec->fromUnicode( found );
00232                      word = te.data();
00233                      /*
00234                      qDebug(QString("hyphenate %1: len %2 and %3 in codec %4")
00235                                .arg(Language)
00236                                .arg(found2.length())
00237                                .arg(strlen(word))
00238                                .arg(codec->name()));
00239                       */
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                             uint 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                             /*
00257                             QString dump(""), dump2("");
00258                             for (i=0; i < strlen(word); ++i) 
00259                             {
00260                                    dump += QChar(word[i]);
00261                                    if ( i < found.length() )
00262                                           dump2 += found[i];
00263                                    if (buffer[i] & 1)
00264                                    {
00265                                           dump += "-";
00266                                           dump2 += "-";
00267                                    }
00268                             }
00269                             qDebug(QString("hy %3+%4: %1 / %2")
00270                                       .arg(dump)
00271                                       .arg(dump2)
00272                                       .arg(startC)
00273                                       .arg(firstC));
00274                              */
00275                             if ( ! hasHyphen ) {
00276                                    it->itemText.hyphenateWord(startC + firstC, found.length(), NULL);
00277                             }
00278                             else if (Automatic) {
00279                                    it->itemText.hyphenateWord(startC + firstC, found.length(), buffer);
00280                             }
00281                             else {
00282                                    QString outs = "";
00283                                    outs += found2[0];
00284                                    for (i = 1; i < found.length()-1; ++i)
00285                                    {
00286                                           outs += found2[i];
00287                                           if(buffer[i] & 1)
00288                                                  outs += "-";
00289                                    }
00290                                    outs += found2.right(1);
00291                                    qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00292                                    PrefsContext* prefs = PrefsManager::instance()->prefsFile->getContext("hyhpen_options");
00293                                    int xpos = prefs->getInt("Xposition", -9999);
00294                                    int ypos = prefs->getInt("Yposition", -9999);
00295                                    HyAsk *dia = new HyAsk((QWidget*)parent(), outs);
00296                                    if ((xpos != -9999) && (ypos != -9999))
00297                                           dia->move(xpos, ypos);
00298                                    qApp->processEvents();
00299                                    if (dia->exec())
00300                                    {
00301                                           outs = dia->Wort->text();
00302                                           uint ii = 0;
00303                                           for (i = 1; i < outs.length()-1; ++i)
00304                                           {
00305                                                  QChar cht = outs[i];
00306                                                  if (cht == "-") 
00307                                                         buffer[ii] = 1;
00308                                                  else
00309                                                         ii++;
00310                                           }
00311                                           it->itemText.hyphenateWord(firstC, found.length(), buffer);
00312                                    }
00313                                    else
00314                                    {
00315                                           free(buffer);
00316                                           buffer = NULL;
00317                                           prefs->set("Xposition", dia->xpos);
00318                                           prefs->set("Yposition", dia->ypos);
00319                                           delete dia;
00320                                           break;
00321                                    }
00322                                    prefs->set("Xposition", dia->xpos);
00323                                    prefs->set("Yposition", dia->ypos);
00324                                    delete dia;
00325                                    qApp->setOverrideCursor(QCursor(waitCursor), true);
00326                             }
00327                      }
00328                      free(buffer);
00329                      buffer = NULL;
00330               }
00331               if (Ccount == 0)
00332                      Ccount++;
00333        }
00334        qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00335        doc->DoDrawing = true;
00336 }
00337 
00338 void Hyphenator::slotDeHyphenate(PageItem* it)
00339 {
00340        if (!(it->asTextFrame()) || (it ->itemText.length() == 0))
00341               return;
00342 
00343        uint a = it->itemText.length();
00344        it->itemText.hyphenateWord(0, a, NULL);
00345        qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00346        doc->DoDrawing = true;
00347 }