Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "scitems.hxx"
21 : #include <editeng/scripttypeitem.hxx>
22 :
23 : #include <com/sun/star/i18n/BreakIterator.hpp>
24 : #include <com/sun/star/i18n/ScriptType.hpp>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <comphelper/processfactory.hxx>
27 :
28 : #include "document.hxx"
29 : #include "cellform.hxx"
30 : #include "patattr.hxx"
31 : #include "scrdata.hxx"
32 : #include "poolhelp.hxx"
33 : #include "attrib.hxx"
34 : #include "globalnames.hxx"
35 : #include "columnspanset.hxx"
36 : #include "table.hxx"
37 :
38 : using namespace com::sun::star;
39 :
40 :
41 : // this file is compiled with exceptions enabled
42 : // put functions here that need exceptions!
43 :
44 :
45 0 : const uno::Reference< i18n::XBreakIterator >& ScDocument::GetBreakIterator()
46 : {
47 0 : if ( !pScriptTypeData )
48 0 : pScriptTypeData = new ScScriptTypeData;
49 0 : if ( !pScriptTypeData->xBreakIter.is() )
50 : {
51 0 : pScriptTypeData->xBreakIter = i18n::BreakIterator::create( comphelper::getProcessComponentContext() );
52 : }
53 0 : return pScriptTypeData->xBreakIter;
54 : }
55 :
56 0 : bool ScDocument::HasStringWeakCharacters( const OUString& rString )
57 : {
58 0 : if (!rString.isEmpty())
59 : {
60 0 : uno::Reference<i18n::XBreakIterator> xBreakIter = GetBreakIterator();
61 0 : if ( xBreakIter.is() )
62 : {
63 0 : sal_Int32 nLen = rString.getLength();
64 :
65 0 : sal_Int32 nPos = 0;
66 0 : do
67 : {
68 0 : sal_Int16 nType = xBreakIter->getScriptType( rString, nPos );
69 0 : if ( nType == i18n::ScriptType::WEAK )
70 0 : return true; // found
71 :
72 0 : nPos = xBreakIter->endOfScript( rString, nPos, nType );
73 : }
74 0 : while ( nPos >= 0 && nPos < nLen );
75 0 : }
76 : }
77 :
78 0 : return false; // none found
79 : }
80 :
81 0 : sal_uInt8 ScDocument::GetStringScriptType( const OUString& rString )
82 : {
83 :
84 0 : sal_uInt8 nRet = 0;
85 0 : if (!rString.isEmpty())
86 : {
87 0 : uno::Reference<i18n::XBreakIterator> xBreakIter = GetBreakIterator();
88 0 : if ( xBreakIter.is() )
89 : {
90 0 : sal_Int32 nLen = rString.getLength();
91 :
92 0 : sal_Int32 nPos = 0;
93 0 : do
94 : {
95 0 : sal_Int16 nType = xBreakIter->getScriptType( rString, nPos );
96 0 : switch ( nType )
97 : {
98 : case i18n::ScriptType::LATIN:
99 0 : nRet |= SCRIPTTYPE_LATIN;
100 0 : break;
101 : case i18n::ScriptType::ASIAN:
102 0 : nRet |= SCRIPTTYPE_ASIAN;
103 0 : break;
104 : case i18n::ScriptType::COMPLEX:
105 0 : nRet |= SCRIPTTYPE_COMPLEX;
106 0 : break;
107 : // WEAK is ignored
108 : }
109 0 : nPos = xBreakIter->endOfScript( rString, nPos, nType );
110 : }
111 0 : while ( nPos >= 0 && nPos < nLen );
112 0 : }
113 : }
114 0 : return nRet;
115 : }
116 :
117 0 : sal_uInt8 ScDocument::GetCellScriptType( const ScAddress& rPos, sal_uLong nNumberFormat )
118 : {
119 0 : sal_uInt8 nStored = GetScriptType(rPos);
120 0 : if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
121 0 : return nStored; // use stored value
122 :
123 : Color* pColor;
124 0 : OUString aStr = ScCellFormat::GetString(*this, rPos, nNumberFormat, &pColor, *xPoolHelper->GetFormTable());
125 :
126 0 : sal_uInt8 nRet = GetStringScriptType( aStr );
127 :
128 0 : SetScriptType(rPos, nRet); // store for later calls
129 :
130 0 : return nRet;
131 : }
132 :
133 0 : sal_uInt8 ScDocument::GetScriptType( SCCOL nCol, SCROW nRow, SCTAB nTab )
134 : {
135 : // if script type is set, don't have to get number formats
136 :
137 0 : ScAddress aPos(nCol, nRow, nTab);
138 0 : sal_uInt8 nStored = GetScriptType(aPos);
139 0 : if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
140 0 : return nStored; // use stored value
141 :
142 : // include number formats from conditional formatting
143 :
144 0 : const ScPatternAttr* pPattern = GetPattern( nCol, nRow, nTab );
145 0 : if (!pPattern) return 0;
146 0 : const SfxItemSet* pCondSet = NULL;
147 0 : if ( !((const ScCondFormatItem&)pPattern->GetItem(ATTR_CONDITIONAL)).GetCondFormatData().empty() )
148 0 : pCondSet = GetCondResult( nCol, nRow, nTab );
149 :
150 0 : sal_uLong nFormat = pPattern->GetNumberFormat( xPoolHelper->GetFormTable(), pCondSet );
151 :
152 0 : return GetCellScriptType(aPos, nFormat);
153 : }
154 :
155 : namespace {
156 :
157 0 : class ScriptTypeAggregator : public sc::ColumnSpanSet::Action
158 : {
159 : ScDocument& mrDoc;
160 : sc::ColumnBlockPosition maBlockPos;
161 : sal_uInt8 mnScriptType;
162 :
163 : public:
164 0 : ScriptTypeAggregator(ScDocument& rDoc) : mrDoc(rDoc), mnScriptType(0) {}
165 :
166 0 : virtual void startColumn(SCTAB nTab, SCCOL nCol) SAL_OVERRIDE
167 : {
168 0 : mrDoc.InitColumnBlockPosition(maBlockPos, nTab, nCol);
169 0 : }
170 :
171 0 : virtual void execute(const ScAddress& rPos, SCROW nLength, bool bVal) SAL_OVERRIDE
172 : {
173 0 : if (!bVal)
174 0 : return;
175 :
176 0 : mnScriptType |= mrDoc.GetRangeScriptType(maBlockPos, rPos, nLength);
177 : };
178 :
179 0 : sal_uInt8 getScriptType() const { return mnScriptType; }
180 : };
181 :
182 : }
183 :
184 0 : sal_uInt8 ScDocument::GetRangeScriptType(
185 : sc::ColumnBlockPosition& rBlockPos, const ScAddress& rPos, SCROW nLength )
186 : {
187 0 : if (!TableExists(rPos.Tab()))
188 0 : return 0;
189 :
190 0 : return maTabs[rPos.Tab()]->GetRangeScriptType(rBlockPos, rPos.Col(), rPos.Row(), rPos.Row()+nLength-1);
191 : }
192 :
193 0 : sal_uInt8 ScDocument::GetRangeScriptType( const ScRangeList& rRanges )
194 : {
195 0 : sc::ColumnSpanSet aSet(false);
196 0 : for (size_t i = 0, n = rRanges.size(); i < n; ++i)
197 : {
198 0 : const ScRange& rRange = *rRanges[i];
199 0 : SCTAB nTab = rRange.aStart.Tab();
200 0 : SCROW nRow1 = rRange.aStart.Row();
201 0 : SCROW nRow2 = rRange.aEnd.Row();
202 0 : for (SCCOL nCol = rRange.aStart.Col(); nCol <= rRange.aEnd.Col(); ++nCol)
203 0 : aSet.set(nTab, nCol, nRow1, nRow2, true);
204 : }
205 :
206 0 : ScriptTypeAggregator aAction(*this);
207 0 : aSet.executeAction(aAction);
208 0 : return aAction.getScriptType();
209 0 : }
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|