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 "cell.hxx"
30 : #include "cellform.hxx"
31 : #include "patattr.hxx"
32 : #include "scrdata.hxx"
33 : #include "poolhelp.hxx"
34 : #include "attrib.hxx"
35 :
36 : using namespace com::sun::star;
37 :
38 : //
39 : // this file is compiled with exceptions enabled
40 : // put functions here that need exceptions!
41 : //
42 :
43 : // -----------------------------------------------------------------------
44 :
45 2728 : const uno::Reference< i18n::XBreakIterator >& ScDocument::GetBreakIterator()
46 : {
47 2728 : if ( !pScriptTypeData )
48 40 : pScriptTypeData = new ScScriptTypeData;
49 2728 : if ( !pScriptTypeData->xBreakIter.is() )
50 : {
51 40 : pScriptTypeData->xBreakIter = i18n::BreakIterator::create( comphelper::getComponentContext(xServiceManager) );
52 : }
53 2728 : return pScriptTypeData->xBreakIter;
54 : }
55 :
56 0 : bool ScDocument::HasStringWeakCharacters( const rtl::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 : while ( nPos >= 0 && nPos < nLen );
75 0 : }
76 : }
77 :
78 0 : return false; // none found
79 : }
80 :
81 2713 : sal_uInt8 ScDocument::GetStringScriptType( const rtl::OUString& rString )
82 : {
83 :
84 2713 : sal_uInt8 nRet = 0;
85 2713 : if (!rString.isEmpty())
86 : {
87 2712 : uno::Reference<i18n::XBreakIterator> xBreakIter = GetBreakIterator();
88 2712 : if ( xBreakIter.is() )
89 : {
90 2712 : sal_Int32 nLen = rString.getLength();
91 :
92 2712 : sal_Int32 nPos = 0;
93 2720 : do
94 : {
95 2720 : sal_Int16 nType = xBreakIter->getScriptType( rString, nPos );
96 2720 : switch ( nType )
97 : {
98 : case i18n::ScriptType::LATIN:
99 2684 : nRet |= SCRIPTTYPE_LATIN;
100 2684 : 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 2720 : nPos = xBreakIter->endOfScript( rString, nPos, nType );
110 : }
111 : while ( nPos >= 0 && nPos < nLen );
112 2712 : }
113 : }
114 2713 : return nRet;
115 : }
116 :
117 2691 : sal_uInt8 ScDocument::GetCellScriptType( ScBaseCell* pCell, sal_uLong nNumberFormat )
118 : {
119 2691 : if ( !pCell )
120 0 : return 0; // empty
121 :
122 2691 : sal_uInt8 nStored = pCell->GetScriptType();
123 2691 : if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
124 0 : return nStored; // use stored value
125 :
126 2691 : rtl::OUString aStr;
127 : Color* pColor;
128 2691 : ScCellFormat::GetString( pCell, nNumberFormat, aStr, &pColor, *xPoolHelper->GetFormTable() );
129 :
130 2691 : sal_uInt8 nRet = GetStringScriptType( aStr );
131 :
132 2691 : pCell->SetScriptType( nRet ); // store for later calls
133 :
134 2691 : return nRet;
135 : }
136 :
137 9924 : sal_uInt8 ScDocument::GetScriptType( SCCOL nCol, SCROW nRow, SCTAB nTab, ScBaseCell* pCell )
138 : {
139 : // if cell is not passed, take from document
140 :
141 9924 : if (!pCell)
142 : {
143 0 : pCell = GetCell( ScAddress( nCol, nRow, nTab ) );
144 0 : if ( !pCell )
145 0 : return 0; // empty
146 : }
147 :
148 : // if script type is set, don't have to get number formats
149 :
150 9924 : sal_uInt8 nStored = pCell->GetScriptType();
151 9924 : if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
152 7234 : return nStored; // use stored value
153 :
154 : // include number formats from conditional formatting
155 :
156 2690 : const ScPatternAttr* pPattern = GetPattern( nCol, nRow, nTab );
157 2690 : if (!pPattern) return 0;
158 2690 : const SfxItemSet* pCondSet = NULL;
159 2690 : if ( !((const ScCondFormatItem&)pPattern->GetItem(ATTR_CONDITIONAL)).GetCondFormatData().empty() )
160 0 : pCondSet = GetCondResult( nCol, nRow, nTab );
161 :
162 2690 : sal_uLong nFormat = pPattern->GetNumberFormat( xPoolHelper->GetFormTable(), pCondSet );
163 2690 : return GetCellScriptType( pCell, nFormat );
164 : }
165 :
166 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|