1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "textmarkuphelper.hxx"
21 #include "accportions.hxx"
25 #include <com/sun/star/text/TextMarkupType.hpp>
26 #include <com/sun/star/accessibility/TextSegment.hpp>
27 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <comphelper/sequence.hxx>
31 #include <osl/diagnose.h>
35 using namespace com::sun::star
;
39 /// @throws css::lang::IllegalArgumentException
40 /// @throws css::uno::RuntimeException
41 SwWrongList
const* (SwTextNode::*
42 getTextMarkupFunc(const sal_Int32 nTextMarkupType
))() const
44 switch ( nTextMarkupType
)
46 case text::TextMarkupType::SPELLCHECK
:
48 return &SwTextNode::GetWrong
;
51 case text::TextMarkupType::PROOFREADING
:
53 // support not implemented yet
57 case text::TextMarkupType::SMARTTAG
:
59 // support not implemented yet
65 throw lang::IllegalArgumentException();
71 // implementation of class <SwTextMarkupoHelper>
72 SwTextMarkupHelper::SwTextMarkupHelper( const SwAccessiblePortionData
& rPortionData
,
73 const SwTextFrame
& rTextFrame
)
74 : mrPortionData( rPortionData
)
75 , m_pTextFrame(&rTextFrame
)
76 , mpTextMarkupList( nullptr )
81 SwTextMarkupHelper::SwTextMarkupHelper( const SwAccessiblePortionData
& rPortionData
,
82 const SwWrongList
& rTextMarkupList
)
83 : mrPortionData( rPortionData
)
84 , m_pTextFrame( nullptr )
85 , mpTextMarkupList( &rTextMarkupList
)
90 std::unique_ptr
<sw::WrongListIteratorCounter
> SwTextMarkupHelper::getIterator(sal_Int32 nTextMarkupType
)
92 std::unique_ptr
<sw::WrongListIteratorCounter
> pIter
;
95 pIter
.reset(new sw::WrongListIteratorCounter(*mpTextMarkupList
));
100 SwWrongList
const* (SwTextNode::*const pGetWrongList
)() const = getTextMarkupFunc(nTextMarkupType
);
103 pIter
.reset(new sw::WrongListIteratorCounter(*m_pTextFrame
, pGetWrongList
));
111 sal_Int32
SwTextMarkupHelper::getTextMarkupCount( const sal_Int32 nTextMarkupType
)
113 sal_Int32
nTextMarkupCount( 0 );
115 std::unique_ptr
<sw::WrongListIteratorCounter
> pIter
= getIterator(nTextMarkupType
);
116 // iterator may handle all items in the underlying text node in the model, which may be more
117 // than what is in the portion data (e.g. if a paragraph is split across multiple pages),
118 // only take into account those that are in the portion data
119 for (sal_uInt16 i
= 0; i
< pIter
->GetElementCount(); i
++)
121 std::optional
<std::pair
<TextFrameIndex
, TextFrameIndex
>> oIndices
= pIter
->GetElementAt(i
);
122 if (oIndices
&& mrPortionData
.IsValidCorePosition(oIndices
->first
) && mrPortionData
.IsValidCorePosition(oIndices
->second
))
126 return nTextMarkupCount
;
129 css::accessibility::TextSegment
130 SwTextMarkupHelper::getTextMarkup( const sal_Int32 nTextMarkupIndex
,
131 const sal_Int32 nTextMarkupType
)
133 if ( nTextMarkupIndex
>= getTextMarkupCount( nTextMarkupType
) ||
134 nTextMarkupIndex
< 0 )
136 throw lang::IndexOutOfBoundsException();
139 css::accessibility::TextSegment aTextMarkupSegment
;
140 aTextMarkupSegment
.SegmentStart
= -1;
141 aTextMarkupSegment
.SegmentEnd
= -1;
143 std::unique_ptr
<sw::WrongListIteratorCounter
> pIter
= getIterator(nTextMarkupType
);
146 std::optional
<std::pair
<TextFrameIndex
, TextFrameIndex
>> oElement
;
147 const sal_uInt16 nIterElementCount
= pIter
->GetElementCount();
148 sal_Int32 nIndexInPortion
= 0;
149 sal_uInt16 nIterIndex
= 0;
150 while (!oElement
&& nIterIndex
< nIterElementCount
)
152 // iterator may handle all items in the underlying text node in the model, which may be more
153 // than what is in the portion data (e.g. if a paragraph is split across multiple pages),
154 // only take into account those that are in the portion data
155 std::optional
<std::pair
<TextFrameIndex
, TextFrameIndex
>> oIndices
= pIter
->GetElementAt(nIterIndex
);
156 if (oIndices
&& mrPortionData
.IsValidCorePosition(oIndices
->first
) && mrPortionData
.IsValidCorePosition(oIndices
->second
))
158 if (nIndexInPortion
== nTextMarkupIndex
)
169 const OUString
& rText
= mrPortionData
.GetAccessibleString();
170 const sal_Int32 nStartPos
=
171 mrPortionData
.GetAccessiblePosition(oElement
->first
);
172 const sal_Int32 nEndPos
=
173 mrPortionData
.GetAccessiblePosition(oElement
->second
);
174 aTextMarkupSegment
.SegmentText
= rText
.copy( nStartPos
, nEndPos
- nStartPos
);
175 aTextMarkupSegment
.SegmentStart
= nStartPos
;
176 aTextMarkupSegment
.SegmentEnd
= nEndPos
;
180 OSL_FAIL( "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
184 return aTextMarkupSegment
;
187 css::uno::Sequence
< css::accessibility::TextSegment
>
188 SwTextMarkupHelper::getTextMarkupAtIndex( const sal_Int32 nCharIndex
,
189 const sal_Int32 nTextMarkupType
)
192 // value of <nCharIndex> is in range [0..length of accessible text)
194 const TextFrameIndex nCoreCharIndex
= mrPortionData
.GetCoreViewPosition(nCharIndex
);
195 // Handling of portions with core length == 0 at the beginning of the
196 // paragraph - e.g. numbering portion.
197 if ( mrPortionData
.GetAccessiblePosition( nCoreCharIndex
) > nCharIndex
)
199 return uno::Sequence
< css::accessibility::TextSegment
>();
202 std::unique_ptr
<sw::WrongListIteratorCounter
> pIter
= getIterator(nTextMarkupType
);
203 std::vector
< css::accessibility::TextSegment
> aTmpTextMarkups
;
206 const OUString
& rText
= mrPortionData
.GetAccessibleString();
207 sal_uInt16
count(pIter
->GetElementCount());
208 for (sal_uInt16 i
= 0; i
< count
; ++i
)
210 auto const oElement(pIter
->GetElementAt(i
));
212 oElement
->first
<= nCoreCharIndex
&&
213 nCoreCharIndex
< oElement
->second
)
215 const sal_Int32 nStartPos
=
216 mrPortionData
.GetAccessiblePosition(oElement
->first
);
217 const sal_Int32 nEndPos
=
218 mrPortionData
.GetAccessiblePosition(oElement
->second
);
219 css::accessibility::TextSegment aTextMarkupSegment
;
220 aTextMarkupSegment
.SegmentText
= rText
.copy( nStartPos
, nEndPos
- nStartPos
);
221 aTextMarkupSegment
.SegmentStart
= nStartPos
;
222 aTextMarkupSegment
.SegmentEnd
= nEndPos
;
223 aTmpTextMarkups
.push_back( aTextMarkupSegment
);
228 return comphelper::containerToSequence(aTmpTextMarkups
);
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */