Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/edit/texteng.cxx
Line
Count
Source
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 <tools/stream.hxx>
21
22
#include <vcl/texteng.hxx>
23
#include <vcl/textview.hxx>
24
#include <vcl/commandevent.hxx>
25
#include <vcl/inputctx.hxx>
26
#include "textdoc.hxx"
27
#include "textdat2.hxx"
28
#include "textundo.hxx"
29
#include "textund2.hxx"
30
#include <svl/ctloptions.hxx>
31
#include <vcl/window.hxx>
32
#include <vcl/settings.hxx>
33
#include <vcl/toolkit/edit.hxx>
34
#include <vcl/virdev.hxx>
35
#include <sal/log.hxx>
36
#include <osl/diagnose.h>
37
38
#include "TextLine.hxx"
39
#include "IdleFormatter.hxx"
40
41
#include <com/sun/star/i18n/XBreakIterator.hpp>
42
43
#include <com/sun/star/i18n/CharacterIteratorMode.hpp>
44
45
#include <com/sun/star/i18n/WordType.hpp>
46
47
#include <com/sun/star/i18n/InputSequenceChecker.hpp>
48
#include <com/sun/star/i18n/InputSequenceCheckMode.hpp>
49
#include <com/sun/star/i18n/ScriptType.hpp>
50
51
#include <comphelper/processfactory.hxx>
52
53
#include <unotools/localedatawrapper.hxx>
54
#include <vcl/unohelp.hxx>
55
56
#include <vcl/svapp.hxx>
57
58
#include <unicode/ubidi.h>
59
60
#include <algorithm>
61
#include <cstddef>
62
#include <cstdlib>
63
#include <memory>
64
#include <o3tl/sorted_vector.hxx>
65
#include <string_view>
66
#include <vector>
67
68
using namespace ::com::sun::star;
69
using namespace ::com::sun::star::uno;
70
71
TextEngine::TextEngine()
72
0
    : mpActiveView {nullptr}
73
0
    , maTextColor {COL_BLACK}
74
0
    , mnMaxTextLen {0}
75
0
    , mnMaxTextWidth {0}
76
0
    , mnCharHeight {0}
77
0
    , mnCurTextWidth {-1}
78
0
    , mnCurTextHeight {0}
79
0
    , mnDefTab {0}
80
0
    , meAlign {TxtAlign::Left}
81
0
    , mbIsFormatting {false}
82
0
    , mbFormatted {false}
83
0
    , mbUpdate {true}
84
0
    , mbModified {false}
85
0
    , mbUndoEnabled {false}
86
0
    , mbIsInUndo {false}
87
0
    , mbDowning {false}
88
0
    , mbRightToLeft {false}
89
0
    , mbHasMultiLineParas {false}
90
0
{
91
0
    mpViews.reset( new TextViews );
92
93
0
    mpIdleFormatter.reset( new IdleFormatter );
94
0
    mpIdleFormatter->SetInvokeHandler( LINK( this, TextEngine, IdleFormatHdl ) );
95
96
0
    mpRefDev = VclPtr<VirtualDevice>::Create();
97
98
0
    ImpInitLayoutMode( mpRefDev );
99
100
0
    ImpInitDoc();
101
102
0
    vcl::Font aFont(mpRefDev->GetFont().GetFamilyName(), Size(0, 0));
103
0
    aFont.SetTransparent( false );
104
0
    Color aFillColor( aFont.GetFillColor() );
105
0
    aFillColor.SetAlpha( 255 );
106
0
    aFont.SetFillColor( aFillColor );
107
0
    SetFont( aFont );
108
0
}
109
110
TextEngine::~TextEngine()
111
0
{
112
0
    mbDowning = true;
113
114
0
    mpIdleFormatter.reset();
115
0
    mpDoc.reset();
116
0
    mpTEParaPortions.reset();
117
0
    mpViews.reset(); // only the list, not the Views
118
0
    mpRefDev.disposeAndClear();
119
0
    mpUndoManager.reset();
120
0
    mpIMEInfos.reset();
121
0
    mpLocaleDataWrapper.reset();
122
0
}
123
124
void TextEngine::InsertView( TextView* pTextView )
125
0
{
126
0
    mpViews->push_back( pTextView );
127
0
    pTextView->SetSelection( TextSelection() );
128
129
0
    if ( !GetActiveView() )
130
0
        SetActiveView( pTextView );
131
0
}
132
133
void TextEngine::RemoveView( TextView* pTextView )
134
0
{
135
0
    TextViews::iterator it = std::find( mpViews->begin(), mpViews->end(), pTextView );
136
0
    if( it != mpViews->end() )
137
0
    {
138
0
        pTextView->HideCursor();
139
0
        mpViews->erase( it );
140
0
        if ( pTextView == GetActiveView() )
141
0
            SetActiveView( nullptr );
142
0
    }
143
0
}
144
145
sal_uInt16 TextEngine::GetViewCount() const
146
0
{
147
0
    return mpViews->size();
148
0
}
149
150
TextView* TextEngine::GetView( sal_uInt16 nView ) const
151
0
{
152
0
    return (*mpViews)[ nView ];
153
0
}
154
155
156
void TextEngine::SetActiveView( TextView* pTextView )
157
0
{
158
0
    if ( pTextView != mpActiveView )
159
0
    {
160
0
        if ( mpActiveView )
161
0
            mpActiveView->HideSelection();
162
163
0
        mpActiveView = pTextView;
164
165
0
        if ( mpActiveView )
166
0
            mpActiveView->ShowSelection();
167
0
    }
168
0
}
169
170
void TextEngine::SetFont( const vcl::Font& rFont )
171
0
{
172
0
    if ( rFont == maFont )
173
0
        return;
174
175
0
    maFont = rFont;
176
    // #i40221# As the font's color now defaults to transparent (since i35764)
177
    //  we have to choose a useful textcolor in this case.
178
    // Otherwise maTextColor and maFont.GetColor() are both transparent...
179
0
    if( rFont.GetColor() == COL_TRANSPARENT )
180
0
        maTextColor = COL_BLACK;
181
0
    else
182
0
        maTextColor = rFont.GetColor();
183
184
    // Do not allow transparent fonts because of selection
185
    // (otherwise delete the background in ImplPaint later differently)
186
0
    maFont.SetTransparent( false );
187
    // Tell VCL not to use the font color, use text color from OutputDevice
188
0
    maFont.SetColor( COL_TRANSPARENT );
189
0
    Color aFillColor( maFont.GetFillColor() );
190
0
    aFillColor.SetAlpha( 255 );
191
0
    maFont.SetFillColor( aFillColor );
192
193
0
    maFont.SetAlignment( ALIGN_TOP );
194
0
    mpRefDev->SetFont( maFont );
195
0
    mnDefTab = mpRefDev->GetTextWidth(u"    "_ustr);
196
0
    if ( !mnDefTab )
197
0
        mnDefTab = mpRefDev->GetTextWidth(u"XXXX"_ustr);
198
0
    if ( !mnDefTab )
199
0
        mnDefTab = 1;
200
0
    mnCharHeight = mpRefDev->GetTextHeight();
201
202
0
    FormatFullDoc();
203
0
    UpdateViews();
204
205
0
    for ( auto nView = mpViews->size(); nView; )
206
0
    {
207
0
        TextView* pView = (*mpViews)[ --nView ];
208
0
        pView->GetWindow()->SetInputContext( InputContext( GetFont(), !pView->IsReadOnly() ? InputContextFlags::Text|InputContextFlags::ExtText : InputContextFlags::NONE ) );
209
0
    }
210
211
0
}
212
213
void TextEngine::SetMaxTextLen( sal_Int32 nLen )
214
0
{
215
0
    mnMaxTextLen = nLen>=0 ? nLen : EDIT_NOLIMIT;
216
0
}
217
218
void TextEngine::SetMaxTextWidth( tools::Long nMaxWidth )
219
0
{
220
0
    if ( nMaxWidth>=0 && nMaxWidth != mnMaxTextWidth )
221
0
    {
222
0
        mnMaxTextWidth = nMaxWidth;
223
0
        FormatFullDoc();
224
0
        UpdateViews();
225
0
    }
226
0
}
227
228
const sal_Unicode static_aLFText[] = { '\n', 0 };
229
const sal_Unicode static_aCRText[] = { '\r', 0 };
230
const sal_Unicode static_aCRLFText[] = { '\r', '\n', 0 };
231
232
static const sal_Unicode* static_getLineEndText( LineEnd aLineEnd )
233
0
{
234
0
    const sal_Unicode* pRet = nullptr;
235
236
0
    switch( aLineEnd )
237
0
    {
238
0
        case LINEEND_LF:
239
0
            pRet = static_aLFText;
240
0
            break;
241
0
        case LINEEND_CR:
242
0
            pRet = static_aCRText;
243
0
            break;
244
0
        case LINEEND_CRLF:
245
0
            pRet = static_aCRLFText;
246
0
            break;
247
0
    }
248
0
    return pRet;
249
0
}
250
251
void  TextEngine::ReplaceText(const TextSelection& rSel, const OUString& rText)
252
0
{
253
0
    ImpInsertText( rSel, rText );
254
0
}
255
256
OUString TextEngine::GetText( LineEnd aSeparator ) const
257
0
{
258
0
    return mpDoc->GetText( static_getLineEndText( aSeparator ) );
259
0
}
260
261
OUString TextEngine::GetTextLines( LineEnd aSeparator ) const
262
0
{
263
0
    OUStringBuffer aText;
264
0
    const sal_uInt32 nParas = mpTEParaPortions->Count();
265
0
    const sal_Unicode* pSep = static_getLineEndText( aSeparator );
266
0
    for ( sal_uInt32 nP = 0; nP < nParas; ++nP )
267
0
    {
268
0
        TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nP );
269
270
0
        const size_t nLines = pTEParaPortion->GetLines().size();
271
0
        for ( size_t nL = 0; nL < nLines; ++nL )
272
0
        {
273
0
            TextLine& rLine = pTEParaPortion->GetLines()[nL];
274
0
            aText.append( pTEParaPortion->GetNode()->GetText().subView(rLine.GetStart(), rLine.GetEnd() - rLine.GetStart()) );
275
0
            if ( pSep && ( ( (nP+1) < nParas ) || ( (nL+1) < nLines ) ) )
276
0
                aText.append(pSep);
277
0
        }
278
0
    }
279
0
    return aText.makeStringAndClear();
