Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / editeng / source / rtf / rtfitem.cxx
blobbf6b002f971efc47af40861a6a8b1cb39bda29d2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <editeng/fontitem.hxx>
22 #include <editeng/postitem.hxx>
23 #include <editeng/wghtitem.hxx>
24 #include <editeng/fhgtitem.hxx>
25 #include <editeng/udlnitem.hxx>
26 #include <editeng/crossedoutitem.hxx>
27 #include <editeng/shdditem.hxx>
28 #include <editeng/autokernitem.hxx>
29 #include <editeng/wrlmitem.hxx>
30 #include <editeng/contouritem.hxx>
31 #include <editeng/colritem.hxx>
32 #include <editeng/kernitem.hxx>
33 #include <editeng/cmapitem.hxx>
34 #include <editeng/escapementitem.hxx>
35 #include <editeng/langitem.hxx>
36 #include <editeng/emphasismarkitem.hxx>
37 #include <editeng/twolinesitem.hxx>
38 #include <editeng/lrspitem.hxx>
39 #include <editeng/ulspitem.hxx>
40 #include <editeng/shaditem.hxx>
41 #include <editeng/borderline.hxx>
42 #include <editeng/boxitem.hxx>
43 #include <editeng/keepitem.hxx>
44 #include <editeng/brushitem.hxx>
45 #include <editeng/lspcitem.hxx>
46 #include <editeng/adjustitem.hxx>
47 #include <editeng/tstpitem.hxx>
48 #include <editeng/spltitem.hxx>
49 #include <editeng/hyphenzoneitem.hxx>
50 #include <editeng/charscaleitem.hxx>
51 #include <editeng/charrotateitem.hxx>
52 #include <editeng/charreliefitem.hxx>
53 #include <editeng/paravertalignitem.hxx>
54 #include <editeng/forbiddenruleitem.hxx>
55 #include <editeng/hngpnctitem.hxx>
56 #include <editeng/scriptspaceitem.hxx>
57 #include <editeng/frmdiritem.hxx>
58 #include <editeng/charhiddenitem.hxx>
60 #include <svtools/rtftoken.h>
61 #include <svl/itempool.hxx>
62 #include <svl/itemiter.hxx>
63 #include <sal/log.hxx>
64 #include <vcl/font.hxx>
66 #include <editeng/svxrtf.hxx>
67 #include <editeng/editids.hrc>
69 #include <limits.h>
71 #define BRACELEFT '{'
72 #define BRACERIGHT '}'
74 using namespace ::com::sun::star;
75 using namespace editeng;
77 void SvxRTFParser::SetScriptAttr( RTF_CharTypeDef eType, SfxItemSet& rSet,
78 SfxPoolItem& rItem )
80 std::optional<sal_uInt16> pNormal;
81 std::optional<sal_uInt16> pCJK;
82 std::optional<sal_uInt16> pCTL;
83 switch( rItem.Which() )
85 case SID_ATTR_CHAR_FONT:
86 pNormal = aPlainMap[SID_ATTR_CHAR_FONT];
87 pCJK = aPlainMap[SID_ATTR_CHAR_CJK_FONT];
88 pCTL = aPlainMap[SID_ATTR_CHAR_CTL_FONT];
89 break;
91 case SID_ATTR_CHAR_FONTHEIGHT:
92 pNormal = aPlainMap[SID_ATTR_CHAR_FONTHEIGHT];
93 pCJK = aPlainMap[SID_ATTR_CHAR_CJK_FONTHEIGHT];
94 pCTL = aPlainMap[SID_ATTR_CHAR_CTL_FONTHEIGHT];
95 break;
97 case SID_ATTR_CHAR_POSTURE:
98 pNormal = aPlainMap[SID_ATTR_CHAR_POSTURE];
99 pCJK = aPlainMap[SID_ATTR_CHAR_CJK_POSTURE];
100 pCTL = aPlainMap[SID_ATTR_CHAR_CTL_POSTURE];
101 break;
103 case SID_ATTR_CHAR_WEIGHT:
104 pNormal = aPlainMap[SID_ATTR_CHAR_WEIGHT];
105 pCJK = aPlainMap[SID_ATTR_CHAR_CJK_WEIGHT];
106 pCTL = aPlainMap[SID_ATTR_CHAR_CTL_WEIGHT];
107 break;
109 case SID_ATTR_CHAR_LANGUAGE:
110 pNormal = aPlainMap[SID_ATTR_CHAR_LANGUAGE];
111 pCJK = aPlainMap[SID_ATTR_CHAR_CJK_LANGUAGE];
112 pCTL = aPlainMap[SID_ATTR_CHAR_CTL_LANGUAGE];
113 break;
115 case 0:
116 // it exist no WhichId - don't set this item
117 break;
119 default:
120 rSet.Put( rItem );
121 break;
124 if( DOUBLEBYTE_CHARTYPE == eType )
126 if( bIsLeftToRightDef && pCJK )
128 rItem.SetWhich( *pCJK );
129 rSet.Put( rItem );
132 else if( !bIsLeftToRightDef )
134 if( pCTL )
136 rItem.SetWhich( *pCTL );
137 rSet.Put( rItem );
140 else
142 if( LOW_CHARTYPE == eType )
144 if( pNormal )
146 rItem.SetWhich( *pNormal );
147 rSet.Put( rItem );
150 else if( HIGH_CHARTYPE == eType )
152 if( pCTL )
154 rItem.SetWhich( *pCTL );
155 rSet.Put( rItem );
158 else
160 if( pCJK )
162 rItem.SetWhich( *pCJK );
163 rSet.Put( rItem );
165 if( pCTL )
167 rItem.SetWhich( *pCTL );
168 rSet.Put( rItem );
170 if( pNormal )
172 rItem.SetWhich( *pNormal );
173 rSet.Put( rItem );
180 void SvxRTFParser::ReadAttr( int nToken, SfxItemSet* pSet )
182 DBG_ASSERT( pSet, "A SfxItemSet has to be provided as argument!" );
183 bool bFirstToken = true;
184 bool bContinue = true;
185 FontLineStyle eUnderline;
186 FontLineStyle eOverline;
187 FontEmphasisMark eEmphasis;
188 RTF_CharTypeDef eCharType = NOTDEF_CHARTYPE;
189 SvxParaVertAlignItem::Align nFontAlign;
191 bool bChkStkPos = !bNewGroup && !aAttrStack.empty();
193 while( bContinue && IsParserWorking() ) // as long as known Attribute are recognized
195 switch( nToken )
197 case RTF_PARD:
198 RTFPardPlain( true, &pSet );
199 break;
201 case RTF_PLAIN:
202 RTFPardPlain( false, &pSet );
203 break;
205 default:
206 do { // middle checked loop
207 if( !bChkStkPos )
208 break;
210 SvxRTFItemStackType* pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get();
211 if( !pCurrent || (pCurrent->mxStartNodeIdx->GetIdx() == mxInsertPosition->GetNodeIdx() &&
212 pCurrent->nSttCnt == mxInsertPosition->GetCntIdx() ))
213 break;
215 int nLastToken = GetStackPtr(-1)->nTokenId;
216 if( RTF_PARD == nLastToken || RTF_PLAIN == nLastToken )
217 break;
219 if (pCurrent->aAttrSet.Count() || !pCurrent->maChildList.empty() ||
220 pCurrent->nStyleNo )
222 // Open a new Group
223 auto xNew(std::make_unique<SvxRTFItemStackType>(*pCurrent, *mxInsertPosition, true));
224 xNew->SetRTFDefaults( GetRTFDefaults() );
226 // "Set" all valid attributes up until this point
227 AttrGroupEnd();
228 pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get(); // can be changed after AttrGroupEnd!
229 xNew->aAttrSet.SetParent( pCurrent ? &pCurrent->aAttrSet : nullptr );
231 aAttrStack.push_back( std::move(xNew) );
232 pCurrent = aAttrStack.back().get();
234 else
235 // continue to use this entry as a new one
236 pCurrent->SetStartPos( *mxInsertPosition );
238 pSet = &pCurrent->aAttrSet;
239 } while( false );
241 switch( nToken )
243 case RTF_INTBL:
244 case RTF_PAGEBB:
245 case RTF_SBYS:
246 case RTF_CS:
247 case RTF_LS:
248 case RTF_ILVL:
249 UnknownAttrToken( nToken );
250 break;
252 case RTF_S:
253 if( bIsInReadStyleTab )
255 if( !bFirstToken )
256 SkipToken();
257 bContinue = false;
259 else
261 sal_uInt16 nStyleNo = -1 == nTokenValue ? 0 : sal_uInt16(nTokenValue);
262 // set StyleNo to the current style on the AttrStack
263 SvxRTFItemStackType* pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get();
264 if( !pCurrent )
265 break;
267 pCurrent->nStyleNo = nStyleNo;
269 break;
271 case RTF_KEEP:
272 if (const TypedWhichId<SvxFormatSplitItem> wid = aPardMap[SID_ATTR_PARA_SPLIT])
274 pSet->Put(SvxFormatSplitItem(false, wid));
276 break;
278 case RTF_KEEPN:
279 if (const TypedWhichId<SvxFormatKeepItem> wid = aPardMap[SID_ATTR_PARA_KEEP])
281 pSet->Put(SvxFormatKeepItem(true, wid));
283 break;
285 case RTF_LEVEL:
286 if (const TypedWhichId<SfxInt16Item> wid = aPardMap[SID_ATTR_PARA_OUTLLEVEL])
288 pSet->Put(SfxInt16Item(wid, static_cast<sal_uInt16>(nTokenValue)));
290 break;
292 case RTF_QL:
293 if (const TypedWhichId<SvxAdjustItem> wid = aPardMap[SID_ATTR_PARA_ADJUST])
295 pSet->Put(SvxAdjustItem(SvxAdjust::Left, wid));
297 break;
298 case RTF_QR:
299 if (const TypedWhichId<SvxAdjustItem> wid = aPardMap[SID_ATTR_PARA_ADJUST])
301 pSet->Put(SvxAdjustItem(SvxAdjust::Right, wid));
303 break;
304 case RTF_QJ:
305 if (const TypedWhichId<SvxAdjustItem> wid = aPardMap[SID_ATTR_PARA_ADJUST])
307 pSet->Put(SvxAdjustItem(SvxAdjust::Block, wid));
309 break;
310 case RTF_QC:
311 if (const TypedWhichId<SvxAdjustItem> wid = aPardMap[SID_ATTR_PARA_ADJUST])
313 pSet->Put(SvxAdjustItem(SvxAdjust::Center, wid));
315 break;
317 case RTF_FI:
318 if (const TypedWhichId<SvxLRSpaceItem> wid = aPardMap[SID_ATTR_LRSPACE])
320 SvxLRSpaceItem aLR(pSet->Get(wid));
321 sal_uInt16 nSz = 0;
322 if( -1 != nTokenValue )
324 if( IsCalcValue() )
325 CalcValue();
326 nSz = sal_uInt16(nTokenValue);
328 aLR.SetTextFirstLineOffset( nSz );
329 pSet->Put( aLR );
331 break;
333 case RTF_LI:
334 case RTF_LIN:
335 if (const TypedWhichId<SvxLRSpaceItem> wid = aPardMap[SID_ATTR_LRSPACE])
337 SvxLRSpaceItem aLR(pSet->Get(wid));
338 sal_uInt16 nSz = 0;
339 if( 0 < nTokenValue )
341 if( IsCalcValue() )
342 CalcValue();
343 nSz = sal_uInt16(nTokenValue);
345 aLR.SetTextLeft( nSz );
346 pSet->Put( aLR );
348 break;
350 case RTF_RI:
351 case RTF_RIN:
352 if (const TypedWhichId<SvxLRSpaceItem> wid = aPardMap[SID_ATTR_LRSPACE])
354 SvxLRSpaceItem aLR(pSet->Get(wid));
355 sal_uInt16 nSz = 0;
356 if( 0 < nTokenValue )
358 if( IsCalcValue() )
359 CalcValue();
360 nSz = sal_uInt16(nTokenValue);
362 aLR.SetRight( nSz );
363 pSet->Put( aLR );
365 break;
367 case RTF_SB:
368 if (const TypedWhichId<SvxULSpaceItem> wid = aPardMap[SID_ATTR_ULSPACE])
370 SvxULSpaceItem aUL(pSet->Get(wid));
371 sal_uInt16 nSz = 0;
372 if( 0 < nTokenValue )
374 if( IsCalcValue() )
375 CalcValue();
376 nSz = sal_uInt16(nTokenValue);
378 aUL.SetUpper( nSz );
379 pSet->Put( aUL );
381 break;
383 case RTF_SA:
384 if (const TypedWhichId<SvxULSpaceItem> wid = aPardMap[SID_ATTR_ULSPACE])
386 SvxULSpaceItem aUL(pSet->Get(wid));
387 sal_uInt16 nSz = 0;
388 if( 0 < nTokenValue )
390 if( IsCalcValue() )
391 CalcValue();
392 nSz = sal_uInt16(nTokenValue);
394 aUL.SetLower( nSz );
395 pSet->Put( aUL );
397 break;
399 case RTF_SLMULT:
400 if (const TypedWhichId<SvxLineSpacingItem> wid = aPardMap[SID_ATTR_PARA_LINESPACE];
401 wid && 1 == nTokenValue)
403 // then switches to multi-line!
404 SvxLineSpacingItem aLSpace(pSet->Get(wid, false));
406 // how much do you get from the line height value?
408 // Proportional-Size:
409 // Ie, the ratio is (n / 240) twips
411 nTokenValue = 240;
412 if( IsCalcValue() )
413 CalcValue();
415 nTokenValue = short( 100 * aLSpace.GetLineHeight() / nTokenValue );
417 aLSpace.SetPropLineSpace( static_cast<sal_uInt16>(nTokenValue) );
418 aLSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
420 pSet->Put( aLSpace );
422 break;
424 case RTF_SL:
425 if (const TypedWhichId<SvxLineSpacingItem> wid = aPardMap[SID_ATTR_PARA_LINESPACE])
427 // Calculate the ratio between the default font and the
428 // specified size. The distance consists of the line height
429 // (100%) and the space above the line (20%).
430 SvxLineSpacingItem aLSpace(0, wid);
432 nTokenValue = !bTokenHasValue ? 0 : nTokenValue;
433 if (1000 == nTokenValue )
434 nTokenValue = 240;
436 SvxLineSpaceRule eLnSpc;
437 if (nTokenValue < 0)
439 eLnSpc = SvxLineSpaceRule::Fix;
440 nTokenValue = -nTokenValue;
442 else if (nTokenValue == 0)
444 //if \sl0 is used, the line spacing is automatically
445 //determined
446 eLnSpc = SvxLineSpaceRule::Auto;
448 else
449 eLnSpc = SvxLineSpaceRule::Min;
451 if (IsCalcValue())
452 CalcValue();
454 if (eLnSpc != SvxLineSpaceRule::Auto)
455 aLSpace.SetLineHeight( static_cast<sal_uInt16>(nTokenValue) );
457 aLSpace.SetLineSpaceRule(eLnSpc);
458 pSet->Put(aLSpace);
460 break;
462 case RTF_NOCWRAP:
463 if (const TypedWhichId<SvxForbiddenRuleItem> wid = aPardMap[SID_ATTR_PARA_FORBIDDEN_RULES])
465 pSet->Put(SvxForbiddenRuleItem(false, wid));
467 break;
468 case RTF_NOOVERFLOW:
469 if (const TypedWhichId<SvxHangingPunctuationItem> wid = aPardMap[SID_ATTR_PARA_HANGPUNCTUATION])
471 pSet->Put(SvxHangingPunctuationItem(false, wid));
473 break;
475 case RTF_ASPALPHA:
476 if (const TypedWhichId<SvxScriptSpaceItem> wid = aPardMap[SID_ATTR_PARA_SCRIPTSPACE])
478 pSet->Put(SvxScriptSpaceItem(true, wid));
480 break;
482 case RTF_FAFIXED:
483 case RTF_FAAUTO: nFontAlign = SvxParaVertAlignItem::Align::Automatic;
484 goto SET_FONTALIGNMENT;
485 case RTF_FAHANG: nFontAlign = SvxParaVertAlignItem::Align::Top;
486 goto SET_FONTALIGNMENT;
487 case RTF_FAVAR: nFontAlign = SvxParaVertAlignItem::Align::Bottom;
488 goto SET_FONTALIGNMENT;
489 case RTF_FACENTER: nFontAlign = SvxParaVertAlignItem::Align::Center;
490 goto SET_FONTALIGNMENT;
491 case RTF_FAROMAN: nFontAlign = SvxParaVertAlignItem::Align::Baseline;
492 goto SET_FONTALIGNMENT;
493 SET_FONTALIGNMENT:
494 if (const TypedWhichId<SvxParaVertAlignItem> wid = aPardMap[SID_PARA_VERTALIGN])
496 pSet->Put(SvxParaVertAlignItem(nFontAlign, wid));
498 break;
500 case RTF_B:
501 case RTF_AB:
502 if( IsAttrSttPos() ) // not in the text flow?
505 SvxWeightItem aTmpItem(
506 nTokenValue ? WEIGHT_BOLD : WEIGHT_NORMAL,
507 SID_ATTR_CHAR_WEIGHT );
508 SetScriptAttr( eCharType, *pSet, aTmpItem);
510 break;
512 case RTF_CAPS:
513 case RTF_SCAPS:
514 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_CHAR_CASEMAP];
515 wid && IsAttrSttPos()) // not in the text flow?
517 SvxCaseMap eCaseMap;
518 if( !nTokenValue )
519 eCaseMap = SvxCaseMap::NotMapped;
520 else if( RTF_CAPS == nToken )
521 eCaseMap = SvxCaseMap::Uppercase;
522 else
523 eCaseMap = SvxCaseMap::SmallCaps;
525 pSet->Put(SvxCaseMapItem(eCaseMap, wid));
527 break;
529 case RTF_DN:
530 case RTF_SUB:
531 if (const sal_uInt16 nEsc = aPlainMap[SID_ATTR_CHAR_ESCAPEMENT])
533 if( -1 == nTokenValue )
534 nTokenValue = 6; //RTF default \dn value in half-points
535 if( IsCalcValue() )
536 CalcValue();
537 const SvxEscapementItem& rOld =
538 static_cast<const SvxEscapementItem&>(pSet->Get( nEsc,false));
539 sal_Int16 nEs;
540 sal_uInt8 nProp;
541 if( DFLT_ESC_AUTO_SUPER == rOld.GetEsc() )
543 nEs = DFLT_ESC_AUTO_SUB;
544 nProp = rOld.GetProportionalHeight();
546 else
548 nEs = (nToken == RTF_SUB) ? DFLT_ESC_AUTO_SUB : -nTokenValue;
549 nProp = (nToken == RTF_SUB) ? DFLT_ESC_PROP : 100;
551 pSet->Put( SvxEscapementItem( nEs, nProp, nEsc ));
553 break;
555 case RTF_NOSUPERSUB:
556 if (const sal_uInt16 nEsc = aPlainMap[SID_ATTR_CHAR_ESCAPEMENT])
558 pSet->Put( SvxEscapementItem( nEsc ));
560 break;
562 case RTF_EXPND:
563 if (TypedWhichId<SvxKerningItem> wid = aPlainMap[SID_ATTR_CHAR_KERNING])
565 if( -1 == nTokenValue )
566 nTokenValue = 0;
567 else
568 nTokenValue *= 5;
569 if( IsCalcValue() )
570 CalcValue();
571 pSet->Put(SvxKerningItem(static_cast<short>(nTokenValue), wid));
573 break;
575 case RTF_KERNING:
576 if (const TypedWhichId<SvxAutoKernItem> wid = aPlainMap[SID_ATTR_CHAR_AUTOKERN])
578 if( -1 == nTokenValue )
579 nTokenValue = 0;
580 else
581 nTokenValue *= 10;
582 if( IsCalcValue() )
583 CalcValue();
584 pSet->Put(SvxAutoKernItem(0 != nTokenValue, wid));
586 break;
588 case RTF_EXPNDTW:
589 if (TypedWhichId<SvxKerningItem> wid = aPlainMap[SID_ATTR_CHAR_KERNING])
591 if( -1 == nTokenValue )
592 nTokenValue = 0;
593 if( IsCalcValue() )
594 CalcValue();
595 pSet->Put(SvxKerningItem(static_cast<short>(nTokenValue), wid));
597 break;
599 case RTF_F:
600 case RTF_AF:
602 const vcl::Font& rSVFont = GetFont( sal_uInt16(nTokenValue) );
603 SvxFontItem aTmpItem( rSVFont.GetFamilyType(),
604 rSVFont.GetFamilyName(), rSVFont.GetStyleName(),
605 rSVFont.GetPitch(), rSVFont.GetCharSet(),
606 SID_ATTR_CHAR_FONT );
607 SetScriptAttr( eCharType, *pSet, aTmpItem );
608 if( RTF_F == nToken )
610 SetEncoding( rSVFont.GetCharSet() );
611 RereadLookahead();
614 break;
616 case RTF_FS:
617 case RTF_AFS:
619 if( -1 == nTokenValue )
620 nTokenValue = 240;
621 else
622 nTokenValue *= 10;
623 // #i66167#
624 // for the SwRTFParser 'IsCalcValue' will be false and for the EditRTFParser
625 // the conversion takes now place in EditRTFParser since for other reasons
626 // the wrong MapUnit might still be use there
627 // if( IsCalcValue() )
628 // CalcValue();
629 SvxFontHeightItem aTmpItem(
630 static_cast<sal_uInt16>(nTokenValue), 100,
631 SID_ATTR_CHAR_FONTHEIGHT );
632 SetScriptAttr( eCharType, *pSet, aTmpItem );
634 break;
636 case RTF_I:
637 case RTF_AI:
638 if( IsAttrSttPos() ) // not in the text flow?
640 SvxPostureItem aTmpItem(
641 nTokenValue ? ITALIC_NORMAL : ITALIC_NONE,
642 SID_ATTR_CHAR_POSTURE );
643 SetScriptAttr( eCharType, *pSet, aTmpItem );
645 break;
647 case RTF_OUTL:
648 if (const TypedWhichId<SvxContourItem> wid = aPlainMap[SID_ATTR_CHAR_CONTOUR];
649 wid && IsAttrSttPos()) // not in the text flow?
651 pSet->Put(SvxContourItem(nTokenValue != 0, wid));
653 break;
655 case RTF_SHAD:
656 if (const TypedWhichId<SvxShadowedItem> wid = aPlainMap[SID_ATTR_CHAR_SHADOWED];
657 wid && IsAttrSttPos()) // not in the text flow?
659 pSet->Put(SvxShadowedItem(nTokenValue != 0, wid));
661 break;
663 case RTF_STRIKE:
664 if (const TypedWhichId<SvxCrossedOutItem> wid = aPlainMap[SID_ATTR_CHAR_STRIKEOUT];
665 wid && IsAttrSttPos()) // not in the text flow?
667 pSet->Put( SvxCrossedOutItem(
668 nTokenValue ? STRIKEOUT_SINGLE : STRIKEOUT_NONE,
669 wid ));
671 break;
673 case RTF_STRIKED:
674 if (const TypedWhichId<SvxCrossedOutItem> wid = aPlainMap[SID_ATTR_CHAR_STRIKEOUT]) // not in the text flow?
676 pSet->Put( SvxCrossedOutItem(
677 nTokenValue ? STRIKEOUT_DOUBLE : STRIKEOUT_NONE,
678 wid ));
680 break;
682 case RTF_UL:
683 if( !IsAttrSttPos() )
684 break;
685 eUnderline = nTokenValue ? LINESTYLE_SINGLE : LINESTYLE_NONE;
686 goto ATTR_SETUNDERLINE;
688 case RTF_ULD:
689 eUnderline = LINESTYLE_DOTTED;
690 goto ATTR_SETUNDERLINE;
691 case RTF_ULDASH:
692 eUnderline = LINESTYLE_DASH;
693 goto ATTR_SETUNDERLINE;
694 case RTF_ULDASHD:
695 eUnderline = LINESTYLE_DASHDOT;
696 goto ATTR_SETUNDERLINE;
697 case RTF_ULDASHDD:
698 eUnderline = LINESTYLE_DASHDOTDOT;
699 goto ATTR_SETUNDERLINE;
700 case RTF_ULDB:
701 eUnderline = LINESTYLE_DOUBLE;
702 goto ATTR_SETUNDERLINE;
703 case RTF_ULNONE:
704 eUnderline = LINESTYLE_NONE;
705 goto ATTR_SETUNDERLINE;
706 case RTF_ULTH:
707 eUnderline = LINESTYLE_BOLD;
708 goto ATTR_SETUNDERLINE;
709 case RTF_ULWAVE:
710 eUnderline = LINESTYLE_WAVE;
711 goto ATTR_SETUNDERLINE;
712 case RTF_ULTHD:
713 eUnderline = LINESTYLE_BOLDDOTTED;
714 goto ATTR_SETUNDERLINE;
715 case RTF_ULTHDASH:
716 eUnderline = LINESTYLE_BOLDDASH;
717 goto ATTR_SETUNDERLINE;
718 case RTF_ULLDASH:
719 eUnderline = LINESTYLE_LONGDASH;
720 goto ATTR_SETUNDERLINE;
721 case RTF_ULTHLDASH:
722 eUnderline = LINESTYLE_BOLDLONGDASH;
723 goto ATTR_SETUNDERLINE;
724 case RTF_ULTHDASHD:
725 eUnderline = LINESTYLE_BOLDDASHDOT;
726 goto ATTR_SETUNDERLINE;
727 case RTF_ULTHDASHDD:
728 eUnderline = LINESTYLE_BOLDDASHDOTDOT;
729 goto ATTR_SETUNDERLINE;
730 case RTF_ULHWAVE:
731 eUnderline = LINESTYLE_BOLDWAVE;
732 goto ATTR_SETUNDERLINE;
733 case RTF_ULULDBWAVE:
734 eUnderline = LINESTYLE_DOUBLEWAVE;
735 goto ATTR_SETUNDERLINE;
737 case RTF_ULW:
738 eUnderline = LINESTYLE_SINGLE;
740 if (const TypedWhichId<SvxWordLineModeItem> wid = aPlainMap[SID_ATTR_CHAR_WORDLINEMODE])
742 pSet->Put(SvxWordLineModeItem(true, wid));
744 goto ATTR_SETUNDERLINE;
746 ATTR_SETUNDERLINE:
747 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_CHAR_UNDERLINE])
749 pSet->Put(SvxUnderlineItem(eUnderline, wid));
751 break;
753 case RTF_ULC:
754 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_CHAR_UNDERLINE])
756 std::unique_ptr<SvxUnderlineItem> aUL(std::make_unique<SvxUnderlineItem>(LINESTYLE_SINGLE, wid));
757 const SfxPoolItem* pItem(nullptr);
759 if (SfxItemState::SET == pSet->GetItemState(wid, false, &pItem))
761 // is switched off ?
762 if( LINESTYLE_NONE == static_cast<const SvxUnderlineItem*>(pItem)->GetLineStyle() )
763 break;
765 aUL.reset(static_cast<SvxUnderlineItem*>(pItem->Clone()));
767 else
769 aUL.reset(static_cast<SvxUnderlineItem*>(pSet->Get(wid, false).Clone()));
772 if(LINESTYLE_NONE == aUL->GetLineStyle())
774 aUL->SetLineStyle(LINESTYLE_SINGLE);
777 aUL->SetColor(GetColor(sal_uInt16(nTokenValue)));
779 pSet->Put(std::move(aUL));
781 break;
783 case RTF_OL:
784 if( !IsAttrSttPos() )
785 break;
786 eOverline = nTokenValue ? LINESTYLE_SINGLE : LINESTYLE_NONE;
787 goto ATTR_SETOVERLINE;
789 case RTF_OLD:
790 eOverline = LINESTYLE_DOTTED;
791 goto ATTR_SETOVERLINE;
792 case RTF_OLDASH:
793 eOverline = LINESTYLE_DASH;
794 goto ATTR_SETOVERLINE;
795 case RTF_OLDASHD:
796 eOverline = LINESTYLE_DASHDOT;
797 goto ATTR_SETOVERLINE;
798 case RTF_OLDASHDD:
799 eOverline = LINESTYLE_DASHDOTDOT;
800 goto ATTR_SETOVERLINE;
801 case RTF_OLDB:
802 eOverline = LINESTYLE_DOUBLE;
803 goto ATTR_SETOVERLINE;
804 case RTF_OLNONE:
805 eOverline = LINESTYLE_NONE;
806 goto ATTR_SETOVERLINE;
807 case RTF_OLTH:
808 eOverline = LINESTYLE_BOLD;
809 goto ATTR_SETOVERLINE;
810 case RTF_OLWAVE:
811 eOverline = LINESTYLE_WAVE;
812 goto ATTR_SETOVERLINE;
813 case RTF_OLTHD:
814 eOverline = LINESTYLE_BOLDDOTTED;
815 goto ATTR_SETOVERLINE;
816 case RTF_OLTHDASH:
817 eOverline = LINESTYLE_BOLDDASH;
818 goto ATTR_SETOVERLINE;
819 case RTF_OLLDASH:
820 eOverline = LINESTYLE_LONGDASH;
821 goto ATTR_SETOVERLINE;
822 case RTF_OLTHLDASH:
823 eOverline = LINESTYLE_BOLDLONGDASH;
824 goto ATTR_SETOVERLINE;
825 case RTF_OLTHDASHD:
826 eOverline = LINESTYLE_BOLDDASHDOT;
827 goto ATTR_SETOVERLINE;
828 case RTF_OLTHDASHDD:
829 eOverline = LINESTYLE_BOLDDASHDOTDOT;
830 goto ATTR_SETOVERLINE;
831 case RTF_OLHWAVE:
832 eOverline = LINESTYLE_BOLDWAVE;
833 goto ATTR_SETOVERLINE;
834 case RTF_OLOLDBWAVE:
835 eOverline = LINESTYLE_DOUBLEWAVE;
836 goto ATTR_SETOVERLINE;
838 case RTF_OLW:
839 eOverline = LINESTYLE_SINGLE;
841 if (const TypedWhichId<SvxWordLineModeItem> wid = aPlainMap[SID_ATTR_CHAR_WORDLINEMODE])
843 pSet->Put(SvxWordLineModeItem(true, wid));
845 goto ATTR_SETOVERLINE;
847 ATTR_SETOVERLINE:
848 if (const TypedWhichId<SvxOverlineItem> wid = aPlainMap[SID_ATTR_CHAR_OVERLINE])
850 pSet->Put(SvxOverlineItem(eOverline, wid));
852 break;
854 case RTF_OLC:
855 if (const TypedWhichId<SvxOverlineItem> wid = aPlainMap[SID_ATTR_CHAR_OVERLINE])
857 std::unique_ptr<SvxOverlineItem> aOL(std::make_unique<SvxOverlineItem>(LINESTYLE_SINGLE, wid));
858 const SfxPoolItem* pItem(nullptr);
860 if (SfxItemState::SET == pSet->GetItemState(wid, false, &pItem))
862 // is switched off ?
863 if( LINESTYLE_NONE == static_cast<const SvxOverlineItem*>(pItem)->GetLineStyle() )
864 break;
866 aOL.reset(static_cast<SvxOverlineItem*>(pItem->Clone()));
868 else
870 aOL.reset(pSet->Get(wid, false).Clone());
873 if(LINESTYLE_NONE == aOL->GetLineStyle())
875 aOL->SetLineStyle(LINESTYLE_SINGLE);
878 aOL->SetColor(GetColor(sal_uInt16(nTokenValue)));
880 pSet->Put(std::move(aOL));
882 break;
884 case RTF_UP:
885 case RTF_SUPER:
886 if (const sal_uInt16 nEsc = aPlainMap[SID_ATTR_CHAR_ESCAPEMENT])
888 if( -1 == nTokenValue )
889 nTokenValue = 6; //RTF default \up value in half-points
890 if( IsCalcValue() )
891 CalcValue();
892 const SvxEscapementItem& rOld =
893 static_cast<const SvxEscapementItem&>(pSet->Get( nEsc,false));
894 sal_Int16 nEs;
895 sal_uInt8 nProp;
896 if( DFLT_ESC_AUTO_SUB == rOld.GetEsc() )
898 nEs = DFLT_ESC_AUTO_SUPER;
899 nProp = rOld.GetProportionalHeight();
901 else
903 nEs = (nToken == RTF_SUPER) ? DFLT_ESC_AUTO_SUPER : nTokenValue;
904 nProp = (nToken == RTF_SUPER) ? DFLT_ESC_PROP : 100;
906 pSet->Put( SvxEscapementItem( nEs, nProp, nEsc ));
908 break;
910 case RTF_CF:
911 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_CHAR_COLOR])
913 pSet->Put(SvxColorItem(GetColor(sal_uInt16(nTokenValue)), wid));
915 break;
916 //#i12501# While cb is clearly documented in the rtf spec, word
917 //doesn't accept it at all
918 #if 0
919 case RTF_CB:
920 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_BRUSH_CHAR])
922 pSet->Put(SvxBrushItem(GetColor(sal_uInt16(nTokenValue)), wid));
924 break;
925 #endif
927 case RTF_LANG:
928 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_CHAR_LANGUAGE])
930 pSet->Put(SvxLanguageItem(LanguageType(nTokenValue), wid));
932 break;
934 case RTF_LANGFE:
935 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_CHAR_CJK_LANGUAGE])
937 pSet->Put(SvxLanguageItem(LanguageType(nTokenValue), wid));
939 break;
940 case RTF_ALANG:
942 SvxLanguageItem aTmpItem( LanguageType(nTokenValue),
943 SID_ATTR_CHAR_LANGUAGE );
944 SetScriptAttr( eCharType, *pSet, aTmpItem );
946 break;
948 case RTF_RTLCH:
949 bIsLeftToRightDef = false;
950 break;
951 case RTF_LTRCH:
952 bIsLeftToRightDef = true;
953 break;
954 case RTF_RTLPAR:
955 if (const TypedWhichId<SvxFrameDirectionItem> wid = aPardMap[SID_ATTR_FRAMEDIRECTION])
957 pSet->Put(SvxFrameDirectionItem(SvxFrameDirection::Horizontal_RL_TB, wid));
959 break;
960 case RTF_LTRPAR:
961 if (const TypedWhichId<SvxFrameDirectionItem> wid = aPardMap[SID_ATTR_FRAMEDIRECTION])
963 pSet->Put(SvxFrameDirectionItem(SvxFrameDirection::Horizontal_LR_TB, wid));
965 break;
966 case RTF_LOCH: eCharType = LOW_CHARTYPE; break;
967 case RTF_HICH: eCharType = HIGH_CHARTYPE; break;
968 case RTF_DBCH: eCharType = DOUBLEBYTE_CHARTYPE; break;
971 case RTF_ACCNONE:
972 eEmphasis = FontEmphasisMark::NONE;
973 goto ATTR_SETEMPHASIS;
974 case RTF_ACCDOT:
975 eEmphasis = (FontEmphasisMark::Dot | FontEmphasisMark::PosAbove);
976 goto ATTR_SETEMPHASIS;
978 case RTF_ACCCOMMA:
979 eEmphasis = (FontEmphasisMark::Accent | FontEmphasisMark::PosAbove);
980 ATTR_SETEMPHASIS:
981 if (const TypedWhichId<SvxEmphasisMarkItem> wid = aPlainMap[SID_ATTR_CHAR_EMPHASISMARK])
983 pSet->Put(SvxEmphasisMarkItem(eEmphasis, wid));
985 break;
987 case RTF_TWOINONE:
988 if (const TypedWhichId<SvxTwoLinesItem> wid = aPlainMap[SID_ATTR_CHAR_TWO_LINES])
990 sal_Unicode cStt, cEnd;
991 switch ( nTokenValue )
993 case 1: cStt = '('; cEnd = ')'; break;
994 case 2: cStt = '['; cEnd = ']'; break;
995 case 3: cStt = '<'; cEnd = '>'; break;
996 case 4: cStt = '{'; cEnd = '}'; break;
997 default: cStt = 0; cEnd = 0; break;
1000 pSet->Put(SvxTwoLinesItem(true, cStt, cEnd, wid));
1002 break;
1004 case RTF_CHARSCALEX :
1005 if (const TypedWhichId<SvxCharScaleWidthItem> wid = aPlainMap[SID_ATTR_CHAR_SCALEWIDTH])
1007 //i21372
1008 if (nTokenValue < 1 || nTokenValue > 600)
1009 nTokenValue = 100;
1010 pSet->Put(SvxCharScaleWidthItem(sal_uInt16(nTokenValue), wid));
1012 break;
1014 case RTF_HORZVERT:
1015 if (const TypedWhichId<SvxCharRotateItem> wid = aPlainMap[SID_ATTR_CHAR_ROTATED])
1017 // RTF knows only 90deg
1018 pSet->Put(SvxCharRotateItem(900_deg10, 1 == nTokenValue, wid));
1020 break;
1022 case RTF_EMBO:
1023 if (const TypedWhichId<SvxCharReliefItem> wid = aPlainMap[SID_ATTR_CHAR_RELIEF])
1025 pSet->Put(SvxCharReliefItem(FontRelief::Embossed, wid));
1027 break;
1028 case RTF_IMPR:
1029 if (const TypedWhichId<SvxCharReliefItem> wid = aPlainMap[SID_ATTR_CHAR_RELIEF])
1031 pSet->Put(SvxCharReliefItem(FontRelief::Engraved, wid));
1033 break;
1034 case RTF_V:
1035 if (const TypedWhichId<SvxCharHiddenItem> wid = aPlainMap[SID_ATTR_CHAR_HIDDEN])
1037 pSet->Put(SvxCharHiddenItem(nTokenValue != 0, wid));
1039 break;
1040 case RTF_CHBGFDIAG:
1041 case RTF_CHBGDKVERT:
1042 case RTF_CHBGDKHORIZ:
1043 case RTF_CHBGVERT:
1044 case RTF_CHBGHORIZ:
1045 case RTF_CHBGDKFDIAG:
1046 case RTF_CHBGDCROSS:
1047 case RTF_CHBGCROSS:
1048 case RTF_CHBGBDIAG:
1049 case RTF_CHBGDKDCROSS:
1050 case RTF_CHBGDKCROSS:
1051 case RTF_CHBGDKBDIAG:
1052 case RTF_CHCBPAT:
1053 case RTF_CHCFPAT:
1054 case RTF_CHSHDNG:
1055 if (aPlainMap[SID_ATTR_BRUSH_CHAR])
1056 ReadBackgroundAttr( nToken, *pSet );
1057 break;
1059 case BRACELEFT:
1061 // tests on Swg internal tokens
1062 bool bHandled = false;
1063 short nSkip = 0;
1064 if( RTF_IGNOREFLAG != GetNextToken())
1065 nSkip = -1;
1066 else if( (nToken = GetNextToken() ) & RTF_SWGDEFS )
1068 bHandled = true;
1069 switch( nToken )
1071 case RTF_PGDSCNO:
1072 case RTF_PGBRK:
1073 case RTF_SOUTLVL:
1074 UnknownAttrToken( nToken );
1075 // overwrite the closing parenthesis
1076 break;
1078 case RTF_SWG_ESCPROP:
1080 // Store percentage change!
1081 sal_uInt8 nProp = sal_uInt8( nTokenValue / 100 );
1082 short nEsc = 0;
1083 if( 1 == ( nTokenValue % 100 ))
1084 // Recognize own auto-flags!
1085 nEsc = DFLT_ESC_AUTO_SUPER;
1087 if (const sal_uInt16 wid = aPlainMap[SID_ATTR_CHAR_ESCAPEMENT])
1088 pSet->Put(SvxEscapementItem(nEsc, nProp, wid));
1090 break;
1092 case RTF_HYPHEN:
1094 SvxHyphenZoneItem aHypenZone(
1095 (nTokenValue & 1) != 0,
1096 aPardMap[SID_ATTR_PARA_HYPHENZONE]);
1097 aHypenZone.SetPageEnd((nTokenValue & 2) != 0);
1099 if( aPardMap[SID_ATTR_PARA_HYPHENZONE] &&
1100 RTF_HYPHLEAD == GetNextToken() &&
1101 RTF_HYPHTRAIL == GetNextToken() &&
1102 RTF_HYPHMAX == GetNextToken() )
1104 aHypenZone.GetMinLead() =
1105 sal_uInt8(GetStackPtr( -2 )->nTokenValue);
1106 aHypenZone.GetMinTrail() =
1107 sal_uInt8(GetStackPtr( -1 )->nTokenValue);
1108 aHypenZone.GetMaxHyphens() =
1109 sal_uInt8(nTokenValue);
1111 pSet->Put( aHypenZone );
1113 else
1114 SkipGroup(); // at the end of the group
1116 break;
1118 // We expect these to be preceded by a RTF_HYPHEN and
1119 // so normally are handled by the RTF_HYPHEN case, but
1120 // if they appear 'bare' in a document then safely skip
1121 // them here
1122 case RTF_HYPHLEAD:
1123 case RTF_HYPHTRAIL:
1124 case RTF_HYPHMAX:
1125 SkipGroup();
1126 break;
1128 case RTF_SHADOW:
1130 bool bSkip = true;
1131 do { // middle check loop
1132 SvxShadowLocation eSL = SvxShadowLocation( nTokenValue );
1133 if( RTF_SHDW_DIST != GetNextToken() )
1134 break;
1135 sal_uInt16 nDist = sal_uInt16( nTokenValue );
1137 if( RTF_SHDW_STYLE != GetNextToken() )
1138 break;
1140 if( RTF_SHDW_COL != GetNextToken() )
1141 break;
1142 sal_uInt16 nCol = sal_uInt16( nTokenValue );
1144 if( RTF_SHDW_FCOL != GetNextToken() )
1145 break;
1147 Color aColor = GetColor( nCol );
1149 if (const TypedWhichId<SvxShadowItem> wid = aPardMap[SID_ATTR_BORDER_SHADOW])
1150 pSet->Put(SvxShadowItem(wid, &aColor, nDist, eSL));
1152 bSkip = false;
1153 } while( false );
1155 if( bSkip )
1156 SkipGroup(); // at the end of the group
1158 break;
1160 default:
1161 bHandled = false;
1162 if( (nToken & ~(0xff | RTF_SWGDEFS)) == RTF_TABSTOPDEF )
1164 nToken = SkipToken( -2 );
1165 ReadTabAttr( nToken, *pSet );
1168 cmc: #i76140, he who consumed the { must consume the }
1169 We rewound to a state of { being the current
1170 token so it is our responsibility to consume the }
1171 token if we consumed the {. We will not have consumed
1172 the { if it belonged to our caller, i.e. if the { we
1173 are handling is the "firsttoken" passed to us then
1174 the *caller* must consume it, not us. Otherwise *we*
1175 should consume it.
1177 if (nToken == BRACELEFT && !bFirstToken)
1179 nToken = GetNextToken();
1180 SAL_WARN_IF( nToken != BRACERIGHT,
1181 "editeng",
1182 "} did not follow { as expected");
1185 else if( (nToken & ~(0xff| RTF_SWGDEFS)) == RTF_BRDRDEF)
1187 nToken = SkipToken( -2 );
1188 ReadBorderAttr( nToken, *pSet );
1190 else // so no more attribute
1191 nSkip = -2;
1192 break;
1195 #if 1
1197 cmc: #i4727# / #i12713# Who owns this closing bracket?
1198 If we read the opening one, we must read this one, if
1199 other is counting the brackets so as to push/pop off
1200 the correct environment then we will have pushed a new
1201 environment for the start { of this, but will not see
1202 the } and so is out of sync for the rest of the
1203 document.
1205 if (bHandled && !bFirstToken)
1206 GetNextToken();
1207 #endif
1209 else
1210 nSkip = -2;
1212 if( nSkip ) // all completely unknown
1214 if (!bFirstToken)
1215 --nSkip; // BRACELEFT: is the next token
1216 SkipToken( nSkip );
1217 bContinue = false;
1220 break;
1221 default:
1222 if( (nToken & ~0xff ) == RTF_TABSTOPDEF )
1223 ReadTabAttr( nToken, *pSet );
1224 else if( (nToken & ~0xff ) == RTF_BRDRDEF )
1225 ReadBorderAttr( nToken, *pSet );
1226 else if( (nToken & ~0xff ) == RTF_SHADINGDEF )
1227 ReadBackgroundAttr( nToken, *pSet );
1228 else
1230 // unknown token, so token "returned in Parser"
1231 if( !bFirstToken )
1232 SkipToken();
1233 bContinue = false;
1237 if( bContinue )
1239 nToken = GetNextToken();
1241 bFirstToken = false;
1245 void SvxRTFParser::ReadTabAttr( int nToken, SfxItemSet& rSet )
1247 bool bMethodOwnsToken = false; // #i52542# patch from cmc.
1248 // then read all the TabStops
1249 SvxTabStop aTabStop;
1250 SvxTabStopItem aAttr(0, 0, SvxTabAdjust::Default, aPardMap[SID_ATTR_TABSTOP]);
1251 bool bContinue = true;
1252 do {
1253 switch( nToken )
1255 case RTF_TB: // BarTab ???
1256 case RTF_TX:
1258 if( IsCalcValue() )
1259 CalcValue();
1260 aTabStop.GetTabPos() = nTokenValue;
1261 aAttr.Insert( aTabStop );
1262 aTabStop = SvxTabStop(); // all values default
1264 break;
1266 case RTF_TQL:
1267 aTabStop.GetAdjustment() = SvxTabAdjust::Left;
1268 break;
1269 case RTF_TQR:
1270 aTabStop.GetAdjustment() = SvxTabAdjust::Right;
1271 break;
1272 case RTF_TQC:
1273 aTabStop.GetAdjustment() = SvxTabAdjust::Center;
1274 break;
1275 case RTF_TQDEC:
1276 aTabStop.GetAdjustment() = SvxTabAdjust::Decimal;
1277 break;
1279 case RTF_TLDOT: aTabStop.GetFill() = '.'; break;
1280 case RTF_TLHYPH: aTabStop.GetFill() = ' '; break;
1281 case RTF_TLUL: aTabStop.GetFill() = '_'; break;
1282 case RTF_TLTH: aTabStop.GetFill() = '-'; break;
1283 case RTF_TLEQ: aTabStop.GetFill() = '='; break;
1285 case BRACELEFT:
1287 // Swg - control BRACELEFT RTF_IGNOREFLAG RTF_TLSWG BRACERIGHT
1288 short nSkip = 0;
1289 if( RTF_IGNOREFLAG != GetNextToken() )
1290 nSkip = -1;
1291 else if( RTF_TLSWG != ( nToken = GetNextToken() ))
1292 nSkip = -2;
1293 else
1295 aTabStop.GetDecimal() = sal_uInt8(nTokenValue & 0xff);
1296 aTabStop.GetFill() = sal_uInt8((nTokenValue >> 8) & 0xff);
1297 // overwrite the closing parenthesis
1298 if (bMethodOwnsToken)
1299 GetNextToken();
1301 if( nSkip )
1303 SkipToken( nSkip ); // Ignore back again
1304 bContinue = false;
1307 break;
1309 default:
1310 bContinue = false;
1312 if( bContinue )
1314 nToken = GetNextToken();
1315 bMethodOwnsToken = true;
1317 } while( bContinue );
1319 // Fill with defaults is still missing!
1320 rSet.Put( aAttr );
1321 SkipToken();
1324 static void SetBorderLine( int nBorderTyp, SvxBoxItem& rItem,
1325 const SvxBorderLine& rBorder )
1327 switch( nBorderTyp )
1329 case RTF_BOX: // run through all levels
1330 case RTF_BRDRT:
1331 rItem.SetLine( &rBorder, SvxBoxItemLine::TOP );
1332 if( RTF_BOX != nBorderTyp )
1333 return;
1334 [[fallthrough]];
1335 case RTF_BRDRB:
1336 rItem.SetLine( &rBorder, SvxBoxItemLine::BOTTOM );
1337 if( RTF_BOX != nBorderTyp )
1338 return;
1339 [[fallthrough]];
1340 case RTF_BRDRL:
1341 rItem.SetLine( &rBorder, SvxBoxItemLine::LEFT );
1342 if( RTF_BOX != nBorderTyp )
1343 return;
1344 [[fallthrough]];
1345 case RTF_BRDRR:
1346 rItem.SetLine( &rBorder, SvxBoxItemLine::RIGHT );
1347 if( RTF_BOX != nBorderTyp )
1348 return;
1352 void SvxRTFParser::ReadBorderAttr( int nToken, SfxItemSet& rSet,
1353 bool bTableDef )
1355 // then read the border attribute
1356 std::unique_ptr<SvxBoxItem> aAttr(std::make_unique<SvxBoxItem>(aPardMap[SID_ATTR_BORDER_OUTER]));
1357 const SfxPoolItem* pItem(nullptr);
1359 if (SfxItemState::SET == rSet.GetItemState(aPardMap[SID_ATTR_BORDER_OUTER], false, &pItem))
1361 aAttr.reset(static_cast<SvxBoxItem*>(pItem->Clone()));
1364 SvxBorderLine aBrd( nullptr, SvxBorderLineWidth::Hairline );
1365 bool bContinue = true;
1366 int nBorderTyp = 0;
1368 tools::Long nWidth = 1;
1369 bool bDoubleWidth = false;
1371 do {
1372 switch( nToken )
1374 case RTF_BOX:
1375 case RTF_BRDRT:
1376 case RTF_BRDRB:
1377 case RTF_BRDRL:
1378 case RTF_BRDRR:
1379 nBorderTyp = nToken;
1380 break;
1382 case RTF_CLBRDRT: // Cell top border
1384 if( bTableDef )
1386 if (nBorderTyp != 0)
1387 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1388 nBorderTyp = RTF_BRDRT;
1390 break;
1392 case RTF_CLBRDRB: // Cell bottom border
1394 if( bTableDef )
1396 if (nBorderTyp != 0)
1397 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1398 nBorderTyp = RTF_BRDRB;
1400 break;
1402 case RTF_CLBRDRL: // Cell left border
1404 if( bTableDef )
1406 if (nBorderTyp != 0)
1407 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1408 nBorderTyp = RTF_BRDRL;
1410 break;
1412 case RTF_CLBRDRR: // Cell right border
1414 if( bTableDef )
1416 if (nBorderTyp != 0)
1417 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1418 nBorderTyp = RTF_BRDRR;
1420 break;
1423 case RTF_BRDRDOT: // dotted border
1424 aBrd.SetBorderLineStyle(SvxBorderLineStyle::DOTTED);
1425 break;
1426 case RTF_BRDRDASH: // dashed border
1427 aBrd.SetBorderLineStyle(SvxBorderLineStyle::DASHED);
1428 break;
1429 case RTF_BRDRHAIR: // hairline border
1431 aBrd.SetBorderLineStyle( SvxBorderLineStyle::SOLID);
1432 aBrd.SetWidth( SvxBorderLineWidth::Hairline );
1434 break;
1435 case RTF_BRDRDB: // Double border
1436 aBrd.SetBorderLineStyle(SvxBorderLineStyle::DOUBLE);
1437 break;
1438 case RTF_BRDRINSET: // inset border
1439 aBrd.SetBorderLineStyle(SvxBorderLineStyle::INSET);
1440 break;
1441 case RTF_BRDROUTSET: // outset border
1442 aBrd.SetBorderLineStyle(SvxBorderLineStyle::OUTSET);
1443 break;
1444 case RTF_BRDRTNTHSG: // ThinThick Small gap
1445 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THINTHICK_SMALLGAP);
1446 break;
1447 case RTF_BRDRTNTHMG: // ThinThick Medium gap
1448 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THINTHICK_MEDIUMGAP);
1449 break;
1450 case RTF_BRDRTNTHLG: // ThinThick Large gap
1451 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THINTHICK_LARGEGAP);
1452 break;
1453 case RTF_BRDRTHTNSG: // ThickThin Small gap
1454 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THICKTHIN_SMALLGAP);
1455 break;
1456 case RTF_BRDRTHTNMG: // ThickThin Medium gap
1457 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THICKTHIN_MEDIUMGAP);
1458 break;
1459 case RTF_BRDRTHTNLG: // ThickThin Large gap
1460 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THICKTHIN_LARGEGAP);
1461 break;
1462 case RTF_BRDREMBOSS: // Embossed border
1463 aBrd.SetBorderLineStyle(SvxBorderLineStyle::EMBOSSED);
1464 break;
1465 case RTF_BRDRENGRAVE: // Engraved border
1466 aBrd.SetBorderLineStyle(SvxBorderLineStyle::ENGRAVED);
1467 break;
1469 case RTF_BRDRS: // single thickness border
1470 bDoubleWidth = false;
1471 break;
1472 case RTF_BRDRTH: // double thickness border width*2
1473 bDoubleWidth = true;
1474 break;
1475 case RTF_BRDRW: // border width <255
1476 nWidth = nTokenValue;
1477 break;
1479 case RTF_BRDRCF: // Border color
1480 aBrd.SetColor( GetColor( sal_uInt16(nTokenValue) ) );
1481 break;
1483 case RTF_BRDRSH: // Shadowed border
1484 rSet.Put( SvxShadowItem( aPardMap[SID_ATTR_BORDER_SHADOW], nullptr, 60 /*3pt*/,
1485 SvxShadowLocation::BottomRight ) );
1486 break;
1488 case RTF_BRSP: // Spacing to content in twip
1490 switch( nBorderTyp )
1492 case RTF_BRDRB:
1493 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::BOTTOM );
1494 break;
1496 case RTF_BRDRT:
1497 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::TOP );
1498 break;
1500 case RTF_BRDRL:
1501 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::LEFT );
1502 break;
1504 case RTF_BRDRR:
1505 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::RIGHT );
1506 break;
1508 case RTF_BOX:
1509 aAttr->SetAllDistances( static_cast<sal_uInt16>(nTokenValue) );
1510 break;
1513 break;
1515 case RTF_BRDRBTW: // Border formatting group
1516 case RTF_BRDRBAR: // Border outside
1517 // TODO unhandled ATM
1518 break;
1520 default:
1521 bContinue = (nToken & ~(0xff| RTF_SWGDEFS)) == RTF_BRDRDEF;
1523 if( bContinue )
1524 nToken = GetNextToken();
1525 } while( bContinue );
1527 // Finally compute the border width
1528 if ( bDoubleWidth ) nWidth *= 2;
1529 aBrd.SetWidth( nWidth );
1531 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1533 rSet.Put( std::move(aAttr) );
1534 SkipToken();
1537 static sal_uInt32 CalcShading( sal_uInt32 nColor, sal_uInt32 nFillColor, sal_uInt8 nShading )
1539 nColor = (nColor * nShading) / 100;
1540 nFillColor = (nFillColor * ( 100 - nShading )) / 100;
1541 return nColor + nFillColor;
1544 void SvxRTFParser::ReadBackgroundAttr( int nToken, SfxItemSet& rSet,
1545 bool bTableDef )
1547 // then read the border attribute
1548 bool bContinue = true;
1549 sal_uInt16 nColor = USHRT_MAX, nFillColor = USHRT_MAX;
1550 sal_uInt8 nFillValue = 0;
1552 sal_uInt16 nWh = ( nToken & ~0xff ) == RTF_CHRFMT
1553 ? aPlainMap[SID_ATTR_BRUSH_CHAR]
1554 : aPardMap[SID_ATTR_BRUSH];
1556 do {
1557 switch( nToken )
1559 case RTF_CLCBPAT:
1560 case RTF_CHCBPAT:
1561 case RTF_CBPAT:
1562 nFillColor = sal_uInt16( nTokenValue );
1563 break;
1565 case RTF_CLCFPAT:
1566 case RTF_CHCFPAT:
1567 case RTF_CFPAT:
1568 nColor = sal_uInt16( nTokenValue );
1569 break;
1571 case RTF_CLSHDNG:
1572 case RTF_CHSHDNG:
1573 case RTF_SHADING:
1574 nFillValue = static_cast<sal_uInt8>( nTokenValue / 100 );
1575 break;
1577 case RTF_CLBGDKHOR:
1578 case RTF_CHBGDKHORIZ:
1579 case RTF_BGDKHORIZ:
1580 case RTF_CLBGDKVERT:
1581 case RTF_CHBGDKVERT:
1582 case RTF_BGDKVERT:
1583 case RTF_CLBGDKBDIAG:
1584 case RTF_CHBGDKBDIAG:
1585 case RTF_BGDKBDIAG:
1586 case RTF_CLBGDKFDIAG:
1587 case RTF_CHBGDKFDIAG:
1588 case RTF_BGDKFDIAG:
1589 case RTF_CLBGDKCROSS:
1590 case RTF_CHBGDKCROSS:
1591 case RTF_BGDKCROSS:
1592 case RTF_CLBGDKDCROSS:
1593 case RTF_CHBGDKDCROSS:
1594 case RTF_BGDKDCROSS:
1595 // dark -> 60%
1596 nFillValue = 60;
1597 break;
1599 case RTF_CLBGHORIZ:
1600 case RTF_CHBGHORIZ:
1601 case RTF_BGHORIZ:
1602 case RTF_CLBGVERT:
1603 case RTF_CHBGVERT:
1604 case RTF_BGVERT:
1605 case RTF_CLBGBDIAG:
1606 case RTF_CHBGBDIAG:
1607 case RTF_BGBDIAG:
1608 case RTF_CLBGFDIAG:
1609 case RTF_CHBGFDIAG:
1610 case RTF_BGFDIAG:
1611 case RTF_CLBGCROSS:
1612 case RTF_CHBGCROSS:
1613 case RTF_BGCROSS:
1614 case RTF_CLBGDCROSS:
1615 case RTF_CHBGDCROSS:
1616 case RTF_BGDCROSS:
1617 // light -> 20%
1618 nFillValue = 20;
1619 break;
1621 default:
1622 if( bTableDef )
1623 bContinue = (nToken & ~(0xff | RTF_TABLEDEF) ) == RTF_SHADINGDEF;
1624 else
1625 bContinue = (nToken & ~0xff) == RTF_SHADINGDEF;
1627 if( bContinue )
1628 nToken = GetNextToken();
1629 } while( bContinue );
1631 Color aCol( COL_WHITE ), aFCol;
1632 if( !nFillValue )
1634 // there was only one of two colors specified or no BrushType
1635 if( USHRT_MAX != nFillColor )
1637 nFillValue = 100;
1638 aCol = GetColor( nFillColor );
1640 else if( USHRT_MAX != nColor )
1641 aFCol = GetColor( nColor );
1643 else
1645 if( USHRT_MAX != nColor )
1646 aCol = GetColor( nColor );
1647 else
1648 aCol = COL_BLACK;
1650 if( USHRT_MAX != nFillColor )
1651 aFCol = GetColor( nFillColor );
1652 else
1653 aFCol = COL_WHITE;
1656 Color aColor;
1657 if( 0 == nFillValue || 100 == nFillValue )
1658 aColor = aCol;
1659 else
1660 aColor = Color(
1661 static_cast<sal_uInt8>(CalcShading( aCol.GetRed(), aFCol.GetRed(), nFillValue )),
1662 static_cast<sal_uInt8>(CalcShading( aCol.GetGreen(), aFCol.GetGreen(), nFillValue )),
1663 static_cast<sal_uInt8>(CalcShading( aCol.GetBlue(), aFCol.GetBlue(), nFillValue )) );
1665 rSet.Put( SvxBrushItem( aColor, nWh ) );
1666 SkipToken();
1670 // pard / plain handling
1671 void SvxRTFParser::RTFPardPlain( bool const bPard, SfxItemSet** ppSet )
1673 if( bNewGroup || aAttrStack.empty() ) // not at the beginning of a new group
1674 return;
1676 SvxRTFItemStackType* pCurrent = aAttrStack.back().get();
1678 int nLastToken = GetStackPtr(-1)->nTokenId;
1679 bool bNewStkEntry = true;
1680 if( RTF_PARD != nLastToken &&
1681 RTF_PLAIN != nLastToken &&
1682 BRACELEFT != nLastToken )
1684 if (pCurrent->aAttrSet.Count() || !pCurrent->maChildList.empty() || pCurrent->nStyleNo)
1686 // open a new group
1687 auto xNew(std::make_unique<SvxRTFItemStackType>(*pCurrent, *mxInsertPosition, true));
1688 xNew->SetRTFDefaults( GetRTFDefaults() );
1690 // Set all until here valid attributes
1691 AttrGroupEnd();
1692 pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get(); // can be changed after AttrGroupEnd!
1693 xNew->aAttrSet.SetParent( pCurrent ? &pCurrent->aAttrSet : nullptr );
1694 aAttrStack.push_back( std::move(xNew) );
1695 pCurrent = aAttrStack.back().get();
1697 else
1699 // continue to use this entry as new
1700 pCurrent->SetStartPos( *mxInsertPosition );
1701 bNewStkEntry = false;
1705 // now reset all to default
1706 if( bNewStkEntry &&
1707 ( pCurrent->aAttrSet.GetParent() || pCurrent->aAttrSet.Count() ))
1709 const SfxPoolItem *pItem, *pDef;
1710 std::map<sal_uInt16, sal_uInt16>::const_iterator aIt;
1711 std::map<sal_uInt16, sal_uInt16>::const_iterator aEnd;
1712 const SfxItemSet* pDfltSet = &GetRTFDefaults();
1713 if( bPard )
1715 pCurrent->nStyleNo = 0;
1716 aIt = aPardMap.data.begin();
1717 aEnd = aPardMap.data.end();
1719 else
1721 aIt = aPlainMap.data.begin();
1722 aEnd = aPlainMap.data.end();
1725 for (; aIt != aEnd; ++aIt)
1727 const sal_uInt16 wid = aIt->second;
1728 // Item set and different -> Set the Default Pool
1729 if (!wid)
1731 else if (SfxItemPool::IsSlot(wid))
1732 pCurrent->aAttrSet.ClearItem(wid);
1733 else if( IsChkStyleAttr() )
1734 pCurrent->aAttrSet.Put(pDfltSet->Get(wid));
1735 else if( !pCurrent->aAttrSet.GetParent() )
1737 if (SfxItemState::SET == pDfltSet->GetItemState(wid, false, &pDef))
1738 pCurrent->aAttrSet.Put( *pDef );
1739 else
1740 pCurrent->aAttrSet.ClearItem(wid);
1742 else if( SfxItemState::SET == pCurrent->aAttrSet.GetParent()->
1743 GetItemState(wid, true, &pItem) &&
1744 *( pDef = &pDfltSet->Get(wid)) != *pItem )
1745 pCurrent->aAttrSet.Put( *pDef );
1746 else
1748 if (SfxItemState::SET == pDfltSet->GetItemState(wid, false, &pDef))
1749 pCurrent->aAttrSet.Put( *pDef );
1750 else
1751 pCurrent->aAttrSet.ClearItem(wid);
1755 else if( bPard )
1756 pCurrent->nStyleNo = 0; // reset Style number
1758 *ppSet = &pCurrent->aAttrSet;
1760 if (bPard)
1761 return;
1763 //Once we have a default font, then any text without a font specifier is
1764 //in the default font, and thus has the default font charset, otherwise
1765 //we can fall back to the ansicpg set codeset
1766 if (nDfltFont != -1)
1768 const vcl::Font& rSVFont = GetFont(sal_uInt16(nDfltFont));
1769 SetEncoding(rSVFont.GetCharSet());
1771 else
1772 SetEncoding(GetCodeSet());
1775 void SvxRTFParser::SetDefault( int nToken, int nValue )
1777 if( !bNewDoc )
1778 return;
1780 SfxItemSet aTmp(*pAttrPool, aWhichMap);
1781 bool bOldFlag = bIsLeftToRightDef;
1782 bIsLeftToRightDef = true;
1783 switch( nToken )
1785 case RTF_ADEFF:
1786 bIsLeftToRightDef = false;
1787 [[fallthrough]];
1788 case RTF_DEFF:
1790 if( -1 == nValue )
1791 nValue = 0;
1792 const vcl::Font& rSVFont = GetFont( sal_uInt16(nValue) );
1793 SvxFontItem aTmpItem(
1794 rSVFont.GetFamilyType(), rSVFont.GetFamilyName(),
1795 rSVFont.GetStyleName(), rSVFont.GetPitch(),
1796 rSVFont.GetCharSet(), SID_ATTR_CHAR_FONT );
1797 SetScriptAttr( NOTDEF_CHARTYPE, aTmp, aTmpItem );
1799 break;
1801 case RTF_ADEFLANG:
1802 bIsLeftToRightDef = false;
1803 [[fallthrough]];
1804 case RTF_DEFLANG:
1805 // store default Language
1806 if( -1 != nValue )
1808 SvxLanguageItem aTmpItem( LanguageType(nValue), SID_ATTR_CHAR_LANGUAGE );
1809 SetScriptAttr( NOTDEF_CHARTYPE, aTmp, aTmpItem );
1811 break;
1813 case RTF_DEFTAB:
1814 if (const sal_uInt16 wid = aPardMap[SID_ATTR_TABSTOP])
1816 // RTF defines 720 twips as default
1817 bIsSetDfltTab = true;
1818 if( -1 == nValue || !nValue )
1819 nValue = 720;
1821 // who would like to have no twips ...
1822 if( IsCalcValue() )
1824 nTokenValue = nValue;
1825 CalcValue();
1826 nValue = nTokenValue;
1829 // Calculate the ratio of default TabWidth / Tabs and
1830 // calculate the corresponding new number.
1831 // ?? how did one come up with 13 ??
1832 sal_uInt16 nTabCount = (SVX_TAB_DEFDIST * 13 ) / sal_uInt16(nValue);
1834 cmc, make sure we have at least one, or all hell breaks loose in
1835 everybody exporters, #i8247#
1837 if (nTabCount < 1)
1838 nTabCount = 1;
1840 // we want Defaulttabs
1841 SvxTabStopItem aNewTab(nTabCount, sal_uInt16(nValue), SvxTabAdjust::Default, wid);
1842 while( nTabCount )
1843 const_cast<SvxTabStop&>(aNewTab[ --nTabCount ]).GetAdjustment() = SvxTabAdjust::Default;
1845 pAttrPool->SetPoolDefaultItem( aNewTab );
1847 break;
1849 bIsLeftToRightDef = bOldFlag;
1851 if( aTmp.Count() )
1853 SfxItemIter aIter( aTmp );
1854 const SfxPoolItem* pItem = aIter.GetCurItem();
1857 pAttrPool->SetPoolDefaultItem( *pItem );
1858 pItem = aIter.NextItem();
1859 } while (pItem);
1863 // default: no conversion, leaving everything in twips.
1864 void SvxRTFParser::CalcValue()
1868 // for tokens that are not evaluated in ReadAttr
1869 void SvxRTFParser::UnknownAttrToken( int )
1873 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */