summaryrefslogtreecommitdiffstats
path: root/lib/kotext/kohyphen/kohyphen.h
blob: af3bef41196f239c5e943568a001f72beb1600db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/******************************************************************************
 *   Copyright (C) 2002 by Alexander Dymo <cloudtemple@mskat.net>             *
 *   Copyright (C) 2002-2003 by Lukas Tinkl <lukas.tinkl@suse.cz>             *
 *   Copyright (C) 2003 David Faure <faure@kde.org>                           *
 *                                                                            *
 *   This program is free software; you can redistribute it and/or modify     *
 *   it under the terms of the GNU Library General Public License as          *
 *   published by the Free Software Foundation; either version 2 of the       *
 *   License, or (at your option) any later version.                          *
 *                                                                            *
 *   This library is distributed in the hope that it will be useful,          *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 *  Library General Public License for more details.                          *
 *                                                                            *
 *  You should have received a copy of the GNU Library General Public License *
 *  along with this library; see the file COPYING.LIB.  If not, write to      *
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.                                               *
 ******************************************************************************/

#ifndef KOHYPHEN_H
#define KOHYPHEN_H

#include <tqmap.h>
#include <tqstring.h>
#include "hyphen.h"

class KoHyphenatorException{
public:
        KoHyphenatorException(const TQString& MessageText): messageText(MessageText) {}
        TQString message() const { return messageText; }
private:
        TQString messageText;
};

/**
 * @short The KoHyphenator class provides an interface to the libhnj hyphenation library
 * @author Alexander Dymo (cloudtemple@mksat.net)
 * @author Lukas Tinkl (lukas.tinkl@suse.cz)
 *
 * The KoHyphenator class provides an interface to the libhnj hyphenation library.
 *
 * Libhnj library was written for TeX and adopted to use with OpenOffice.
 * This library tries to adopt it for KDE.
 *
 * It handles the hyphenation dictionary loading for the specified language.
 * TQString -> char* conversion is done by using of settings in
 * dicts.xml file in datadir/koffice/hyphdicts (dictionaries are also located
 * there).
 *
 * The @ref hyphenate() functions returns TQString containing hyphenation
 * chars (0xad) or char* in format of hnj_hyphen_hyphenate() function from
 * libhnj library.
 */
class KoHyphenator{
public:
        /**
         * Returns the single KoHyphenator instance (singleton pattern)
         * Beware that this might throw an exception in case of an installation problem!
         * Catch KoHyphenatorExceptions!
         */
        static KoHyphenator* self();

        ~KoHyphenator();

        /**
         * Checks if the letter in position pos is placed before the hyphen.
         *
         * Can be used to check if the line break at given position
         * should be forced and automatic hyphen added.
         */
        bool checkHyphenPos(const TQString& str, int pos, const TQString& lang) const;

        /**
         * Returns the pointer to the string in hnj_hyphen_hyphenate() format
         * (that is hyphenation function from underlying libhnj library).
         *
         * The string is array of integer numbers. Each odd number marks
         * that hyphen can be added after the character in the position
         * of that number. The returned string must be deleted with "delete[] x;"
         *
         * For example, for the string "example" the returning value is "01224400".
         *
         * @param str String to be hyphenated.
         *
         * @param lang Language for the hyphenation dictionary to be loaded.
         * Language: two chars containing the ISO 639-1 code
         * (for example "en", "uk", etc.) (could be lang_COUNTRY as well).
        */
        char *hyphens(const TQString& str, const TQString& lang) const;

        /**
         * Hyphenates the string str and returns the string with
         * hyphenation marks in it.
         *
         * @param str String to be hyphenated.
         *
         * @param lang Language for the hyphenation dictionary to be loaded.
         * Language: two chars containing the ISO 639-1 code
         * (for example "en", "uk", etc.) (could be lang_COUNTRY as well).
         */
        TQString hyphenate(const TQString& str, const TQString& lang) const;

private:
        /**
         * @return the encoding of dictionary for the language @p lang.
         */
        TQTextCodec* codecForLang(const TQString& lang) const;

        KoHyphenator();
        HyphenDict *dict(const TQString &lang) const;

        TQMap<TQString, HyphenDict*> dicts;
        struct EncodingStruct {
            EncodingStruct() // for TQMap
                : encoding(), codec(0L) {}
            EncodingStruct(const TQCString& _encoding)
                : encoding(_encoding), codec(0L) {}
            TQCString encoding;
            TQTextCodec* codec;
        };
        typedef TQMap<TQString, EncodingStruct> EncodingMap;
        mutable EncodingMap encodings; // key is the language code

        static KoHyphenator* s_self;
};

#endif