1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <osl/file.hxx>
23 #include <tools/debug.hxx>
24 #include <o3tl/char16_t2wchar_t.hxx>
26 #include <sal/config.h>
27 #include <cppuhelper/factory.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <cppuhelper/supportsservice.hxx>
31 #include <i18nlangtag/languagetag.hxx>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/linguistic2/XNumberText.hpp>
34 #include <unotools/pathoptions.hxx>
35 #include <osl/thread.h>
37 #include <Numbertext.hxx>
39 using namespace ::osl
;
40 using namespace ::cppu
;
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::linguistic2
;
46 static std::mutex
& GetNumberTextMutex()
48 static std::mutex aMutex
;
54 class NumberText_Impl
: public ::cppu::WeakImplHelper
<XNumberText
, XServiceInfo
>
56 Numbertext m_aNumberText
;
59 virtual ~NumberText_Impl() override
{}
60 void EnsureInitialized();
64 NumberText_Impl(const NumberText_Impl
&) = delete;
65 NumberText_Impl
& operator=(const NumberText_Impl
&) = delete;
67 // XServiceInfo implementation
68 virtual OUString SAL_CALL
getImplementationName() override
;
69 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
70 virtual Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
;
72 // XNumberText implementation
73 virtual OUString SAL_CALL
getNumberText(const OUString
& aText
,
74 const ::css::lang::Locale
& rLocale
) override
;
75 virtual css::uno::Sequence
<css::lang::Locale
> SAL_CALL
getAvailableLanguages() override
;
79 NumberText_Impl::NumberText_Impl()
80 : m_bInitialized(false)
84 void NumberText_Impl::EnsureInitialized()
89 // set this to true at the very start to prevent loops because of
90 // implicitly called functions below
91 m_bInitialized
= true;
93 // set default numbertext path to where those get installed
95 OUString
aURL(SvtPathOptions().GetNumbertextPath());
96 osl::FileBase::getSystemPathFromFileURL(aURL
, aPhysPath
);
99 const rtl_TextEncoding eEnc
= RTL_TEXTENCODING_UTF8
;
102 const rtl_TextEncoding eEnc
= osl_getThreadTextEncoding();
104 OString path
= OUStringToOString(aPhysPath
, eEnc
);
105 m_aNumberText
.set_prefix(std::string(path
));
108 OUString SAL_CALL
NumberText_Impl::getNumberText(const OUString
& rText
, const Locale
& rLocale
)
110 std::scoped_lock
aGuard(GetNumberTextMutex());
112 // libnumbertext supports Language + Country tags (separated by "_" or "-")
113 LanguageTag
aLanguageTag(rLocale
);
114 OUString
aCode(aLanguageTag
.getLanguage());
115 OUString
aCountry(aLanguageTag
.getCountry());
116 OUString
aScript(aLanguageTag
.getScript());
117 if (!aScript
.isEmpty())
118 aCode
+= "-" + aScript
;
119 if (!aCountry
.isEmpty())
120 aCode
+= "-" + aCountry
;
121 OString
aLangCode(OUStringToOString(aCode
, RTL_TEXTENCODING_ASCII_US
));
123 std::wstring
sResult(o3tl::toW(rText
.getStr()));
125 OString
aInput(OUStringToOString(rText
, RTL_TEXTENCODING_UTF8
));
126 std::wstring sResult
= Numbertext::string2wstring(std::string(aInput
));
128 bool result
= m_aNumberText
.numbertext(sResult
, std::string(aLangCode
));
129 DBG_ASSERT(result
, "numbertext: false");
131 OUString
aResult(o3tl::toU(sResult
));
133 OUString aResult
= OUString::fromUtf8(Numbertext::wstring2string(sResult
));
138 uno::Sequence
<Locale
> SAL_CALL
NumberText_Impl::getAvailableLanguages()
140 std::scoped_lock
aGuard(GetNumberTextMutex());
142 Sequence
<css::lang::Locale
> aRes
;
146 OUString SAL_CALL
NumberText_Impl::getImplementationName()
148 return u
"com.sun.star.lingu2.NumberText"_ustr
;
151 sal_Bool SAL_CALL
NumberText_Impl::supportsService(const OUString
& ServiceName
)
153 return cppu::supportsService(this, ServiceName
);
156 Sequence
<OUString
> SAL_CALL
NumberText_Impl::getSupportedServiceNames()
158 return { u
"com.sun.star.linguistic2.NumberText"_ustr
};
161 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
162 lingucomponent_NumberText_get_implementation(css::uno::XComponentContext
*,
163 css::uno::Sequence
<css::uno::Any
> const&)
165 return cppu::acquire(new NumberText_Impl());
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */