LibreOffice Module sw (master) 1
textmarkuphelper.cxx
Go to the documentation of this file.
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 "textmarkuphelper.hxx"
21#include "accportions.hxx"
22
23#include <vector>
24
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>
29
31#include <osl/diagnose.h>
32#include <ndtxt.hxx>
33#include <wrong.hxx>
34
35using namespace com::sun::star;
36
37// helper functions
38namespace {
41 SwWrongList const* (SwTextNode::*
42 getTextMarkupFunc(const sal_Int32 nTextMarkupType))() const
43 {
44 switch ( nTextMarkupType )
45 {
46 case text::TextMarkupType::SPELLCHECK:
47 {
49 }
50 break;
51 case text::TextMarkupType::PROOFREADING:
52 {
53 // support not implemented yet
54 return nullptr;
55 }
56 break;
57 case text::TextMarkupType::SMARTTAG:
58 {
59 // support not implemented yet
60 return nullptr;
61 }
62 break;
63 default:
64 {
65 throw lang::IllegalArgumentException();
66 }
67 }
68 }
69}
70
71// implementation of class <SwTextMarkupoHelper>
73 const SwTextFrame& rTextFrame)
74 : mrPortionData( rPortionData )
75 , m_pTextFrame(&rTextFrame)
76 , mpTextMarkupList( nullptr )
77{
78}
79
80// #i108125#
82 const SwWrongList& rTextMarkupList )
83 : mrPortionData( rPortionData )
84 , m_pTextFrame( nullptr )
85 , mpTextMarkupList( &rTextMarkupList )
86{
87}
88
89
90std::unique_ptr<sw::WrongListIteratorCounter> SwTextMarkupHelper::getIterator(sal_Int32 nTextMarkupType)
91{
92 std::unique_ptr<sw::WrongListIteratorCounter> pIter;
94 {
96 }
97 else
98 {
99 assert(m_pTextFrame);
100 SwWrongList const* (SwTextNode::*const pGetWrongList)() const = getTextMarkupFunc(nTextMarkupType);
101 if (pGetWrongList)
102 {
103 pIter.reset(new sw::WrongListIteratorCounter(*m_pTextFrame, pGetWrongList));
104 }
105 }
106
107 return pIter;
108}
109
110
111sal_Int32 SwTextMarkupHelper::getTextMarkupCount( const sal_Int32 nTextMarkupType )
112{
113 sal_Int32 nTextMarkupCount( 0 );
114
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++)
120 {
121 std::optional<std::pair<TextFrameIndex, TextFrameIndex>> oIndices = pIter->GetElementAt(i);
122 if (oIndices && mrPortionData.IsValidCorePosition(oIndices->first) && mrPortionData.IsValidCorePosition(oIndices->second))
123 nTextMarkupCount++;
124 }
125
126 return nTextMarkupCount;
127}
128
129css::accessibility::TextSegment
130 SwTextMarkupHelper::getTextMarkup( const sal_Int32 nTextMarkupIndex,
131 const sal_Int32 nTextMarkupType )
132{
133 if ( nTextMarkupIndex >= getTextMarkupCount( nTextMarkupType ) ||
134 nTextMarkupIndex < 0 )
135 {
136 throw lang::IndexOutOfBoundsException();
137 }
138
139 css::accessibility::TextSegment aTextMarkupSegment;
140 aTextMarkupSegment.SegmentStart = -1;
141 aTextMarkupSegment.SegmentEnd = -1;
142
143 std::unique_ptr<sw::WrongListIteratorCounter> pIter = getIterator(nTextMarkupType);
144 if (pIter)
145 {
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)
151 {
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))
157 {
158 if (nIndexInPortion == nTextMarkupIndex)
159 oElement = oIndices;
160
161 nIndexInPortion++;
162 }
163
164 nIterIndex++;
165 }
166
167 if (oElement)
168 {
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;
177 }
178 else
179 {
180 OSL_FAIL( "<SwTextMarkupHelper::getTextMarkup(..)> - missing <SwWrongArea> instance" );
181 }
182 }
183
184 return aTextMarkupSegment;
185}
186
187css::uno::Sequence< css::accessibility::TextSegment >
188 SwTextMarkupHelper::getTextMarkupAtIndex( const sal_Int32 nCharIndex,
189 const sal_Int32 nTextMarkupType )
190{
191 // assumption:
192 // value of <nCharIndex> is in range [0..length of accessible text)
193
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 )
198 {
199 return uno::Sequence< css::accessibility::TextSegment >();
200 }
201
202 std::unique_ptr<sw::WrongListIteratorCounter> pIter = getIterator(nTextMarkupType);
203 std::vector< css::accessibility::TextSegment > aTmpTextMarkups;
204 if (pIter)
205 {
206 const OUString& rText = mrPortionData.GetAccessibleString();
207 sal_uInt16 count(pIter->GetElementCount());
208 for (sal_uInt16 i = 0; i < count; ++i)
209 {
210 auto const oElement(pIter->GetElementAt(i));
211 if (oElement &&
212 oElement->first <= nCoreCharIndex &&
213 nCoreCharIndex < oElement->second)
214 {
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 );
224 }
225 }
226 }
227
228 return comphelper::containerToSequence(aTmpTextMarkups );
229}
230
231/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
collect text portion data from the layout through SwPortionHandler interface
Definition: accportions.hxx:40
TextFrameIndex GetCoreViewPosition(sal_Int32 nPos) const
get the position in the core view string for a given (accessibility) position
const OUString & GetAccessibleString() const
get the text string, as presented by the layout
sal_Int32 GetAccessiblePosition(TextFrameIndex nPos) const
get the position in the accessibility string for a given view position
bool IsValidCorePosition(TextFrameIndex nPos) const
Determine whether this core position is valid for these portions.
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:168
SwTextFrame const * m_pTextFrame
const SwAccessiblePortionData & mrPortionData
SwTextMarkupHelper(const SwAccessiblePortionData &rPortionData, const SwTextFrame &rTextFrame)
sal_Int32 getTextMarkupCount(const sal_Int32 nTextMarkupType)
std::unique_ptr< sw::WrongListIteratorCounter > getIterator(sal_Int32 nTextMarkupType)
css::uno::Sequence< css::accessibility::TextSegment > getTextMarkupAtIndex(const sal_Int32 nCharIndex, const sal_Int32 nTextMarkupType)
css::accessibility::TextSegment getTextMarkup(const sal_Int32 nTextMarkupIndex, const sal_Int32 nTextMarkupType)
const SwWrongList * mpTextMarkupList
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:112
SwWrongList * GetWrong()
Definition: txtedt.cxx:2271
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
int i