diff options
author | Caolán McNamara <caolanm@redhat.com> | 2012-03-07 23:50:19 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2012-03-08 12:02:37 +0000 |
commit | 5b395625901b1f9c2314a0b9a13f4876a3ae17ce (patch) | |
tree | 42f91df0ee35f2a55bac7da11442a4273c32b445 /i18nutil | |
parent | 14c4c26cea838b3fffb0b17ba440c29a6066fad1 (diff) |
de-uno-ize ScriptTypeDetector
leave a UnoScriptTypeDetector implementation that points to the
trivial ScriptTypeDetector for external consumers, e.g. sdext
Diffstat (limited to 'i18nutil')
-rw-r--r-- | i18nutil/Library_i18nutil.mk | 1 | ||||
-rw-r--r-- | i18nutil/Package_inc.mk | 1 | ||||
-rw-r--r-- | i18nutil/inc/i18nutil/scripttypedetector.hxx | 47 | ||||
-rw-r--r-- | i18nutil/source/utility/scripttypedetector.cxx | 141 |
4 files changed, 190 insertions, 0 deletions
diff --git a/i18nutil/Library_i18nutil.mk b/i18nutil/Library_i18nutil.mk index e3de16e7244f..a05c3d017f89 100644 --- a/i18nutil/Library_i18nutil.mk +++ b/i18nutil/Library_i18nutil.mk @@ -48,6 +48,7 @@ $(eval $(call gb_Library_add_linked_libs,i18nutil,\ $(eval $(call gb_Library_add_exception_objects,i18nutil,\ i18nutil/source/utility/casefolding \ i18nutil/source/utility/oneToOneMapping \ + i18nutil/source/utility/scripttypedetector \ i18nutil/source/utility/unicode \ i18nutil/source/utility/widthfolding \ )) diff --git a/i18nutil/Package_inc.mk b/i18nutil/Package_inc.mk index 92a9e71de50c..3ea2b55d46be 100644 --- a/i18nutil/Package_inc.mk +++ b/i18nutil/Package_inc.mk @@ -30,6 +30,7 @@ $(eval $(call gb_Package_Package,i18nutil_inc,$(SRCDIR)/i18nutil/inc)) $(eval $(call gb_Package_add_file,i18nutil_inc,inc/i18nutil/i18nutildllapi.h,i18nutil/i18nutildllapi.h)) $(eval $(call gb_Package_add_file,i18nutil_inc,inc/i18nutil/casefolding.hxx,i18nutil/casefolding.hxx)) $(eval $(call gb_Package_add_file,i18nutil_inc,inc/i18nutil/oneToOneMapping.hxx,i18nutil/oneToOneMapping.hxx)) +$(eval $(call gb_Package_add_file,i18nutil_inc,inc/i18nutil/scripttypedetector.hxx,i18nutil/scripttypedetector.hxx)) $(eval $(call gb_Package_add_file,i18nutil_inc,inc/i18nutil/unicode.hxx,i18nutil/unicode.hxx)) $(eval $(call gb_Package_add_file,i18nutil_inc,inc/i18nutil/widthfolding.hxx,i18nutil/widthfolding.hxx)) diff --git a/i18nutil/inc/i18nutil/scripttypedetector.hxx b/i18nutil/inc/i18nutil/scripttypedetector.hxx new file mode 100644 index 000000000000..9b732ef21353 --- /dev/null +++ b/i18nutil/inc/i18nutil/scripttypedetector.hxx @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#ifndef _I18N_SCRIPTTYPEDETECTOR_HXX_ +#define _I18N_SCRIPTTYPEDETECTOR_HXX_ + +#include "i18nutildllapi.h" +#include <rtl/ustring.hxx> + +class I18NUTIL_DLLPUBLIC ScriptTypeDetector +{ +public: + static sal_Int32 beginOfScriptDirection( const rtl::OUString& Text, sal_Int32 nPos, sal_Int16 scriptDirection ); + static sal_Int32 endOfScriptDirection( const rtl::OUString& Text, sal_Int32 nPos, sal_Int16 scriptDirection ); + static sal_Int16 getScriptDirection( const rtl::OUString& Text, sal_Int32 nPos, sal_Int16 defaultScriptDirection ); + static sal_Int32 beginOfCTLScriptType( const rtl::OUString& Text, sal_Int32 nPos ); + static sal_Int32 endOfCTLScriptType( const rtl::OUString& Text, sal_Int32 nPos ); + static sal_Int16 getCTLScriptType( const rtl::OUString& Text, sal_Int32 nPos ); +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/i18nutil/source/utility/scripttypedetector.cxx b/i18nutil/source/utility/scripttypedetector.cxx new file mode 100644 index 000000000000..1c70f5e155f5 --- /dev/null +++ b/i18nutil/source/utility/scripttypedetector.cxx @@ -0,0 +1,141 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <com/sun/star/i18n/CTLScriptType.hpp> +#include <com/sun/star/i18n/ScriptDirection.hpp> +#include <com/sun/star/i18n/UnicodeScript.hpp> +#include <i18nutil/scripttypedetector.hxx> +#include <i18nutil/unicode.hxx> + +using namespace com::sun::star::i18n; + +static sal_Int16 scriptDirection[] = { + ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_LEFT_TO_RIGHT = 0, + ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT = 1, + ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_EUROPEAN_NUMBER = 2, + ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_EUROPEAN_NUMBER_SEPARATOR = 3, + ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_EUROPEAN_NUMBER_TERMINATOR = 4, + ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_ARABIC_NUMBER = 5, + ScriptDirection::NEUTRAL, // DirectionProperty_COMMON_NUMBER_SEPARATOR = 6, + ScriptDirection::NEUTRAL, // DirectionProperty_BLOCK_SEPARATOR = 7, + ScriptDirection::NEUTRAL, // DirectionProperty_SEGMENT_SEPARATOR = 8, + ScriptDirection::NEUTRAL, // DirectionProperty_WHITE_SPACE_NEUTRAL = 9, + ScriptDirection::NEUTRAL, // DirectionProperty_OTHER_NEUTRAL = 10, + ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_LEFT_TO_RIGHT_EMBEDDING = 11, + ScriptDirection::LEFT_TO_RIGHT, // DirectionProperty_LEFT_TO_RIGHT_OVERRIDE = 12, + ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT_ARABIC = 13, + ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT_EMBEDDING = 14, + ScriptDirection::RIGHT_TO_LEFT, // DirectionProperty_RIGHT_TO_LEFT_OVERRIDE = 15, + ScriptDirection::NEUTRAL, // DirectionProperty_POP_DIRECTIONAL_FORMAT = 16, + ScriptDirection::NEUTRAL, // DirectionProperty_DIR_NON_SPACING_MARK = 17, + ScriptDirection::NEUTRAL, // DirectionProperty_BOUNDARY_NEUTRAL = 18, +}; + +sal_Int16 ScriptTypeDetector::getScriptDirection( const rtl::OUString& Text, sal_Int32 nPos, sal_Int16 defaultScriptDirection ) +{ + sal_Int16 dir = scriptDirection[unicode::getUnicodeDirection(Text[nPos])]; + return (dir == ScriptDirection::NEUTRAL) ? defaultScriptDirection : dir; +} + +// return value '-1' means either the direction on nPos is not same as scriptDirection or nPos is out of range. +sal_Int32 ScriptTypeDetector::beginOfScriptDirection( const rtl::OUString& Text, sal_Int32 nPos, sal_Int16 direction ) +{ + sal_Int32 cPos = nPos; + + if (cPos < Text.getLength()) { + for (; cPos >= 0; cPos--) { + if (direction != getScriptDirection(Text, cPos, direction)) + break; + } + } + return cPos == nPos ? -1 : cPos + 1; +} + +sal_Int32 ScriptTypeDetector::endOfScriptDirection( const rtl::OUString& Text, sal_Int32 nPos, sal_Int16 direction ) +{ + sal_Int32 cPos = nPos; + sal_Int32 len = Text.getLength(); + + if (cPos >=0) { + for (; cPos < len; cPos++) { + if (direction != getScriptDirection(Text, cPos, direction)) + break; + } + } + return cPos == nPos ? -1 : cPos; +} + +sal_Int16 ScriptTypeDetector::getCTLScriptType( const rtl::OUString& Text, sal_Int32 nPos ) +{ + static ScriptTypeList typeList[] = { + { UnicodeScript_kHebrew, UnicodeScript_kHebrew, CTLScriptType::CTL_HEBREW }, // 10 + { UnicodeScript_kArabic, UnicodeScript_kArabic, CTLScriptType::CTL_ARABIC }, // 11 + { UnicodeScript_kDevanagari, UnicodeScript_kDevanagari, CTLScriptType::CTL_INDIC }, // 14 + { UnicodeScript_kThai, UnicodeScript_kThai, CTLScriptType::CTL_THAI }, // 24 + { UnicodeScript_kScriptCount, UnicodeScript_kScriptCount, CTLScriptType::CTL_UNKNOWN } // 88 + }; + + return unicode::getUnicodeScriptType(Text[nPos], typeList, CTLScriptType::CTL_UNKNOWN); +} + +// Begin of Script Type is inclusive. +sal_Int32 ScriptTypeDetector::beginOfCTLScriptType( const rtl::OUString& Text, sal_Int32 nPos ) +{ + if (nPos < 0) + return 0; + else if (nPos >= Text.getLength()) + return Text.getLength(); + else { + sal_Int16 cType = getCTLScriptType(Text, nPos); + for (nPos--; nPos >= 0; nPos--) { + if (cType != getCTLScriptType(Text, nPos)) + break; + } + return nPos + 1; + } +} + +// End of the Script Type is exclusive, the return value pointing to the begin of next script type +sal_Int32 ScriptTypeDetector::endOfCTLScriptType( const rtl::OUString& Text, sal_Int32 nPos ) +{ + if (nPos < 0) + return 0; + else if (nPos >= Text.getLength()) + return Text.getLength(); + else { + sal_Int16 cType = getCTLScriptType(Text, nPos); + sal_Int32 len = Text.getLength(); + for (nPos++; nPos < len; nPos++) { + if (cType != getCTLScriptType(Text, nPos)) + break; + } + return nPos; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |