xref: /AOO41X/main/sw/source/core/access/textmarkuphelper.cxx (revision efeef26f81c84063fb0a91bde3856d4a51172d90)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sw.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <textmarkuphelper.hxx>
27cdf0e10cSrcweir #include <accportions.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <vector>
30cdf0e10cSrcweir #include <algorithm>
31cdf0e10cSrcweir #include <comphelper/stlunosequence.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <errhdl.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/text/TextMarkupType.hpp>
36cdf0e10cSrcweir #include <com/sun/star/accessibility/TextSegment.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <ndtxt.hxx>
39cdf0e10cSrcweir #include <wrong.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace com::sun::star;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // helper functions
44cdf0e10cSrcweir namespace {
getTextMarkupList(const SwTxtNode & rTxtNode,const sal_Int32 nTextMarkupType)45cdf0e10cSrcweir     const SwWrongList* getTextMarkupList( const SwTxtNode& rTxtNode,
46cdf0e10cSrcweir                                           const sal_Int32 nTextMarkupType )
47cdf0e10cSrcweir         throw (::com::sun::star::lang::IllegalArgumentException,
48cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException)
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         const SwWrongList* pTextMarkupList( 0 );
51cdf0e10cSrcweir         switch ( nTextMarkupType )
52cdf0e10cSrcweir         {
53cdf0e10cSrcweir             case text::TextMarkupType::SPELLCHECK:
54cdf0e10cSrcweir             {
55cdf0e10cSrcweir                 pTextMarkupList = rTxtNode.GetWrong();
56cdf0e10cSrcweir             }
57cdf0e10cSrcweir             break;
58cdf0e10cSrcweir             case text::TextMarkupType::PROOFREADING:
59cdf0e10cSrcweir             {
60cdf0e10cSrcweir                 // support not implemented yet
61cdf0e10cSrcweir                 pTextMarkupList = 0;
62cdf0e10cSrcweir             }
63cdf0e10cSrcweir             break;
64cdf0e10cSrcweir             case text::TextMarkupType::SMARTTAG:
65cdf0e10cSrcweir             {
66cdf0e10cSrcweir                 // support not implemented yet
67cdf0e10cSrcweir                 pTextMarkupList = 0;
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir             break;
70cdf0e10cSrcweir             default:
71cdf0e10cSrcweir             {
72cdf0e10cSrcweir                 throw lang::IllegalArgumentException();
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         return pTextMarkupList;
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir // implementation of class <SwTextMarkupoHelper>
SwTextMarkupHelper(const SwAccessiblePortionData & rPortionData,const SwTxtNode & rTxtNode)81cdf0e10cSrcweir SwTextMarkupHelper::SwTextMarkupHelper( const SwAccessiblePortionData& rPortionData,
82cdf0e10cSrcweir                                         const SwTxtNode& rTxtNode )
83cdf0e10cSrcweir     : mrPortionData( rPortionData )
84cdf0e10cSrcweir     // --> OD 2010-02-19 #i108125#
85cdf0e10cSrcweir     , mpTxtNode( &rTxtNode )
86cdf0e10cSrcweir     , mpTextMarkupList( 0 )
87cdf0e10cSrcweir     // <--
88cdf0e10cSrcweir {
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir // --> OD 2010-02-19 #i108125#
SwTextMarkupHelper(const SwAccessiblePortionData & rPortionData,const SwWrongList & rTextMarkupList)92cdf0e10cSrcweir SwTextMarkupHelper::SwTextMarkupHelper( const SwAccessiblePortionData& rPortionData,
93cdf0e10cSrcweir                                         const SwWrongList& rTextMarkupList )
94cdf0e10cSrcweir     : mrPortionData( rPortionData )
95cdf0e10cSrcweir     , mpTxtNode( 0 )
96cdf0e10cSrcweir     , mpTextMarkupList( &rTextMarkupList )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir }
99cdf0e10cSrcweir // <--
100cdf0e10cSrcweir 
getTextMarkupCount(const sal_Int32 nTextMarkupType)101cdf0e10cSrcweir sal_Int32 SwTextMarkupHelper::getTextMarkupCount( const sal_Int32 nTextMarkupType )
102cdf0e10cSrcweir         throw (::com::sun::star::lang::IllegalArgumentException,
103cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     sal_Int32 nTextMarkupCount( 0 );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     // --> OD 2010-02-19 #i108125#
108cdf0e10cSrcweir     const SwWrongList* pTextMarkupList =
109cdf0e10cSrcweir                             mpTextMarkupList
110cdf0e10cSrcweir                             ? mpTextMarkupList
111cdf0e10cSrcweir                             : getTextMarkupList( *mpTxtNode, nTextMarkupType );
112cdf0e10cSrcweir     // <--
113cdf0e10cSrcweir     if ( pTextMarkupList )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         nTextMarkupCount = pTextMarkupList->Count();
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     return nTextMarkupCount;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir ::com::sun::star::accessibility::TextSegment
getTextMarkup(const sal_Int32 nTextMarkupIndex,const sal_Int32 nTextMarkupType)121cdf0e10cSrcweir         SwTextMarkupHelper::getTextMarkup( const sal_Int32 nTextMarkupIndex,
122cdf0e10cSrcweir                                            const sal_Int32 nTextMarkupType )
123cdf0e10cSrcweir         throw (::com::sun::star::lang::IndexOutOfBoundsException,
124cdf0e10cSrcweir                ::com::sun::star::lang::IllegalArgumentException,
125cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     if ( nTextMarkupIndex >= getTextMarkupCount( nTextMarkupType ) ||
128cdf0e10cSrcweir          nTextMarkupIndex < 0 )
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         throw lang::IndexOutOfBoundsException();
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     ::com::sun::star::accessibility::TextSegment aTextMarkupSegment;
134cdf0e10cSrcweir     aTextMarkupSegment.SegmentStart = -1;
135cdf0e10cSrcweir     aTextMarkupSegment.SegmentEnd = -1;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     // --> OD 2010-02-19 #i108125#
138cdf0e10cSrcweir     const SwWrongList* pTextMarkupList =
139cdf0e10cSrcweir                             mpTextMarkupList
140cdf0e10cSrcweir                             ? mpTextMarkupList
141cdf0e10cSrcweir                             : getTextMarkupList( *mpTxtNode, nTextMarkupType );
142cdf0e10cSrcweir     // <--
143cdf0e10cSrcweir     if ( pTextMarkupList )
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         const SwWrongArea* pTextMarkup =
146cdf0e10cSrcweir                 pTextMarkupList->GetElement( static_cast<sal_uInt16>(nTextMarkupIndex) );
147cdf0e10cSrcweir         if ( pTextMarkup )
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             const ::rtl::OUString rText = mrPortionData.GetAccessibleString();
150cdf0e10cSrcweir             const sal_Int32 nStartPos =
151cdf0e10cSrcweir                             mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos );
152cdf0e10cSrcweir             const sal_Int32 nEndPos =
153cdf0e10cSrcweir                             mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos + pTextMarkup->mnLen );
154cdf0e10cSrcweir             aTextMarkupSegment.SegmentText = rText.copy( nStartPos, nEndPos - nStartPos );
155cdf0e10cSrcweir             aTextMarkupSegment.SegmentStart = nStartPos;
156cdf0e10cSrcweir             aTextMarkupSegment.SegmentEnd = nEndPos;
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         else
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             ASSERT( false,
161cdf0e10cSrcweir                     "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     return aTextMarkupSegment;
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::com::sun::star::accessibility::TextSegment >
getTextMarkupAtIndex(const sal_Int32 nCharIndex,const sal_Int32 nTextMarkupType)169cdf0e10cSrcweir         SwTextMarkupHelper::getTextMarkupAtIndex( const sal_Int32 nCharIndex,
170cdf0e10cSrcweir                                                   const sal_Int32 nTextMarkupType )
171cdf0e10cSrcweir         throw (::com::sun::star::lang::IndexOutOfBoundsException,
172cdf0e10cSrcweir                ::com::sun::star::lang::IllegalArgumentException,
173cdf0e10cSrcweir                ::com::sun::star::uno::RuntimeException)
174cdf0e10cSrcweir {
175cdf0e10cSrcweir     // assumption:
176cdf0e10cSrcweir     // value of <nCharIndex> is in range [0..length of accessible text)
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     const sal_uInt16 nCoreCharIndex = mrPortionData.GetModelPosition( nCharIndex );
179cdf0e10cSrcweir     // Handling of portions with core length == 0 at the beginning of the
180cdf0e10cSrcweir     // paragraph - e.g. numbering portion.
181cdf0e10cSrcweir     if ( mrPortionData.GetAccessiblePosition( nCoreCharIndex ) > nCharIndex )
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         return uno::Sequence< ::com::sun::star::accessibility::TextSegment >();
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     // --> OD 2010-02-19 #i108125#
187cdf0e10cSrcweir     const SwWrongList* pTextMarkupList =
188cdf0e10cSrcweir                             mpTextMarkupList
189cdf0e10cSrcweir                             ? mpTextMarkupList
190cdf0e10cSrcweir                             : getTextMarkupList( *mpTxtNode, nTextMarkupType );
191cdf0e10cSrcweir     // <--
192cdf0e10cSrcweir     ::std::vector< ::com::sun::star::accessibility::TextSegment > aTmpTextMarkups;
193cdf0e10cSrcweir     if ( pTextMarkupList )
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         const ::rtl::OUString rText = mrPortionData.GetAccessibleString();
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         const sal_uInt16 nTextMarkupCount = pTextMarkupList->Count();
198cdf0e10cSrcweir         for ( sal_uInt16 nTextMarkupIdx = 0; nTextMarkupIdx < nTextMarkupCount; ++nTextMarkupIdx )
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             const SwWrongArea* pTextMarkup =
201cdf0e10cSrcweir                     pTextMarkupList->GetElement( static_cast<sal_uInt16>(nTextMarkupIdx) );
202cdf0e10cSrcweir             ASSERT( pTextMarkup,
203cdf0e10cSrcweir                     "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
204cdf0e10cSrcweir             if ( pTextMarkup &&
205cdf0e10cSrcweir                  pTextMarkup->mnPos <= nCoreCharIndex &&
206cdf0e10cSrcweir                  nCoreCharIndex < ( pTextMarkup->mnPos + pTextMarkup->mnLen ) )
207cdf0e10cSrcweir             {
208cdf0e10cSrcweir                 const sal_Int32 nStartPos =
209cdf0e10cSrcweir                     mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos );
210cdf0e10cSrcweir                 const sal_Int32 nEndPos =
211cdf0e10cSrcweir                     mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos + pTextMarkup->mnLen );
212cdf0e10cSrcweir                 ::com::sun::star::accessibility::TextSegment aTextMarkupSegment;
213cdf0e10cSrcweir                 aTextMarkupSegment.SegmentText = rText.copy( nStartPos, nEndPos - nStartPos );
214cdf0e10cSrcweir                 aTextMarkupSegment.SegmentStart = nStartPos;
215cdf0e10cSrcweir                 aTextMarkupSegment.SegmentEnd = nEndPos;
216cdf0e10cSrcweir                 aTmpTextMarkups.push_back( aTextMarkupSegment );
217cdf0e10cSrcweir             }
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir     }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     uno::Sequence< ::com::sun::star::accessibility::TextSegment > aTextMarkups(
222cdf0e10cSrcweir                                                     aTmpTextMarkups.size() );
223cdf0e10cSrcweir     ::std::copy( aTmpTextMarkups.begin(), aTmpTextMarkups.end(),
224cdf0e10cSrcweir                  ::comphelper::stl_begin( aTextMarkups ) );
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     return aTextMarkups;
227cdf0e10cSrcweir }
228