]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/letest/letsutil.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / test / letest / letsutil.cpp
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: letsutil.cpp
9 *
10 * created on: 04/25/2006
11 * created by: Eric R. Mader
12 */
13
14 #include "unicode/utypes.h"
15 #include "unicode/unistr.h"
16 #include "unicode/ubidi.h"
17
18 #include "layout/LETypes.h"
19 #include "layout/LEScripts.h"
20 #include "layout/LayoutEngine.h"
21 #include "layout/LELanguages.h"
22
23 #include "OpenTypeLayoutEngine.h"
24
25 #include "letest.h"
26 #include "letsutil.h"
27
28 U_NAMESPACE_USE
29
30 char *getCString(const UnicodeString *uString)
31 {
32 if (uString == NULL) {
33 return NULL;
34 }
35
36 le_int32 uLength = uString->length();
37 le_int32 cLength = uString->extract(0, uLength, NULL, 0, US_INV);
38 char *cString = NEW_ARRAY(char, cLength + 1);
39
40 uString->extract(0, uLength, cString, cLength, US_INV);
41 cString[cLength] = '\0';
42
43 return cString;
44 }
45
46 char *getUTF8String(const UnicodeString *uString)
47 {
48 if (uString == NULL) {
49 return NULL;
50 }
51
52 le_int32 uLength = uString->length();
53 le_int32 cLength = uString->extract(0, uLength, NULL, 0, "UTF-8");
54 char *cString = NEW_ARRAY(char, cLength + 1);
55
56 uString->extract(0, uLength, cString, cLength, "UTF-8");
57
58 cString[cLength] = '\0';
59
60 return cString;
61 }
62
63 void freeCString(char *cString)
64 {
65 DELETE_ARRAY(cString);
66 }
67
68 le_bool getRTL(const UnicodeString &text)
69 {
70 UBiDiLevel paraLevel;
71 UErrorCode status = U_ZERO_ERROR;
72 le_int32 charCount = text.length();
73 UBiDi *ubidi = ubidi_openSized(charCount, 0, &status);
74
75 ubidi_setPara(ubidi, text.getBuffer(), charCount, UBIDI_DEFAULT_LTR, NULL, &status);
76 paraLevel = ubidi_getParaLevel(ubidi);
77 ubidi_close(ubidi);
78
79 return paraLevel & 1;
80 }
81
82 le_int32 getLanguageCode(const char *lang)
83 {
84 if (strlen(lang) != 3) {
85 return -1;
86 }
87
88 LETag langTag = (LETag) ((lang[0] << 24) + (lang[1] << 16) + (lang[2] << 8) + 0x20);
89
90 for (le_int32 i = 0; i < languageCodeCount; i += 1) {
91 if (langTag == OpenTypeLayoutEngine::languageTags[i]) {
92 return i;
93 }
94 }
95
96 return -1;
97 }
98