1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: scripttypedetector.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
34 #include <com/sun/star/i18n/CTLScriptType.hpp>
35 #include <com/sun/star/i18n/ScriptDirection.hpp>
36 #include <com/sun/star/i18n/UnicodeScript.hpp>
37 #include <scripttypedetector.hxx>
38 #include <i18nutil/unicode.hxx>
40 // ----------------------------------------------------
41 // class ScriptTypeDetector
42 // ----------------------------------------------------;
44 using namespace com::sun::star::i18n
;
46 ScriptTypeDetector::ScriptTypeDetector()
50 ScriptTypeDetector::~ScriptTypeDetector()
54 static sal_Int16 scriptDirection
[] = {
55 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_LEFT_TO_RIGHT = 0,
56 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT = 1,
57 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_EUROPEAN_NUMBER = 2,
58 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_EUROPEAN_NUMBER_SEPARATOR = 3,
59 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_EUROPEAN_NUMBER_TERMINATOR = 4,
60 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_ARABIC_NUMBER = 5,
61 ScriptDirection::NEUTRAL
, // DirectionProperty_COMMON_NUMBER_SEPARATOR = 6,
62 ScriptDirection::NEUTRAL
, // DirectionProperty_BLOCK_SEPARATOR = 7,
63 ScriptDirection::NEUTRAL
, // DirectionProperty_SEGMENT_SEPARATOR = 8,
64 ScriptDirection::NEUTRAL
, // DirectionProperty_WHITE_SPACE_NEUTRAL = 9,
65 ScriptDirection::NEUTRAL
, // DirectionProperty_OTHER_NEUTRAL = 10,
66 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_LEFT_TO_RIGHT_EMBEDDING = 11,
67 ScriptDirection::LEFT_TO_RIGHT
, // DirectionProperty_LEFT_TO_RIGHT_OVERRIDE = 12,
68 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT_ARABIC = 13,
69 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT_EMBEDDING = 14,
70 ScriptDirection::RIGHT_TO_LEFT
, // DirectionProperty_RIGHT_TO_LEFT_OVERRIDE = 15,
71 ScriptDirection::NEUTRAL
, // DirectionProperty_POP_DIRECTIONAL_FORMAT = 16,
72 ScriptDirection::NEUTRAL
, // DirectionProperty_DIR_NON_SPACING_MARK = 17,
73 ScriptDirection::NEUTRAL
, // DirectionProperty_BOUNDARY_NEUTRAL = 18,
77 ScriptTypeDetector::getScriptDirection( const ::rtl::OUString
& Text
, sal_Int32 nPos
, sal_Int16 defaultScriptDirection
) throw (::com::sun::star::uno::RuntimeException
)
79 sal_Int16 dir
= scriptDirection
[unicode::getUnicodeDirection(Text
[nPos
])];
80 return (dir
== ScriptDirection::NEUTRAL
) ? defaultScriptDirection
: dir
;
83 // return value '-1' means either the direction on nPos is not same as scriptDirection or nPos is out of range.
85 ScriptTypeDetector::beginOfScriptDirection( const ::rtl::OUString
& Text
, sal_Int32 nPos
, sal_Int16 direction
) throw (::com::sun::star::uno::RuntimeException
)
87 sal_Int32 cPos
= nPos
;
89 if (cPos
< Text
.getLength()) {
90 for (; cPos
>= 0; cPos
--) {
91 if (direction
!= getScriptDirection(Text
, cPos
, direction
))
95 return cPos
== nPos
? -1 : cPos
+ 1;
99 ScriptTypeDetector::endOfScriptDirection( const ::rtl::OUString
& Text
, sal_Int32 nPos
, sal_Int16 direction
) throw (::com::sun::star::uno::RuntimeException
)
101 sal_Int32 cPos
= nPos
;
102 sal_Int32 len
= Text
.getLength();
105 for (; cPos
< len
; cPos
++) {
106 if (direction
!= getScriptDirection(Text
, cPos
, direction
))
110 return cPos
== nPos
? -1 : cPos
;
114 ScriptTypeDetector::getCTLScriptType( const ::rtl::OUString
& Text
, sal_Int32 nPos
) throw (::com::sun::star::uno::RuntimeException
)
116 static ScriptTypeList typeList
[] = {
117 { UnicodeScript_kHebrew
, UnicodeScript_kHebrew
, CTLScriptType::CTL_HEBREW
}, // 10
118 { UnicodeScript_kArabic
, UnicodeScript_kArabic
, CTLScriptType::CTL_ARABIC
}, // 11
119 { UnicodeScript_kDevanagari
, UnicodeScript_kDevanagari
, CTLScriptType::CTL_INDIC
}, // 14
120 { UnicodeScript_kThai
, UnicodeScript_kThai
, CTLScriptType::CTL_THAI
}, // 24
121 { UnicodeScript_kScriptCount
, UnicodeScript_kScriptCount
, CTLScriptType::CTL_UNKNOWN
} // 88
124 return unicode::getUnicodeScriptType(Text
[nPos
], typeList
, CTLScriptType::CTL_UNKNOWN
);
127 // Begin of Script Type is inclusive.
129 ScriptTypeDetector::beginOfCTLScriptType( const ::rtl::OUString
& Text
, sal_Int32 nPos
) throw (::com::sun::star::uno::RuntimeException
)
133 else if (nPos
>= Text
.getLength())
134 return Text
.getLength();
136 sal_Int16 cType
= getCTLScriptType(Text
, nPos
);
137 for (nPos
--; nPos
>= 0; nPos
--) {
138 if (cType
!= getCTLScriptType(Text
, nPos
))
145 // End of the Script Type is exclusive, the return value pointing to the begin of next script type
147 ScriptTypeDetector::endOfCTLScriptType( const ::rtl::OUString
& Text
, sal_Int32 nPos
) throw (::com::sun::star::uno::RuntimeException
)
151 else if (nPos
>= Text
.getLength())
152 return Text
.getLength();
154 sal_Int16 cType
= getCTLScriptType(Text
, nPos
);
155 sal_Int32 len
= Text
.getLength();
156 for (nPos
++; nPos
< len
; nPos
++) {
157 if (cType
!= getCTLScriptType(Text
, nPos
))
164 const sal_Char sDetector
[] = "draft.com.sun.star.i18n.ScriptTypeDetector";
166 rtl::OUString SAL_CALL
167 ScriptTypeDetector::getImplementationName() throw( ::com::sun::star::uno::RuntimeException
)
169 return ::rtl::OUString::createFromAscii(sDetector
);
173 ScriptTypeDetector::supportsService(const rtl::OUString
& ServiceName
) throw( ::com::sun::star::uno::RuntimeException
)
175 return !ServiceName
.compareToAscii(sDetector
);
178 ::com::sun::star::uno::Sequence
< rtl::OUString
> SAL_CALL
179 ScriptTypeDetector::getSupportedServiceNames() throw( ::com::sun::star::uno::RuntimeException
)
181 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aRet(1);
182 aRet
[0] = ::rtl::OUString::createFromAscii(sDetector
);