1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2009-2014, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  n2builder.h
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2009nov25
14 *   created by: Markus W. Scherer
15 */
16 
17 #ifndef __N2BUILDER_H__
18 #define __N2BUILDER_H__
19 
20 #include "unicode/utypes.h"
21 
22 #if !UCONFIG_NO_NORMALIZATION
23 
24 #include "unicode/errorcode.h"
25 #include "unicode/unistr.h"
26 #include "normalizer2impl.h"  // for IX_COUNT
27 #include "toolutil.h"
28 #include "utrie2.h"
29 
30 U_NAMESPACE_BEGIN
31 
32 extern UBool beVerbose, haveCopyright;
33 
34 struct Norm;
35 
36 class BuilderReorderingBuffer;
37 class ExtraDataWriter;
38 
39 class Normalizer2DataBuilder {
40 public:
41     Normalizer2DataBuilder(UErrorCode &errorCode);
42     ~Normalizer2DataBuilder();
43 
44     enum OverrideHandling {
45         OVERRIDE_NONE,
46         OVERRIDE_ANY,
47         OVERRIDE_PREVIOUS
48     };
49 
50     void setOverrideHandling(OverrideHandling oh);
51 
52     enum Optimization {
53         OPTIMIZE_NORMAL,
54         OPTIMIZE_FAST
55     };
56 
setOptimization(Optimization opt)57     void setOptimization(Optimization opt) { optimization=opt; }
58 
59     void setCC(UChar32 c, uint8_t cc);
60     void setOneWayMapping(UChar32 c, const UnicodeString &m);
61     void setRoundTripMapping(UChar32 c, const UnicodeString &m);
62     void removeMapping(UChar32 c);
63 
64     void setUnicodeVersion(const char *v);
65 
66     void writeBinaryFile(const char *filename);
67     void writeCSourceFile(const char *filename);
68 
69 private:
70     friend class CompositionBuilder;
71     friend class Decomposer;
72     friend class ExtraDataWriter;
73     friend class Norm16Writer;
74 
75     // No copy constructor nor assignment operator.
76     Normalizer2DataBuilder(const Normalizer2DataBuilder &other);
77     Normalizer2DataBuilder &operator=(const Normalizer2DataBuilder &other);
78 
79     Norm *allocNorm();
80     Norm *getNorm(UChar32 c);
81     Norm *createNorm(UChar32 c);
82     Norm *checkNormForMapping(Norm *p, UChar32 c);  // check for permitted overrides
83 
84     const Norm &getNormRef(UChar32 c) const;
85     uint8_t getCC(UChar32 c) const;
86     UBool combinesWithCCBetween(const Norm &norm, uint8_t lowCC, uint8_t highCC) const;
87     UChar32 combine(const Norm &norm, UChar32 trail) const;
88 
89     void addComposition(UChar32 start, UChar32 end, uint32_t value);
90     UBool decompose(UChar32 start, UChar32 end, uint32_t value);
91     void reorder(Norm *p, BuilderReorderingBuffer &buffer);
92     UBool hasNoCompBoundaryAfter(BuilderReorderingBuffer &buffer);
93     void setHangulData();
94     int32_t writeMapping(UChar32 c, const Norm *p, UnicodeString &dataString);
95     void writeCompositions(UChar32 c, const Norm *p, UnicodeString &dataString);
96     void writeExtraData(UChar32 c, uint32_t value, ExtraDataWriter &writer);
getCenterNoNoDelta()97     int32_t getCenterNoNoDelta() {
98         return indexes[Normalizer2Impl::IX_MIN_MAYBE_YES]-Normalizer2Impl::MAX_DELTA-1;
99     }
100     void writeNorm16(UChar32 start, UChar32 end, uint32_t value);
101     void processData();
102 
103     UTrie2 *normTrie;
104     UToolMemory *normMem;
105     Norm *norms;
106 
107     int32_t phase;
108     OverrideHandling overrideHandling;
109 
110     Optimization optimization;
111 
112     int32_t indexes[Normalizer2Impl::IX_COUNT];
113     UTrie2 *norm16Trie;
114     int32_t norm16TrieLength;
115     UnicodeString extraData;
116     uint8_t smallFCD[0x100];
117 
118     UVersionInfo unicodeVersion;
119 };
120 
121 U_NAMESPACE_END
122 
123 #endif // #if !UCONFIG_NO_NORMALIZATION
124 
125 #endif  // __N2BUILDER_H__
126