1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************* 3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 4f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * Copyright (C) 1999-2014, International Business Machines 5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Corporation and others. All Rights Reserved. 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************* 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * file name: letsutil.cpp 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * created on: 04/25/2006 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * created by: Eric R. Mader 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h" 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/unistr.h" 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ubidi.h" 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "layout/LETypes.h" 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "layout/LEScripts.h" 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "layout/LayoutEngine.h" 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "layout/LELanguages.h" 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 23f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#ifndef USING_ICULEHB 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "OpenTypeLayoutEngine.h" 25f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "letest.h" 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "letsutil.h" 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_USE 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruchar *getCString(const UnicodeString *uString) 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (uString == NULL) { 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return NULL; 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru le_int32 uLength = uString->length(); 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru le_int32 cLength = uString->extract(0, uLength, NULL, 0, US_INV); 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char *cString = NEW_ARRAY(char, cLength + 1); 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uString->extract(0, uLength, cString, cLength, US_INV); 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cString[cLength] = '\0'; 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return cString; 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 4885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hochar *getCString(const LEUnicode16 *uChars) 4985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{ 5085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (uChars == NULL) { 5185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return NULL; 5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho const UnicodeString ustring(uChars); 5585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 5685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return getCString(&ustring); 5785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho} 5885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruchar *getUTF8String(const UnicodeString *uString) 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (uString == NULL) { 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return NULL; 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru le_int32 uLength = uString->length(); 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru le_int32 cLength = uString->extract(0, uLength, NULL, 0, "UTF-8"); 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char *cString = NEW_ARRAY(char, cLength + 1); 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uString->extract(0, uLength, cString, cLength, "UTF-8"); 70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cString[cLength] = '\0'; 72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return cString; 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid freeCString(char *cString) 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru DELETE_ARRAY(cString); 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querule_bool getRTL(const UnicodeString &text) 82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 8385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho UBiDiLevel level = 0; 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status = U_ZERO_ERROR; 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru le_int32 charCount = text.length(); 8685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho le_int32 limit = -1; 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBiDi *ubidi = ubidi_openSized(charCount, 0, &status); 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ubidi_setPara(ubidi, text.getBuffer(), charCount, UBIDI_DEFAULT_LTR, NULL, &status); 9085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 9185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // TODO: Should check that there's only a single logical run... 9285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho ubidi_getLogicalRun(ubidi, 0, &limit, &level); 9385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ubidi_close(ubidi); 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 9685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return level & 1; 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querule_int32 getLanguageCode(const char *lang) 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (strlen(lang) != 3) { 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return -1; 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru LETag langTag = (LETag) ((lang[0] << 24) + (lang[1] << 16) + (lang[2] << 8) + 0x20); 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 107f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#ifndef USING_ICULEHB 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (le_int32 i = 0; i < languageCodeCount; i += 1) { 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (langTag == OpenTypeLayoutEngine::languageTags[i]) { 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return i; 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 113f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return -1; 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 118