Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <textmarkuphelper.hxx>
31 : : #include <accportions.hxx>
32 : :
33 : : #include <vector>
34 : : #include <algorithm>
35 : : #include <comphelper/stlunosequence.hxx>
36 : :
37 : :
38 : : #include <com/sun/star/text/TextMarkupType.hpp>
39 : : #include <com/sun/star/accessibility/TextSegment.hpp>
40 : :
41 : : #include <ndtxt.hxx>
42 : : #include <wrong.hxx>
43 : :
44 : : using namespace com::sun::star;
45 : :
46 : : // helper functions
47 : : namespace {
48 : 0 : const SwWrongList* getTextMarkupList( const SwTxtNode& rTxtNode,
49 : : const sal_Int32 nTextMarkupType )
50 : : throw (::com::sun::star::lang::IllegalArgumentException,
51 : : ::com::sun::star::uno::RuntimeException)
52 : : {
53 : 0 : const SwWrongList* pTextMarkupList( 0 );
54 [ # # # # ]: 0 : switch ( nTextMarkupType )
55 : : {
56 : : case text::TextMarkupType::SPELLCHECK:
57 : : {
58 : 0 : pTextMarkupList = rTxtNode.GetWrong();
59 : : }
60 : 0 : break;
61 : : case text::TextMarkupType::PROOFREADING:
62 : : {
63 : : // support not implemented yet
64 : 0 : pTextMarkupList = 0;
65 : : }
66 : 0 : break;
67 : : case text::TextMarkupType::SMARTTAG:
68 : : {
69 : : // support not implemented yet
70 : 0 : pTextMarkupList = 0;
71 : : }
72 : 0 : break;
73 : : default:
74 : : {
75 [ # # ]: 0 : throw lang::IllegalArgumentException();
76 : : }
77 : : }
78 : :
79 : 0 : return pTextMarkupList;
80 : : }
81 : : }
82 : :
83 : : // implementation of class <SwTextMarkupoHelper>
84 : 0 : SwTextMarkupHelper::SwTextMarkupHelper( const SwAccessiblePortionData& rPortionData,
85 : : const SwTxtNode& rTxtNode )
86 : : : mrPortionData( rPortionData )
87 : : // #i108125#
88 : : , mpTxtNode( &rTxtNode )
89 : 0 : , mpTextMarkupList( 0 )
90 : : {
91 : 0 : }
92 : :
93 : : // #i108125#
94 : 0 : SwTextMarkupHelper::SwTextMarkupHelper( const SwAccessiblePortionData& rPortionData,
95 : : const SwWrongList& rTextMarkupList )
96 : : : mrPortionData( rPortionData )
97 : : , mpTxtNode( 0 )
98 : 0 : , mpTextMarkupList( &rTextMarkupList )
99 : : {
100 : 0 : }
101 : :
102 : 0 : sal_Int32 SwTextMarkupHelper::getTextMarkupCount( const sal_Int32 nTextMarkupType )
103 : : throw (::com::sun::star::lang::IllegalArgumentException,
104 : : ::com::sun::star::uno::RuntimeException)
105 : : {
106 : 0 : sal_Int32 nTextMarkupCount( 0 );
107 : :
108 : : // #i108125#
109 : : const SwWrongList* pTextMarkupList =
110 : : mpTextMarkupList
111 : : ? mpTextMarkupList
112 [ # # ]: 0 : : getTextMarkupList( *mpTxtNode, nTextMarkupType );
113 [ # # ]: 0 : if ( pTextMarkupList )
114 : : {
115 : 0 : nTextMarkupCount = pTextMarkupList->Count();
116 : : }
117 : :
118 : 0 : return nTextMarkupCount;
119 : : }
120 : : ::com::sun::star::accessibility::TextSegment
121 : 0 : SwTextMarkupHelper::getTextMarkup( const sal_Int32 nTextMarkupIndex,
122 : : const sal_Int32 nTextMarkupType )
123 : : throw (::com::sun::star::lang::IndexOutOfBoundsException,
124 : : ::com::sun::star::lang::IllegalArgumentException,
125 : : ::com::sun::star::uno::RuntimeException)
126 : : {
127 [ # # ][ # # ]: 0 : if ( nTextMarkupIndex >= getTextMarkupCount( nTextMarkupType ) ||
[ # # ]
128 : : nTextMarkupIndex < 0 )
129 : : {
130 [ # # ]: 0 : throw lang::IndexOutOfBoundsException();
131 : : }
132 : :
133 : 0 : ::com::sun::star::accessibility::TextSegment aTextMarkupSegment;
134 : 0 : aTextMarkupSegment.SegmentStart = -1;
135 : 0 : aTextMarkupSegment.SegmentEnd = -1;
136 : :
137 : : // #i108125#
138 : : const SwWrongList* pTextMarkupList =
139 : : mpTextMarkupList
140 : : ? mpTextMarkupList
141 [ # # ][ # # ]: 0 : : getTextMarkupList( *mpTxtNode, nTextMarkupType );
142 [ # # ]: 0 : if ( pTextMarkupList )
143 : : {
144 : : const SwWrongArea* pTextMarkup =
145 [ # # ]: 0 : pTextMarkupList->GetElement( static_cast<sal_uInt16>(nTextMarkupIndex) );
146 [ # # ]: 0 : if ( pTextMarkup )
147 : : {
148 [ # # ]: 0 : const ::rtl::OUString rText = mrPortionData.GetAccessibleString();
149 : : const sal_Int32 nStartPos =
150 [ # # ]: 0 : mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos );
151 : : const sal_Int32 nEndPos =
152 [ # # ]: 0 : mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos + pTextMarkup->mnLen );
153 : 0 : aTextMarkupSegment.SegmentText = rText.copy( nStartPos, nEndPos - nStartPos );
154 : 0 : aTextMarkupSegment.SegmentStart = nStartPos;
155 : 0 : aTextMarkupSegment.SegmentEnd = nEndPos;
156 : : }
157 : : else
158 : : {
159 : : OSL_FAIL( "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
160 : : }
161 : : }
162 : :
163 : 0 : return aTextMarkupSegment;
164 : : }
165 : :
166 : : ::com::sun::star::uno::Sequence< ::com::sun::star::accessibility::TextSegment >
167 : 0 : SwTextMarkupHelper::getTextMarkupAtIndex( const sal_Int32 nCharIndex,
168 : : const sal_Int32 nTextMarkupType )
169 : : throw (::com::sun::star::lang::IndexOutOfBoundsException,
170 : : ::com::sun::star::lang::IllegalArgumentException,
171 : : ::com::sun::star::uno::RuntimeException)
172 : : {
173 : : // assumption:
174 : : // value of <nCharIndex> is in range [0..length of accessible text)
175 : :
176 [ # # ]: 0 : const sal_uInt16 nCoreCharIndex = mrPortionData.GetModelPosition( nCharIndex );
177 : : // Handling of portions with core length == 0 at the beginning of the
178 : : // paragraph - e.g. numbering portion.
179 [ # # ][ # # ]: 0 : if ( mrPortionData.GetAccessiblePosition( nCoreCharIndex ) > nCharIndex )
180 : : {
181 [ # # ]: 0 : return uno::Sequence< ::com::sun::star::accessibility::TextSegment >();
182 : : }
183 : :
184 : : // #i108125#
185 : : const SwWrongList* pTextMarkupList =
186 : : mpTextMarkupList
187 : : ? mpTextMarkupList
188 [ # # ][ # # ]: 0 : : getTextMarkupList( *mpTxtNode, nTextMarkupType );
189 [ # # ]: 0 : ::std::vector< ::com::sun::star::accessibility::TextSegment > aTmpTextMarkups;
190 [ # # ]: 0 : if ( pTextMarkupList )
191 : : {
192 [ # # ]: 0 : const ::rtl::OUString rText = mrPortionData.GetAccessibleString();
193 : :
194 : 0 : const sal_uInt16 nTextMarkupCount = pTextMarkupList->Count();
195 [ # # ]: 0 : for ( sal_uInt16 nTextMarkupIdx = 0; nTextMarkupIdx < nTextMarkupCount; ++nTextMarkupIdx )
196 : : {
197 : : const SwWrongArea* pTextMarkup =
198 [ # # ]: 0 : pTextMarkupList->GetElement( static_cast<sal_uInt16>(nTextMarkupIdx) );
199 : : OSL_ENSURE( pTextMarkup,
200 : : "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
201 [ # # ][ # # ]: 0 : if ( pTextMarkup &&
[ # # ]
202 : : pTextMarkup->mnPos <= nCoreCharIndex &&
203 : : nCoreCharIndex < ( pTextMarkup->mnPos + pTextMarkup->mnLen ) )
204 : : {
205 : : const sal_Int32 nStartPos =
206 [ # # ]: 0 : mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos );
207 : : const sal_Int32 nEndPos =
208 [ # # ]: 0 : mrPortionData.GetAccessiblePosition( pTextMarkup->mnPos + pTextMarkup->mnLen );
209 : 0 : ::com::sun::star::accessibility::TextSegment aTextMarkupSegment;
210 : 0 : aTextMarkupSegment.SegmentText = rText.copy( nStartPos, nEndPos - nStartPos );
211 : 0 : aTextMarkupSegment.SegmentStart = nStartPos;
212 : 0 : aTextMarkupSegment.SegmentEnd = nEndPos;
213 [ # # ]: 0 : aTmpTextMarkups.push_back( aTextMarkupSegment );
214 : : }
215 : 0 : }
216 : : }
217 : :
218 : : uno::Sequence< ::com::sun::star::accessibility::TextSegment > aTextMarkups(
219 [ # # ]: 0 : aTmpTextMarkups.size() );
220 : : ::std::copy( aTmpTextMarkups.begin(), aTmpTextMarkups.end(),
221 [ # # ][ # # ]: 0 : ::comphelper::stl_begin( aTextMarkups ) );
222 : :
223 [ # # ][ # # ]: 0 : return aTextMarkups;
224 : : }
225 : :
226 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|