280
0
}
281
282
OUString TextEngine::GetText( sal_uInt32 nPara ) const
283
0
{
284
0
    return mpDoc->GetText( nPara );
285
0
}
286
287
sal_Int32 TextEngine::GetTextLen() const
288
0
{
289
0
    return mpDoc->GetTextLen( static_getLineEndText( LINEEND_LF ) );
290
0
}
291
292
sal_Int32 TextEngine::GetTextLen( const TextSelection& rSel ) const
293
0
{
294
0
    TextSelection aSel( rSel );
295
0
    aSel.Justify();
296
0
    ValidateSelection( aSel );
297
0
    return mpDoc->GetTextLen( static_getLineEndText( LINEEND_LF ), &aSel );
298
0
}
299
300
sal_Int32 TextEngine::GetTextLen( const sal_uInt32 nPara ) const
301
0
{
302
0
    return mpDoc->GetNodes()[ nPara ]->GetText().getLength();
303
0
}
304
305
void TextEngine::SetUpdateMode( bool bUpdate )
306
0
{
307
0
    if ( bUpdate != mbUpdate )
308
0
    {
309
0
        mbUpdate = bUpdate;
310
0
        if ( mbUpdate )
311
0
        {
312
0
            FormatAndUpdate( GetActiveView() );
313
0
            if ( GetActiveView() )
314
0
                GetActiveView()->ShowCursor();
315
0
        }
316
0
    }
317
0
}
318
319
bool TextEngine::DoesKeyChangeText( const KeyEvent& rKeyEvent )
320
0
{
321
0
    bool bDoesChange = false;
322
323
0
    KeyFuncType eFunc = rKeyEvent.GetKeyCode().GetFunction();
324
0
    if ( eFunc != KeyFuncType::DONTKNOW )
325
0
    {
326
0
        switch ( eFunc )
327
0
        {
328
0
            case KeyFuncType::UNDO:
329
0
            case KeyFuncType::REDO:
330
0
            case KeyFuncType::CUT:
331
0
            case KeyFuncType::PASTE:
332
0
                bDoesChange = true;
333
0
                break;
334
0
            default:
335
                // might get handled below
336
0
                eFunc = KeyFuncType::DONTKNOW;
337
0
        }
338
0
    }
339
0
    if ( eFunc == KeyFuncType::DONTKNOW )
340
0
    {
341
0
        switch ( rKeyEvent.GetKeyCode().GetCode() )
342
0
        {
343
0
            case KEY_DELETE:
344
0
            case KEY_BACKSPACE:
345
0
                if ( !rKeyEvent.GetKeyCode().IsMod2() )
346
0
                    bDoesChange = true;
347
0
                break;
348
0
            case KEY_RETURN:
349
0
            case KEY_TAB:
350
0
                if ( !rKeyEvent.GetKeyCode().IsMod1() && !rKeyEvent.GetKeyCode().IsMod2() )
351
0
                    bDoesChange = true;
352
0
                break;
353
0
            default:
354
0
                bDoesChange = TextEngine::IsSimpleCharInput( rKeyEvent );
355
0
        }
356
0
    }
357
0
    return bDoesChange;
358
0
}
359
360
bool TextEngine::IsSimpleCharInput( const KeyEvent& rKeyEvent )
361
0
{
362
0
    return rKeyEvent.GetCharCode() >= 32 && rKeyEvent.GetCharCode() != 127 &&
363
0
        KEY_MOD1 != (rKeyEvent.GetKeyCode().GetModifier() & ~KEY_SHIFT) && // (ssa) #i45714#:
364
0
        KEY_MOD2 != (rKeyEvent.GetKeyCode().GetModifier() & ~KEY_SHIFT);  // check for Ctrl and Alt separately
365
0
}
366
367
void TextEngine::ImpInitDoc()
368
0
{
369
0
    if ( mpDoc )
370
0
        mpDoc->Clear();
371
0
    else
372
0
        mpDoc.reset( new TextDoc );
373
374
0
    mpTEParaPortions.reset(new TEParaPortions);
375
376
0
    std::unique_ptr<TextNode> pNode(new TextNode( OUString() ));
377
0
    mpDoc->GetNodes().insert( mpDoc->GetNodes().begin(), std::move(pNode) );
378
379
0
    TEParaPortion* pIniPortion = new TEParaPortion( mpDoc->GetNodes().begin()->get() );
380
0
    mpTEParaPortions->Insert( pIniPortion, 0 );
381
382
0
    mbFormatted = false;
383
384
0
    ImpParagraphRemoved( TEXT_PARA_ALL );
385
0
    ImpParagraphInserted( 0 );
386
0
}
387
388
OUString TextEngine::GetText( const TextSelection& rSel, LineEnd aSeparator ) const
389
0
{
390
0
    if ( !rSel.HasRange() )
391
0
        return OUString();
392
393
0
    TextSelection aSel( rSel );
394
0
    aSel.Justify();
395
396
0
    OUStringBuffer aText;
397
0
    const sal_uInt32 nStartPara = aSel.GetStart().GetPara();
398
0
    const sal_uInt32 nEndPara = aSel.GetEnd().GetPara();
399
0
    const sal_Unicode* pSep = static_getLineEndText( aSeparator );
400
0
    for ( sal_uInt32 nNode = aSel.GetStart().GetPara(); nNode <= nEndPara; ++nNode )
401
0
    {
402
0
        TextNode* pNode = mpDoc->GetNodes()[ nNode ].get();
403
404
0
        sal_Int32 nStartPos = 0;
405
0
        sal_Int32 nEndPos = pNode->GetText().getLength();
406
0
        if ( nNode == nStartPara )
407
0
            nStartPos = aSel.GetStart().GetIndex();
408
0
        if ( nNode == nEndPara ) // may also be == nStart!
409
0
            nEndPos = aSel.GetEnd().GetIndex();
410
411
0
        aText.append(pNode->GetText().subView(nStartPos, nEndPos-nStartPos));
412
0
        if ( nNode < nEndPara )
413
0
            aText.append(pSep);
414
0
    }
415
0
    return aText.makeStringAndClear();
416
0
}
417
418
void TextEngine::ImpRemoveText()
419
0
{
420
0
    ImpInitDoc();
421
422
0
    const TextSelection aEmptySel;
423
0
    for (TextView* pView : *mpViews)
424
0
    {
425
0
        pView->ImpSetSelection( aEmptySel );
426
0
    }
427
0
    ResetUndo();
428
0
}
429
430
void TextEngine::SetText( const OUString& rText )
431
0
{
432
0
    ImpRemoveText();
433
434
0
    const bool bUndoCurrentlyEnabled = IsUndoEnabled();
435
    // the manually inserted text cannot be reversed by the user
436
0
    EnableUndo( false );
437
438
0
    const TextSelection aEmptySel;
439
440
0
    TextPaM aPaM;
441
0
    if ( !rText.isEmpty() )
442
0
        aPaM = ImpInsertText( aEmptySel, rText );
443
444
0
    for (TextView* pView : *mpViews)
445
0
    {
446
0
        pView->ImpSetSelection( aEmptySel );
447
448
        // if no text, then no Format&Update => the text remains
449
0
        if ( rText.isEmpty() && GetUpdateMode() )
450
0
            pView->Invalidate();
451
0
    }
452
453
0
    if( rText.isEmpty() )  // otherwise needs invalidation later; !bFormatted is sufficient
454
0
        mnCurTextHeight = 0;
455
456
0
    FormatAndUpdate();
457
458
0
    EnableUndo( bUndoCurrentlyEnabled );
459
0
    SAL_WARN_IF( HasUndoManager() && GetUndoManager().GetUndoActionCount(), "vcl", "SetText: Undo!" );
460
0
}
461
462
void TextEngine::CursorMoved( sal_uInt32 nNode )
463
0
{
464
    // delete empty attribute; but only if paragraph is not empty!
465
0
    TextNode* pNode = mpDoc->GetNodes()[ nNode ].get();
466
0
    if ( pNode && pNode->GetCharAttribs().HasEmptyAttribs() && !pNode->GetText().isEmpty() )
467
0
        pNode->GetCharAttribs().DeleteEmptyAttribs();
468
0
}
469
470
void TextEngine::ImpRemoveChars( const TextPaM& rPaM, sal_Int32 nChars )
471
0
{
472
0
    SAL_WARN_IF( !nChars, "vcl", "ImpRemoveChars: 0 Chars?!" );
473
0
    if ( IsUndoEnabled() && !IsInUndo() )
474
0
    {
475
        // attributes have to be saved for UNDO before RemoveChars!
476
0
        TextNode* pNode = mpDoc->GetNodes()[ rPaM.GetPara() ].get();
477
0
        OUString aStr( pNode->GetText().copy( rPaM.GetIndex(), nChars ) );
478
479
        // check if attributes are being deleted or changed
480
0
        const sal_Int32 nStart = rPaM.GetIndex();
481
0
        const sal_Int32 nEnd = nStart + nChars;
482
0
        for ( sal_uInt16 nAttr = pNode->GetCharAttribs().Count(); nAttr; )
483
0
        {
484
0
            TextCharAttrib& rAttr = pNode->GetCharAttribs().GetAttrib( --nAttr );
485
0
            if ( ( rAttr.GetEnd() >= nStart ) && ( rAttr.GetStart() < nEnd ) )
486
0
            {
487
0
                break;  // for
488
0
            }
489
0
        }
490
0
        InsertUndo( std::make_unique<TextUndoRemoveChars>( this, rPaM, aStr ) );
491
0
    }
492
493
0
    mpDoc->RemoveChars( rPaM, nChars );
494
0
    ImpCharsRemoved( rPaM.GetPara(), rPaM.GetIndex(), nChars );
495
0
}
496
497
TextPaM TextEngine::ImpConnectParagraphs( sal_uInt32 nLeft, sal_uInt32 nRight )
498
0
{
499
0
    SAL_WARN_IF( nLeft == nRight, "vcl", "ImpConnectParagraphs: connect the very same paragraph ?" );
500
501
0
    TextNode* pLeft = mpDoc->GetNodes()[ nLeft ].get();
502
0
    TextNode* pRight = mpDoc->GetNodes()[ nRight ].get();
503
504
0
    if ( IsUndoEnabled() && !IsInUndo() )
505
0
        InsertUndo( std::make_unique<TextUndoConnectParas>( this, nLeft, pLeft->GetText().getLength() ) );
506
507
    // first lookup Portions, as pRight is gone after ConnectParagraphs
508
0
    TEParaPortion* pLeftPortion = mpTEParaPortions->GetObject( nLeft );
509
0
    TEParaPortion* pRightPortion = mpTEParaPortions->GetObject( nRight );
510
0
    SAL_WARN_IF( !pLeft || !pLeftPortion, "vcl", "ImpConnectParagraphs(1): Hidden Portion" );
511
0
    SAL_WARN_IF( !pRight || !pRightPortion, "vcl", "ImpConnectParagraphs(2): Hidden Portion" );
512
513
0
    TextPaM aPaM = mpDoc->ConnectParagraphs( pLeft, pRight );
514
0
    ImpParagraphRemoved( nRight );
515
516
0
    pLeftPortion->MarkSelectionInvalid( aPaM.GetIndex() );
517
518
0
    mpTEParaPortions->Remove( nRight );
519
    // the right Node is deleted by EditDoc::ConnectParagraphs()
520
521
0
    return aPaM;
522
0
}
523
524
TextPaM TextEngine::ImpDeleteText( const TextSelection& rSel )
525
0
{
526
0
    if ( !rSel.HasRange() )
527
0
        return rSel.GetStart();
528
529
0
    TextSelection aSel( rSel );
530
0
    aSel.Justify();
531
0
    TextPaM aStartPaM( aSel.GetStart() );
532
0
    TextPaM aEndPaM( aSel.GetEnd() );
533
534
0
    CursorMoved( aStartPaM.GetPara() ); // so that newly-adjusted attributes vanish
535
0
    CursorMoved( aEndPaM.GetPara() );   // so that newly-adjusted attributes vanish
536
537
0
    SAL_WARN_IF( !mpDoc->IsValidPaM( aStartPaM ), "vcl", "ImpDeleteText(1): bad Index" );
538
0
    SAL_WARN_IF( !mpDoc->IsValidPaM( aEndPaM ), "vcl", "ImpDeleteText(2): bad Index" );
539
540
0
    const sal_uInt32 nStartNode = aStartPaM.GetPara();
541
0
    sal_uInt32 nEndNode = aEndPaM.GetPara();
542
543
    // remove all Nodes inbetween
544
0
    for ( sal_uInt32 z = nStartNode+1; z < nEndNode; ++z )
545
0
    {
546
        // always nStartNode+1, because of Remove()!
547
0
        ImpRemoveParagraph( nStartNode+1 );
548
0
    }
549
550
0
    if ( nStartNode != nEndNode )
551
0
    {
552
        // the remainder of StartNodes...
553
0
        TextNode* pLeft = mpDoc->GetNodes()[ nStartNode ].get();
554
0
        sal_Int32 nChars = pLeft->GetText().getLength() - aStartPaM.GetIndex();
555
0
        if ( nChars )
556
0
        {
557
0
            ImpRemoveChars( aStartPaM, nChars );
558
0
            TEParaPortion* pPortion = mpTEParaPortions->GetObject( nStartNode );
559
0
            SAL_WARN_IF( !pPortion, "vcl", "ImpDeleteText(3): bad Index" );
560
0
            pPortion->MarkSelectionInvalid( aStartPaM.GetIndex() );
561
0
        }
562
563
        // the beginning of EndNodes...
564
0
        nEndNode = nStartNode+1;    // the other paragraphs were deleted
565
0
        nChars = aEndPaM.GetIndex();
566
0
        if ( nChars )
567
0
        {
568
0
            aEndPaM.GetPara() = nEndNode;
569
0
            aEndPaM.GetIndex() = 0;
570
0
            ImpRemoveChars( aEndPaM, nChars );
571
0
            TEParaPortion* pPortion = mpTEParaPortions->GetObject( nEndNode );
572
0
            SAL_WARN_IF( !pPortion, "vcl", "ImpDeleteText(4): bad Index" );
573
0
            pPortion->MarkSelectionInvalid( 0 );
574
0
        }
575
576
        // connect...
577
0
        aStartPaM = ImpConnectParagraphs( nStartNode, nEndNode );
578
0
    }
579
0
    else
580
0
    {
581
0
        const sal_Int32 nChars = aEndPaM.GetIndex() - aStartPaM.GetIndex();
582
0
        ImpRemoveChars( aStartPaM, nChars );
583
0
        TEParaPortion* pPortion = mpTEParaPortions->GetObject( nStartNode );
584
0
        SAL_WARN_IF( !pPortion, "vcl", "ImpDeleteText(5): bad Index" );
585
0
        pPortion->MarkInvalid( aEndPaM.GetIndex(), aStartPaM.GetIndex() - aEndPaM.GetIndex() );
586
0
    }
587
588
//  UpdateSelections();
589
0
    TextModified();
590
0
    return aStartPaM;
591
0
}
592
593
void TextEngine::ImpRemoveParagraph( sal_uInt32 nPara )
594
0
{
595
0
    std::unique_ptr<TextNode> pNode = std::move(mpDoc->GetNodes()[ nPara ]);
596
597
    // the Node is handled by Undo and is deleted if appropriate
598
0
    mpDoc->GetNodes().erase( mpDoc->GetNodes().begin() + nPara );
599
0
    if ( IsUndoEnabled() && !IsInUndo() )
600
0
        InsertUndo( std::make_unique<TextUndoDelPara>( this, pNode.release(), nPara ) );
601
602
0
    mpTEParaPortions->Remove( nPara );
603
604
0
    ImpParagraphRemoved( nPara );
605
0
}
606
607
uno::Reference < i18n::XExtendedInputSequenceChecker > const & TextEngine::GetInputSequenceChecker()
608
0
{
609
0
    if ( !mxISC.is() )
610
0
    {
611
0
        mxISC = i18n::InputSequenceChecker::create( ::comphelper::getProcessComponentContext() );
612
0
    }
613
0
    return mxISC;
614
0
}
615
616
bool TextEngine::IsInputSequenceCheckingRequired( sal_Unicode c, const TextSelection& rCurSel ) const
617
0
{
618
    // get the index that really is first
619
0
    const sal_Int32 nFirstPos = std::min(rCurSel.GetStart().GetIndex(), rCurSel.GetEnd().GetIndex());
620
621
0
    bool bIsSequenceChecking =
622
0
        SvtCTLOptions::IsCTLFontEnabled() &&
623
0
        SvtCTLOptions::IsCTLSequenceChecking() &&
624
0
        nFirstPos != 0; /* first char needs not to be checked */
625
626
0
    if (bIsSequenceChecking)
627
0
    {
628
0
        uno::Reference< i18n::XBreakIterator > xBI = const_cast<TextEngine *>(this)->GetBreakIterator();
629
0
        bIsSequenceChecking = xBI.is() && i18n::ScriptType::COMPLEX == xBI->getScriptType( OUString( c ), 0 );
630
0
    }
631
632
0
    return bIsSequenceChecking;
633
0
}
634
635
TextPaM TextEngine::ImpInsertText( const TextSelection& rCurSel, sal_Unicode c, bool bOverwrite )
636
0
{
637
0
    return ImpInsertText( c, rCurSel, bOverwrite  );
638
0
}
639
640
TextPaM TextEngine::ImpInsertText( sal_Unicode c, const TextSelection& rCurSel, bool bOverwrite, bool bIsUserInput )
641
0
{
642
0
    SAL_WARN_IF( c == '\n', "vcl", "InsertText: NewLine!" );
643
0
    SAL_WARN_IF( c == '\r', "vcl", "InsertText: NewLine!" );
644
645
0
    TextPaM aPaM( rCurSel.GetStart() );
646
0
    TextNode* pNode = mpDoc->GetNodes()[ aPaM.GetPara() ].get();
647
648
0
    bool bDoOverwrite = bOverwrite && ( aPaM.GetIndex() < pNode->GetText().getLength() );
649
650
0
    bool bUndoAction = rCurSel.HasRange() || bDoOverwrite;
651
652
0
    if ( bUndoAction )
653
0
        UndoActionStart();
654
655
0
    if ( rCurSel.HasRange() )
656
0
    {
657
0
        aPaM = ImpDeleteText( rCurSel );
658
0
    }
659
0
    else if ( bDoOverwrite )
660
0
    {
661
        // if selection, then don't overwrite a character
662
0
        TextSelection aTmpSel( aPaM );
663
0
        ++aTmpSel.GetEnd().GetIndex();
664
0
        ImpDeleteText( aTmpSel );
665
0
    }
666
667
0
    if (bIsUserInput && IsInputSequenceCheckingRequired( c, rCurSel ))
668
0
    {
669
0
        uno::Reference < i18n::XExtendedInputSequenceChecker > xISC = GetInputSequenceChecker();
670
671
0
        if (xISC.is())
672
0
        {
673
0
            sal_Int32 nTmpPos = aPaM.GetIndex();
674
0
            sal_Int16 nCheckMode = SvtCTLOptions::IsCTLSequenceCheckingRestricted() ?
675
0
                    i18n::InputSequenceCheckMode::STRICT : i18n::InputSequenceCheckMode::BASIC;
676
677
            // the text that needs to be checked is only the one
678
            // before the current cursor position
679
0
            OUString aOldText( mpDoc->GetText( aPaM.GetPara() ).copy(0, nTmpPos) );
680
0
            if (SvtCTLOptions::IsCTLSequenceCheckingTypeAndReplace())
681
0
            {
682
0
                OUString aNewText( aOldText );
683
0
                xISC->correctInputSequence( aNewText, nTmpPos - 1, c, nCheckMode );
684
685
                // find position of first character that has changed
686
0
                const sal_Int32 nOldLen = aOldText.getLength();
687
0
                const sal_Int32 nNewLen = aNewText.getLength();
688
0
                const sal_Unicode *pOldTxt = aOldText.getStr();
689
0
                const sal_Unicode *pNewTxt = aNewText.getStr();
690
0
                sal_Int32 nChgPos = 0;
691
0
                while ( nChgPos < nOldLen && nChgPos < nNewLen &&
692
0
                        pOldTxt[nChgPos] == pNewTxt[nChgPos] )
693
0
                    ++nChgPos;
694
695
0
                OUString aChgText( aNewText.copy( nChgPos ) );
696
697
                // select text from first pos to be changed to current pos
698
0
                TextSelection aSel( TextPaM( aPaM.GetPara(), nChgPos ), aPaM );
699
700
0
                if (!aChgText.isEmpty())
701
                    // ImpInsertText implicitly handles undo...
702
0
                    return ImpInsertText( aSel, aChgText );
703
0
                else
704
0
                    return aPaM;
705
0
            }
706
0
            else
707
0
            {
708
                // should the character be ignored (i.e. not get inserted) ?
709
0
                if (!xISC->checkInputSequence( aOldText, nTmpPos - 1, c, nCheckMode ))
710
0
                    return aPaM;    // nothing to be done -> no need for undo
711
0
            }
712
0
        }
713
714
        // at this point now we will insert the character 'normally' some lines below...
715
0
    }
716
717
0
    if ( IsUndoEnabled() && !IsInUndo() )
718
0
    {
719
0
        std::unique_ptr<TextUndoInsertChars> pNewUndo(new TextUndoInsertChars( this, aPaM, OUString(c) ));
720
0
        bool bTryMerge = !bDoOverwrite && ( c != ' ' );
721
0
        InsertUndo( std::move(pNewUndo), bTryMerge );
722
0
    }
723
724
0
    TEParaPortion* pPortion = mpTEParaPortions->GetObject( aPaM.GetPara() );
725
0
    pPortion->MarkInvalid( aPaM.GetIndex(), 1 );
726
0
    if ( c == '\t' )
727
0
        pPortion->SetNotSimpleInvalid();
728
0
    aPaM = mpDoc->InsertText( aPaM, c );
729
0
    ImpCharsInserted( aPaM.GetPara(), aPaM.GetIndex()-1, 1 );
730
731
0
    TextModified();
732
733
0
    if ( bUndoAction )
734
0
        UndoActionEnd();
735
736
0
    return aPaM;
737
0
}
738
739
TextPaM TextEngine::ImpInsertText( const TextSelection& rCurSel, const OUString& rStr )
740
0
{
741
0
    UndoActionStart();
742
743
0
    TextPaM aPaM;
744
745
0
    if ( rCurSel.HasRange() )
746
0
        aPaM = ImpDeleteText( rCurSel );
747
0
    else
748
0
        aPaM = rCurSel.GetEnd();
749
750
0
    OUString aText(convertLineEnd(rStr, LINEEND_LF));
751
752
0
    sal_Int32 nStart = 0;
753
0
    while ( nStart < aText.getLength() )
754
0
    {
755
0
        sal_Int32 nEnd = aText.indexOf( LINE_SEP, nStart );
756
0
        if (nEnd == -1)
757
0
            nEnd = aText.getLength(); // do not dereference!
758
759
        // Start == End => empty line
760
0
        if ( nEnd > nStart )
761
0
        {
762
0
            OUString aLine(aText.copy(nStart, nEnd-nStart));
763
0
            if ( IsUndoEnabled() && !IsInUndo() )
764
0
                InsertUndo( std::make_unique<TextUndoInsertChars>( this, aPaM, aLine ) );
765
766
0
            TEParaPortion* pPortion = mpTEParaPortions->GetObject( aPaM.GetPara() );
767
0
            pPortion->MarkInvalid( aPaM.GetIndex(), aLine.getLength() );
768
0
            if (aLine.indexOf( '\t' ) != -1)
769
0
                pPortion->SetNotSimpleInvalid();
770
771
0
            aPaM = mpDoc->InsertText( aPaM, aLine );
772
0
            ImpCharsInserted( aPaM.GetPara(), aPaM.GetIndex()-aLine.getLength(), aLine.getLength() );
773
774
0
        }
775
0
        if ( nEnd < aText.getLength() )
776
0
            aPaM = ImpInsertParaBreak( aPaM );
777
778
0
        if ( nEnd == aText.getLength() )    // #108611# prevent overflow in "nStart = nEnd+1" calculation
779
0
            break;
780
781
0
        nStart = nEnd+1;
782
0
    }
783
784
0
    UndoActionEnd();
785
786
0
    TextModified();
787
0
    return aPaM;
788
0
}
789
790
TextPaM TextEngine::ImpInsertParaBreak( const TextSelection& rCurSel )
791
0
{
792
0
    TextPaM aPaM;
793
0
    if ( rCurSel.HasRange() )
794
0
        aPaM = ImpDeleteText( rCurSel );
795
0
    else
796
0
        aPaM = rCurSel.GetEnd();
797
798
0
    return ImpInsertParaBreak( aPaM );
799
0
}
800
801
TextPaM TextEngine::ImpInsertParaBreak( const TextPaM& rPaM )
802
0
{
803
0
    if ( IsUndoEnabled() && !IsInUndo() )
804
0
        InsertUndo( std::make_unique<TextUndoSplitPara>( this, rPaM.GetPara(), rPaM.GetIndex() ) );
805
806
0
    TextNode* pNode = mpDoc->GetNodes()[ rPaM.GetPara() ].get();
807
0
    bool bFirstParaContentChanged = rPaM.GetIndex() < pNode->GetText().getLength();
808
809
0
    TextPaM aPaM( mpDoc->InsertParaBreak( rPaM ) );
810
811
0
    TEParaPortion* pPortion = mpTEParaPortions->GetObject( rPaM.GetPara() );
812
0
    SAL_WARN_IF( !pPortion, "vcl", "ImpInsertParaBreak: Hidden Portion" );
813
0
    pPortion->MarkInvalid( rPaM.GetIndex(), 0 );
814
815
0
    TextNode* pNewNode = mpDoc->GetNodes()[ aPaM.GetPara() ].get();
816
0
    TEParaPortion* pNewPortion = new TEParaPortion( pNewNode );
817
0
    mpTEParaPortions->Insert( pNewPortion, aPaM.GetPara() );
818
0
    ImpParagraphInserted( aPaM.GetPara() );
819
820
0
    CursorMoved( rPaM.GetPara() );  // if empty attribute created
821
0
    TextModified();
822
823
0
    if ( bFirstParaContentChanged )
824
0
        Broadcast( TextHint( SfxHintId::TextParaContentChanged, rPaM.GetPara() ) );
825
826
0
    return aPaM;
827
0
}
828
829
tools::Rectangle TextEngine::PaMtoEditCursor( const TextPaM& rPaM, bool bSpecial )
830
0
{
831
0
    SAL_WARN_IF( !GetUpdateMode(), "vcl", "PaMtoEditCursor: GetUpdateMode()" );
832
833
0
    tools::Rectangle aEditCursor;
834
0
    tools::Long nY = 0;
835
836
0
    if ( !mbHasMultiLineParas )
837
0
    {
838
0
        nY = rPaM.GetPara() * mnCharHeight;
839
0
    }
840
0
    else
841
0
    {
842
0
        for ( sal_uInt32 nPortion = 0; nPortion < rPaM.GetPara(); ++nPortion )
843
0
        {
844
0
            TEParaPortion* pPortion = mpTEParaPortions->GetObject(nPortion);
845
0
            nY += pPortion->GetLines().size() * mnCharHeight;
846
0
        }
847
0
    }
848
849
0
    aEditCursor = GetEditCursor( rPaM, bSpecial );
850
0
    aEditCursor.AdjustTop(nY );
851
0
    aEditCursor.AdjustBottom(nY );
852
0
    return aEditCursor;
853
0
}
854
855
tools::Rectangle TextEngine::GetEditCursor( const TextPaM& rPaM, bool bSpecial, bool bPreferPortionStart )
856
0
{
857
0
    if ( !IsFormatted() && !IsFormatting() )
858
0
        FormatAndUpdate();
859
860
0
    TEParaPortion* pPortion = mpTEParaPortions->GetObject( rPaM.GetPara() );
861
    //TextNode* pNode = mpDoc->GetNodes().GetObject( rPaM.GetPara() );
862
863
    /*
864
      bSpecial: If behind the last character of a made up line, stay at the
865
                  end of the line, not at the start of the next line.
866
      Purpose:  - really END = > behind the last character
867
                - to selection...
868
869
    */
870
871
0
    tools::Long nY = 0;
872
0
    sal_Int32 nCurIndex = 0;
873
0
    TextLine* pLine = nullptr;
874
0
    for (TextLine & rTmpLine : pPortion->GetLines())
875
0
    {
876
0
        if ( ( rTmpLine.GetStart() == rPaM.GetIndex() ) || ( rTmpLine.IsIn( rPaM.GetIndex(), bSpecial ) ) )
877
0
        {
878
0
            pLine = &rTmpLine;
879
0
            break;
880
0
        }
881
882
0
        nCurIndex = nCurIndex + rTmpLine.GetLen();
883
0
        nY += mnCharHeight;
884
0
    }
885
0
    if ( !pLine )
886
0
    {
887
        // Cursor at end of paragraph
888
0
        SAL_WARN_IF( rPaM.GetIndex() != nCurIndex, "vcl", "GetEditCursor: Bad Index!" );
889
890
0
        pLine = & ( pPortion->GetLines().back() );
891
0
        nY -= mnCharHeight;
892
0
    }
893
894
0
    tools::Rectangle aEditCursor;
895
896
0
    aEditCursor.SetTop( nY );
897
0
    nY += mnCharHeight;
898
0
    aEditCursor.SetBottom( nY-1 );
899
900
    // search within the line
901
0
    tools::Long nX = ImpGetXPos( rPaM.GetPara(), pLine, rPaM.GetIndex(), bPreferPortionStart );
902
0
    aEditCursor.SetLeft(nX);
903
0
    aEditCursor.SetRight(nX);
904
0
    return aEditCursor;
905
0
}
906
907
tools::Long TextEngine::ImpGetXPos( sal_uInt32 nPara, TextLine* pLine, sal_Int32 nIndex, bool bPreferPortionStart )
908
0
{
909
0
    SAL_WARN_IF( ( nIndex < pLine->GetStart() ) || ( nIndex > pLine->GetEnd() ) , "vcl", "ImpGetXPos: Bad parameters!" );
910
911
0
    bool bDoPreferPortionStart = bPreferPortionStart;
912
    // Assure that the portion belongs to this line
913
0
    if ( nIndex == pLine->GetStart() )
914
0
        bDoPreferPortionStart = true;
915
0
    else if ( nIndex == pLine->GetEnd() )
916
0
        bDoPreferPortionStart = false;
917
918
0
    TEParaPortion* pParaPortion = mpTEParaPortions->GetObject( nPara );
919
920
0
    sal_Int32 nTextPortionStart = 0;
921
0
    std::size_t nTextPortion = pParaPortion->GetTextPortions().FindPortion( nIndex, nTextPortionStart, bDoPreferPortionStart );
922
923
0
    SAL_WARN_IF( ( nTextPortion < pLine->GetStartPortion() ) || ( nTextPortion > pLine->GetEndPortion() ), "vcl", "GetXPos: Portion not in current line!" );
924
925
0
    TETextPortion& rPortion = pParaPortion->GetTextPortions()[ nTextPortion ];
926
927
0
    tools::Long nX = ImpGetPortionXOffset( nPara, pLine, nTextPortion );
928
929
0
    tools::Long nPortionTextWidth = rPortion.GetWidth();
930
931
0
    if ( nTextPortionStart != nIndex )
932
0
    {
933
        // Search within portion...
934
0
        if ( nIndex == ( nTextPortionStart + rPortion.GetLen() ) )
935
0
        {
936
            // End of Portion
937
0
            if ( ( rPortion.GetKind() == PORTIONKIND_TAB ) ||
938
0
                 ( !IsRightToLeft() && !rPortion.IsRightToLeft() ) ||
939
0
                 ( IsRightToLeft() && rPortion.IsRightToLeft() ) )
940
0
            {
941
0
                nX += nPortionTextWidth;
942
0
                if ( ( rPortion.GetKind() == PORTIONKIND_TAB ) && ( (nTextPortion+1) < pParaPortion->GetTextPortions().size() ) )
943
0
                {
944
0
                    TETextPortion& rNextPortion = pParaPortion->GetTextPortions()[ nTextPortion+1 ];
945
0
                    if (rNextPortion.GetKind() != PORTIONKIND_TAB && IsRightToLeft() != rNextPortion.IsRightToLeft())
946
0
                    {
947
                        // End of the tab portion, use start of next for cursor pos
948
0
                        SAL_WARN_IF( bPreferPortionStart, "vcl", "ImpGetXPos: How can we get here!" );
949
0
                        nX = ImpGetXPos( nPara, pLine, nIndex, true );
950
0
                    }
951
952
0
                }
953
0
            }
954
0
        }
955
0
        else if ( rPortion.GetKind() == PORTIONKIND_TEXT )
956
0
        {
957
0
            SAL_WARN_IF( nIndex == pLine->GetStart(), "vcl", "ImpGetXPos: Strange behavior" );
958
959
0
            tools::Long nPosInPortion = CalcTextWidth( nPara, nTextPortionStart, nIndex-nTextPortionStart );
960
961
0
            if (IsRightToLeft() == rPortion.IsRightToLeft())
962
0
            {
963
0
                nX += nPosInPortion;
964
0
            }
965
0
            else
966
0
            {
967
0
                nX += nPortionTextWidth - nPosInPortion;
968
0
            }
969
0
        }
970
0
    }
971
0
    else // if ( nIndex == pLine->GetStart() )
972
0
    {
973
0
        if (rPortion.GetKind() != PORTIONKIND_TAB && IsRightToLeft() != rPortion.IsRightToLeft())
974
0
        {
975
0
            nX += nPortionTextWidth;
976
0
        }
977
0
    }
978
979
0
    return nX;
980
0
}
981
982
const TextAttrib* TextEngine::FindAttrib( const TextPaM& rPaM, sal_uInt16 nWhich ) const
983
0
{
984
0
    const TextAttrib* pAttr = nullptr;
985
0
    const TextCharAttrib* pCharAttr = FindCharAttrib( rPaM, nWhich );
986
0
    if ( pCharAttr )
987
0
        pAttr = &pCharAttr->GetAttr();
988
0
    return pAttr;
989
0
}
990
991
const TextCharAttrib* TextEngine::FindCharAttrib( const TextPaM& rPaM, sal_uInt16 nWhich ) const
992
0
{
993
0
    const TextCharAttrib* pAttr = nullptr;
994
0
    TextNode* pNode = mpDoc->GetNodes()[ rPaM.GetPara() ].get();
995
0
    if (pNode && (rPaM.GetIndex() <= pNode->GetText().getLength()))
996
0
        pAttr = pNode->GetCharAttribs().FindAttrib( nWhich, rPaM.GetIndex() );
997
0
    return pAttr;
998
0
}
999
1000
TextPaM TextEngine::GetPaM( const Point& rDocPos )
1001
0
{
1002
0
    SAL_WARN_IF( !GetUpdateMode(), "vcl", "GetPaM: GetUpdateMode()" );
1003
1004
0
    tools::Long nY = 0;
1005
0
    for ( sal_uInt32 nPortion = 0; nPortion < mpTEParaPortions->Count(); ++nPortion )
1006
0
    {
1007
0
        TEParaPortion* pPortion = mpTEParaPortions->GetObject( nPortion );
1008
0
        tools::Long nTmpHeight = pPortion->GetLines().size() * mnCharHeight;
1009
0
        nY += nTmpHeight;
1010
0
        if ( nY > rDocPos.Y() )
1011
0
        {
1012
0
            nY -= nTmpHeight;
1013
0
            Point aPosInPara( rDocPos );
1014
0
            aPosInPara.AdjustY( -nY );
1015
1016
0
            TextPaM aPaM( nPortion, 0 );
1017
0
            aPaM.GetIndex() = ImpFindIndex( nPortion, aPosInPara );
1018
0
            return aPaM;
1019
0
        }
1020
0
    }
1021
1022
    // not found - go to last visible
1023
0
    const sal_uInt32 nLastNode = static_cast<sal_uInt32>(mpDoc->GetNodes().size() - 1);
1024
0
    TextNode* pLast = mpDoc->GetNodes()[ nLastNode ].get();
1025
0
    return TextPaM( nLastNode, pLast->GetText().getLength() );
1026
0
}
1027
1028
sal_Int32 TextEngine::ImpFindIndex( sal_uInt32 nPortion, const Point& rPosInPara )
1029
0
{
1030
0
    SAL_WARN_IF( !IsFormatted(), "vcl", "GetPaM: Not formatted" );
1031
0
    TEParaPortion* pPortion = mpTEParaPortions->GetObject( nPortion );
1032
1033
0
    sal_Int32 nCurIndex = 0;
1034
1035
0
    tools::Long nY = 0;
1036
0
    TextLine* pLine = nullptr;
1037
0
    std::vector<TextLine>::size_type nLine;
1038
0
    for ( nLine = 0; nLine < pPortion->GetLines().size(); nLine++ )
1039
0
    {
1040
0
        TextLine& rmpLine = pPortion->GetLines()[ nLine ];
1041
0
        nY += mnCharHeight;
1042
0
        if ( nY > rPosInPara.Y() )  // that's it
1043
0
        {
1044
0
            pLine = &rmpLine;
1045
0
            break;                  // correct Y-Position not needed
1046
0
        }
1047
0
    }
1048
1049
0
    assert(pLine && "ImpFindIndex: pLine ?");
1050
1051
0
    nCurIndex = GetCharPos( nPortion, nLine, rPosInPara.X() );
1052
1053
0
    if ( nCurIndex && ( nCurIndex == pLine->GetEnd() ) &&
1054
0
         ( pLine != &( pPortion->GetLines().back() ) ) )
1055
0
    {
1056
0
        uno::Reference < i18n::XBreakIterator > xBI = GetBreakIterator();
1057
0
        sal_Int32 nCount = 1;
1058
0
        nCurIndex = xBI->previousCharacters( pPortion->GetNode()->GetText(), nCurIndex, GetLocale(), i18n::CharacterIteratorMode::SKIPCELL, nCount, nCount );
1059
0
    }
1060
0
    return nCurIndex;
1061
0
}
1062
1063
sal_Int32 TextEngine::GetCharPos( sal_uInt32 nPortion, std::vector<TextLine>::size_type nLine, tools::Long nXPos )
1064
0
{
1065
1066
0
    TEParaPortion* pPortion = mpTEParaPortions->GetObject( nPortion );
1067
0
    TextLine& rLine = pPortion->GetLines()[ nLine ];
1068
1069
0
    sal_Int32 nCurIndex = rLine.GetStart();
1070
1071
0
    tools::Long nTmpX = rLine.GetStartX();
1072
0
    if ( nXPos <= nTmpX )
1073
0
        return nCurIndex;
1074
1075
0
    for ( std::size_t i = rLine.GetStartPortion(); i <= rLine.GetEndPortion(); i++ )
1076
0
    {
1077
0
        TETextPortion& rTextPortion = pPortion->GetTextPortions()[ i ];
1078
0
        nTmpX += rTextPortion.GetWidth();
1079
1080
0
        if ( nTmpX > nXPos )
1081
0
        {
1082
0
            if( rTextPortion.GetLen() > 1 )
1083
0
            {
1084
0
                nTmpX -= rTextPortion.GetWidth();  // position before Portion
1085
                // TODO: Optimize: no GetTextBreak if fixed-width Font
1086
0
                vcl::Font aFont;
1087
0
                SeekCursor( nPortion, nCurIndex+1, aFont, nullptr );
1088
0
                mpRefDev->SetFont( aFont);
1089
0
                tools::Long nPosInPortion = nXPos-nTmpX;
1090
0
                if ( IsRightToLeft() != rTextPortion.IsRightToLeft() )
1091
0
                    nPosInPortion = rTextPortion.GetWidth() - nPosInPortion;
1092
0
                nCurIndex = mpRefDev->GetTextBreak( pPortion->GetNode()->GetText(), nPosInPortion, nCurIndex );
1093
                // MT: GetTextBreak should assure that we are not within a CTL cell...
1094
0
            }
1095
0
            return nCurIndex;
1096
0
        }
1097
0
        nCurIndex += rTextPortion.GetLen();
1098
0
    }
1099
0
    return nCurIndex;
1100
0
}
1101
1102
tools::Long TextEngine::GetTextHeight() const
1103
0
{
1104
0
    SAL_WARN_IF( !GetUpdateMode(), "vcl", "GetTextHeight: GetUpdateMode()" );
1105
1106
0
    if ( !IsFormatted() && !IsFormatting() )
1107
0
        const_cast<TextEngine*>(this)->FormatAndUpdate();
1108
1109
0
    return mnCurTextHeight;
1110
0
}
1111
1112
tools::Long TextEngine::GetTextHeight( sal_uInt32 nParagraph ) const
1113
0
{
1114
0
    SAL_WARN_IF( !GetUpdateMode(), "vcl", "GetTextHeight: GetUpdateMode()" );
1115
1116
0
    if ( !IsFormatted() && !IsFormatting() )
1117
0
        const_cast<TextEngine*>(this)->FormatAndUpdate();
1118
1119
0
    return CalcParaHeight( nParagraph );
1120
0
}
1121
1122
tools::Long TextEngine::CalcTextWidth( sal_uInt32 nPara )
1123
0
{
1124
0
    tools::Long nParaWidth = 0;
1125
0
    TEParaPortion* pPortion = mpTEParaPortions->GetObject( nPara );
1126
0
    for ( auto nLine = pPortion->GetLines().size(); nLine; )
1127
0
    {
1128
0
        tools::Long nLineWidth = 0;
1129
0
        TextLine& rLine = pPortion->GetLines()[ --nLine ];
1130
0
        for ( std::size_t nTP = rLine.GetStartPortion(); nTP <= rLine.GetEndPortion(); nTP++ )
1131
0
        {
1132
0
            TETextPortion& rTextPortion = pPortion->GetTextPortions()[ nTP ];
1133
0
            nLineWidth += rTextPortion.GetWidth();
1134
0
        }
1135
0
        if ( nLineWidth > nParaWidth )
1136
0
            nParaWidth = nLineWidth;
1137
0
    }
1138
0
    return nParaWidth;
1139
0
}
1140
1141
tools::Long TextEngine::CalcTextWidth()
1142
0
{
1143
0
    if ( !IsFormatted() && !IsFormatting() )
1144
0
        FormatAndUpdate();
1145
1146
0
    if ( mnCurTextWidth < 0 )
1147
0
    {
1148
0
        mnCurTextWidth = 0;
1149
0
        for ( sal_uInt32 nPara = mpTEParaPortions->Count(); nPara; )
1150
0
        {
1151
0
            const tools::Long nParaWidth = CalcTextWidth( --nPara );
1152
0
            if ( nParaWidth > mnCurTextWidth )
1153
0
                mnCurTextWidth = nParaWidth;
1154
0
        }
1155
0
    }
1156
0
    return mnCurTextWidth+1;// wider by 1, as CreateLines breaks at >=
1157
0
}
1158
1159
tools::Long TextEngine::CalcTextHeight() const
1160
0
{
1161
0
    SAL_WARN_IF( !GetUpdateMode(), "vcl", "CalcTextHeight: GetUpdateMode()" );
1162
1163
0
    tools::Long nY = 0;
1164
0
    for ( auto nPortion = mpTEParaPortions->Count(); nPortion; )
1165
0
        nY += CalcParaHeight( --nPortion );
1166
0
    return nY;
1167
0
}
1168
1169
tools::Long TextEngine::CalcTextWidth( sal_uInt32 nPara, sal_Int32 nPortionStart, sal_Int32 nLen )
1170
0
{
1171
#ifdef DBG_UTIL
1172
    // within the text there must not be a Portion change (attribute/tab)!
1173
    sal_Int32 nTabPos = mpDoc->GetNodes()[ nPara ]->GetText().indexOf( '\t', nPortionStart );
1174
    SAL_WARN_IF( nTabPos != -1 && nTabPos < (nPortionStart+nLen), "vcl", "CalcTextWidth: Tab!" );
1175
#endif
1176
1177
0
    vcl::Font aFont;
1178
0
    SeekCursor( nPara, nPortionStart+1, aFont, nullptr );
1179
0
    mpRefDev->SetFont( aFont );
1180
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
1181
0
    tools::Long nWidth = mpRefDev->GetTextWidth( pNode->GetText(), nPortionStart, nLen );
1182
0
    return nWidth;
1183
0
}
1184
1185
void TextEngine::GetTextPortionRange(const TextPaM& rPaM, sal_Int32& nStart, sal_Int32& nEnd)
1186
0
{
1187
0
    nStart = 0;
1188
0
    nEnd = 0;
1189
0
    TEParaPortion* pParaPortion = mpTEParaPortions->GetObject( rPaM.GetPara() );
1190
0
    for ( std::size_t i = 0; i < pParaPortion->GetTextPortions().size(); ++i )
1191
0
    {
1192
0
        TETextPortion& rTextPortion = pParaPortion->GetTextPortions()[ i ];
1193
0
        if (nStart + rTextPortion.GetLen() > rPaM.GetIndex())
1194
0
        {
1195
0
            nEnd = nStart + rTextPortion.GetLen();
1196
0
            return;
1197
0
        }
1198
0
        else
1199
0
        {
1200
0
            nStart += rTextPortion.GetLen();
1201
0
        }
1202
0
    }
1203
0
}
1204
1205
sal_uInt16 TextEngine::GetLineCount( sal_uInt32 nParagraph ) const
1206
0
{
1207
0
    SAL_WARN_IF( nParagraph >= mpTEParaPortions->Count(), "vcl", "GetLineCount: Out of range" );
1208
1209
0
    TEParaPortion* pPPortion = mpTEParaPortions->GetObject( nParagraph );
1210
0
    if ( pPPortion )
1211
0
        return pPPortion->GetLines().size();
1212
1213
0
    return 0;
1214
0
}
1215
1216
sal_Int32 TextEngine::GetLineLen( sal_uInt32 nParagraph, sal_uInt16 nLine ) const
1217
0
{
1218
0
    SAL_WARN_IF( nParagraph >= mpTEParaPortions->Count(), "vcl", "GetLineCount: Out of range" );
1219
1220
0
    TEParaPortion* pPPortion = mpTEParaPortions->GetObject( nParagraph );
1221
0
    if ( pPPortion && ( nLine < pPPortion->GetLines().size() ) )
1222
0
    {
1223
0
        return pPPortion->GetLines()[ nLine ].GetLen();
1224
0
    }
1225
1226
0
    return 0;
1227
0
}
1228
1229
tools::Long TextEngine::CalcParaHeight( sal_uInt32 nParagraph ) const
1230
0
{
1231
0
    tools::Long nHeight = 0;
1232
1233
0
    TEParaPortion* pPPortion = mpTEParaPortions->GetObject( nParagraph );
1234
0
    SAL_WARN_IF( !pPPortion, "vcl", "GetParaHeight: paragraph not found" );
1235
0
    if ( pPPortion )
1236
0
        nHeight = pPPortion->GetLines().size() * mnCharHeight;
1237
1238
0
    return nHeight;
1239
0
}
1240
1241
Range TextEngine::GetInvalidYOffsets( sal_uInt32 nPortion )
1242
0
{
1243
0
    TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPortion );
