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 .
20 #include <scitems.hxx>
22 #include <com/sun/star/i18n/BreakIterator.hpp>
23 #include <com/sun/star/i18n/ScriptType.hpp>
24 #include <comphelper/processfactory.hxx>
26 #include <document.hxx>
27 #include <cellform.hxx>
28 #include <patattr.hxx>
29 #include <scrdata.hxx>
30 #include <poolhelp.hxx>
32 #include <columnspanset.hxx>
35 using namespace com::sun::star
;
37 // this file is compiled with exceptions enabled
38 // put functions here that need exceptions!
40 const uno::Reference
< i18n::XBreakIterator
>& ScDocument::GetBreakIterator()
42 if ( !pScriptTypeData
)
43 pScriptTypeData
.reset( new ScScriptTypeData
);
44 if ( !pScriptTypeData
->xBreakIter
.is() )
46 pScriptTypeData
->xBreakIter
= i18n::BreakIterator::create( comphelper::getProcessComponentContext() );
48 return pScriptTypeData
->xBreakIter
;
51 bool ScDocument::HasStringWeakCharacters( const OUString
& rString
)
53 if (!rString
.isEmpty())
55 uno::Reference
<i18n::XBreakIterator
> xBreakIter
= GetBreakIterator();
56 if ( xBreakIter
.is() )
58 sal_Int32 nLen
= rString
.getLength();
63 sal_Int16 nType
= xBreakIter
->getScriptType( rString
, nPos
);
64 if ( nType
== i18n::ScriptType::WEAK
)
67 nPos
= xBreakIter
->endOfScript( rString
, nPos
, nType
);
69 while ( nPos
>= 0 && nPos
< nLen
);
73 return false; // none found
76 SvtScriptType
ScDocument::GetStringScriptType( const OUString
& rString
)
78 SvtScriptType nRet
= SvtScriptType::NONE
;
79 if (!rString
.isEmpty())
81 uno::Reference
<i18n::XBreakIterator
> xBreakIter
= GetBreakIterator();
82 if ( xBreakIter
.is() )
84 sal_Int32 nLen
= rString
.getLength();
89 sal_Int16 nType
= xBreakIter
->getScriptType( rString
, nPos
);
92 case i18n::ScriptType::LATIN
:
93 nRet
|= SvtScriptType::LATIN
;
95 case i18n::ScriptType::ASIAN
:
96 nRet
|= SvtScriptType::ASIAN
;
98 case i18n::ScriptType::COMPLEX
:
99 nRet
|= SvtScriptType::COMPLEX
;
103 nPos
= xBreakIter
->endOfScript( rString
, nPos
, nType
);
105 while ( nPos
>= 0 && nPos
< nLen
);
111 SvtScriptType
ScDocument::GetCellScriptType( const ScAddress
& rPos
, sal_uInt32 nNumberFormat
,
112 const ScRefCellValue
* pCell
)
114 SvtScriptType nStored
= GetScriptType(rPos
);
115 if ( nStored
!= SvtScriptType::UNKNOWN
) // stored value valid?
116 return nStored
; // use stored value
121 aStr
= ScCellFormat::GetString(*pCell
, nNumberFormat
, &pColor
, nullptr, *this);
123 aStr
= ScCellFormat::GetString(*this, rPos
, nNumberFormat
, &pColor
, nullptr);
125 SvtScriptType nRet
= GetStringScriptType( aStr
);
127 SetScriptType(rPos
, nRet
); // store for later calls
132 SvtScriptType
ScDocument::GetScriptType( SCCOL nCol
, SCROW nRow
, SCTAB nTab
, const ScRefCellValue
* pCell
)
134 // if script type is set, don't have to get number formats
136 ScAddress
aPos(nCol
, nRow
, nTab
);
137 SvtScriptType nStored
= GetScriptType(aPos
);
138 if ( nStored
!= SvtScriptType::UNKNOWN
) // stored value valid?
139 return nStored
; // use stored value
141 // include number formats from conditional formatting
143 const ScPatternAttr
* pPattern
= GetPattern( nCol
, nRow
, nTab
);
144 if (!pPattern
) return SvtScriptType::NONE
;
145 const SfxItemSet
* pCondSet
= nullptr;
146 if ( !pPattern
->GetItem(ATTR_CONDITIONAL
).GetCondFormatData().empty() )
147 pCondSet
= GetCondResult( nCol
, nRow
, nTab
);
149 sal_uInt32 nFormat
= pPattern
->GetNumberFormat( GetFormatTable(), pCondSet
);
151 return GetCellScriptType(aPos
, nFormat
, pCell
);
156 class ScriptTypeAggregator
: public sc::ColumnSpanSet::Action
159 sc::ColumnBlockPosition maBlockPos
;
160 SvtScriptType mnScriptType
;
163 explicit ScriptTypeAggregator(ScDocument
& rDoc
) : mrDoc(rDoc
), mnScriptType(SvtScriptType::NONE
) {}
165 virtual void startColumn(SCTAB nTab
, SCCOL nCol
) override
167 mrDoc
.InitColumnBlockPosition(maBlockPos
, nTab
, nCol
);
170 virtual void execute(const ScAddress
& rPos
, SCROW nLength
, bool bVal
) override
175 mnScriptType
|= mrDoc
.GetRangeScriptType(maBlockPos
, rPos
, nLength
);
178 SvtScriptType
getScriptType() const { return mnScriptType
; }
183 SvtScriptType
ScDocument::GetRangeScriptType(
184 sc::ColumnBlockPosition
& rBlockPos
, const ScAddress
& rPos
, SCROW nLength
)
186 if (!HasTable(rPos
.Tab()))
187 return SvtScriptType::NONE
;
189 return maTabs
[rPos
.Tab()]->GetRangeScriptType(rBlockPos
, rPos
.Col(), rPos
.Row(), rPos
.Row()+nLength
-1);
192 SvtScriptType
ScDocument::GetRangeScriptType( const ScRangeList
& rRanges
)
194 sc::ColumnSpanSet aSet
;
195 for (size_t i
= 0, n
= rRanges
.size(); i
< n
; ++i
)
197 const ScRange
& rRange
= rRanges
[i
];
198 SCTAB nTab
= rRange
.aStart
.Tab();
199 SCROW nRow1
= rRange
.aStart
.Row();
200 SCROW nRow2
= rRange
.aEnd
.Row();
201 for (SCCOL nCol
= rRange
.aStart
.Col(); nCol
<= rRange
.aEnd
.Col(); ++nCol
)
202 aSet
.set(*this, nTab
, nCol
, nRow1
, nRow2
, true);
205 ScriptTypeAggregator
aAction(*this);
206 aSet
.executeAction(*this, aAction
);
207 return aAction
.getScriptType();
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */