• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  *******************************************************************************
5  *
6  *   Copyright (C) 1999-2014, International Business Machines
7  *   Corporation and others.  All Rights Reserved.
8  *
9  *******************************************************************************
10  *   file name:  letsutil.cpp
11  *
12  *   created on: 04/25/2006
13  *   created by: Eric R. Mader
14  */
15 
16 #include "unicode/utypes.h"
17 #include "unicode/unistr.h"
18 #include "unicode/ubidi.h"
19 
20 #include "layout/LETypes.h"
21 #include "layout/LEScripts.h"
22 #include "layout/LayoutEngine.h"
23 #include "layout/LELanguages.h"
24 
25 #ifndef USING_ICULEHB
26 #include "OpenTypeLayoutEngine.h"
27 #endif
28 
29 #include "letest.h"
30 #include "letsutil.h"
31 
32 U_NAMESPACE_USE
33 
getCString(const UnicodeString * uString)34 char *getCString(const UnicodeString *uString)
35 {
36     if (uString == NULL) {
37         return NULL;
38     }
39 
40     le_int32 uLength = uString->length();
41     le_int32 cLength = uString->extract(0, uLength, NULL, 0, US_INV);
42     char *cString = NEW_ARRAY(char, cLength + 1);
43 
44     uString->extract(0, uLength, cString, cLength, US_INV);
45     cString[cLength] = '\0';
46 
47     return cString;
48 }
49 
getCString(const LEUnicode16 * uChars)50 char *getCString(const LEUnicode16 *uChars)
51 {
52     if (uChars == NULL) {
53         return NULL;
54     }
55 
56     const UnicodeString ustring(uChars);
57 
58     return getCString(&ustring);
59 }
60 
getUTF8String(const UnicodeString * uString)61 char *getUTF8String(const UnicodeString *uString)
62 {
63     if (uString == NULL) {
64         return NULL;
65     }
66 
67     le_int32 uLength = uString->length();
68     le_int32 cLength = uString->extract(0, uLength, NULL, 0, "UTF-8");
69     char *cString = NEW_ARRAY(char, cLength + 1);
70 
71     uString->extract(0, uLength, cString, cLength, "UTF-8");
72 
73     cString[cLength] = '\0';
74 
75     return cString;
76 }
77 
freeCString(char * cString)78 void freeCString(char *cString)
79 {
80     DELETE_ARRAY(cString);
81 }
82 
getRTL(const UnicodeString & text)83 le_bool getRTL(const UnicodeString &text)
84 {
85     UBiDiLevel level = 0;
86     UErrorCode status = U_ZERO_ERROR;
87     le_int32 charCount = text.length();
88     le_int32 limit = -1;
89     UBiDi *ubidi = ubidi_openSized(charCount, 0, &status);
90 
91     ubidi_setPara(ubidi, text.getBuffer(), charCount, UBIDI_DEFAULT_LTR, NULL, &status);
92 
93     // TODO: Should check that there's only a single logical run...
94     ubidi_getLogicalRun(ubidi, 0, &limit, &level);
95 
96     ubidi_close(ubidi);
97 
98     return level & 1;
99 }
100 
getLanguageCode(const char * lang)101 le_int32 getLanguageCode(const char *lang)
102 {
103     if (strlen(lang) != 3) {
104         return -1;
105     }
106 
107     LETag langTag = (LETag) ((lang[0] << 24) + (lang[1] << 16) + (lang[2] << 8) + 0x20);
108 
109 #ifndef USING_ICULEHB
110     for (le_int32 i = 0; i < languageCodeCount; i += 1) {
111         if (langTag == OpenTypeLayoutEngine::languageTags[i]) {
112             return i;
113         }
114     }
115 #else
116     if (!strcmp(lang, "JAN")) return janLanguageCode;
117     if (!strcmp(lang, "KOR")) return korLanguageCode;
118     if (!strcmp(lang, "ZHT")) return zhtLanguageCode;
119     if (!strcmp(lang, "ZHS")) return zhsLanguageCode;
120     if (!strcmp(lang, "HIN")) return hinLanguageCode;
121     if (!strcmp(lang, "MAR")) return marLanguageCode;
122     if (!strcmp(lang, "ROM")) return romLanguageCode;
123 #endif
124 
125 
126     return -1;
127 }
128 
129