1244
0
    sal_uInt16 nLines = pTEParaPortion->GetLines().size();
1245
0
    sal_uInt16 nLastInvalid, nFirstInvalid = 0;
1246
0
    sal_uInt16 nLine;
1247
0
    for ( nLine = 0; nLine < nLines; nLine++ )
1248
0
    {
1249
0
        TextLine& rL = pTEParaPortion->GetLines()[ nLine ];
1250
0
        if ( rL.IsInvalid() )
1251
0
        {
1252
0
            nFirstInvalid = nLine;
1253
0
            break;
1254
0
        }
1255
0
    }
1256
1257
0
    for ( nLastInvalid = nFirstInvalid; nLastInvalid < nLines; nLastInvalid++ )
1258
0
    {
1259
0
        TextLine& rL = pTEParaPortion->GetLines()[ nLine ];
1260
0
        if ( rL.IsValid() )
1261
0
            break;
1262
0
    }
1263
1264
0
    if ( nLastInvalid >= nLines )
1265
0
        nLastInvalid = nLines-1;
1266
1267
0
    return Range( nFirstInvalid*mnCharHeight, ((nLastInvalid+1)*mnCharHeight)-1 );
1268
0
}
1269
1270
sal_uInt32 TextEngine::GetParagraphCount() const
1271
0
{
1272
0
    return static_cast<sal_uInt32>(mpDoc->GetNodes().size());
1273
0
}
1274
1275
void TextEngine::EnableUndo( bool bEnable )
1276
0
{
1277
    // delete list when switching mode
1278
0
    if ( bEnable != IsUndoEnabled() )
1279
0
        ResetUndo();
1280
1281
0
    mbUndoEnabled = bEnable;
1282
0
}
1283
1284
SfxUndoManager& TextEngine::GetUndoManager()
1285
0
{
1286
0
    if ( !mpUndoManager )
1287
0
        mpUndoManager.reset( new TextUndoManager( this ) );
1288
0
    return *mpUndoManager;
1289
0
}
1290
1291
void TextEngine::UndoActionStart( sal_uInt16 nId )
1292
0
{
1293
0
    if ( IsUndoEnabled() && !IsInUndo() )
1294
0
    {
1295
0
        GetUndoManager().EnterListAction( OUString(), OUString(), nId, ViewShellId(-1) );
1296
0
    }
1297
0
}
1298
1299
void TextEngine::UndoActionEnd()
1300
0
{
1301
0
    if ( IsUndoEnabled() && !IsInUndo() )
1302
0
        GetUndoManager().LeaveListAction();
1303
0
}
1304
1305
void TextEngine::InsertUndo( std::unique_ptr<TextUndo> pUndo, bool bTryMerge )
1306
0
{
1307
0
    SAL_WARN_IF( IsInUndo(), "vcl", "InsertUndo: in Undo mode!" );
1308
0
    GetUndoManager().AddUndoAction( std::move(pUndo), bTryMerge );
1309
0
}
1310
1311
void TextEngine::ResetUndo()
1312
0
{
1313
0
    if ( mpUndoManager )
1314
0
        mpUndoManager->Clear();
1315
0
}
1316
1317
void TextEngine::InsertContent( std::unique_ptr<TextNode> pNode, sal_uInt32 nPara )
1318
0
{
1319
0
    SAL_WARN_IF( !pNode, "vcl", "InsertContent: NULL-Pointer!" );
1320
0
    SAL_WARN_IF( !IsInUndo(), "vcl", "InsertContent: only in Undo()!" );
1321
0
    TEParaPortion* pNew = new TEParaPortion( pNode.get() );
1322
0
    mpTEParaPortions->Insert( pNew, nPara );
1323
0
    mpDoc->GetNodes().insert( mpDoc->GetNodes().begin() + nPara, std::move(pNode) );
1324
0
    ImpParagraphInserted( nPara );
1325
0
}
1326
1327
TextPaM TextEngine::SplitContent( sal_uInt32 nNode, sal_Int32 nSepPos )
1328
0
{
1329
#ifdef DBG_UTIL
1330
    TextNode* pNode = mpDoc->GetNodes()[ nNode ].get();
1331
    assert(pNode && "SplitContent: Invalid Node!");
1332
    SAL_WARN_IF( !IsInUndo(), "vcl", "SplitContent: only in Undo()!" );
1333
    SAL_WARN_IF( nSepPos > pNode->GetText().getLength(), "vcl", "SplitContent: Bad index" );
1334
#endif
1335
0
    TextPaM aPaM( nNode, nSepPos );
1336
0
    return ImpInsertParaBreak( aPaM );
1337
0
}
1338
1339
TextPaM TextEngine::ConnectContents( sal_uInt32 nLeftNode )
1340
0
{
1341
0
    SAL_WARN_IF( !IsInUndo(), "vcl", "ConnectContent: only in Undo()!" );
1342
0
    return ImpConnectParagraphs( nLeftNode, nLeftNode+1 );
1343
0
}
1344
1345
void TextEngine::SeekCursor( sal_uInt32 nPara, sal_Int32 nPos, vcl::Font& rFont, OutputDevice* pOutDev )
1346
0
{
1347
0
    rFont = maFont;
1348
0
    if ( pOutDev )
1349
0
        pOutDev->SetTextColor( maTextColor );
1350
1351
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
1352
0
    sal_uInt16 nAttribs = pNode->GetCharAttribs().Count();
1353
0
    for ( sal_uInt16 nAttr = 0; nAttr < nAttribs; nAttr++ )
1354
0
    {
1355
0
        TextCharAttrib& rAttrib = pNode->GetCharAttribs().GetAttrib( nAttr );
1356
0
        if ( rAttrib.GetStart() > nPos )
1357
0
            break;
1358
1359
        // When seeking don't use Attr that start there!
1360
        // Do not use empty attributes:
1361
        // - If just being setup and empty => no effect on Font
1362
        // - Characters that are setup in an empty paragraph become visible right away.
1363
0
        if ( ( ( rAttrib.GetStart() < nPos ) && ( rAttrib.GetEnd() >= nPos ) )
1364
0
                    || pNode->GetText().isEmpty() )
1365
0
        {
1366
0
            if ( rAttrib.Which() != TEXTATTR_FONTCOLOR )
1367
0
            {
1368
0
                rAttrib.GetAttr().SetFont(rFont);
1369
0
            }
1370
0
            else
1371
0
            {
1372
0
                if ( pOutDev )
1373
0
                    pOutDev->SetTextColor( static_cast<const TextAttribFontColor&>(rAttrib.GetAttr()).GetColor() );
1374
0
            }
1375
0
        }
1376
0
    }
1377
1378
0
    if ( !(mpIMEInfos && mpIMEInfos->pAttribs && ( mpIMEInfos->aPos.GetPara() == nPara ) &&
1379
0
        ( nPos > mpIMEInfos->aPos.GetIndex() ) && ( nPos <= ( mpIMEInfos->aPos.GetIndex() + mpIMEInfos->nLen ) )) )
1380
0
        return;
1381
1382
0
    ExtTextInputAttr nAttr = mpIMEInfos->pAttribs[ nPos - mpIMEInfos->aPos.GetIndex() - 1 ];
1383
0
    if ( nAttr & ExtTextInputAttr::Underline )
1384
0
        rFont.SetUnderline( LINESTYLE_SINGLE );
1385
0
    else if ( nAttr & ExtTextInputAttr::DoubleUnderline )
1386
0
        rFont.SetUnderline( LINESTYLE_DOUBLE );
1387
0
    else if ( nAttr & ExtTextInputAttr::BoldUnderline )
1388
0
        rFont.SetUnderline( LINESTYLE_BOLD );
1389
0
    else if ( nAttr & ExtTextInputAttr::DottedUnderline )
1390
0
        rFont.SetUnderline( LINESTYLE_DOTTED );
1391
0
    else if ( nAttr & ExtTextInputAttr::DashDotUnderline )
1392
0
        rFont.SetUnderline( LINESTYLE_DOTTED );
1393
0
    if ( nAttr & ExtTextInputAttr::RedText )
1394
0
        rFont.SetColor( COL_RED );
1395
0
    else if ( nAttr & ExtTextInputAttr::HalfToneText )
1396
0
        rFont.SetColor( COL_LIGHTGRAY );
1397
0
    if ( nAttr & ExtTextInputAttr::Highlight )
1398
0
    {
1399
0
        const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
1400
0
        rFont.SetColor( rStyleSettings.GetHighlightTextColor() );
1401
0
        rFont.SetFillColor( rStyleSettings.GetHighlightColor() );
1402
0
        rFont.SetTransparent( false );
1403
0
    }
1404
0
    else if ( nAttr & ExtTextInputAttr::GrayWaveline )
1405
0
    {
1406
0
        rFont.SetUnderline( LINESTYLE_WAVE );
1407
//          if( pOut )
1408
//              pOut->SetTextLineColor( COL_LIGHTGRAY );
1409
0
    }
1410
0
}
1411
1412
void TextEngine::FormatAndUpdate( TextView* pCurView )
1413
0
{
1414
0
    if ( mbDowning )
1415
0
        return;
1416
1417
0
    if ( IsInUndo() )
1418
0
        IdleFormatAndUpdate( pCurView );
1419
0
    else
1420
0
    {
1421
0
        FormatDoc();
1422
0
        UpdateViews( pCurView );
1423
0
    }
1424
0
}
1425
1426
void TextEngine::IdleFormatAndUpdate( TextView* pCurView, sal_uInt16 nMaxTimerRestarts )
1427
0
{
1428
0
    mpIdleFormatter->DoIdleFormat( pCurView, nMaxTimerRestarts );
1429
0
}
1430
1431
void TextEngine::TextModified()
1432
0
{
1433
0
    mbFormatted = false;
1434
0
    mbModified = true;
1435
0
}
1436
1437
void TextEngine::UpdateViews( TextView* pCurView )
1438
0
{
1439
0
    if ( !GetUpdateMode() || IsFormatting() || maInvalidRect.IsEmpty() )
1440
0
        return;
1441
1442
0
    SAL_WARN_IF( !IsFormatted(), "vcl", "UpdateViews: Doc not formatted!" );
1443
1444
0
    for (TextView* pView : *mpViews)
1445
0
    {
1446
0
        pView->HideCursor();
1447
1448
0
        tools::Rectangle aClipRect( maInvalidRect );
1449
0
        const Size aOutSz = pView->GetWindow()->GetOutputSizePixel();
1450
0
        const tools::Rectangle aVisArea( pView->GetStartDocPos(), aOutSz );
1451
0
        aClipRect.Intersection( aVisArea );
1452
0
        if ( !aClipRect.IsEmpty() )
1453
0
        {
1454
            // translate into window coordinates
1455
0
            Point aNewPos = pView->GetWindowPos( aClipRect.TopLeft() );
1456
0
            if ( IsRightToLeft() )
1457
0
                aNewPos.AdjustX( -(aOutSz.Width() - 1) );
1458
0
            aClipRect.SetPos( aNewPos );
1459
1460
0
            pView->GetWindow()->Invalidate( aClipRect );
1461
0
        }
1462
0
    }
1463
1464
0
    if ( pCurView )
1465
0
    {
1466
0
        pCurView->ShowCursor( pCurView->IsAutoScroll() );
1467
0
    }
1468
1469
0
    maInvalidRect = tools::Rectangle();
1470
0
}
1471
1472
IMPL_LINK_NOARG(TextEngine, IdleFormatHdl, Timer *, void)
1473
0
{
1474
0
    FormatAndUpdate( mpIdleFormatter->GetView() );
1475
0
}
1476
1477
void TextEngine::CheckIdleFormatter()
1478
0
{
1479
0
    mpIdleFormatter->ForceTimeout();
1480
0
}
1481
1482
void TextEngine::FormatFullDoc()
1483
0
{
1484
0
    for ( sal_uInt32 nPortion = 0; nPortion < mpTEParaPortions->Count(); ++nPortion )
1485
0
    {
1486
0
        TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPortion );
1487
0
        pTEParaPortion->MarkSelectionInvalid( 0 );
1488
0
    }
