24#include <tqtextcodec.h>
28#include <tdeapplication.h>
29#include <kstandarddirs.h>
30#include <kstaticdeleter.h>
39static KStaticDeleter<KoHyphenator> kohyphensd;
44 kohyphensd.setObject( s_self,
new KoHyphenator );
48KoHyphenator::KoHyphenator()
52 TQString path = kapp->dirs()->findResource(
"data",
"koffice/hyphdicts/dicts.xml");
53#ifdef DEBUG_HYPHENATOR
54 kdDebug() << path << endl;
61 throw KoHyphenatorException(
"Could not create KoHyphenator instance." );
64 TQDomNodeList records;
67 for (TQDomNode n = config.firstChild(); !n.isNull(); n = n.nextSibling())
68 if (n.nodeName() ==
"dicts")
70 records = n.childNodes();
71 for (uint i = 0; i < records.count(); i++)
73 TQDomNamedNodeMap attr = records.item(i).attributes();
74 if (attr.contains(
"lang") && attr.contains(
"encoding")) {
75 TQString lang = attr.namedItem(
"lang").nodeValue();
76 TQString encoding = attr.namedItem(
"encoding").nodeValue();
77#ifdef DEBUG_HYPHENATOR
78 kdDebug() <<
"KoHyphenator: found lang=" << lang <<
" encoding=" << encoding << endl;
80 encodings.insert( lang,
81 EncodingStruct( encoding.latin1() ) );
89KoHyphenator::~KoHyphenator()
91 for (TQMap<TQString, HyphenDict*>::iterator it = dicts.begin(); it != dicts.end(); ++it)
94 hnj_hyphen_free((*it));
100 char *x =
new char[str.length()+1];
103 TQTextCodec *codec = codecForLang(lang);
104 hnj_hyphen_hyphenate(dict(lang), (
const char *)(codec->fromUnicode(str)), str.length(), x);
106 catch (KoHyphenatorException &e)
108#ifdef DEBUG_HYPHENATOR
109 kdDebug() << e.message().latin1() << endl;
111 for (uint j = 0; j < str.length(); j++)
113 x[str.length()] =
'\0';
120 char* x =
new char[str.length()+1];
124 TQTextCodec *codec = codecForLang(lang);
125 hnj_hyphen_hyphenate(dict(lang), (
const char *)(codec->fromUnicode(str)), str.length(), x);
127 catch (KoHyphenatorException &e)
129#ifdef DEBUG_HYPHENATOR
130 kdDebug() << e.message() << endl;
139#ifdef DEBUG_HYPHENATOR
140 kdDebug() <<
"loop: i=" << i <<
", j=" << j <<
", x=" << x <<
", res=" << res << endl;
144 res.insert(j+1, TQChar(0xad));
155#ifdef DEBUG_HYPHENATOR
156 kdDebug() <<
"string: " << str << endl;
159 char *hyph =
hyphens(str, lang);
161#ifdef DEBUG_HYPHENATOR
162 kdDebug() <<
"result: " << hyph << endl;
163 kdDebug() <<
"checked position: " << pos << endl;
165 bool ret = ((hyph[pos] % 2) != 0);
170HyphenDict *KoHyphenator::dict(
const TQString &_lang)
const
172 TQString lang( _lang );
174 if (encodings.find(lang) == encodings.end())
176 int underscore = lang.find(
'_');
177 if ( underscore > -1 ) {
178 lang.truncate( underscore );
179 if (encodings.find(lang) == encodings.end())
180 throw KoHyphenatorException( TQString(
"No dictionary for %1").arg(lang) );
183 throw KoHyphenatorException( TQString(
"No dictionary for %1").arg(lang) );
185 if (dicts.find(lang) == dicts.end())
187#ifdef DEBUG_HYPHENATOR
188 kdDebug() <<
"Searching dictionary for '" << lang <<
"' language..." << endl;
190 TQString path = kapp->dirs()->findResource(
"data",
"koffice/hyphdicts/hyph_" + lang +
".dic");
193#ifdef DEBUG_HYPHENATOR
194 kdDebug() <<
"Loading dictionary for '" << lang <<
"' language: path = " << path << endl;
196 const_cast<KoHyphenator*
>(
this)->dicts.insert( lang, hnj_hyphen_load(TQFile::encodeName(path)) );
197 if (dicts.find(lang) == dicts.end())
199#ifdef DEBUG_HYPHENATOR
200 kdDebug() <<
"No dictionary loaded" << endl;
202 throw(KoHyphenatorException( TQString(
"Could not load dictionary for the language: %1").arg(lang) ));
206 throw(KoHyphenatorException( TQString(
"Could not load dictionary for the language: %1").arg(lang) ));
211TQTextCodec* KoHyphenator::codecForLang(
const TQString& lang)
const
213 EncodingMap::Iterator it = encodings.find(lang);
214 if (it == encodings.end())
216 int underscore = lang.find(
'_');
217 if ( underscore > -1 ) {
218 TQString _lang( lang );
219 _lang.truncate( underscore );
220 it = encodings.find(_lang);
223 if (it != encodings.end())
227 (*it).codec = TQTextCodec::codecForName((*it).encoding);
230 return TQTextCodec::codecForMib(106);
The KoHyphenator class provides an interface to the libhnj hyphenation library.
bool checkHyphenPos(const TQString &str, int pos, const TQString &lang) const
Checks if the letter in position pos is placed before the hyphen.
static KoHyphenator * self()
Returns the single KoHyphenator instance (singleton pattern) Beware that this might throw an exceptio...
char * hyphens(const TQString &str, const TQString &lang) const
Returns the pointer to the string in hnj_hyphen_hyphenate() format (that is hyphenation function from...
TQString hyphenate(const TQString &str, const TQString &lang) const
Hyphenates the string str and returns the string with hyphenation marks in it.