1 /* 2 ******************************************************************************* 3 * 4 * Copyright (C) 1999-2008, 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 *getCString(const LEUnicode16 *uChars) 47 { 48 if (uChars == NULL) { 49 return NULL; 50 } 51 52 const UnicodeString ustring(uChars); 53 54 return getCString(&ustring); 55 } 56 57 char *getUTF8String(const UnicodeString *uString) 58 { 59 if (uString == NULL) { 60 return NULL; 61 } 62 63 le_int32 uLength = uString->length(); 64 le_int32 cLength = uString->extract(0, uLength, NULL, 0, "UTF-8"); 65 char *cString = NEW_ARRAY(char, cLength + 1); 66 67 uString->extract(0, uLength, cString, cLength, "UTF-8"); 68 69 cString[cLength] = '\0'; 70 71 return cString; 72 } 73 74 void freeCString(char *cString) 75 { 76 DELETE_ARRAY(cString); 77 } 78 79 le_bool getRTL(const UnicodeString &text) 80 { 81 UBiDiLevel level = 0; 82 UErrorCode status = U_ZERO_ERROR; 83 le_int32 charCount = text.length(); 84 le_int32 limit = -1; 85 UBiDi *ubidi = ubidi_openSized(charCount, 0, &status); 86 87 ubidi_setPara(ubidi, text.getBuffer(), charCount, UBIDI_DEFAULT_LTR, NULL, &status); 88 89 // TODO: Should check that there's only a single logical run... 90 ubidi_getLogicalRun(ubidi, 0, &limit, &level); 91 92 ubidi_close(ubidi); 93 94 return level & 1; 95 } 96 97 le_int32 getLanguageCode(const char *lang) 98 { 99 if (strlen(lang) != 3) { 100 return -1; 101 } 102 103 LETag langTag = (LETag) ((lang[0] << 24) + (lang[1] << 16) + (lang[2] << 8) + 0x20); 104 105 for (le_int32 i = 0; i < languageCodeCount; i += 1) { 106 if (langTag == OpenTypeLayoutEngine::languageTags[i]) { 107 return i; 108 } 109 } 110 111 return -1; 112 } 113 114