android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / access / textmarkuphelper.cxx
blobae221f49983105fc39649129667afb8ae739fc3b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
23 #include <vector>
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>
32 #include <ndtxt.hxx>
33 #include <wrong.hxx>
35 using namespace com::sun::star;
37 // helper functions
38 namespace {
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;
50 break;
51 case text::TextMarkupType::PROOFREADING:
53 // support not implemented yet
54 return nullptr;
56 break;
57 case text::TextMarkupType::SMARTTAG:
59 // support not implemented yet
60 return nullptr;
62 break;
63 default:
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 )
80 // #i108125#
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;
93 if (mpTextMarkupList)
95 pIter.reset(new sw::WrongListIteratorCounter(*mpTextMarkupList));
97 else
99 assert(m_pTextFrame);
100 SwWrongList const* (SwTextNode::*const pGetWrongList)() const = getTextMarkupFunc(nTextMarkupType);
101 if (pGetWrongList)
103 pIter.reset(new sw::WrongListIteratorCounter(*m_pTextFrame, pGetWrongList));
107 return pIter;
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))
123 nTextMarkupCount++;
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);
144 if (pIter)
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)
159 oElement = oIndices;
161 nIndexInPortion++;
164 nIterIndex++;
167 if (oElement)
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;
178 else
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 )
191 // assumption:
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;
204 if (pIter)
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));
211 if (oElement &&
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: */