1 /*
2  **********************************************************************
3  *   Copyright (C) 2003, International Business Machines
4  *   Corporation and others.  All Rights Reserved.
5  **********************************************************************
6  */
7 
8 #include "layout/LETypes.h"
9 #include "layout/LEFontInstance.h"
10 
11 #include "unicode/locid.h"
12 
13 #include "layout/RunArrays.h"
14 
15 U_NAMESPACE_BEGIN
16 
17 const char RunArray::fgClassID = 0;
18 
RunArray(le_int32 initialCapacity)19 RunArray::RunArray(le_int32 initialCapacity)
20     : fClientArrays(FALSE), fLimits(NULL), fCount(0), fCapacity(initialCapacity)
21 {
22     if (initialCapacity > 0) {
23         fLimits = LE_NEW_ARRAY(le_int32, fCapacity);
24     }
25 }
26 
~RunArray()27 RunArray::~RunArray()
28 {
29     if (! fClientArrays) {
30         LE_DELETE_ARRAY(fLimits);
31         fLimits = NULL;
32     }
33 }
34 
ensureCapacity()35 le_int32 RunArray::ensureCapacity()
36 {
37     if (fCount >= fCapacity) {
38         if (fCapacity == 0) {
39             fCapacity = INITIAL_CAPACITY;
40             init(fCapacity);
41         } else {
42             fCapacity += (fCapacity < CAPACITY_GROW_LIMIT ? fCapacity : CAPACITY_GROW_LIMIT);
43             grow(fCapacity);
44         }
45     }
46 
47     return fCount++;
48 }
49 
init(le_int32 capacity)50 void RunArray::init(le_int32 capacity)
51 {
52     fLimits = LE_NEW_ARRAY(le_int32, capacity);
53 }
54 
grow(le_int32 newCapacity)55 void RunArray::grow(le_int32 newCapacity)
56 {
57     fLimits = (le_int32 *) LE_GROW_ARRAY(fLimits, newCapacity);
58 }
59 
add(le_int32 limit)60 le_int32 RunArray::add(le_int32 limit)
61 {
62     if (fClientArrays) {
63         return -1;
64     }
65 
66     le_int32  index  = ensureCapacity();
67     le_int32 *limits = (le_int32 *) fLimits;
68 
69     limits[index] = limit;
70 
71     return index;
72 }
73 
74 const char FontRuns::fgClassID = 0;
75 
FontRuns(le_int32 initialCapacity)76 FontRuns::FontRuns(le_int32 initialCapacity)
77     : RunArray(initialCapacity), fFonts(NULL)
78 {
79     if (initialCapacity > 0) {
80         fFonts = LE_NEW_ARRAY(const LEFontInstance *, initialCapacity);
81     }
82 }
83 
~FontRuns()84 FontRuns::~FontRuns()
85 {
86     if (! fClientArrays) {
87         LE_DELETE_ARRAY(fFonts);
88         fFonts = NULL;
89     }
90 }
91 
init(le_int32 capacity)92 void FontRuns::init(le_int32 capacity)
93 {
94     RunArray::init(capacity);
95     fFonts = LE_NEW_ARRAY(const LEFontInstance *, capacity);
96 }
97 
grow(le_int32 capacity)98 void FontRuns::grow(le_int32 capacity)
99 {
100     RunArray::grow(capacity);
101     fFonts = (const LEFontInstance **) LE_GROW_ARRAY(fFonts, capacity);
102 }
103 
add(const LEFontInstance * font,le_int32 limit)104 le_int32 FontRuns::add(const LEFontInstance *font, le_int32 limit)
105 {
106     le_int32 index = RunArray::add(limit);
107 
108     if (index >= 0) {
109         LEFontInstance **fonts = (LEFontInstance **) fFonts;
110 
111         fonts[index] = (LEFontInstance *) font;
112     }
113 
114     return index;
115 }
116 
getFont(le_int32 run) const117 const LEFontInstance *FontRuns::getFont(le_int32 run) const
118 {
119     if (run < 0 || run >= getCount()) {
120         return NULL;
121     }
122 
123     return fFonts[run];
124 }
125 
126 const char LocaleRuns::fgClassID = 0;
127 
LocaleRuns(le_int32 initialCapacity)128 LocaleRuns::LocaleRuns(le_int32 initialCapacity)
129     : RunArray(initialCapacity), fLocales(NULL)
130 {
131     if (initialCapacity > 0) {
132         fLocales = LE_NEW_ARRAY(const Locale *, initialCapacity);
133     }
134 }
135 
~LocaleRuns()136 LocaleRuns::~LocaleRuns()
137 {
138     if (! fClientArrays) {
139         LE_DELETE_ARRAY(fLocales);
140         fLocales = NULL;
141     }
142 }
143 
init(le_int32 capacity)144 void LocaleRuns::init(le_int32 capacity)
145 {
146     RunArray::init(capacity);
147     fLocales = LE_NEW_ARRAY(const Locale *, capacity);
148 }
149 
grow(le_int32 capacity)150 void LocaleRuns::grow(le_int32 capacity)
151 {
152     RunArray::grow(capacity);
153     fLocales = (const Locale **) LE_GROW_ARRAY(fLocales, capacity);
154 }
155 
add(const Locale * locale,le_int32 limit)156 le_int32 LocaleRuns::add(const Locale *locale, le_int32 limit)
157 {
158     le_int32 index = RunArray::add(limit);
159 
160     if (index >= 0) {
161         Locale **locales = (Locale **) fLocales;
162 
163         locales[index] = (Locale *) locale;
164     }
165 
166     return index;
167 }
168 
getLocale(le_int32 run) const169 const Locale *LocaleRuns::getLocale(le_int32 run) const
170 {
171     if (run < 0 || run >= getCount()) {
172         return NULL;
173     }
174 
175     return fLocales[run];
176 }
177 
178 const char ValueRuns::fgClassID = 0;
179 
ValueRuns(le_int32 initialCapacity)180 ValueRuns::ValueRuns(le_int32 initialCapacity)
181     : RunArray(initialCapacity), fValues(NULL)
182 {
183     if (initialCapacity > 0) {
184         fValues = LE_NEW_ARRAY(le_int32, initialCapacity);
185     }
186 }
187 
~ValueRuns()188 ValueRuns::~ValueRuns()
189 {
190     if (! fClientArrays) {
191         LE_DELETE_ARRAY(fValues);
192         fValues = NULL;
193     }
194 }
195 
init(le_int32 capacity)196 void ValueRuns::init(le_int32 capacity)
197 {
198     RunArray::init(capacity);
199     fValues = LE_NEW_ARRAY(le_int32, capacity);
200 }
201 
grow(le_int32 capacity)202 void ValueRuns::grow(le_int32 capacity)
203 {
204     RunArray::grow(capacity);
205     fValues = (const le_int32 *) LE_GROW_ARRAY(fValues, capacity);
206 }
207 
add(le_int32 value,le_int32 limit)208 le_int32 ValueRuns::add(le_int32 value, le_int32 limit)
209 {
210     le_int32 index = RunArray::add(limit);
211 
212     if (index >= 0) {
213         le_int32 *values = (le_int32 *) fValues;
214 
215         values[index] = value;
216     }
217 
218     return index;
219 }
220 
getValue(le_int32 run) const221 le_int32 ValueRuns::getValue(le_int32 run) const
222 {
223     if (run < 0 || run >= getCount()) {
224         return -1;
225     }
226 
227     return fValues[run];
228 }
229 
230 U_NAMESPACE_END
231