1 /** \file hyphman.h
2     \brief AlReader hyphenation manager
3 
4     (c) Alan, http://alreader.kms.ru/
5 
6     Adapted for CREngine by Vadim Lopatin
7 
8     This source code is distributed under the terms of
9     GNU General Public License.
10 
11     See LICENSE file for details.
12 
13 */
14 
15 #ifndef _HYPHEN_
16 #define _HYPHEN_
17 
18 #include "lvtypes.h"
19 #include "lvstream.h"
20 #include "lvhashtable.h"
21 
22 #define WORD_LENGTH   2048
23 //#define MAX_REAL_WORD 24
24 
25 // min value supported by algorithms is 1 (max is arbitrary 10)
26 // 0 means to use the defaults per HyphMethod
27 // if set to >= 1, the values apply to all HyphMethods
28 #define HYPH_MIN_HYPHEN_MIN 0
29 #define HYPH_MAX_HYPHEN_MIN 10
30 // Default for global HyphMan values is 0: use per-HyphMethod defaults
31 #define HYPH_DEFAULT_HYPHEN_MIN 0
32 // Default for per-HyphMethod values (value enforced by algorithms
33 // previously was 2, so let's keep that as the default)
34 #define HYPHMETHOD_DEFAULT_HYPHEN_MIN 2
35 
36 // Don't trust soft-hyphens when using dict or algo methods
37 #define HYPH_DEFAULT_TRUST_SOFT_HYPHENS 0
38 
39 class HyphMethod
40 {
41 protected:
42     lString32 _id;
43     int _left_hyphen_min;
44     int _right_hyphen_min;
45 public:
46     HyphMethod(lString32 id, int leftHyphenMin=HYPHMETHOD_DEFAULT_HYPHEN_MIN, int rightHyphenMin=HYPHMETHOD_DEFAULT_HYPHEN_MIN)
_id(id)47         : _id(id)
48         , _left_hyphen_min(leftHyphenMin)
49         , _right_hyphen_min(rightHyphenMin)
50         { }
getId()51     lString32 getId() { return _id; }
52     virtual bool hyphenate( const lChar32 * str, int len, lUInt16 * widths, lUInt8 * flags, lUInt16 hyphCharWidth, lUInt16 maxWidth, size_t flagSize=1 ) = 0;
~HyphMethod()53     virtual ~HyphMethod() { }
getCount()54     virtual lUInt32 getCount() { return 0; }
getSize()55     virtual lUInt32 getSize() { return 0; }
getLeftHyphenMin()56     virtual int getLeftHyphenMin() { return _left_hyphen_min; }
getRightHyphenMin()57     virtual int getRightHyphenMin() { return _right_hyphen_min; }
58 };
59 
60 
61 enum HyphDictType
62 {
63 	HDT_NONE,      // disable hyphenation
64 	HDT_ALGORITHM, // universal
65 	HDT_SOFTHYPHENS, // from soft hyphens in text
66 	HDT_DICT_ALAN, // tex/alreader
67     HDT_DICT_TEX   // tex/fbreader
68 };
69 
70 class HyphDictionary
71 {
72 	HyphDictType _type;
73 	lString32 _title;
74 	lString32 _id;
75 	lString32 _filename;
76 public:
HyphDictionary(HyphDictType type,lString32 title,lString32 id,lString32 filename)77 	HyphDictionary( HyphDictType type, lString32 title, lString32 id, lString32 filename )
78 		: _type(type), _title(title), _id( id ), _filename( filename ) { }
getType()79 	HyphDictType getType() const { return _type; }
getTitle()80 	lString32 getTitle() const { return _title; }
getId()81 	lString32 getId() const { return _id; }
getFilename()82 	lString32 getFilename() const { return _filename; }
83 	bool activate();
getHash()84 	virtual lUInt32 getHash() const { return getTitle().getHash(); }
~HyphDictionary()85     virtual ~HyphDictionary() { }
86 };
87 
88 #define HYPH_DICT_ID_NONE U"@none"
89 #define HYPH_DICT_ID_ALGORITHM U"@algorithm"
90 #define HYPH_DICT_ID_SOFTHYPHENS U"@softhyphens"
91 #define HYPH_DICT_ID_DICTIONARY U"@dictionary"
92 
93 class HyphDictionaryList
94 {
95 	LVPtrVector<HyphDictionary> _list;
96 	void addDefault();
97 public:
add(HyphDictionary * dict)98     void add(HyphDictionary * dict) { _list.add(dict); }
length()99 	int length() { return _list.length(); }
get(int index)100 	HyphDictionary * get( int index ) { return (index>=0 && index<+_list.length()) ? _list[index] : NULL; }
HyphDictionaryList()101 	HyphDictionaryList() { addDefault(); }
102     bool open(lString32 hyphDirectory, bool clear = true);
103 	HyphDictionary * find( const lString32& id );
104 	bool activate( lString32 id );
105 };
106 
107 #define DEF_HYPHENATION_DICT "English_US.pattern"
108 // We'll be loading English_US.pattern even if non-english users
109 // may never use it, but it's a bit tedious not going with it.
110 // It might use around 1M of memory, but it will avoid re-rendering
111 // the document if the book does not contain any language tag, and
112 // we end up going with it anyway.
113 
114 class TexHyph;
115 class AlgoHyph;
116 class SoftHyphensHyph;
117 
118 class HyphDataLoader
119 {
120 public:
HyphDataLoader()121     HyphDataLoader() {}
~HyphDataLoader()122     virtual ~HyphDataLoader() {}
123 	virtual LVStreamRef loadData(lString32 id) = 0;
124 };
125 
126 /// hyphenation manager
127 class HyphMan
128 {
129     friend class HyphDictionary;
130     friend class TexHyph;
131     friend class AlgoHyph;
132     friend class SoftHyphensHyph;
133     // Obsolete: now fetched from TextLangMan main lang TextLangCfg
134     // static HyphMethod * _method;
135     // static HyphDictionary * _selectedDictionary;
136     static HyphDictionaryList * _dictList; // available hyph dict files (+ none/algo/softhyphens)
137     static LVHashTable<lString32, HyphMethod*> _loaded_hyph_methods; // methods with loaded dictionaries
138     static HyphDataLoader* _dataLoader;
139     static int _LeftHyphenMin;
140     static int _RightHyphenMin;
141     static int _TrustSoftHyphens;
142 public:
143     static void uninit();
144     static bool initDictionaries(lString32 dir, bool clear = true);
getDictList()145     static HyphDictionaryList * getDictList() { return _dictList; }
146     static bool addDictionaryItem(HyphDictionary* dict);
147     static void setDataLoader(HyphDataLoader* loader);
activateDictionary(lString32 id)148     static bool activateDictionary( lString32 id ) { return _dictList->activate(id); }
149     static HyphDictionary * getSelectedDictionary(); // was: { return _selectedDictionary; }
getLeftHyphenMin()150     static int getLeftHyphenMin() { return _LeftHyphenMin; }
getRightHyphenMin()151     static int getRightHyphenMin() { return _RightHyphenMin; }
152     static bool setLeftHyphenMin( int left_hyphen_min );
153     static bool setRightHyphenMin( int right_hyphen_min );
getTrustSoftHyphens()154     static int getTrustSoftHyphens() { return _TrustSoftHyphens; }
155     static bool setTrustSoftHyphens( int trust_soft_hyphen );
156     static bool isEnabled();
157     static HyphMethod * getHyphMethodForDictionary( lString32 id, int leftHyphenMin=HYPHMETHOD_DEFAULT_HYPHEN_MIN,
158                                                         int rightHyphenMin=HYPHMETHOD_DEFAULT_HYPHEN_MIN );
159 
160     HyphMan();
161     ~HyphMan();
162 
163     static bool hyphenate( const lChar32 * str, int len, lUInt16 * widths, lUInt8 * flags, lUInt16 hyphCharWidth, lUInt16 maxWidth, size_t flagSize=1 );
164     /* Obsolete:
165     inline static bool hyphenate( const lChar32 * str, int len, lUInt16 * widths, lUInt8 * flags, lUInt16 hyphCharWidth, lUInt16 maxWidth, size_t flagSize=1 )
166     {
167         return _method->hyphenate( str, len, widths, flags, hyphCharWidth, maxWidth, flagSize );
168     }
169     */
170 };
171 
172 #endif
173