1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
5 * (C) Copyright IBM Corp. 1998-2013 - All Rights Reserved
9 #include "layout/LETypes.h"
10 #include "layout/loengine.h"
11 #include "layout/plruns.h"
13 #include "unicode/locid.h"
15 #include "layout/LayoutEngine.h"
16 #include "layout/RunArrays.h"
20 U_CAPI pl_fontRuns * U_EXPORT2
21 pl_openFontRuns(const le_font **fonts,
22 const le_int32 *limits,
25 return (pl_fontRuns *) new FontRuns((const LEFontInstance **) fonts, limits, count);
28 U_CAPI pl_fontRuns * U_EXPORT2
29 pl_openEmptyFontRuns(le_int32 initialCapacity)
31 return (pl_fontRuns *) new FontRuns(initialCapacity);
35 pl_closeFontRuns(pl_fontRuns *fontRuns)
37 FontRuns *fr = (FontRuns *) fontRuns;
42 U_CAPI le_int32 U_EXPORT2
43 pl_getFontRunCount(const pl_fontRuns *fontRuns)
45 const FontRuns *fr = (const FontRuns *) fontRuns;
51 return fr->getCount();
55 pl_resetFontRuns(pl_fontRuns *fontRuns)
57 FontRuns *fr = (FontRuns *) fontRuns;
64 U_CAPI le_int32 U_EXPORT2
65 pl_getFontRunLastLimit(const pl_fontRuns *fontRuns)
67 const FontRuns *fr = (const FontRuns *) fontRuns;
73 return fr->getLimit();
76 U_CAPI le_int32 U_EXPORT2
77 pl_getFontRunLimit(const pl_fontRuns *fontRuns,
80 const FontRuns *fr = (const FontRuns *) fontRuns;
86 return fr->getLimit(run);
89 U_CAPI const le_font * U_EXPORT2
90 pl_getFontRunFont(const pl_fontRuns *fontRuns,
93 const FontRuns *fr = (const FontRuns *) fontRuns;
99 return (const le_font *) fr->getFont(run);
102 U_CAPI le_int32 U_EXPORT2
103 pl_addFontRun(pl_fontRuns *fontRuns,
107 FontRuns *fr = (FontRuns *) fontRuns;
113 return fr->add((const LEFontInstance *) font, limit);
116 U_CAPI pl_valueRuns * U_EXPORT2
117 pl_openValueRuns(const le_int32 *values,
118 const le_int32 *limits,
121 return (pl_valueRuns *) new ValueRuns(values, limits, count);
124 U_CAPI pl_valueRuns * U_EXPORT2
125 pl_openEmptyValueRuns(le_int32 initialCapacity)
127 return (pl_valueRuns *) new ValueRuns(initialCapacity);
130 U_CAPI void U_EXPORT2
131 pl_closeValueRuns(pl_valueRuns *valueRuns)
133 ValueRuns *vr = (ValueRuns *) valueRuns;
138 U_CAPI le_int32 U_EXPORT2
139 pl_getValueRunCount(const pl_valueRuns *valueRuns)
141 const ValueRuns *vr = (const ValueRuns *) valueRuns;
147 return vr->getCount();
150 U_CAPI void U_EXPORT2
151 pl_resetValueRuns(pl_valueRuns *valueRuns)
153 ValueRuns *vr = (ValueRuns *) valueRuns;
160 U_CAPI le_int32 U_EXPORT2
161 pl_getValueRunLastLimit(const pl_valueRuns *valueRuns)
163 const ValueRuns *vr = (const ValueRuns *) valueRuns;
169 return vr->getLimit();
172 U_CAPI le_int32 U_EXPORT2
173 pl_getValueRunLimit(const pl_valueRuns *valueRuns,
176 const ValueRuns *vr = (const ValueRuns *) valueRuns;
182 return vr->getLimit(run);
185 U_CAPI le_int32 U_EXPORT2
186 pl_getValueRunValue(const pl_valueRuns *valueRuns,
189 const ValueRuns *vr = (const ValueRuns *) valueRuns;
195 return vr->getValue(run);
198 U_CAPI le_int32 U_EXPORT2
199 pl_addValueRun(pl_valueRuns *valueRuns,
203 ValueRuns *vr = (ValueRuns *) valueRuns;
209 return vr->add(value, limit);
213 class ULocRuns : public LocaleRuns
217 * Construct a <code>LocaleRuns</code> object from pre-existing arrays of locales
220 * @param locales is the address of an array of locale name strings. This array,
221 * and the <code>Locale</code> objects to which it points, must remain valid until
222 * the <code>LocaleRuns</code> object is destroyed.
224 * @param limits is the address of an array of limit indices. This array must remain valid until the
225 * <code>LocaleRuns</code> object is destroyed.
227 * @param count is the number of entries in the two arrays.
231 ULocRuns(const char **locales, const le_int32 *limits, le_int32 count);
234 * Construct an empty <code>LoIDRuns</code> object. Clients can add locale and limit
235 * indices arrays using the <code>add</code> method.
237 * @param initialCapacity is the initial size of the locale and limit indices arrays. If
238 * this value is zero, no arrays will be allocated.
244 ULocRuns(le_int32 initialCapacity);
247 * The destructor; virtual so that subclass destructors are invoked as well.
254 * Get the name of the locale assoicated with the given run
255 * of text. Use <code>RunArray::getLimit(run)</code> to get the corresponding
258 * @param run is the index into the font and limit indices arrays.
260 * @return the locale name associated with the given text run.
262 * @see RunArray::getLimit
266 const char *getLocaleName(le_int32 run) const;
269 * Add a <code>Locale</code> and limit index pair to the data arrays and return
270 * the run index where the data was stored. This method calls
271 * <code>RunArray::add(limit)</code> which will create or grow the arrays as needed.
273 * If the <code>ULocRuns</code> object was created with a client-supplied
274 * locale and limit indices arrays, this method will return a run index of -1.
276 * Subclasses should not override this method. Rather they should provide a new <code>add</code>
277 * method which takes a locale name and a limit index along with whatever other data they implement.
278 * The new <code>add</code> method should first call this method to grow the font and limit indices
279 * arrays, and use the returned run index to store data their own arrays.
281 * @param locale is the name of the locale to add. This object must remain valid
282 * until the <code>ULocRuns</code> object is destroyed.
284 * @param limit is the limit index to add
286 * @return the run index where the locale and limit index were stored, or -1 if the data cannot be stored.
290 le_int32 add(const char *locale, le_int32 limit);
293 * ICU "poor man's RTTI", returns a UClassID for this class.
297 static inline UClassID getStaticClassID();
300 * ICU "poor man's RTTI", returns a UClassID for the actual class.
304 virtual inline UClassID getDynamicClassID() const;
307 virtual void init(le_int32 capacity);
308 virtual void grow(le_int32 capacity);
313 inline ULocRuns(const ULocRuns &other);
314 inline ULocRuns &operator=(const ULocRuns & /*other*/) { return *this; };
315 const char **fLocaleNames;
318 inline ULocRuns::ULocRuns()
319 : LocaleRuns(0), fLocaleNames(NULL)
321 // nothing else to do...
324 inline ULocRuns::ULocRuns(const ULocRuns & /*other*/)
325 : LocaleRuns(0), fLocaleNames(NULL)
327 // nothing else to do...
330 static const Locale **getLocales(const char **localeNames, le_int32 count)
332 Locale **locales = LE_NEW_ARRAY(Locale *, count);
334 for (int i = 0; i < count; i += 1) {
335 locales[i] = new Locale(Locale::createFromName(localeNames[i]));
338 return (const Locale **) locales;
341 ULocRuns::ULocRuns(const char **locales, const le_int32 *limits, le_int32 count)
342 : LocaleRuns(getLocales(locales, count), limits, count), fLocaleNames(locales)
344 // nothing else to do...
347 ULocRuns::ULocRuns(le_int32 initialCapacity)
348 : LocaleRuns(initialCapacity), fLocaleNames(NULL)
350 if(initialCapacity > 0) {
351 fLocaleNames = LE_NEW_ARRAY(const char *, initialCapacity);
355 ULocRuns::~ULocRuns()
357 le_int32 count = getCount();
359 for(int i = 0; i < count; i += 1) {
364 LE_DELETE_ARRAY(fLocales);
367 LE_DELETE_ARRAY(fLocaleNames);
372 void ULocRuns::init(le_int32 capacity)
374 LocaleRuns::init(capacity);
375 fLocaleNames = LE_NEW_ARRAY(const char *, capacity);
378 void ULocRuns::grow(le_int32 capacity)
380 LocaleRuns::grow(capacity);
381 fLocaleNames = (const char **) LE_GROW_ARRAY(fLocaleNames, capacity);
384 le_int32 ULocRuns::add(const char *locale, le_int32 limit)
386 Locale *loc = new Locale(Locale::createFromName(locale));
387 le_int32 index = LocaleRuns::add(loc, limit);
390 char **localeNames = (char **) fLocaleNames;
392 localeNames[index] = (char *) locale;
398 const char *ULocRuns::getLocaleName(le_int32 run) const
400 if (run < 0 || run >= getCount()) {
404 return fLocaleNames[run];
406 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ULocRuns)
409 U_CAPI pl_localeRuns * U_EXPORT2
410 pl_openLocaleRuns(const char **locales,
411 const le_int32 *limits,
414 return (pl_localeRuns *) new ULocRuns(locales, limits, count);
417 U_CAPI pl_localeRuns * U_EXPORT2
418 pl_openEmptyLocaleRuns(le_int32 initialCapacity)
420 return (pl_localeRuns *) new ULocRuns(initialCapacity);
423 U_CAPI void U_EXPORT2
424 pl_closeLocaleRuns(pl_localeRuns *localeRuns)
426 ULocRuns *lr = (ULocRuns *) localeRuns;
431 U_CAPI le_int32 U_EXPORT2
432 pl_getLocaleRunCount(const pl_localeRuns *localeRuns)
434 const ULocRuns *lr = (const ULocRuns *) localeRuns;
440 return lr->getCount();
443 U_CAPI void U_EXPORT2
444 pl_resetLocaleRuns(pl_localeRuns *localeRuns)
446 ULocRuns *lr = (ULocRuns *) localeRuns;
453 U_CAPI le_int32 U_EXPORT2
454 pl_getLocaleRunLastLimit(const pl_localeRuns *localeRuns)
456 const ULocRuns *lr = (const ULocRuns *) localeRuns;
462 return lr->getLimit();
465 U_CAPI le_int32 U_EXPORT2
466 pl_getLocaleRunLimit(const pl_localeRuns *localeRuns,
469 const ULocRuns *lr = (const ULocRuns *) localeRuns;
475 return lr->getLimit(run);
478 U_CAPI const char * U_EXPORT2
479 pl_getLocaleRunLocale(const pl_localeRuns *localeRuns,
482 const ULocRuns *lr = (const ULocRuns *) localeRuns;
488 return lr->getLocaleName(run);
491 U_CAPI le_int32 U_EXPORT2
492 pl_addLocaleRun(pl_localeRuns *localeRuns,
496 ULocRuns *lr = (ULocRuns *) localeRuns;
502 return lr->add(locale, limit);