Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/svdraw/svdotxed.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
21
#include <svx/svdotext.hxx>
22
#include <svx/svdmodel.hxx>
23
#include <svx/svdoutl.hxx>
24
#include <editeng/editdata.hxx>
25
#include <editeng/outliner.hxx>
26
#include <editeng/outlobj.hxx>
27
#include <editeng/editstat.hxx>
28
#include <svl/itemset.hxx>
29
#include <editeng/eeitem.hxx>
30
#include <svx/sdtfchim.hxx>
31
#include <svx/sdtfsitm.hxx>
32
#include <svx/svddef.hxx>
33
#include <textchain.hxx>
34
#include <svx/annotation/ObjectAnnotationData.hxx>
35
36
bool SdrTextObj::HasTextEdit() const
37
0
{
38
    // linked text objects may be changed (no automatic reload)
39
0
    return true;
40
0
}
41
42
bool SdrTextObj::BegTextEdit(SdrOutliner& rOutl)
43
0
{
44
0
    if (mpEditingOutliner!=nullptr) return false; // Textedit might already run in another View!
45
0
    mpEditingOutliner=&rOutl;
46
47
0
    mbInEditMode = true;
48
49
0
    OutlinerMode nOutlinerMode = OutlinerMode::OutlineObject;
50
0
    if ( !IsOutlText() )
51
0
        nOutlinerMode = OutlinerMode::TextObject;
52
0
    rOutl.Init( nOutlinerMode );
53
0
    rOutl.SetRefDevice(getSdrModelFromSdrObject().GetRefDevice());
54
55
0
    bool bFitToSize(IsFitToSize());
56
0
    bool bContourFrame=IsContourTextFrame();
57
0
    ImpSetTextEditParams();
58
59
0
    if (!bContourFrame) {
60
0
        EEControlBits nStat=rOutl.GetControlWord();
61
0
        nStat|=EEControlBits::AUTOPAGESIZE;
62
0
        if (bFitToSize || IsAutoFit())
63
0
            nStat|=EEControlBits::STRETCHING;
64
0
        else
65
0
            nStat&=~EEControlBits::STRETCHING;
66
0
        rOutl.SetControlWord(nStat);
67
0
    }
68
69
    // disable AUTOPAGESIZE if IsChainable (might be required for overflow check)
70
0
    if ( IsChainable() ) {
71
0
        EEControlBits nStat1=rOutl.GetControlWord();
72
0
        nStat1 &=~EEControlBits::AUTOPAGESIZE;
73
0
        rOutl.SetControlWord(nStat1);
74
0
    }
75
76
77
0
    OutlinerParaObject* pOutlinerParaObject = GetOutlinerParaObject();
78
0
    if(pOutlinerParaObject!=nullptr)
79
0
    {
80
0
        rOutl.SetFixedCellHeight(GetMergedItem(SDRATTR_TEXT_USEFIXEDCELLHEIGHT).GetValue());
81
0
        rOutl.SetText(*GetOutlinerParaObject());
82
0
    }
83
84
    // if necessary, set frame attributes for the first (new) paragraph of the
85
    // outliner
86
0
    if( !HasTextImpl( &rOutl ) )
87
0
    {
88
        // Outliner has no text so we must set some
89
        // empty text so the outliner initialise itself
90
0
        rOutl.SetText( u""_ustr, rOutl.GetParagraph( 0 ) );
91
92
0
        if(GetStyleSheet())
93
0
            rOutl.SetStyleSheet( 0, GetStyleSheet());
94
95
        // When setting the "hard" attributes for first paragraph, the Parent
96
        // pOutlAttr (i. e. the template) has to be removed temporarily. Else,
97
        // at SetParaAttribs(), all attributes contained in the parent become
98
        // attributed hard to the paragraph.
99
0
        const SfxItemSet& rSet = GetObjectItemSet();
100
0
        SfxItemSet aFilteredSet(SfxItemSet::makeFixedSfxItemSet<EE_ITEMS_START, EE_ITEMS_END>(*rSet.GetPool()));
101
0
        aFilteredSet.Put(rSet);
102
0
        rOutl.SetParaAttribs(0, aFilteredSet);
103
0
    }
104
0
    if (bFitToSize)
105
0
    {
106
0
        tools::Rectangle aAnchorRect;
107
0
        tools::Rectangle aTextRect;
108
0
        TakeTextRect(rOutl, aTextRect, false,
109
0
            &aAnchorRect);
110
0
        double aFitXCorrection(1.0);
111
0
        ImpSetCharStretching(rOutl,aTextRect.GetSize(),aAnchorRect.GetSize(),aFitXCorrection);
112
0
    }
113
0
    else if (IsAutoFit())
114
0
    {
115
0
        setupAutoFitText(rOutl);
116
0
    }
117
118
0
    if(pOutlinerParaObject)
119
0
    {
120
0
        if (maGeo.m_nRotationAngle || IsFontwork())
121
0
        {
122
            // only repaint here, no real objectchange
123
0
            BroadcastObjectChange();
124
0
        }
125
0
    }
126
127
0
    rOutl.UpdateFields();
128
0
    rOutl.ClearModifyFlag();
129
130
0
    return true;
131
0
}
132
133
void SdrTextObj::TakeTextEditArea(Size* pPaperMin, Size* pPaperMax, tools::Rectangle* pViewInit, tools::Rectangle* pViewMin) const
134
0
{
135
0
    bool bFitToSize(IsFitToSize());
136
0
    Size aPaperMin,aPaperMax;
137
0
    tools::Rectangle aViewInit;
138
0
    TakeTextAnchorRect(aViewInit);
139
0
    if (maGeo.m_nRotationAngle) {
140
0
        Point aCenter(aViewInit.Center());
141
0
        aCenter-=aViewInit.TopLeft();
142
0
        Point aCenter0(aCenter);
143
0
        RotatePoint(aCenter,Point(),maGeo.mfSinRotationAngle,maGeo.mfCosRotationAngle);
144
0
        aCenter-=aCenter0;
145
0
        aViewInit.Move(aCenter.X(),aCenter.Y());
146
0
    }
147
0
    Size aAnkSiz(aViewInit.GetSize());
148
0
    aAnkSiz.AdjustWidth( -1 ); aAnkSiz.AdjustHeight( -1 ); // because GetSize() adds 1
149
0
    Size aMaxSiz(1000000,1000000);
150
0
    Size aTmpSiz(getSdrModelFromSdrObject().GetMaxObjSize());
151
0
    if (aTmpSiz.Width()!=0) aMaxSiz.setWidth(aTmpSiz.Width() );
152
0
    if (aTmpSiz.Height()!=0) aMaxSiz.setHeight(aTmpSiz.Height() );
153
154
    // Done earlier since used in else tree below
155
0
    SdrTextHorzAdjust eHAdj(GetTextHorizontalAdjust());
156
0
    SdrTextVertAdjust eVAdj(GetTextVerticalAdjust());
157
158
0
    if(IsTextFrame())
159
0
    {
160
0
        tools::Long nMinWdt=GetMinTextFrameWidth();
161
0
        tools::Long nMinHgt=GetMinTextFrameHeight();
162
0
        tools::Long nMaxWdt=GetMaxTextFrameWidth();
163
0
        tools::Long nMaxHgt=GetMaxTextFrameHeight();
164
0
        if (nMinWdt<1) nMinWdt=1;
165
0
        if (nMinHgt<1) nMinHgt=1;
166
0
        if (!bFitToSize) {
167
0
            if (nMaxWdt==0 || nMaxWdt>aMaxSiz.Width())  nMaxWdt=aMaxSiz.Width();
168
0
            if (nMaxHgt==0 || nMaxHgt>aMaxSiz.Height()) nMaxHgt=aMaxSiz.Height();
169
170
0
            if (!IsAutoGrowWidth() )
171
0
            {
172
0
                nMinWdt = aAnkSiz.Width();
173
0
                nMaxWdt = nMinWdt;
174
0
            }
175
176
0
            if (!IsAutoGrowHeight())
177
0
            {
178
0
                nMinHgt = aAnkSiz.Height();
179
0
                nMaxHgt = nMinHgt;
180
0
            }
181
182
0
            SdrTextAniKind      eAniKind=GetTextAniKind();
183
0
            SdrTextAniDirection eAniDirection=GetTextAniDirection();
184
185
0
            bool bInEditMode = IsInEditMode();
186
187
0
            if (!bInEditMode && (eAniKind==SdrTextAniKind::Scroll || eAniKind==SdrTextAniKind::Alternate || eAniKind==SdrTextAniKind::Slide))
188
0
            {
189
                // ticker text uses an unlimited paper size
190
0
                if (eAniDirection==SdrTextAniDirection::Left || eAniDirection==SdrTextAniDirection::Right) nMaxWdt=1000000;
191
0
                if (eAniDirection==SdrTextAniDirection::Up || eAniDirection==SdrTextAniDirection::Down) nMaxHgt=1000000;
192
0
            }
193
194
0
            bool bChainedFrame = IsChainable();
195
            // Might be required for overflow check working: do limit height to frame if box is chainable.
196
0
            if (!bChainedFrame) {
197
                // #i119885# Do not limit/force height to geometrical frame (vice versa for vertical writing)
198
0
                if(IsVerticalWriting())
199
0
                {
200
0
                    nMaxWdt = 1000000;
201
0
                }
202
0
                else
203
0
                {
204
0
                    nMaxHgt = 1000000;
205
0
                }
206
0
            }
207
208
0
            aPaperMax.setWidth(nMaxWdt );
209
0
            aPaperMax.setHeight(nMaxHgt );
210
0
        }
211
0
        else
212
0
        {
213
0
            aPaperMax=aMaxSiz;
214
0
        }
215
0
        aPaperMin.setWidth(nMinWdt );
216
0
        aPaperMin.setHeight(nMinHgt );
217
0
    }
218
0
    else
219
0
    {
220
        // aPaperMin needs to be set to object's size if full width is activated
221
        // for hor or ver writing respectively
222
0
        if((SDRTEXTHORZADJUST_BLOCK == eHAdj && !IsVerticalWriting())
223
0
            || (SDRTEXTVERTADJUST_BLOCK == eVAdj && IsVerticalWriting()))
224
0
        {
225
0
            aPaperMin = aAnkSiz;
226
0
        }
227
228
0
        aPaperMax=aMaxSiz;
229
0
    }
230
231
0
    if (pViewMin!=nullptr) {
232
0
        *pViewMin=aViewInit;
233
234
0
        tools::Long nXFree=aAnkSiz.Width()-aPaperMin.Width();
235
0
        if (eHAdj==SDRTEXTHORZADJUST_LEFT) pViewMin->AdjustRight( -nXFree );
236
0
        else if (eHAdj==SDRTEXTHORZADJUST_RIGHT) pViewMin->AdjustLeft(nXFree );
237
0
        else { pViewMin->AdjustLeft(nXFree/2 ); pViewMin->SetRight(pViewMin->Left()+aPaperMin.Width() ); }
238
239
0
        tools::Long nYFree=aAnkSiz.Height()-aPaperMin.Height();
240
0
        if (eVAdj==SDRTEXTVERTADJUST_TOP) pViewMin->AdjustBottom( -nYFree );
241
0
        else if (eVAdj==SDRTEXTVERTADJUST_BOTTOM) pViewMin->AdjustTop(nYFree );
242
0
        else { pViewMin->AdjustTop(nYFree/2 ); pViewMin->SetBottom(pViewMin->Top()+aPaperMin.Height() ); }
243
0
    }
244
245
    // PaperSize should grow automatically in most cases
246
0
    if(IsVerticalWriting())
247
0
        aPaperMin.setWidth( 0 );
248
0
    else
249
0
        aPaperMin.setHeight( 0 );
250
251
0
    if(eHAdj!=SDRTEXTHORZADJUST_BLOCK || bFitToSize) {
252
0
        aPaperMin.setWidth(0 );
253
0
    }
254
255
    // For complete vertical adjustment support, set paper min height to 0, here.
256
0
    if(SDRTEXTVERTADJUST_BLOCK != eVAdj || bFitToSize)
257
0
    {
258
0
        aPaperMin.setHeight( 0 );
259
0
    }
260
261
0
    if (pPaperMin!=nullptr) *pPaperMin=aPaperMin;
262
0
    if (pPaperMax!=nullptr) *pPaperMax=aPaperMax;
263
0
    if (pViewInit!=nullptr) *pViewInit=aViewInit;
264
0
}
265
266
void SdrTextObj::EndTextEdit(SdrOutliner& rOutl)
267
0
{
268
0
    if(rOutl.IsModified())
269
0
    {
270
271
        // to make the gray field background vanish again
272
0
        rOutl.UpdateFields();
273
274
0
        std::optional<OutlinerParaObject> pNewText = rOutl.CreateParaObject( 0, rOutl.GetParagraphCount() );
275
276
        // need to end edit mode early since SetOutlinerParaObject already
277
        // uses GetCurrentBoundRect() which needs to take the text into account
278
        // to work correct
279
0
        mbInEditMode = false;
280
281
        // We don't want broadcasting if we are merely trying to move to next box (this prevents infinite loops)
282
0
        if (IsChainable() && GetTextChain()->GetSwitchingToNextBox(this)) {
283
0
            GetTextChain()->SetSwitchingToNextBox(this, false);
284
0
            if( getActiveText() )
285
0
            {
286
0
                getActiveText()->SetOutlinerParaObject( std::move(pNewText) );
287
0
            }
288
0
        } else { // If we are not doing in-chaining switching just set the ParaObject
289
0
            SetOutlinerParaObject(std::move(pNewText));
290
0
        }
291
292
        // The fit stored with the shape belongs to the text before the edit, so fit afresh.
293
0
        if (IsAutoFit())
294
0
        {
295
0
            SdrTextFitToSizeTypeItem aItem(GetObjectItem(SDRATTR_TEXT_FITTOSIZE));
296
0
            if (aItem.getFontScale() != 0.0 || aItem.getSpacingScale() != 0.0)
297
0
            {
298
0
                aItem.setFontScale(0.0);
299
0
                aItem.setSpacingScale(0.0);
300
0
                SetObjectItem(aItem);
301
0
            }
302
0
        }
303
304
0
        if (isAnnotationObject())
305
0
        {
306
0
            auto xTextAPI(getAnnotationData()->mxAnnotation->getTextApiObject());
307
0
            std::optional<OutlinerParaObject> pAnnotationText = rOutl.CreateParaObject(0, rOutl.GetParagraphCount());
308
0
            xTextAPI->SetText(*pAnnotationText);
309
0
        }
310
0
    }
311
312
    /* Chaining-related code */
313
0
    rOutl.ClearOverflowingParaNum();
314
315
0
    mpEditingOutliner = nullptr;
316
0
    rOutl.Clear();
317
0
    EEControlBits nStat = rOutl.GetControlWord();
318
0
    nStat &= ~EEControlBits::AUTOPAGESIZE;
319
0
    rOutl.SetControlWord(nStat);
320
321
0
    mbInEditMode = false;
322
0
}
323
324
EEAnchorMode SdrTextObj::GetOutlinerViewAnchorMode() const
325
0
{
326
0
    SdrTextHorzAdjust eH=GetTextHorizontalAdjust();
327
0
    SdrTextVertAdjust eV=GetTextVerticalAdjust();
328
0
    EEAnchorMode eRet=EEAnchorMode::TopLeft;
329
0
    if (IsContourTextFrame()) return eRet;
330
0
    if (eH==SDRTEXTHORZADJUST_LEFT) {
331
0
        if (eV==SDRTEXTVERTADJUST_TOP) {
332
0
            eRet=EEAnchorMode::TopLeft;
333
0
        } else if (eV==SDRTEXTVERTADJUST_BOTTOM) {
334
0
            eRet=EEAnchorMode::BottomLeft;
335
0
        } else {
336
0
            eRet=EEAnchorMode::VCenterLeft;
337
0
        }
338
0
    } else if (eH==SDRTEXTHORZADJUST_RIGHT) {
339
0
        if (eV==SDRTEXTVERTADJUST_TOP) {
340
0
            eRet=EEAnchorMode::TopRight;
341
0
        } else if (eV==SDRTEXTVERTADJUST_BOTTOM) {
342
0
            eRet=EEAnchorMode::BottomRight;
343
0
        } else {
344
0
            eRet=EEAnchorMode::VCenterRight;
345
0
        }
346
0
    } else {
347
0
        if (eV==SDRTEXTVERTADJUST_TOP) {
348
0
            eRet=EEAnchorMode::TopHCenter;
349
0
        } else if (eV==SDRTEXTVERTADJUST_BOTTOM) {
350
0
            eRet=EEAnchorMode::BottomHCenter;
351
0
        } else {
352
0
            eRet=EEAnchorMode::VCenterHCenter;
353
0
        }
354
0
    }
355
0
    return eRet;
356
0
}
357
358
void SdrTextObj::ImpSetTextEditParams() const
359
0
{
360
0
    if (mpEditingOutliner==nullptr)
361
0
        return;
362
363
0
    bool bUpdBuf=mpEditingOutliner->SetUpdateLayout(false);
364
0
    Size aPaperMin;
365
0
    Size aPaperMax;
366
0
    tools::Rectangle aEditArea;
367
0
    TakeTextEditArea(&aPaperMin,&aPaperMax,&aEditArea,nullptr);
368
0
    bool bContourFrame=IsContourTextFrame();
369
0
    mpEditingOutliner->SetMinAutoPaperSize(aPaperMin);
370
0
    mpEditingOutliner->SetMaxAutoPaperSize(aPaperMax);
371
0
    mpEditingOutliner->SetPaperSize(Size());
372
0
    mpEditingOutliner->SetTextColumns(GetTextColumnsNumber(), GetTextColumnsSpacing());
373
0
    if (bContourFrame) {
374
0
        tools::Rectangle aAnchorRect;
375
0
        TakeTextAnchorRect(aAnchorRect);
376
0
        ImpSetContourPolygon(*mpEditingOutliner,aAnchorRect, true);
377
0
    }
378
0
    if (bUpdBuf) mpEditingOutliner->SetUpdateLayout(true);
379
0
}
380
381
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */