1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
31 #include <com/sun/star/i18n/CTLScriptType.hpp>
32 #include <com/sun/star/i18n/ScriptDirection.hpp>
33 #include <com/sun/star/i18n/UnicodeScript.hpp>
34 #include <scripttypedetector.hxx>
35 #include <i18nutil/unicode.hxx>
37 // ----------------------------------------------------
38 // class ScriptTypeDetector
39 // ----------------------------------------------------;
41 using namespace com::sun::star::i18n
;
43 ScriptTypeDetector::ScriptTypeDetector()
47 ScriptTypeDetector::~ScriptTypeDetector()
51 static sal_Int16 scriptDirection
[] = {
52 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_LEFT_TO_RIGHT = 0,
53 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT = 1,
54 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_EUROPEAN_NUMBER = 2,
55 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_EUROPEAN_NUMBER_SEPARATOR = 3,
56 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_EUROPEAN_NUMBER_TERMINATOR = 4,
57 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_ARABIC_NUMBER = 5,
58 ScriptDirection::NEUTRAL
, // DirectionProperty_COMMON_NUMBER_SEPARATOR = 6,
59 ScriptDirection::NEUTRAL
, // DirectionProperty_BLOCK_SEPARATOR = 7,
60 ScriptDirection::NEUTRAL
, // DirectionProperty_SEGMENT_SEPARATOR = 8,
61 ScriptDirection::NEUTRAL
, // DirectionProperty_WHITE_SPACE_NEUTRAL = 9,
62 ScriptDirection::NEUTRAL
, // DirectionProperty_OTHER_NEUTRAL = 10,
63 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_LEFT_TO_RIGHT_EMBEDDING = 11,
64 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_LEFT_TO_RIGHT_OVERRIDE = 12,
65 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT_ARABIC = 13,
66 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT_EMBEDDING = 14,
67 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT_OVERRIDE = 15,
68 ScriptDirection::NEUTRAL
, // DirectionProperty_POP_DIRECTIONAL_FORMAT = 16,
69 ScriptDirection::NEUTRAL
, // DirectionProperty_DIR_NON_SPACING_MARK = 17,
70 ScriptDirection::NEUTRAL
, // DirectionProperty_BOUNDARY_NEUTRAL = 18,
74 ScriptTypeDetector::getScriptDirection( const ::rtl::OUString
& Text
, sal_Int32 nPos
, sal_Int16 defaultScriptDirection
) throw (::com::sun::star::uno::RuntimeException
)
76 sal_Int16 dir
= scriptDirection
[unicode::getUnicodeDirection(Text
[nPos
])];
77 return (dir
== ScriptDirection::NEUTRAL
) ? defaultScriptDirection
: dir
;
80 // return value '-1' means either the direction on nPos is not same as scriptDirection or nPos is out of range.
82 ScriptTypeDetector::beginOfScriptDirection( const ::rtl::OUString
& Text
, sal_Int32 nPos
, sal_Int16 direction
) throw (::com::sun::star::uno::RuntimeException
)
84 sal_Int32 cPos
= nPos
;
86 if (cPos
< Text
.getLength()) {
87 for (; cPos
>= 0; cPos
--) {
88 if (direction
!= getScriptDirection(Text
, cPos
, direction
))
92 return cPos
== nPos
? -1 : cPos
+ 1;
96 ScriptTypeDetector::endOfScriptDirection( const ::rtl::OUString
& Text
, sal_Int32 nPos
, sal_Int16 direction
) throw (::com::sun::star::uno::RuntimeException
)
98 sal_Int32 cPos
= nPos
;
99 sal_Int32 len
= Text
.getLength();
102 for (; cPos
< len
; cPos
++) {
103 if (direction
!= getScriptDirection(Text
, cPos
, direction
))
107 return cPos
== nPos
? -1 : cPos
;
111 ScriptTypeDetector::getCTLScriptType( const ::rtl::OUString
& Text
, sal_Int32 nPos
) throw (::com::sun::star::uno::RuntimeException
)
113 static ScriptTypeList typeList
[] = {
114 { UnicodeScript_kHebrew
, UnicodeScript_kHebrew
, CTLScriptType::CTL_HEBREW
}, // 10
115 { UnicodeScript_kArabic
, UnicodeScript_kArabic
, CTLScriptType::CTL_ARABIC
}, // 11
116 { UnicodeScript_kDevanagari
, UnicodeScript_kDevanagari
, CTLScriptType::CTL_INDIC
}, // 14
117 { UnicodeScript_kThai
, UnicodeScript_kThai
, CTLScriptType::CTL_THAI
}, // 24
118 { UnicodeScript_kScriptCount
, UnicodeScript_kScriptCount
, CTLScriptType::CTL_UNKNOWN
} // 88
121 return unicode::getUnicodeScriptType(Text
[nPos
], typeList
, CTLScriptType::CTL_UNKNOWN
);
124 // Begin of Script Type is inclusive.
126 ScriptTypeDetector::beginOfCTLScriptType( const ::rtl::OUString
& Text
, sal_Int32 nPos
) throw (::com::sun::star::uno::RuntimeException
)
130 else if (nPos
>= Text
.getLength())
131 return Text
.getLength();
133 sal_Int16 cType
= getCTLScriptType(Text
, nPos
);
134 for (nPos
--; nPos
>= 0; nPos
--) {
135 if (cType
!= getCTLScriptType(Text
, nPos
))
142 // End of the Script Type is exclusive, the return value pointing to the begin of next script type
144 ScriptTypeDetector::endOfCTLScriptType( const ::rtl::OUString
& Text
, sal_Int32 nPos
) throw (::com::sun::star::uno::RuntimeException
)
148 else if (nPos
>= Text
.getLength())
149 return Text
.getLength();
151 sal_Int16 cType
= getCTLScriptType(Text
, nPos
);
152 sal_Int32 len
= Text
.getLength();
153 for (nPos
++; nPos
< len
; nPos
++) {
154 if (cType
!= getCTLScriptType(Text
, nPos
))
161 const sal_Char sDetector
[] = "draft.com.sun.star.i18n.ScriptTypeDetector";
163 rtl::OUString SAL_CALL
164 ScriptTypeDetector::getImplementationName() throw( ::com::sun::star::uno::RuntimeException
)
166 return ::rtl::OUString::createFromAscii(sDetector
);
170 ScriptTypeDetector::supportsService(const rtl::OUString
& ServiceName
) throw( ::com::sun::star::uno::RuntimeException
)
172 return !ServiceName
.compareToAscii(sDetector
);
175 ::com::sun::star::uno::Sequence
< rtl::OUString
> SAL_CALL
176 ScriptTypeDetector::getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException
)
178 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aRet(1);
179 aRet
[0] = ::rtl::OUString::createFromAscii(sDetector
);