1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <drawinglayer/primitive2d/textbreakuphelper.hxx>
#include <drawinglayer/primitive2d/textdecoratedprimitive2d.hxx>
#include <com/sun/star/i18n/BreakIterator.hpp>
#include <comphelper/processfactory.hxx>
#include <com/sun/star/i18n/CharacterIteratorMode.hpp>
#include <com/sun/star/i18n/WordType.hpp>
#include <com/sun/star/i18n/CharType.hpp>
namespace drawinglayer
{
namespace primitive2d
{
TextBreakupHelper::TextBreakupHelper(const TextSimplePortionPrimitive2D& rSource)
: mrSource(rSource),
mxResult(),
maTextLayouter(),
maDecTrans(),
mbNoDXArray(false)
{
OSL_ENSURE(dynamic_cast< const TextSimplePortionPrimitive2D* >(&mrSource), "TextBreakupHelper with illegal primitive created (!)");
maDecTrans = mrSource.getTextTransform();
mbNoDXArray = mrSource.getDXArray().empty();
if(mbNoDXArray)
{
// init TextLayouter when no dxarray
maTextLayouter.setFontAttribute(
mrSource.getFontAttribute(),
maDecTrans.getScale().getX(),
maDecTrans.getScale().getY(),
mrSource.getLocale());
}
}
TextBreakupHelper::~TextBreakupHelper()
{
}
void TextBreakupHelper::breakupPortion(Primitive2DContainer& rTempResult, sal_Int32 nIndex, sal_Int32 nLength, bool bWordLineMode)
{
if(nLength && !(nIndex == mrSource.getTextPosition() && nLength == mrSource.getTextLength()))
{
// prepare values for new portion
basegfx::B2DHomMatrix aNewTransform;
::std::vector< double > aNewDXArray;
const bool bNewStartIsNotOldStart(nIndex > mrSource.getTextPosition());
if(!mbNoDXArray)
{
// prepare new DXArray for the single word
aNewDXArray = ::std::vector< double >(
mrSource.getDXArray().begin() + (nIndex - mrSource.getTextPosition()),
mrSource.getDXArray().begin() + ((nIndex + nLength) - mrSource.getTextPosition()));
}
if(bNewStartIsNotOldStart)
{
// needs to be moved to a new start position
double fOffset(0.0);
if(mbNoDXArray)
{
// evaluate using TextLayouter
fOffset = maTextLayouter.getTextWidth(mrSource.getText(), mrSource.getTextPosition(), nIndex);
}
else
{
// get from DXArray
const sal_Int32 nIndex2(nIndex - mrSource.getTextPosition());
fOffset = mrSource.getDXArray()[nIndex2 - 1];
}
// need offset without FontScale for building the new transformation. The
// new transformation will be multiplied with the current text transformation
// so FontScale would be double
double fOffsetNoScale(fOffset);
const double fFontScaleX(maDecTrans.getScale().getX());
if(!basegfx::fTools::equal(fFontScaleX, 1.0)
&& !basegfx::fTools::equalZero(fFontScaleX))
{
fOffsetNoScale /= fFontScaleX;
}
// apply needed offset to transformation
aNewTransform.translate(fOffsetNoScale, 0.0);
if(!mbNoDXArray)
{
// DXArray values need to be corrected with the offset, too. Here,
// take the scaled offset since the DXArray is scaled
const sal_uInt32 nArraySize(aNewDXArray.size());
for(sal_uInt32 a(0); a < nArraySize; a++)
{
aNewDXArray[a] -= fOffset;
}
}
}
// add text transformation to new transformation
aNewTransform = maDecTrans.getB2DHomMatrix() * aNewTransform;
// callback to allow evtl. changes
const bool bCreate(allowChange(rTempResult.size(), aNewTransform, nIndex, nLength));
if(bCreate)
{
// check if we have a decorated primitive as source
const TextDecoratedPortionPrimitive2D* pTextDecoratedPortionPrimitive2D =
dynamic_cast< const TextDecoratedPortionPrimitive2D* >(&mrSource);
if(pTextDecoratedPortionPrimitive2D)
{
// create a TextDecoratedPortionPrimitive2D
rTempResult.push_back(
new TextDecoratedPortionPrimitive2D(
aNewTransform,
mrSource.getText(),
nIndex,
nLength,
aNewDXArray,
mrSource.getFontAttribute(),
mrSource.getLocale(),
mrSource.getFontColor(),
mrSource.getTextFillColor(),
pTextDecoratedPortionPrimitive2D->getOverlineColor(),
pTextDecoratedPortionPrimitive2D->getTextlineColor(),
pTextDecoratedPortionPrimitive2D->getFontOverline(),
pTextDecoratedPortionPrimitive2D->getFontUnderline(),
pTextDecoratedPortionPrimitive2D->getUnderlineAbove(),
pTextDecoratedPortionPrimitive2D->getTextStrikeout(),
// reset WordLineMode when BreakupUnit::Word is executed; else copy original
!bWordLineMode && pTextDecoratedPortionPrimitive2D->getWordLineMode(),
pTextDecoratedPortionPrimitive2D->getTextEmphasisMark(),
pTextDecoratedPortionPrimitive2D->getEmphasisMarkAbove(),
pTextDecoratedPortionPrimitive2D->getEmphasisMarkBelow(),
pTextDecoratedPortionPrimitive2D->getTextRelief(),
pTextDecoratedPortionPrimitive2D->getShadow()));
}
else
{
// create a SimpleTextPrimitive
rTempResult.push_back(
new TextSimplePortionPrimitive2D(
aNewTransform,
mrSource.getText(),
nIndex,
nLength,
aNewDXArray,
mrSource.getFontAttribute(),
mrSource.getLocale(),
mrSource.getFontColor()));
}
}
}
}
bool TextBreakupHelper::allowChange(sal_uInt32 /*nCount*/, basegfx::B2DHomMatrix& /*rNewTransform*/, sal_uInt32 /*nIndex*/, sal_uInt32 /*nLength*/)
{
return true;
}
void TextBreakupHelper::breakup(BreakupUnit aBreakupUnit)
{
if(mrSource.getTextLength())
{
Primitive2DContainer aTempResult;
static css::uno::Reference< css::i18n::XBreakIterator > xBreakIterator;
if(!xBreakIterator.is())
{
css::uno::Reference< css::uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
xBreakIterator = css::i18n::BreakIterator::create(xContext);
}
const OUString& rTxt = mrSource.getText();
const sal_Int32 nTextLength(mrSource.getTextLength());
const css::lang::Locale& rLocale = mrSource.getLocale();
const sal_Int32 nTextPosition(mrSource.getTextPosition());
sal_Int32 nCurrent(nTextPosition);
switch(aBreakupUnit)
{
case BreakupUnit::Character:
{
sal_Int32 nDone;
sal_Int32 nNextCellBreak(xBreakIterator->nextCharacters(rTxt, nTextPosition, rLocale, css::i18n::CharacterIteratorMode::SKIPCELL, 0, nDone));
sal_Int32 a(nTextPosition);
for(; a < nTextPosition + nTextLength; a++)
{
if(a == nNextCellBreak)
{
breakupPortion(aTempResult, nCurrent, a - nCurrent, false);
nCurrent = a;
nNextCellBreak = xBreakIterator->nextCharacters(rTxt, a, rLocale, css::i18n::CharacterIteratorMode::SKIPCELL, 1, nDone);
}
}
breakupPortion(aTempResult, nCurrent, a - nCurrent, false);
break;
}
case BreakupUnit::Word:
{
css::i18n::Boundary nNextWordBoundary(xBreakIterator->getWordBoundary(rTxt, nTextPosition, rLocale, css::i18n::WordType::ANY_WORD, true));
sal_Int32 a(nTextPosition);
for(; a < nTextPosition + nTextLength; a++)
{
if(a == nNextWordBoundary.endPos)
{
if(a > nCurrent)
{
breakupPortion(aTempResult, nCurrent, a - nCurrent, true);
}
nCurrent = a;
// skip spaces (maybe enhanced with a bool later if needed)
{
const sal_Int32 nEndOfSpaces(xBreakIterator->endOfCharBlock(rTxt, a, rLocale, css::i18n::CharType::SPACE_SEPARATOR));
if(nEndOfSpaces > a)
{
nCurrent = nEndOfSpaces;
}
}
nNextWordBoundary = xBreakIterator->getWordBoundary(rTxt, a + 1, rLocale, css::i18n::WordType::ANY_WORD, true);
}
}
if(a > nCurrent)
{
breakupPortion(aTempResult, nCurrent, a - nCurrent, true);
}
break;
}
}
mxResult = aTempResult;
}
}
const Primitive2DContainer& TextBreakupHelper::getResult(BreakupUnit aBreakupUnit) const
{
if(mxResult.empty())
{
const_cast< TextBreakupHelper* >(this)->breakup(aBreakupUnit);
}
return mxResult;
}
} // end of namespace primitive2d
} // end of namespace drawinglayer
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|