letsutil.cpp revision 85bf2e2fbc60a9f938064abc8127d61da7d19882
1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *******************************************************************************
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *   Copyright (C) 1999-2008, 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
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "OpenTypeLayoutEngine.h"
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "letest.h"
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "letsutil.h"
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_USE
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruchar *getCString(const UnicodeString *uString)
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (uString == NULL) {
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 uLength = uString->length();
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 cLength = uString->extract(0, uLength, NULL, 0, US_INV);
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *cString = NEW_ARRAY(char, cLength + 1);
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uString->extract(0, uLength, cString, cLength, US_INV);
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    cString[cLength] = '\0';
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return cString;
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
4685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hochar *getCString(const LEUnicode16 *uChars)
4785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
4885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (uChars == NULL) {
4985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return NULL;
5085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
5185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    const UnicodeString ustring(uChars);
5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return getCString(&ustring);
5585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
5685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruchar *getUTF8String(const UnicodeString *uString)
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (uString == NULL) {
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 uLength = uString->length();
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 cLength = uString->extract(0, uLength, NULL, 0, "UTF-8");
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    char *cString = NEW_ARRAY(char, cLength + 1);
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uString->extract(0, uLength, cString, cLength, "UTF-8");
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    cString[cLength] = '\0';
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return cString;
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid freeCString(char *cString)
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    DELETE_ARRAY(cString);
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querule_bool getRTL(const UnicodeString &text)
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
8185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UBiDiLevel level = 0;
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UErrorCode status = U_ZERO_ERROR;
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 charCount = text.length();
8485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    le_int32 limit = -1;
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBiDi *ubidi = ubidi_openSized(charCount, 0, &status);
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ubidi_setPara(ubidi, text.getBuffer(), charCount, UBIDI_DEFAULT_LTR, NULL, &status);
8885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
8985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // TODO: Should check that there's only a single logical run...
9085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    ubidi_getLogicalRun(ubidi, 0, &limit, &level);
9185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ubidi_close(ubidi);
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
9485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return level & 1;
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querule_int32 getLanguageCode(const char *lang)
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (strlen(lang) != 3) {
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return -1;
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    LETag langTag = (LETag) ((lang[0] << 24) + (lang[1] << 16) + (lang[2] << 8) + 0x20);
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (le_int32 i = 0; i < languageCodeCount; i += 1) {
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (langTag == OpenTypeLayoutEngine::languageTags[i]) {
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            return i;
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return -1;
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
114