1489
0
    mbFormatted = false;
1490
0
    FormatDoc();
1491
0
}
1492
1493
void TextEngine::FormatDoc()
1494
0
{
1495
0
    if ( IsFormatted() || !GetUpdateMode() || IsFormatting() )
1496
0
        return;
1497
1498
0
    mbIsFormatting = true;
1499
0
    mbHasMultiLineParas = false;
1500
1501
0
    tools::Long nY = 0;
1502
0
    bool bGrow = false;
1503
1504
0
    maInvalidRect = tools::Rectangle(); // clear
1505
0
    for ( sal_uInt32 nPara = 0; nPara < mpTEParaPortions->Count(); ++nPara )
1506
0
    {
1507
0
        TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
1508
0
        if ( pTEParaPortion->IsInvalid() )
1509
0
        {
1510
0
            const tools::Long nOldParaWidth = mnCurTextWidth >= 0 ? CalcTextWidth( nPara ) : -1;
1511
1512
0
            Broadcast( TextHint( SfxHintId::TextFormatPara, nPara ) );
1513
1514
0
            if ( CreateLines( nPara ) )
1515
0
                bGrow = true;
1516
1517
            // set InvalidRect only once
1518
0
            if ( maInvalidRect.IsEmpty() )
1519
0
            {
1520
                // otherwise remains Empty() for Paperwidth 0 (AutoPageSize)
1521
0
                const tools::Long nWidth = mnMaxTextWidth
1522
0
                    ? mnMaxTextWidth
1523
0
                    : std::numeric_limits<tools::Long>::max();
1524
0
                const Range aInvRange( GetInvalidYOffsets( nPara ) );
1525
0
                maInvalidRect = tools::Rectangle( Point( 0, nY+aInvRange.Min() ),
1526
0
                    Size( nWidth, aInvRange.Len() ) );
1527
0
            }
1528
0
            else
1529
0
            {
1530
0
                maInvalidRect.SetBottom( nY + CalcParaHeight( nPara ) );
1531
0
            }
1532
1533
0
            if ( mnCurTextWidth >= 0 )
1534
0
            {
1535
0
                const tools::Long nNewParaWidth = CalcTextWidth( nPara );
1536
0
                if ( nNewParaWidth >= mnCurTextWidth )
1537
0
                    mnCurTextWidth = nNewParaWidth;
1538
0
                else if ( nOldParaWidth >= mnCurTextWidth )
1539
0
                    mnCurTextWidth = -1;
1540
0
            }
1541
0
        }
1542
0
        else if ( bGrow )
1543
0
        {
1544
0
            maInvalidRect.SetBottom( nY + CalcParaHeight( nPara ) );
1545
0
        }
1546
0
        nY += CalcParaHeight( nPara );
1547
0
        if ( !mbHasMultiLineParas && pTEParaPortion->GetLines().size() > 1 )
1548
0
            mbHasMultiLineParas = true;
1549
0
    }
1550
1551
0
    if ( !maInvalidRect.IsEmpty() )
1552
0
    {
1553
0
        const tools::Long nNewHeight = CalcTextHeight();
1554
0
        const tools::Long nDiff = nNewHeight - mnCurTextHeight;
1555
0
        if ( nNewHeight < mnCurTextHeight )
1556
0
        {
1557
0
            maInvalidRect.SetBottom( std::max( nNewHeight, mnCurTextHeight ) );
1558
0
            if ( maInvalidRect.IsEmpty() )
1559
0
            {
1560
0
                maInvalidRect.SetTop( 0 );
1561
                // Left and Right are not evaluated, but set because of IsEmpty
1562
0
                maInvalidRect.SetLeft( 0 );
1563
0
                maInvalidRect.SetRight( mnMaxTextWidth );
1564
0
            }
1565
0
        }
1566
1567
0
        mnCurTextHeight = nNewHeight;
1568
0
        if ( nDiff )
1569
0
        {
1570
0
            mbFormatted = true;
1571
0
            Broadcast( TextHint( SfxHintId::TextHeightChanged ) );
1572
0
        }
1573
0
    }
1574
1575
0
    mbIsFormatting = false;
1576
0
    mbFormatted = true;
1577
1578
0
    Broadcast( TextHint( SfxHintId::TextFormatted ) );
1579
0
}
1580
1581
void TextEngine::CreateAndInsertEmptyLine( sal_uInt32 nPara )
1582
0
{
1583
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
1584
0
    TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
1585
1586
0
    TextLine aTmpLine;
1587
0
    aTmpLine.SetStart( pNode->GetText().getLength() );
1588
0
    aTmpLine.SetEnd( aTmpLine.GetStart() );
1589
1590
0
    if ( ImpGetAlign() == TxtAlign::Center )
1591
0
        aTmpLine.SetStartX( static_cast<short>(mnMaxTextWidth / 2) );
1592
0
    else if ( ImpGetAlign() == TxtAlign::Right )
1593
0
        aTmpLine.SetStartX( static_cast<short>(mnMaxTextWidth) );
1594
0
    else
1595
0
        aTmpLine.SetStartX( mpDoc->GetLeftMargin() );
1596
1597
0
    bool bLineBreak = !pNode->GetText().isEmpty();
1598
1599
0
    TETextPortion aDummyPortion( 0 );
1600
0
    aDummyPortion.GetWidth() = 0;
1601
0
    pTEParaPortion->GetTextPortions().push_back( aDummyPortion );
1602
1603
0
    if ( bLineBreak )
1604
0
    {
1605
        // -2: The new one is already inserted.
1606
0
        const std::size_t nPos = pTEParaPortion->GetTextPortions().size() - 1;
1607
0
        aTmpLine.SetStartPortion( nPos );
1608
0
        aTmpLine.SetEndPortion( nPos );
1609
0
    }
1610
0
    pTEParaPortion->GetLines().push_back( aTmpLine );
1611
0
}
1612
1613
void TextEngine::ImpBreakLine( sal_uInt32 nPara, TextLine* pLine, sal_Int32 nPortionStart, tools::Long nRemainingWidth )
1614
0
{
1615
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
1616
1617
    // Font still should be adjusted
1618
0
    sal_Int32 nMaxBreakPos = mpRefDev->GetTextBreak( pNode->GetText(), nRemainingWidth, nPortionStart );
1619
1620
0
    SAL_WARN_IF( nMaxBreakPos >= pNode->GetText().getLength(), "vcl", "ImpBreakLine: Break?!" );
1621
1622
0
    if ( nMaxBreakPos == -1 )   // GetTextBreak() != GetTextSize()
1623
0
        nMaxBreakPos = pNode->GetText().getLength() - 1;
1624
1625
0
    uno::Reference < i18n::XBreakIterator > xBI = GetBreakIterator();
1626
0
    i18n::LineBreakHyphenationOptions aHyphOptions( nullptr, uno::Sequence< beans::PropertyValue >(), 1 );
1627
1628
0
    i18n::LineBreakUserOptions aUserOptions;
1629
0
    aUserOptions.forbiddenBeginCharacters = ImpGetLocaleDataWrapper()->getForbiddenCharacters().beginLine;
1630
0
    aUserOptions.forbiddenEndCharacters = ImpGetLocaleDataWrapper()->getForbiddenCharacters().endLine;
1631
0
    aUserOptions.applyForbiddenRules = true;
1632
0
    aUserOptions.allowPunctuationOutsideMargin = false;
1633
0
    aUserOptions.allowHyphenateEnglish = false;
1634
1635
0
    static const css::lang::Locale aDefLocale;
1636
0
    i18n::LineBreakResults aLBR = xBI->getLineBreak( pNode->GetText(), nMaxBreakPos, aDefLocale, pLine->GetStart(), aHyphOptions, aUserOptions );
1637
0
    sal_Int32 nBreakPos = aLBR.breakIndex;
1638
0
    if ( nBreakPos <= pLine->GetStart() )
1639
0
    {
1640
0
        nBreakPos = nMaxBreakPos;
1641
0
        if ( nBreakPos <= pLine->GetStart() )
1642
0
            nBreakPos = pLine->GetStart() + 1;  // infinite loop otherwise!
1643
0
    }
1644
1645
    // the damaged Portion is the End Portion
1646
0
    pLine->SetEnd( nBreakPos );
1647
0
    const std::size_t nEndPortion = SplitTextPortion( nPara, nBreakPos );
1648
1649
0
    if ( nBreakPos >= pLine->GetStart() &&
1650
0
         nBreakPos < pNode->GetText().getLength() &&
1651
0
         pNode->GetText()[ nBreakPos ] == ' ' )
1652
0
    {
1653
        // generally suppress blanks at the end of line
1654
0
        TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
1655
0
        TETextPortion& rTP = pTEParaPortion->GetTextPortions()[ nEndPortion ];
1656
0
        SAL_WARN_IF( nBreakPos <= pLine->GetStart(), "vcl", "ImpBreakLine: SplitTextPortion at beginning of line?" );
1657
0
        rTP.GetWidth() = CalcTextWidth( nPara, nBreakPos-rTP.GetLen(), rTP.GetLen()-1 );
1658
0
    }
1659
0
    pLine->SetEndPortion( nEndPortion );
1660
0
}
1661
1662
std::size_t TextEngine::SplitTextPortion( sal_uInt32 nPara, sal_Int32 nPos )
1663
0
{
1664
1665
    // the Portion at nPos is being split, unless there is already a switch at nPos
1666
0
    if ( nPos == 0 )
1667
0
        return 0;
1668
1669
0
    std::size_t nSplitPortion;
1670
0
    sal_Int32 nTmpPos = 0;
1671
0
    TETextPortion* pTextPortion = nullptr;
1672
0
    TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
1673
0
    const std::size_t nPortions = pTEParaPortion->GetTextPortions().size();
1674
0
    for ( nSplitPortion = 0; nSplitPortion < nPortions; nSplitPortion++ )
1675
0
    {
1676
0
        TETextPortion& rTP = pTEParaPortion->GetTextPortions()[nSplitPortion];
1677
0
        nTmpPos += rTP.GetLen();
1678
0
        if ( nTmpPos >= nPos )
1679
0
        {
1680
0
            if ( nTmpPos == nPos )  // nothing needs splitting
1681
0
                return nSplitPortion;
1682
0
            pTextPortion = &rTP;
1683
0
            break;
1684
0
        }
1685
0
    }
1686
1687
0
    assert(pTextPortion && "SplitTextPortion: position outside of region!");
1688
1689
0
    const sal_Int32 nOverlapp = nTmpPos - nPos;
1690
0
    pTextPortion->GetLen() -= nOverlapp;
1691
0
    pTextPortion->GetWidth() = CalcTextWidth( nPara, nPos-pTextPortion->GetLen(), pTextPortion->GetLen() );
1692
0
    TETextPortion aNewPortion( nOverlapp );
1693
0
    pTEParaPortion->GetTextPortions().insert( pTEParaPortion->GetTextPortions().begin() + nSplitPortion + 1, aNewPortion );
1694
1695
0
    return nSplitPortion;
1696
0
}
1697
1698
void TextEngine::CreateTextPortions( sal_uInt32 nPara, sal_Int32 nStartPos )
1699
0
{
1700
0
    TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
1701
0
    TextNode* pNode = pTEParaPortion->GetNode();
1702
0
    SAL_WARN_IF( pNode->GetText().isEmpty(), "vcl", "CreateTextPortions: should not be used for empty paragraphs!" );
1703
1704
0
    o3tl::sorted_vector<sal_Int32> aPositions;
1705
0
    o3tl::sorted_vector<sal_Int32>::const_iterator aPositionsIt;
1706
0
    aPositions.insert(0);
1707
1708
0
    const sal_uInt16 nAttribs = pNode->GetCharAttribs().Count();
1709
0
    for ( sal_uInt16 nAttr = 0; nAttr < nAttribs; nAttr++ )
1710
0
    {
1711
0
        TextCharAttrib& rAttrib = pNode->GetCharAttribs().GetAttrib( nAttr );
1712
1713
0
        aPositions.insert( rAttrib.GetStart() );
1714
0
        aPositions.insert( rAttrib.GetEnd() );
1715
0
    }
1716
0
    aPositions.insert( pNode->GetText().getLength() );
1717
1718
0
    const std::vector<TEWritingDirectionInfo>& rWritingDirections = pTEParaPortion->GetWritingDirectionInfos();
1719
0
    for ( const auto& rWritingDirection : rWritingDirections )
1720
0
        aPositions.insert( rWritingDirection.nStartPos );
1721
1722
0
    if ( mpIMEInfos && mpIMEInfos->pAttribs && ( mpIMEInfos->aPos.GetPara() == nPara ) )
1723
0
    {
1724
0
        ExtTextInputAttr nLastAttr = ExtTextInputAttr(0xffff);
1725
0
        for( sal_Int32 n = 0; n < mpIMEInfos->nLen; n++ )
1726
0
        {
1727
0
            if ( mpIMEInfos->pAttribs[n] != nLastAttr )
1728
0
            {
1729
0
                aPositions.insert( mpIMEInfos->aPos.GetIndex() + n );
1730
0
                nLastAttr = mpIMEInfos->pAttribs[n];
1731
0
            }
1732
0
        }
1733
0
    }
1734
1735
0
    sal_Int32 nTabPos = pNode->GetText().indexOf( '\t' );
1736
0
    while ( nTabPos != -1 )
1737
0
    {
1738
0
        aPositions.insert( nTabPos );
1739
0
        aPositions.insert( nTabPos + 1 );
1740
0
        nTabPos = pNode->GetText().indexOf( '\t', nTabPos+1 );
1741
0
    }
1742
1743
    // Delete starting with...
1744
    // Unfortunately, the number of TextPortions does not have to be
1745
    // equal to aPositions.Count(), because of linebreaks
1746
0
    sal_Int32 nPortionStart = 0;
1747
0
    std::size_t nInvPortion = 0;
1748
0
    std::size_t nP;
1749
0
    for ( nP = 0; nP < pTEParaPortion->GetTextPortions().size(); nP++ )
1750
0
    {
1751
0
        TETextPortion& rTmpPortion = pTEParaPortion->GetTextPortions()[nP];
1752
0
        nPortionStart += rTmpPortion.GetLen();
1753
0
        if ( nPortionStart >= nStartPos )
1754
0
        {
1755
0
            nPortionStart -= rTmpPortion.GetLen();
1756
0
            nInvPortion = nP;
1757
0
            break;
1758
0
        }
1759
0
    }
1760
0
    OSL_ENSURE(nP < pTEParaPortion->GetTextPortions().size()
1761
0
            || pTEParaPortion->GetTextPortions().empty(),
1762
0
            "CreateTextPortions: Nothing to delete!");
1763
0
    if ( nInvPortion && ( nPortionStart+pTEParaPortion->GetTextPortions()[nInvPortion].GetLen() > nStartPos ) )
1764
0
    {
1765
        // better one before...
1766
        // But only if it was within the Portion; otherwise it might be
1767
        // the only one in the previous line!
1768
0
        nInvPortion--;
1769
0
        nPortionStart -= pTEParaPortion->GetTextPortions()[nInvPortion].GetLen();
1770
0
    }
1771
0
    pTEParaPortion->GetTextPortions().DeleteFromPortion( nInvPortion );
1772
1773
    // a Portion might have been created by a line break
1774
0
    aPositions.insert( nPortionStart );
1775
1776
0
    aPositionsIt = aPositions.find( nPortionStart );
1777
0
    SAL_WARN_IF( aPositionsIt == aPositions.end(), "vcl", "CreateTextPortions: nPortionStart not found" );
1778
1779
0
    if ( aPositionsIt != aPositions.end() )
1780
0
    {
1781
0
        o3tl::sorted_vector<sal_Int32>::const_iterator nextIt = aPositionsIt;
1782
0
        for ( ++nextIt; nextIt != aPositions.end(); ++aPositionsIt, ++nextIt )
1783
0
        {
1784
0
            TETextPortion aNew( *nextIt - *aPositionsIt );
1785
0
            pTEParaPortion->GetTextPortions().push_back( aNew );
1786
0
        }
1787
0
    }
1788
0
    OSL_ENSURE(pTEParaPortion->GetTextPortions().size(), "CreateTextPortions: No Portions?!");
1789
0
}
1790
1791
void TextEngine::RecalcTextPortion( sal_uInt32 nPara, sal_Int32 nStartPos, sal_Int32 nNewChars )
1792
0
{
1793
0
    TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
1794
0
    OSL_ENSURE(pTEParaPortion->GetTextPortions().size(), "RecalcTextPortion: no Portions!");
1795
0
    OSL_ENSURE(nNewChars, "RecalcTextPortion: Diff == 0");
1796
1797
0
    TextNode* const pNode = pTEParaPortion->GetNode();
1798
0
    if ( nNewChars > 0 )
1799
0
    {
1800
        // If an Attribute is starting/ending at nStartPos, or there is a tab
1801
        // before nStartPos => a new Portion starts.
1802
        // Otherwise the Portion is extended at nStartPos.
1803
        // Or if at the very beginning ( StartPos 0 ) followed by a tab...
1804
0
        if ( ( pNode->GetCharAttribs().HasBoundingAttrib( nStartPos ) ) ||
1805
0
             ( nStartPos && ( pNode->GetText()[ nStartPos - 1 ] == '\t' ) ) ||
1806
0
             ( !nStartPos && ( nNewChars < pNode->GetText().getLength() ) && pNode->GetText()[ nNewChars ] == '\t' ) )
1807
0
        {
1808
0
            std::size_t nNewPortionPos = 0;
1809
0
            if ( nStartPos )
1810
0
                nNewPortionPos = SplitTextPortion( nPara, nStartPos ) + 1;
1811
1812
            // Here could be an empty Portion if the paragraph was empty,
1813
            // or a new line was created by a hard line-break.
1814
0
            if ( ( nNewPortionPos < pTEParaPortion->GetTextPortions().size() ) &&
1815
0
                    !pTEParaPortion->GetTextPortions()[nNewPortionPos].GetLen() )
1816
0
            {
1817
                // use the empty Portion
1818
0
                pTEParaPortion->GetTextPortions()[nNewPortionPos].GetLen() = nNewChars;
1819
0
            }
1820
0
            else
1821
0
            {
1822
0
                TETextPortion aNewPortion( nNewChars );
1823
0
                pTEParaPortion->GetTextPortions().insert( pTEParaPortion->GetTextPortions().begin() + nNewPortionPos, aNewPortion );
1824
0
            }
1825
0
        }
1826
0
        else
1827
0
        {
1828
0
            sal_Int32 nPortionStart {0};
1829
0
            const std::size_t nTP = pTEParaPortion->GetTextPortions().FindPortion( nStartPos, nPortionStart );
1830
0
            TETextPortion& rTP = pTEParaPortion->GetTextPortions()[ nTP ];
1831
0
            rTP.GetLen() += nNewChars;
1832
0
            rTP.GetWidth() = -1;
1833
0
        }
1834
0
    }
1835
0
    else
1836
0
    {
1837
        // Shrink or remove Portion
1838
        // Before calling this function, ensure that no Portions were in the deleted range!
1839
1840
        // There must be no Portion reaching into or starting within,
1841
        // thus: nStartPos <= nPos <= nStartPos - nNewChars(neg.)
1842
0
        std::size_t nPortion = 0;
1843
0
        sal_Int32 nPos = 0;
1844
0
        const sal_Int32 nEnd = nStartPos-nNewChars;
1845
0
        const std::size_t nPortions = pTEParaPortion->GetTextPortions().size();
1846
0
        TETextPortion* pTP = nullptr;
1847
0
        for ( nPortion = 0; nPortion < nPortions; nPortion++ )
1848
0
        {
1849
0
            pTP = &pTEParaPortion->GetTextPortions()[ nPortion ];
1850
0
            if ( ( nPos+pTP->GetLen() ) > nStartPos )
1851
0
            {
1852
0
                SAL_WARN_IF( nPos > nStartPos, "vcl", "RecalcTextPortion: Bad Start!" );
1853
0
                SAL_WARN_IF( nPos+pTP->GetLen() < nEnd, "vcl", "RecalcTextPortion: Bad End!" );
1854
0
                break;
1855
0
            }
1856
0
            nPos += pTP->GetLen();
1857
0
        }
1858
0
        SAL_WARN_IF( !pTP, "vcl", "RecalcTextPortion: Portion not found!" );
1859
0
        if ( ( nPos == nStartPos ) && ( (nPos+pTP->GetLen()) == nEnd ) )
1860
0
        {
1861
            // remove Portion
1862
0
            pTEParaPortion->GetTextPortions().erase( pTEParaPortion->GetTextPortions().begin() + nPortion );
1863
0
        }
1864
0
        else
1865
0
        {
1866
0
            SAL_WARN_IF( pTP->GetLen() <= (-nNewChars), "vcl", "RecalcTextPortion: Portion too small to shrink!" );
1867
0
            pTP->GetLen() += nNewChars;
1868
0
        }
1869
0
        OSL_ENSURE( pTEParaPortion->GetTextPortions().size(),
1870
0
                "RecalcTextPortion: none are left!" );
1871
0
    }
1872
0
}
1873
1874
void TextEngine::ImpPaint( OutputDevice* pOutDev, const Point& rStartPos, tools::Rectangle const* pPaintArea, TextSelection const* pSelection )
1875
0
{
1876
0
    if ( !GetUpdateMode() )
1877
0
        return;
1878
1879
0
    if ( !IsFormatted() )
1880
0
        FormatDoc();
1881
1882
0
    vcl::Window* const pOutWin = pOutDev->GetOwnerWindow();
1883
0
    const bool bTransparent = (pOutWin && pOutWin->IsPaintTransparent());
1884
1885
0
    tools::Long nY = rStartPos.Y();
1886
1887
0
    TextPaM const* pSelStart = nullptr;
1888
0
    TextPaM const* pSelEnd = nullptr;
1889
0
    if ( pSelection && pSelection->HasRange() )
1890
0
    {
1891
0
        const bool bInvers = pSelection->GetEnd() < pSelection->GetStart();
1892
0
        pSelStart = !bInvers ? &pSelection->GetStart() : &pSelection->GetEnd();
1893
0
        pSelEnd = bInvers ? &pSelection->GetStart() : &pSelection->GetEnd();
1894
0
    }
1895
1896
0
    const StyleSettings& rStyleSettings = pOutDev->GetSettings().GetStyleSettings();
1897
1898
    // for all paragraphs
1899
0
    for ( sal_uInt32 nPara = 0; nPara < mpTEParaPortions->Count(); ++nPara )
1900
0
    {
1901
0
        TEParaPortion* pPortion = mpTEParaPortions->GetObject( nPara );
1902
        // in case while typing Idle-Formatting, asynchronous Paint
1903
0
        if ( pPortion->IsInvalid() )
1904
0
            return;
1905
1906
0
        const tools::Long nParaHeight = CalcParaHeight( nPara );
1907
0
        if ( !pPaintArea || ( ( nY + nParaHeight ) > pPaintArea->Top() ) )
1908
0
        {
1909
            // for all lines of the paragraph
1910
0
            sal_Int32 nIndex = 0;
1911
0
            for ( auto & rLine : pPortion->GetLines() )
1912
0
            {
1913
0
                Point aTmpPos( rStartPos.X() + rLine.GetStartX(), nY );
1914
1915
0
                if ( !pPaintArea || ( ( nY + mnCharHeight ) > pPaintArea->Top() ) )
1916
0
                {
1917
                    // for all Portions of the line
1918
0
                    nIndex = rLine.GetStart();
1919
0
                    for ( std::size_t y = rLine.GetStartPortion(); y <= rLine.GetEndPortion(); y++ )
1920
0
                    {
1921
0
                        OSL_ENSURE(pPortion->GetTextPortions().size(),
1922
0
                                "ImpPaint: Line without Textportion!");
1923
0
                        TETextPortion& rTextPortion = pPortion->GetTextPortions()[ y ];
1924
1925
0
                        ImpInitLayoutMode( pOutDev /*, pTextPortion->IsRightToLeft() */);
1926
1927
0
                        const tools::Long nTxtWidth = rTextPortion.GetWidth();
1928
0
                        aTmpPos.setX( rStartPos.X() + ImpGetOutputOffset( nPara, &rLine, nIndex, nIndex ) );
1929
1930
                        // only print if starting in the visible region
1931
0
                        if ( ( aTmpPos.X() + nTxtWidth ) >= 0 )
1932
0
                        {
1933
0
                            switch ( rTextPortion.GetKind() )
1934
0
                            {
1935
0
                                case PORTIONKIND_TEXT:
1936
0
                                    {
1937
0
                                        vcl::Font aFont;
1938
0
                                        SeekCursor( nPara, nIndex+1, aFont, pOutDev );
1939
0
                                        if( bTransparent )
1940
0
                                            aFont.SetTransparent( true );
1941
0
                                        else if ( pSelection )
1942
0
                                            aFont.SetTransparent( false );
1943
0
                                        pOutDev->SetFont( aFont );
1944
1945
0
                                        sal_Int32 nTmpIndex = nIndex;
1946
0
                                        sal_Int32 nEnd = nTmpIndex + rTextPortion.GetLen();
1947
0
                                        Point aPos = aTmpPos;
1948
1949
0
                                        bool bDone = false;
1950
0
                                        if ( pSelStart )
1951
0
                                        {
1952
                                            // is a part of it in the selection?
1953
0
                                            const TextPaM aTextStart( nPara, nTmpIndex );
1954
0
                                            const TextPaM aTextEnd( nPara, nEnd );
1955
0
                                            if ( ( aTextStart < *pSelEnd ) && ( aTextEnd > *pSelStart ) )
1956
0
                                            {
1957
                                                // 1) vcl::Region before Selection
1958
0
                                                if ( aTextStart < *pSelStart )
1959
0
                                                {
1960
0
                                                    const sal_Int32 nL = pSelStart->GetIndex() - nTmpIndex;
1961
0
                                                    pOutDev->SetFont( aFont);
1962
0
                                                    pOutDev->SetTextFillColor();
1963
0
                                                    aPos.setX( rStartPos.X() + ImpGetOutputOffset( nPara, &rLine, nTmpIndex, nTmpIndex+nL ) );
1964
0
                                                    pOutDev->DrawText( aPos, pPortion->GetNode()->GetText(), nTmpIndex, nL );
1965
0
                                                    nTmpIndex = nTmpIndex + nL;
1966
1967
0
                                                }
1968
                                                // 2) vcl::Region with Selection
1969
0
                                                sal_Int32 nL = nEnd - nTmpIndex;
1970
0
                                                if ( aTextEnd > *pSelEnd )
1971
0
                                                    nL = pSelEnd->GetIndex() - nTmpIndex;
1972
0
                                                if ( nL )
1973
0
                                                {
1974
0
                                                    const Color aOldTextColor = pOutDev->GetTextColor();
1975
0
                                                    pOutDev->SetTextColor( rStyleSettings.GetHighlightTextColor() );
1976
0
                                                    pOutDev->SetTextFillColor( rStyleSettings.GetHighlightColor() );
1977
0
                                                    aPos.setX( rStartPos.X() + ImpGetOutputOffset( nPara, &rLine, nTmpIndex, nTmpIndex+nL ) );
1978
0
                                                    pOutDev->DrawText( aPos, pPortion->GetNode()->GetText(), nTmpIndex, nL );
1979
0
                                                    pOutDev->SetTextColor( aOldTextColor );
1980
0
                                                    pOutDev->SetTextFillColor();
1981
0
                                                    nTmpIndex = nTmpIndex + nL;
1982
0
                                                }
1983
1984
                                                // 3) vcl::Region after Selection
1985
0
                                                if ( nTmpIndex < nEnd )
1986
0
                                                {
1987
0
                                                    nL = nEnd-nTmpIndex;
1988
0
                                                    pOutDev->SetTextFillColor();
1989
0
                                                    aPos.setX( rStartPos.X() + ImpGetOutputOffset( nPara, &rLine, nTmpIndex, nTmpIndex+nL ) );
1990
0
                                                    pOutDev->DrawText( aPos, pPortion->GetNode()->GetText(), nTmpIndex, nEnd-nTmpIndex );
1991
0
                                                }
1992
0
                                                bDone = true;
1993
0
                                            }
1994
0
                                        }
1995
0
                                        if ( !bDone )
1996
0
                                        {
1997
0
                                            pOutDev->SetTextFillColor();
1998
0
                                            aPos.setX( rStartPos.X() + ImpGetOutputOffset( nPara, &rLine, nTmpIndex, nEnd ) );
1999
0
                                            pOutDev->DrawText( aPos, pPortion->GetNode()->GetText(), nTmpIndex, nEnd-nTmpIndex );
2000
0
                                        }
2001
0
                                    }
2002
0
                                    break;
2003
0
                                case PORTIONKIND_TAB:
2004
                                    // for HideSelection() only Range, pSelection = 0.
2005
0
                                    if ( pSelStart ) // also implies pSelEnd
2006
0
                                    {
2007
0
                                        const tools::Rectangle aTabArea( aTmpPos, Point( aTmpPos.X()+nTxtWidth, aTmpPos.Y()+mnCharHeight-1 ) );
2008
                                        // is the Tab in the Selection???
2009
0
                                        const TextPaM aTextStart(nPara, nIndex);
2010
0
                                        const TextPaM aTextEnd(nPara, nIndex + 1);
2011
0
                                        if ((aTextStart < *pSelEnd) && (aTextEnd > *pSelStart))
2012
0
                                        {
2013
0
                                            auto popIt = pOutDev->ScopedPush(vcl::PushFlags::FILLCOLOR);
2014
0
                                            pOutDev->SetFillColor(
2015
0
                                                rStyleSettings.GetHighlightColor());
2016
0
                                            pOutDev->DrawRect(aTabArea);
2017
0
                                        }
2018
0
                                        else
2019
0
                                        {
2020
0
                                            pOutDev->Erase( aTabArea );
2021
0
                                        }
2022
0
                                    }
2023
0
                                    break;
2024
0
                                default:
2025
0
                                    OSL_FAIL( "ImpPaint: Unknown Portion-Type !" );
2026
0
                            }
2027
0
                        }
2028
2029
0
                        nIndex += rTextPortion.GetLen();
2030
0
                    }
2031
0
                }
2032
2033
0
                nY += mnCharHeight;
2034
2035
0
                if ( pPaintArea && ( nY >= pPaintArea->Bottom() ) )
2036
0
                    break;  // no more visible actions
2037
0
            }
2038
0
        }
2039
0
        else
2040
0
        {
2041
0
            nY += nParaHeight;
2042
0
        }
2043
2044
0
        if ( pPaintArea && ( nY > pPaintArea->Bottom() ) )
2045
0
            break;  // no more visible actions
2046
0
    }
2047
0
}
2048
2049
bool TextEngine::CreateLines( sal_uInt32 nPara )
2050
0
{
2051
    // bool: changing Height of Paragraph Yes/No - true/false
2052
2053
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
2054
0
    TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
2055
0
    SAL_WARN_IF( !pTEParaPortion->IsInvalid(), "vcl", "CreateLines: Portion not invalid!" );
2056
2057
0
    const auto nOldLineCount = pTEParaPortion->GetLines().size();
2058
2059
    // fast special case for empty paragraphs
2060
0
    if ( pTEParaPortion->GetNode()->GetText().isEmpty() )
2061
0
    {
2062
0
        if ( !pTEParaPortion->GetTextPortions().empty() )
2063
0
            pTEParaPortion->GetTextPortions().Reset();
2064
0
        pTEParaPortion->GetLines().clear();
2065
0
        CreateAndInsertEmptyLine( nPara );
2066
0
        pTEParaPortion->SetValid();
2067
0
        return nOldLineCount != pTEParaPortion->GetLines().size();
2068
0
    }
2069
2070
    // initialization
2071
0
    if ( pTEParaPortion->GetLines().empty() )
2072
0
    {
2073
0
        pTEParaPortion->GetLines().emplace_back( );
2074
0
    }
2075
2076
0
    const sal_Int32 nInvalidDiff = pTEParaPortion->GetInvalidDiff();
2077
0
    const sal_Int32 nInvalidStart = pTEParaPortion->GetInvalidPosStart();
2078
0
    const sal_Int32 nInvalidEnd = nInvalidStart + std::abs( nInvalidDiff );
2079
0
    bool bQuickFormat = false;
2080
2081
0
    if ( pTEParaPortion->GetWritingDirectionInfos().empty() )
2082
0
        ImpInitWritingDirections( nPara );
2083
2084
0
    if ( pTEParaPortion->GetWritingDirectionInfos().size() == 1 && pTEParaPortion->IsSimpleInvalid() )
2085
0
    {
2086
0
        bQuickFormat = nInvalidDiff != 0;
2087
0
        if ( nInvalidDiff < 0 )
2088
0
        {
2089
            // check if deleting across Portion border
2090
0
            sal_Int32 nPos = 0;
2091
0
            for ( const auto & rTP : pTEParaPortion->GetTextPortions() )
2092
0
            {
2093
                // there must be no Start/End in the deleted region
2094
0
                nPos += rTP.GetLen();
2095
0
                if ( nPos > nInvalidStart && nPos < nInvalidEnd )
2096
0
                {
2097
0
                    bQuickFormat = false;
2098
0
                    break;
2099
0
                }
2100
0
            }
2101
0
        }
2102
0
    }
2103
2104
0
    if ( bQuickFormat )
2105
0
        RecalcTextPortion( nPara, nInvalidStart, nInvalidDiff );
2106
0
    else
2107
0
        CreateTextPortions( nPara, nInvalidStart );
2108
2109
    // search for line with InvalidPos; start a line prior
2110
    // flag lines => do not remove!
2111
2112
0
    sal_uInt16 nLine = pTEParaPortion->GetLines().size()-1;
2113
0
    for ( sal_uInt16 nL = 0; nL <= nLine; nL++ )
2114
0
    {
2115
0
        TextLine& rLine = pTEParaPortion->GetLines()[ nL ];
2116
0
        if ( rLine.GetEnd() > nInvalidStart )
2117
0
        {
2118
0
            nLine = nL;
2119
0
            break;
2120
0
        }
2121
0
        rLine.SetValid();
2122
0
    }
2123
    // start a line before...
2124
    // if typing at the end, the line before cannot change
2125
0
    if ( nLine && ( !pTEParaPortion->IsSimpleInvalid() || ( nInvalidEnd < pNode->GetText().getLength() ) || ( nInvalidDiff <= 0 ) ) )
2126
0
        nLine--;
2127
2128
0
    TextLine* pLine =  &( pTEParaPortion->GetLines()[ nLine ] );
2129
2130
    // format all lines starting here
2131
0
    std::size_t nDelFromLine = TETextPortionList::npos;
2132
2133
0
    sal_Int32 nIndex = pLine->GetStart();
2134
0
    TextLine aSaveLine( *pLine );
2135
2136
0
    while ( nIndex < pNode->GetText().getLength() )
2137
0
    {
2138
0
        bool bEOL = false;
2139
0
        sal_Int32 nPortionStart = 0;
2140
0
        sal_Int32 nPortionEnd = 0;
2141
2142
0
        sal_Int32 nTmpPos = nIndex;
2143
0
        std::size_t nTmpPortion = pLine->GetStartPortion();
2144
0
        tools::Long nTmpWidth = mpDoc->GetLeftMargin();
2145
        // do not subtract margin; it is included in TmpWidth
2146
0
        tools::Long nXWidth = std::max(
2147
0
            mnMaxTextWidth ? mnMaxTextWidth : std::numeric_limits<tools::Long>::max(), nTmpWidth);
2148
2149
        // search for Portion that does not fit anymore into line
2150
0
        TETextPortion* pPortion = nullptr;
2151
0
        bool bBrokenLine = false;
2152
2153
0
        while ( ( nTmpWidth <= nXWidth ) && !bEOL && ( nTmpPortion < pTEParaPortion->GetTextPortions().size() ) )
2154
0
        {
2155
0
            nPortionStart = nTmpPos;
2156
0
            pPortion = &pTEParaPortion->GetTextPortions()[ nTmpPortion ];
2157
0
            SAL_WARN_IF( !pPortion->GetLen(), "vcl", "CreateLines: Empty Portion!" );
2158
0
            if ( pNode->GetText()[ nTmpPos ] == '\t' )
2159
0
            {
2160
0
                tools::Long nCurPos = nTmpWidth-mpDoc->GetLeftMargin();
2161
0
                nTmpWidth = ((nCurPos/mnDefTab)+1)*mnDefTab+mpDoc->GetLeftMargin();
2162
0
                pPortion->GetWidth() = nTmpWidth - nCurPos - mpDoc->GetLeftMargin();
2163
                // infinite loop, if this is the first token of the line and nTmpWidth > aPaperSize.Width !!!
2164
0
                if ( ( nTmpWidth >= nXWidth ) && ( nTmpPortion == pLine->GetStartPortion() ) )
2165
0
                {
2166
                    // adjust Tab
2167
0
                    pPortion->GetWidth() = nXWidth-1;
2168
0
                    nTmpWidth = pPortion->GetWidth();
2169
0
                    bEOL = true;
2170
0
                    bBrokenLine = true;
2171
0
                }
2172
0
                pPortion->GetKind() = PORTIONKIND_TAB;
2173
0
            }
2174
0
            else
2175
0
            {
2176
2177
0
                pPortion->GetWidth() = CalcTextWidth( nPara, nTmpPos, pPortion->GetLen() );
2178
0
                nTmpWidth += pPortion->GetWidth();
2179
2180
0
                pPortion->SetRightToLeft( ImpGetRightToLeft( nPara, nTmpPos+1 ) );
2181
0
                pPortion->GetKind() = PORTIONKIND_TEXT;
2182
0
            }
2183
2184
0
            nTmpPos += pPortion->GetLen();
2185
0
            nPortionEnd = nTmpPos;
2186
0
            nTmpPortion++;
2187
0
        }
2188
2189
        // this was perhaps one Portion too far
2190
0
        bool bFixedEnd = false;
2191
0
        if ( nTmpWidth > nXWidth )
2192
0
        {
2193
0
            assert(pPortion);
2194
2195
0
            nPortionEnd = nTmpPos;
2196
0
            nTmpPos -= pPortion->GetLen();
2197
0
            nPortionStart = nTmpPos;
2198
0
            nTmpPortion--;
2199
0
            bEOL = false;
2200
2201
0
            nTmpWidth -= pPortion->GetWidth();
2202
0
            if ( pPortion->GetKind() == PORTIONKIND_TAB )
2203
0
            {
2204
0
                bEOL = true;
2205
0
                bFixedEnd = true;
2206
0
            }
2207
0
        }
2208
0
        else
2209
0
        {
2210
0
            bEOL = true;
2211
0
            pLine->SetEnd( nPortionEnd );
2212
0
            OSL_ENSURE(pTEParaPortion->GetTextPortions().size(),
2213
0
                    "CreateLines: No TextPortions?");
2214
0
            pLine->SetEndPortion( pTEParaPortion->GetTextPortions().size() - 1 );
2215
0
        }
2216
2217
0
        if ( bFixedEnd )
2218
0
        {
2219
0
            pLine->SetEnd( nPortionStart );
2220
0
            pLine->SetEndPortion( nTmpPortion-1 );
2221
0
        }
2222
0
        else if ( bBrokenLine )
2223
0
        {
2224
0
            pLine->SetEnd( nPortionStart+1 );
2225
0
            pLine->SetEndPortion( nTmpPortion-1 );
2226
0
        }
2227
0
        else if ( !bEOL )
2228
0
        {
2229
0
            SAL_WARN_IF( (nPortionEnd-nPortionStart) != pPortion->GetLen(), "vcl", "CreateLines: There is a Portion after all?!" );
2230
0
            const tools::Long nRemainingWidth = mnMaxTextWidth - nTmpWidth;
2231
0
            ImpBreakLine( nPara, pLine, nPortionStart, nRemainingWidth );
2232
0
        }
2233
2234
0
        if ( ( ImpGetAlign() == TxtAlign::Center ) || ( ImpGetAlign() == TxtAlign::Right ) )
2235
0
        {
2236
            // adjust
2237
0
            tools::Long nTextWidth = 0;
2238
0
            for ( std::size_t nTP = pLine->GetStartPortion(); nTP <= pLine->GetEndPortion(); nTP++ )
2239
0
            {
2240
0
                TETextPortion& rTextPortion = pTEParaPortion->GetTextPortions()[ nTP ];
2241
0
                nTextWidth += rTextPortion.GetWidth();
2242
0
            }
2243
0
            const tools::Long nSpace = mnMaxTextWidth - nTextWidth;
2244
0
            if ( nSpace > 0 )
2245
0
            {
2246
0
                if ( ImpGetAlign() == TxtAlign::Center )
2247
0
                    pLine->SetStartX( static_cast<sal_uInt16>(nSpace / 2) );
2248
0
                else    // TxtAlign::Right
2249
0
                    pLine->SetStartX( static_cast<sal_uInt16>(nSpace) );
2250
0
            }
2251
0
        }
2252
0
        else
2253
0
        {
2254
0
            pLine->SetStartX( mpDoc->GetLeftMargin() );
2255
0
        }
2256
2257
         // check if the line has to be printed again
2258
0
        pLine->SetInvalid();
2259
2260
0
        if ( pTEParaPortion->IsSimpleInvalid() )
2261
0
        {
2262
            // Change due to simple TextChange...
2263
            // Do not abort formatting, as Portions might have to be split!
2264
            // Once it is ok to abort, then validate the following lines!
2265
            // But mark as valid, thus reduce printing...
2266
0
            if ( pLine->GetEnd() < nInvalidStart )
2267
0
            {
2268
0
                if ( *pLine == aSaveLine )
2269
0
                {
2270
0
                    pLine->SetValid();
2271
0
                }
2272
0
            }
2273
0
            else
2274
0
            {
2275
0
                const sal_Int32 nStart = pLine->GetStart();
2276
0
                const sal_Int32 nEnd = pLine->GetEnd();
2277
2278
0
                if ( nStart > nInvalidEnd )
2279
0
                {
2280
0
                    if ( ( ( nStart-nInvalidDiff ) == aSaveLine.GetStart() ) &&
2281
0
                            ( ( nEnd-nInvalidDiff ) == aSaveLine.GetEnd() ) )
2282
0
                    {
2283
0
                        pLine->SetValid();
2284
0
                        if ( bQuickFormat )
2285
0
                        {
2286
0
                            pTEParaPortion->CorrectValuesBehindLastFormattedLine( nLine );
2287
0
                            break;
2288
0
                        }
2289
0
                    }
2290
0
                }
2291
0
                else if ( bQuickFormat && ( nEnd > nInvalidEnd) )
2292
0
                {
2293
                    // If the invalid line ends such that the next line starts
2294
                    // at the 'same' position as before (no change in line breaks),
2295
                    // the text width does not have to be recalculated.
2296
0
                    if ( nEnd == ( aSaveLine.GetEnd() + nInvalidDiff ) )
2297
0
                    {
2298
0
                        pTEParaPortion->CorrectValuesBehindLastFormattedLine( nLine );
2299
0
                        break;
2300
0
                    }
2301
0
                }
2302
0
            }
2303
0
        }
2304
2305
0
        nIndex = pLine->GetEnd();   // next line Start = previous line End
2306
                                    // because nEnd is past the last char!
2307
2308
0
        const std::size_t nEndPortion = pLine->GetEndPortion();
2309
2310
        // next line or new line
2311
0
        pLine = nullptr;
2312
0
        if ( nLine < pTEParaPortion->GetLines().size()-1 )
2313
0
            pLine = &( pTEParaPortion->GetLines()[ ++nLine ] );
2314
0
        if ( pLine && ( nIndex >= pNode->GetText().getLength() ) )
2315
0
        {
2316
0
            nDelFromLine = nLine;
2317
0
            break;
2318
0
        }
2319
0
        if ( !pLine )
2320
0
        {
2321
0
            if ( nIndex < pNode->GetText().getLength() )
2322
0
            {
2323
0
                ++nLine;
2324
0
                pTEParaPortion->GetLines().insert( pTEParaPortion->GetLines().begin() + nLine, TextLine() );
2325
0
                pLine = &pTEParaPortion->GetLines()[nLine];
2326
0
            }
2327
0
            else
2328
0
            {
2329
0
                break;
2330
0
            }
2331
0
        }
2332
0
        aSaveLine = *pLine;
2333
0
        pLine->SetStart( nIndex );
2334
0
        pLine->SetEnd( nIndex );
2335
0
        pLine->SetStartPortion( nEndPortion+1 );
2336
0
        pLine->SetEndPortion( nEndPortion+1 );
2337
2338
0
    }   // while ( Index < Len )
2339
2340
0
    if (nDelFromLine != TETextPortionList::npos)
2341
0
    {
2342
0
        pTEParaPortion->GetLines().erase( pTEParaPortion->GetLines().begin() + nDelFromLine,
2343
0
                                          pTEParaPortion->GetLines().end() );
2344
0
    }
2345
2346
0
    SAL_WARN_IF( pTEParaPortion->GetLines().empty(), "vcl", "CreateLines: No Line!" );
2347
2348
0
    pTEParaPortion->SetValid();
2349
2350
0
    return nOldLineCount != pTEParaPortion->GetLines().size();
2351
0
}
2352
2353
OUString TextEngine::GetWord( const TextPaM& rCursorPos, TextPaM* pStartOfWord, TextPaM* pEndOfWord )
2354
0
{
2355
0
    OUString aWord;
2356
0
    if ( rCursorPos.GetPara() < mpDoc->GetNodes().size() )
2357
0
    {
2358
0
        TextSelection aSel( rCursorPos );
2359
0
        TextNode* pNode = mpDoc->GetNodes()[ rCursorPos.GetPara() ].get();
2360
0
        uno::Reference < i18n::XBreakIterator > xBI = GetBreakIterator();
2361
0
        i18n::Boundary aBoundary = xBI->getWordBoundary( pNode->GetText(), rCursorPos.GetIndex(), GetLocale(), i18n::WordType::ANYWORD_IGNOREWHITESPACES, true );
2362
        // tdf#57879 - expand selection to the left to include connector punctuation and search for additional word boundaries
2363
0
        if (aBoundary.startPos > 0 && aBoundary.startPos < pNode->GetText().getLength() && u_charType(pNode->GetText()[aBoundary.startPos]) == U_CONNECTOR_PUNCTUATION)
2364
0
        {
2365
0
            aBoundary.startPos = xBI->getWordBoundary(pNode->GetText(), aBoundary.startPos - 1,
2366
0
                GetLocale(), css::i18n::WordType::ANYWORD_IGNOREWHITESPACES, true).startPos;
2367
0
        }
2368
0
        while (aBoundary.startPos > 0 && u_charType(pNode->GetText()[aBoundary.startPos - 1]) == U_CONNECTOR_PUNCTUATION)
2369
0
        {
2370
0
            aBoundary.startPos = std::min(aBoundary.startPos,
2371
0
                xBI->getWordBoundary( pNode->GetText(), aBoundary.startPos - 2,
2372
0
                    GetLocale(), css::i18n::WordType::ANYWORD_IGNOREWHITESPACES, true).startPos);
2373
0
        }
2374
        // tdf#57879 - expand selection to the right to include connector punctuation and search for additional word boundaries
2375
0
        if (aBoundary.endPos > 0 && aBoundary.endPos < pNode->GetText().getLength() && u_charType(pNode->GetText()[aBoundary.endPos - 1]) == U_CONNECTOR_PUNCTUATION)
2376
0
        {
2377
0
            aBoundary.endPos = xBI->getWordBoundary(pNode->GetText(), aBoundary.endPos,
2378
0
                GetLocale(), css::i18n::WordType::ANYWORD_IGNOREWHITESPACES, true).endPos;
2379
0
        }
2380
0
        while (aBoundary.endPos < pNode->GetText().getLength() && u_charType(pNode->GetText()[aBoundary.endPos]) == U_CONNECTOR_PUNCTUATION)
2381
0
        {
2382
0
            aBoundary.endPos = xBI->getWordBoundary(pNode->GetText(), aBoundary.endPos + 1,
2383
0
                GetLocale(), css::i18n::WordType::ANYWORD_IGNOREWHITESPACES, true).endPos;
2384
0
        }
2385
0
        aSel.GetStart().GetIndex() = aBoundary.startPos;
2386
0
        aSel.GetEnd().GetIndex() = aBoundary.endPos;
2387
0
        aWord = pNode->GetText().copy( aSel.GetStart().GetIndex(), aSel.GetEnd().GetIndex() - aSel.GetStart().GetIndex() );
2388
0
        if ( pStartOfWord )
2389
0
            *pStartOfWord = aSel.GetStart();
2390
0
        if (pEndOfWord)
2391
0
            *pEndOfWord = aSel.GetEnd();
2392
0
    }
2393
0
    return aWord;
2394
0
}
2395
2396
bool TextEngine::Read( SvStream& rInput, const TextSelection* pSel )
2397
0
{
2398
0
    const bool bUpdate = GetUpdateMode();
2399
0
    SetUpdateMode( false );
2400
2401
0
    UndoActionStart();
2402
0
    TextSelection aSel;
2403
0
    if ( pSel )
2404
0
        aSel = *pSel;
2405
0
    else
2406
0
    {
2407
0
        const sal_uInt32 nParas = static_cast<sal_uInt32>(mpDoc->GetNodes().size());
2408
0
        TextNode* pNode = mpDoc->GetNodes()[ nParas - 1 ].get();
2409
0
        aSel = TextPaM( nParas-1 , pNode->GetText().getLength() );
2410
0
    }
2411
2412
0
    if ( aSel.HasRange() )
2413
0
        aSel = ImpDeleteText( aSel );
2414
2415
0
    OStringBuffer aLine;
2416
0
    bool bDone = rInput.ReadLine( aLine );
2417
0
    OUString aTmpStr(OStringToOUString(aLine, rInput.GetStreamCharSet()));
2418
0
    while ( bDone )
2419
0
    {
2420
0
        aSel = ImpInsertText( aSel, aTmpStr );
2421
0
        bDone = rInput.ReadLine( aLine );
2422
0
        aTmpStr = OStringToOUString(aLine, rInput.GetStreamCharSet());
2423
0
        if ( bDone )
2424
0
            aSel = ImpInsertParaBreak( aSel.GetEnd() );
2425
0
    }
2426
2427
0
    UndoActionEnd();
2428
2429
0
    const TextSelection aNewSel( aSel.GetEnd(), aSel.GetEnd() );
2430
2431
    // so that FormatAndUpdate does not access the invalid selection
2432
0
    if ( GetActiveView() )
2433
0
        GetActiveView()->ImpSetSelection( aNewSel );
2434
2435
0
    SetUpdateMode( bUpdate );
2436
0
    FormatAndUpdate( GetActiveView() );
2437
2438
0
    return rInput.GetError() == ERRCODE_NONE;
2439
0
}
2440
2441
void TextEngine::Write( SvStream& rOutput )
2442
0
{
2443
0
    TextSelection aSel;
2444
0
    const sal_uInt32 nParas = static_cast<sal_uInt32>(mpDoc->GetNodes().size());
2445
0
    TextNode* pSelNode = mpDoc->GetNodes()[ nParas - 1 ].get();
2446
0
    aSel.GetStart() = TextPaM( 0, 0 );
2447
0
    aSel.GetEnd() = TextPaM( nParas-1, pSelNode->GetText().getLength() );
2448
2449
0
    for ( sal_uInt32 nPara = aSel.GetStart().GetPara(); nPara <= aSel.GetEnd().GetPara(); ++nPara  )
2450
0
    {
2451
0
        TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
2452
2453
0
        const sal_Int32 nStartPos = nPara == aSel.GetStart().GetPara()
2454
0
            ? aSel.GetStart().GetIndex() : 0;
2455
0
        const sal_Int32 nEndPos = nPara == aSel.GetEnd().GetPara()
2456
0
            ? aSel.GetEnd().GetIndex() : pNode->GetText().getLength();
2457
2458
0
        const OUString aText = pNode->GetText().copy( nStartPos, nEndPos-nStartPos );
2459
0
        rOutput.WriteLine(OUStringToOString(aText, rOutput.GetStreamCharSet()));
2460
0
    }
2461
0
}
2462
2463
void TextEngine::RemoveAttribs( sal_uInt32 nPara )
2464
0
{
2465
0
    if ( nPara >= mpDoc->GetNodes().size() )
2466
0
        return;
2467
2468
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
2469
0
    if ( pNode->GetCharAttribs().Count() )
2470
0
    {
2471
0
        pNode->GetCharAttribs().Clear();
2472
2473
0
        TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
2474
0
        pTEParaPortion->MarkSelectionInvalid( 0 );
2475
2476
0
        mbFormatted = false;
2477
2478
0
        IdleFormatAndUpdate( nullptr, 0xFFFF );
2479
0
    }
2480
0
}
2481
2482
void TextEngine::SetAttrib( const TextAttrib& rAttr, sal_uInt32 nPara, sal_Int32 nStart, sal_Int32 nEnd )
2483
0
{
2484
2485
    // For now do not check if Attributes overlap!
2486
    // This function is for TextEditors that want to _quickly_ generate the Syntax-Highlight
2487
2488
    // As TextEngine is currently intended only for TextEditors, there is no Undo for Attributes!
2489
2490
0
    if ( nPara >= mpDoc->GetNodes().size() )
2491
0
        return;
2492
2493
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
2494
0
    TEParaPortion* pTEParaPortion = mpTEParaPortions->GetObject( nPara );
2495
2496
0
    const sal_Int32 nMax = pNode->GetText().getLength();
2497
0
    if ( nStart > nMax )
2498
0
        nStart = nMax;
2499
0
    if ( nEnd > nMax )
2500
0
        nEnd = nMax;
2501
2502
0
    pNode->GetCharAttribs().InsertAttrib( std::make_unique<TextCharAttrib>( rAttr, nStart, nEnd ) );
2503
0
    pTEParaPortion->MarkSelectionInvalid( nStart );
2504
2505
0
    mbFormatted = false;
2506
0
    IdleFormatAndUpdate( nullptr, 0xFFFF );
2507
0
}
2508
2509
void TextEngine::SetTextAlign( TxtAlign eAlign )
2510
0
{
2511
0
    if ( eAlign != meAlign )
2512
0
    {
2513
0
        meAlign = eAlign;
2514
0
        FormatFullDoc();
2515
0
        UpdateViews();
2516
0
    }
2517
0
}
2518
2519
void TextEngine::ValidateSelection( TextSelection& rSel ) const
2520
0
{
2521
0
    ValidatePaM( rSel.GetStart() );
2522
0
    ValidatePaM( rSel.GetEnd() );
2523
0
}
2524
2525
void TextEngine::ValidatePaM( TextPaM& rPaM ) const
2526
0
{
2527
0
    const sal_uInt32 nParas = static_cast<sal_uInt32>(mpDoc->GetNodes().size());
2528
0
    if ( rPaM.GetPara() >= nParas )
2529
0
    {
2530
0
        rPaM.GetPara() = nParas ? nParas-1 : 0;
2531
0
        rPaM.GetIndex() = TEXT_INDEX_ALL;
2532
0
    }
2533
2534
0
    const sal_Int32 nMaxIndex = GetTextLen( rPaM.GetPara() );
2535
0
    if ( rPaM.GetIndex() > nMaxIndex )
2536
0
        rPaM.GetIndex() = nMaxIndex;
2537
0
}
2538
2539
// adjust State & Selection
2540
2541
void TextEngine::ImpParagraphInserted( sal_uInt32 nPara )
2542
0
{
2543
    // No adjustment needed for the active View;
2544
    // but for all passive Views the Selection needs adjusting.
2545
0
    if ( mpViews->size() > 1 )
2546
0
    {
2547
0
        for ( auto nView = mpViews->size(); nView; )
2548
0
        {
2549
0
            TextView* pView = (*mpViews)[ --nView ];
2550
0
            if ( pView != GetActiveView() )
2551
0
            {
2552
0
                for ( int n = 0; n <= 1; n++ )
2553
0
                {
2554
0
                    TextPaM& rPaM = n ? pView->GetSelection().GetStart(): pView->GetSelection().GetEnd();
2555
0
                    if ( rPaM.GetPara() >= nPara )
2556
0
                        rPaM.GetPara()++;
2557
0
                }
2558
0
            }
2559
0
        }
2560
0
    }
2561
0
    Broadcast( TextHint( SfxHintId::TextParaInserted, nPara ) );
2562
0
}
2563
2564
void TextEngine::ImpParagraphRemoved( sal_uInt32 nPara )
2565
0
{
2566
0
    if ( mpViews->size() > 1 )
2567
0
    {
2568
0
        for ( auto nView = mpViews->size(); nView; )
2569
0
        {
2570
0
            TextView* pView = (*mpViews)[ --nView ];
2571
0
            if ( pView != GetActiveView() )
2572
0
            {
2573
0
                const sal_uInt32 nParas = static_cast<sal_uInt32>(mpDoc->GetNodes().size());
2574
0
                for ( int n = 0; n <= 1; n++ )
2575
0
                {
2576
0
                    TextPaM& rPaM = n ? pView->GetSelection().GetStart(): pView->GetSelection().GetEnd();
2577
0
                    if ( rPaM.GetPara() > nPara )
2578
0
                        rPaM.GetPara()--;
2579
0
                    else if ( rPaM.GetPara() == nPara )
2580
0
                    {
2581
0
                        rPaM.GetIndex() = 0;
2582
0
                        if ( rPaM.GetPara() >= nParas )
2583
0
                            rPaM.GetPara()--;
2584
0
                    }
2585
0
                }
2586
0
            }
2587
0
        }
2588
0
    }
2589
0
    Broadcast( TextHint( SfxHintId::TextParaRemoved, nPara ) );
2590
0
}
2591
2592
void TextEngine::ImpCharsRemoved( sal_uInt32 nPara, sal_Int32 nPos, sal_Int32 nChars )
2593
0
{
2594
0
    if ( mpViews->size() > 1 )
2595
0
    {
2596
0
        for ( auto nView = mpViews->size(); nView; )
2597
0
        {
2598
0
            TextView* pView = (*mpViews)[ --nView ];
2599
0
            if ( pView != GetActiveView() )
2600
0
            {
2601
0
                const sal_Int32 nEnd = nPos + nChars;
2602
0
                for ( int n = 0; n <= 1; n++ )
2603
0
                {
2604
0
                    TextPaM& rPaM = n ? pView->GetSelection().GetStart(): pView->GetSelection().GetEnd();
2605
0
                    if ( rPaM.GetPara() == nPara )
2606
0
                    {
2607
0
                        if ( rPaM.GetIndex() > nEnd )
2608
0
                            rPaM.GetIndex() = rPaM.GetIndex() - nChars;
2609
0
                        else if ( rPaM.GetIndex() > nPos )
2610
0
                            rPaM.GetIndex() = nPos;
2611
0
                    }
2612
0
                }
2613
0
            }
2614
0
        }
2615
0
    }
2616
0
    Broadcast( TextHint( SfxHintId::TextParaContentChanged, nPara ) );
2617
0
}
2618
2619
void TextEngine::ImpCharsInserted( sal_uInt32 nPara, sal_Int32 nPos, sal_Int32 nChars )
2620
0
{
2621
0
    if ( mpViews->size() > 1 )
2622
0
    {
2623
0
        for ( auto nView = mpViews->size(); nView; )
2624
0
        {
2625
0
            TextView* pView = (*mpViews)[ --nView ];
2626
0
            if ( pView != GetActiveView() )
2627
0
            {
2628
0
                for ( int n = 0; n <= 1; n++ )
2629
0
                {
2630
0
                    TextPaM& rPaM = n ? pView->GetSelection().GetStart(): pView->GetSelection().GetEnd();
2631
0
                    if ( rPaM.GetPara() == nPara )
2632
0
                    {
2633
0
                        if ( rPaM.GetIndex() >= nPos )
2634
0
                            rPaM.GetIndex() += nChars;
2635
0
                    }
2636
0
                }
2637
0
            }
2638
0
        }
2639
0
    }
2640
0
    Broadcast( TextHint( SfxHintId::TextParaContentChanged, nPara ) );
2641
0
}
2642
2643
void TextEngine::Draw( OutputDevice* pDev, const Point& rPos )
2644
0
{
2645
0
    ImpPaint( pDev, rPos, nullptr );
2646
0
}
2647
2648
void TextEngine::SetLeftMargin( sal_uInt16 n )
2649
0
{
2650
0
    mpDoc->SetLeftMargin( n );
2651
0
}
2652
2653
uno::Reference< i18n::XBreakIterator > const & TextEngine::GetBreakIterator()
2654
0
{
2655
0
    if ( !mxBreakIterator.is() )
2656
0
        mxBreakIterator = vcl::unohelper::CreateBreakIterator();
2657
0
    SAL_WARN_IF( !mxBreakIterator.is(), "vcl", "BreakIterator: Failed to create!" );
2658
0
    return mxBreakIterator;
2659
0
}
2660
2661
void TextEngine::SetLocale( const css::lang::Locale& rLocale )
2662
0
{
2663
0
    maLocale = rLocale;
2664
0
    mpLocaleDataWrapper.reset();
2665
0
}
2666
2667
css::lang::Locale const & TextEngine::GetLocale()
2668
0
{
2669
0
    if ( maLocale.Language.isEmpty() )
2670
0
    {
2671
0
        maLocale = Application::GetSettings().GetUILanguageTag().getLocale();   // TODO: why UI locale?
2672
0
    }
2673
0
    return maLocale;
2674
0
}
2675
2676
LocaleDataWrapper* TextEngine::ImpGetLocaleDataWrapper()
2677
0
{
2678
0
    if ( !mpLocaleDataWrapper )
2679
0
        mpLocaleDataWrapper.reset( new LocaleDataWrapper( LanguageTag( GetLocale()) ) );
2680
2681
0
    return mpLocaleDataWrapper.get();
2682
0
}
2683
2684
void TextEngine::SetRightToLeft( bool bR2L )
2685
0
{
2686
0
    if ( mbRightToLeft != bR2L )
2687
0
    {
2688
0
        mbRightToLeft = bR2L;
2689
0
        meAlign = bR2L ? TxtAlign::Right : TxtAlign::Left;
2690
0
        FormatFullDoc();
2691
0
        UpdateViews();
2692
0
    }
2693
0
}
2694
2695
void TextEngine::ImpInitWritingDirections( sal_uInt32 nPara )
2696
0
{
2697
0
    TEParaPortion* pParaPortion = mpTEParaPortions->GetObject( nPara );
2698
0
    std::vector<TEWritingDirectionInfo>& rInfos = pParaPortion->GetWritingDirectionInfos();
2699
0
    rInfos.clear();
2700
2701
0
    if ( !pParaPortion->GetNode()->GetText().isEmpty() )
2702
0
    {
2703
0
        const UBiDiLevel nBidiLevel = IsRightToLeft() ? 1 /*RTL*/ : 0 /*LTR*/;
2704
0
        OUString aText( pParaPortion->GetNode()->GetText() );
2705
2706
        // Bidi functions from icu 2.0
2707
2708
0
        UErrorCode nError = U_ZERO_ERROR;
2709
0
        UBiDi* pBidi = ubidi_openSized( aText.getLength(), 0, &nError );
2710
0
        nError = U_ZERO_ERROR;
2711
2712
0
        ubidi_setPara( pBidi, reinterpret_cast<const UChar *>(aText.getStr()), aText.getLength(), nBidiLevel, nullptr, &nError );
2713
0
        nError = U_ZERO_ERROR;
2714
2715
0
        tools::Long nCount = ubidi_countRuns( pBidi, &nError );
2716
2717
0
        int32_t nStart = 0;
2718
0
        int32_t nEnd;
2719
0
        UBiDiLevel nCurrDir;
2720
2721
0
        for ( tools::Long nIdx = 0; nIdx < nCount; ++nIdx )
2722
0
        {
2723
0
            ubidi_getLogicalRun( pBidi, nStart, &nEnd, &nCurrDir );
2724
            // bit 0 of nCurrDir indicates direction
2725
0
            rInfos.emplace_back( /*bLeftToRight*/ nCurrDir % 2 == 0, nStart, nEnd );
2726
0
            nStart = nEnd;
2727
0
        }
2728
2729
0
        ubidi_close( pBidi );
2730
0
    }
2731
2732
    // No infos mean no CTL and default dir is L2R...
2733
0
    if ( rInfos.empty() )
2734
0
        rInfos.emplace_back( 0, 0, pParaPortion->GetNode()->GetText().getLength() );
2735
2736
0
}
2737
2738
bool TextEngine::ImpGetRightToLeft( sal_uInt32 nPara, sal_Int32 nPos )
2739
0
{
2740
0
    bool bRightToLeft = false;
2741
2742
0
    TextNode* pNode = mpDoc->GetNodes()[ nPara ].get();
2743
0
    if ( pNode && !pNode->GetText().isEmpty() )
2744
0
    {
2745
0
        TEParaPortion* pParaPortion = mpTEParaPortions->GetObject( nPara );
2746
0
        if ( pParaPortion->GetWritingDirectionInfos().empty() )
2747
0
            ImpInitWritingDirections( nPara );
2748
2749
0
        std::vector<TEWritingDirectionInfo>& rDirInfos = pParaPortion->GetWritingDirectionInfos();
2750
0
        for ( const auto& rWritingDirectionInfo : rDirInfos )
2751
0
        {
2752
0
            if ( rWritingDirectionInfo.nStartPos <= nPos && rWritingDirectionInfo.nEndPos >= nPos )
2753
0
            {
2754
0
                bRightToLeft = !rWritingDirectionInfo.bLeftToRight;
2755
0
                break;
2756
0
            }
2757
0
        }
2758
0
    }
2759
0
    return bRightToLeft;
2760
0
}
2761
2762
tools::Long TextEngine::ImpGetPortionXOffset( sal_uInt32 nPara, TextLine const * pLine, std::size_t nTextPortion )
2763
0
{
2764
0
    tools::Long nX = pLine->GetStartX();
2765
2766
0
    TEParaPortion* pParaPortion = mpTEParaPortions->GetObject( nPara );
2767
2768
0
    for ( std::size_t i = pLine->GetStartPortion(); i < nTextPortion; i++ )
2769
0
    {
2770
0
        TETextPortion& rPortion = pParaPortion->GetTextPortions()[ i ];
2771
0
        nX += rPortion.GetWidth();
2772
0
    }
2773
2774
0
    TETextPortion& rDestPortion = pParaPortion->GetTextPortions()[ nTextPortion ];
2775
0
    if ( rDestPortion.GetKind() != PORTIONKIND_TAB )
2776
0
    {
2777
0
        if ( !IsRightToLeft() && rDestPortion.IsRightToLeft() )
2778
0
        {
2779
            // Portions behind must be added, visual before this portion
2780
0
            std::size_t nTmpPortion = nTextPortion+1;
2781
0
            while ( nTmpPortion <= pLine->GetEndPortion() )
2782
0
            {
2783
0
                TETextPortion& rNextTextPortion = pParaPortion->GetTextPortions()[ nTmpPortion ];
2784
0
                if ( rNextTextPortion.IsRightToLeft() && ( rNextTextPortion.GetKind() != PORTIONKIND_TAB ) )
2785
0
                    nX += rNextTextPortion.GetWidth();
2786
0
                else
2787
0
                    break;
2788
0
                nTmpPortion++;
2789
0
            }
2790
            // Portions before must be removed, visual behind this portion
2791
0
            nTmpPortion = nTextPortion;
2792
0
            while ( nTmpPortion > pLine->GetStartPortion() )
2793
0
            {
2794
0
                --nTmpPortion;
2795
0
                TETextPortion& rPrevTextPortion = pParaPortion->GetTextPortions()[ nTmpPortion ];
2796
0
                if ( rPrevTextPortion.IsRightToLeft() && ( rPrevTextPortion.GetKind() != PORTIONKIND_TAB ) )
2797
0
                    nX -= rPrevTextPortion.GetWidth();
2798
0
                else
2799
0
                    break;
2800
0
            }
2801
0
        }
2802
0
        else if ( IsRightToLeft() && !rDestPortion.IsRightToLeft() )
2803
0
        {
2804
            // Portions behind must be removed, visual behind this portion
2805
0
            std::size_t nTmpPortion = nTextPortion+1;
2806
0
            while ( nTmpPortion <= pLine->GetEndPortion() )
2807
0
            {
2808
0
                TETextPortion& rNextTextPortion = pParaPortion->GetTextPortions()[ nTmpPortion ];
2809
0
                if ( !rNextTextPortion.IsRightToLeft() && ( rNextTextPortion.GetKind() != PORTIONKIND_TAB ) )
2810
0
                    nX += rNextTextPortion.GetWidth();
2811
0
                else
2812
0
                    break;
2813
0
                nTmpPortion++;
2814
0
            }
2815
            // Portions before must be added, visual before this portion
2816
0
            nTmpPortion = nTextPortion;
2817
0
            while ( nTmpPortion > pLine->GetStartPortion() )
2818
0
            {
2819
0
                --nTmpPortion;
2820
0
                TETextPortion& rPrevTextPortion = pParaPortion->GetTextPortions()[ nTmpPortion ];
2821
0
                if ( !rPrevTextPortion.IsRightToLeft() && ( rPrevTextPortion.GetKind() != PORTIONKIND_TAB ) )
2822
0
                    nX -= rPrevTextPortion.GetWidth();
2823
0
                else
2824
0
                    break;
2825
0
            }
2826
0
        }
2827
0
    }
2828
2829
0
    return nX;
2830
0
}
2831
2832
void TextEngine::ImpInitLayoutMode( OutputDevice* pOutDev )
2833
0
{
2834
0
    vcl::text::ComplexTextLayoutFlags nLayoutMode = pOutDev->GetLayoutMode();
2835
2836
0
    nLayoutMode &= ~vcl::text::ComplexTextLayoutFlags(vcl::text::ComplexTextLayoutFlags::BiDiRtl | vcl::text::ComplexTextLayoutFlags::BiDiStrong );
2837
2838
0
    pOutDev->SetLayoutMode( nLayoutMode );
2839
0
}
2840
2841
TxtAlign TextEngine::ImpGetAlign() const
2842
0
{
2843
0
    TxtAlign eAlign = meAlign;
2844
0
    if ( IsRightToLeft() )
2845
0
    {
2846
0
        if ( eAlign == TxtAlign::Left )
2847
0
            eAlign = TxtAlign::Right;
2848
0
        else if ( eAlign == TxtAlign::Right )
2849
0
            eAlign = TxtAlign::Left;
2850
0
    }
2851
0
    return eAlign;
2852
0
}
2853
2854
tools::Long TextEngine::ImpGetOutputOffset( sal_uInt32 nPara, TextLine* pLine, sal_Int32 nIndex, sal_Int32 nIndex2 )
2855
0
{
2856
0
    TEParaPortion* pPortion = mpTEParaPortions->GetObject( nPara );
2857
2858
0
    sal_Int32 nPortionStart {0};
2859
0
    const std::size_t nPortion = pPortion->GetTextPortions().FindPortion( nIndex, nPortionStart, true );
2860
2861
0
    TETextPortion& rTextPortion = pPortion->GetTextPortions()[ nPortion ];
2862
2863
0
    tools::Long nX;
2864
2865
0
    if ( ( nIndex == nPortionStart ) && ( nIndex == nIndex2 )  )
2866
0
    {
2867
        // Output of full portion, so we need portion x offset.
2868
        // Use ImpGetPortionXOffset, because GetXPos may deliver left or right position from portion, depending on R2L, L2R
2869
0
        nX = ImpGetPortionXOffset( nPara, pLine, nPortion );
2870
0
        if ( IsRightToLeft() )
2871
0
        {
2872
0
            nX = -nX - rTextPortion.GetWidth();
2873
0
        }
2874
0
    }
2875
0
    else
2876
0
    {
2877
0
        nX = ImpGetXPos( nPara, pLine, nIndex, nIndex == nPortionStart );
2878
0
        if ( nIndex2 != nIndex )
2879
0
        {
2880
0
            const tools::Long nX2 = ImpGetXPos( nPara, pLine, nIndex2 );
2881
0
            if ( ( !IsRightToLeft() && ( nX2 < nX ) ) ||
2882
0
                 ( IsRightToLeft() && ( nX2 > nX ) ) )
2883
0
            {
2884
0
                nX = nX2;
2885
0
            }
2886
0
        }
2887
0
        if ( IsRightToLeft() )
2888
0
        {
2889
0
            nX = -nX;
2890
0
        }
2891
0
    }
2892
2893
0
    return nX;
2894
0
}
2895
2896
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */