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 26602 : const uno::Reference< i18n::XBreakIterator >& ScDocument::GetBreakIterator()
46 : {
47 26602 : if ( !pScriptTypeData )
48 242 : pScriptTypeData = new ScScriptTypeData;
49 26602 : if ( !pScriptTypeData->xBreakIter.is() )
50 : {
51 242 : pScriptTypeData->xBreakIter = i18n::BreakIterator::create( comphelper::getProcessComponentContext() );
52 : }
53 26602 : 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 26608 : sal_uInt8 ScDocument::GetStringScriptType( const OUString& rString )
82 : {
83 :
84 26608 : sal_uInt8 nRet = 0;
85 26608 : if (!rString.isEmpty())
86 : {
87 26366 : uno::Reference<i18n::XBreakIterator> xBreakIter = GetBreakIterator();
88 26366 : if ( xBreakIter.is() )
89 : {
90 26366 : sal_Int32 nLen = rString.getLength();
91 :
92 26366 : sal_Int32 nPos = 0;
93 26375 : do
94 : {
95 26375 : sal_Int16 nType = xBreakIter->getScriptType( rString, nPos );
96 26375 : switch ( nType )
97 : {
98 : case i18n::ScriptType::LATIN:
99 26337 : nRet |= SCRIPTTYPE_LATIN;
100 26337 : break;
101 : case i18n::ScriptType::ASIAN:
102 1 : nRet |= SCRIPTTYPE_ASIAN;
103 1 : break;
104 : case i18n::ScriptType::COMPLEX:
105 0 : nRet |= SCRIPTTYPE_COMPLEX;
106 0 : break;
107 : // WEAK is ignored
108 : }
109 26375 : nPos = xBreakIter->endOfScript( rString, nPos, nType );
110 : }
111 26375 : while ( nPos >= 0 && nPos < nLen );
112 26366 : }
113 : }
114 26608 : return nRet;
115 : }
116 :
117 16829 : sal_uInt8 ScDocument::GetCellScriptType( const ScAddress& rPos, sal_uLong nNumberFormat )
118 : {
119 16829 : sal_uInt8 nStored = GetScriptType(rPos);
120 16829 : if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
121 13763 : return nStored; // use stored value
122 :
123 : Color* pColor;
124 3066 : OUString aStr = ScCellFormat::GetString(*this, rPos, nNumberFormat, &pColor, *xPoolHelper->GetFormTable());
125 :
126 3066 : sal_uInt8 nRet = GetStringScriptType( aStr );
127 :
128 3066 : SetScriptType(rPos, nRet); // store for later calls
129 :
130 3066 : return nRet;
131 : }
132 :
133 2390 : 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 2390 : ScAddress aPos(nCol, nRow, nTab);
138 2390 : sal_uInt8 nStored = GetScriptType(aPos);
139 2390 : if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
140 2168 : return nStored; // use stored value
141 :
142 : // include number formats from conditional formatting
143 :
144 222 : const ScPatternAttr* pPattern = GetPattern( nCol, nRow, nTab );
145 222 : if (!pPattern) return 0;
146 222 : const SfxItemSet* pCondSet = NULL;
147 222 : if ( !((const ScCondFormatItem&)pPattern->GetItem(ATTR_CONDITIONAL)).GetCondFormatData().empty() )
148 21 : pCondSet = GetCondResult( nCol, nRow, nTab );
149 :
150 222 : sal_uLong nFormat = pPattern->GetNumberFormat( xPoolHelper->GetFormTable(), pCondSet );
151 :
152 222 : return GetCellScriptType(aPos, nFormat);
153 : }
154 :
155 : namespace {
156 :
157 4 : class ScriptTypeAggregator : public sc::ColumnSpanSet::Action
158 : {
159 : ScDocument& mrDoc;
160 : sc::ColumnBlockPosition maBlockPos;
161 : sal_uInt8 mnScriptType;
162 :
163 : public:
164 4 : ScriptTypeAggregator(ScDocument& rDoc) : mrDoc(rDoc), mnScriptType(0) {}
165 :
166 12 : virtual void startColumn(SCTAB nTab, SCCOL nCol) SAL_OVERRIDE
167 : {
168 12 : mrDoc.InitColumnBlockPosition(maBlockPos, nTab, nCol);
169 12 : }
170 :
171 26 : virtual void execute(const ScAddress& rPos, SCROW nLength, bool bVal) SAL_OVERRIDE
172 : {
173 26 : if (!bVal)
174 40 : return;
175 :
176 12 : mnScriptType |= mrDoc.GetRangeScriptType(maBlockPos, rPos, nLength);
177 : };
178 :
179 4 : sal_uInt8 getScriptType() const { return mnScriptType; }
180 : };
181 :
182 : }
183 :
184 12 : sal_uInt8 ScDocument::GetRangeScriptType(
185 : sc::ColumnBlockPosition& rBlockPos, const ScAddress& rPos, SCROW nLength )
186 : {
187 12 : if (!TableExists(rPos.Tab()))
188 0 : return 0;
189 :
190 12 : return maTabs[rPos.Tab()]->GetRangeScriptType(rBlockPos, rPos.Col(), rPos.Row(), rPos.Row()+nLength-1);
191 : }
192 :
193 4 : sal_uInt8 ScDocument::GetRangeScriptType( const ScRangeList& rRanges )
194 : {
195 4 : sc::ColumnSpanSet aSet(false);
196 8 : for (size_t i = 0, n = rRanges.size(); i < n; ++i)
197 : {
198 4 : const ScRange& rRange = *rRanges[i];
199 4 : SCTAB nTab = rRange.aStart.Tab();
200 4 : SCROW nRow1 = rRange.aStart.Row();
201 4 : SCROW nRow2 = rRange.aEnd.Row();
202 16 : for (SCCOL nCol = rRange.aStart.Col(); nCol <= rRange.aEnd.Col(); ++nCol)
203 12 : aSet.set(nTab, nCol, nRow1, nRow2, true);
204 : }
205 :
206 8 : ScriptTypeAggregator aAction(*this);
207 4 : aSet.executeAction(aAction);
208 8 : return aAction.getScriptType();
209 102 : }
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|