nss: upgrade to release 3.73
[LibreOffice.git] / editeng / source / rtf / rtfitem.cxx
blob5be9b0a7493ecec94a5d4a70d88486e4bce4605a
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 const sal_uInt16 *pNormal = nullptr, *pCJK = nullptr, *pCTL = nullptr;
81 switch( rItem.Which() )
83 case SID_ATTR_CHAR_FONT:
84 pNormal = &aPlainMap.nFont;
85 pCJK = &aPlainMap.nCJKFont;
86 pCTL = &aPlainMap.nCTLFont;
87 break;
89 case SID_ATTR_CHAR_FONTHEIGHT:
90 pNormal = &aPlainMap.nFontHeight;
91 pCJK = &aPlainMap.nCJKFontHeight;
92 pCTL = &aPlainMap.nCTLFontHeight;
93 break;
95 case SID_ATTR_CHAR_POSTURE:
96 pNormal = &aPlainMap.nPosture;
97 pCJK = &aPlainMap.nCJKPosture;
98 pCTL = &aPlainMap.nCTLPosture;
99 break;
101 case SID_ATTR_CHAR_WEIGHT:
102 pNormal = &aPlainMap.nWeight;
103 pCJK = &aPlainMap.nCJKWeight;
104 pCTL = &aPlainMap.nCTLWeight;
105 break;
107 case SID_ATTR_CHAR_LANGUAGE:
108 pNormal = &aPlainMap.nLanguage;
109 pCJK = &aPlainMap.nCJKLanguage;
110 pCTL = &aPlainMap.nCTLLanguage;
111 break;
113 case 0:
114 // it exist no WhichId - don't set this item
115 break;
117 default:
118 rSet.Put( rItem );
119 break;
122 if( DOUBLEBYTE_CHARTYPE == eType )
124 if( bIsLeftToRightDef && pCJK )
126 rItem.SetWhich( *pCJK );
127 rSet.Put( rItem );
130 else if( !bIsLeftToRightDef )
132 if( pCTL )
134 rItem.SetWhich( *pCTL );
135 rSet.Put( rItem );
138 else
140 if( LOW_CHARTYPE == eType )
142 if( pNormal )
144 rItem.SetWhich( *pNormal );
145 rSet.Put( rItem );
148 else if( HIGH_CHARTYPE == eType )
150 if( pCTL )
152 rItem.SetWhich( *pCTL );
153 rSet.Put( rItem );
156 else
158 if( pCJK )
160 rItem.SetWhich( *pCJK );
161 rSet.Put( rItem );
163 if( pCTL )
165 rItem.SetWhich( *pCTL );
166 rSet.Put( rItem );
168 if( pNormal )
170 rItem.SetWhich( *pNormal );
171 rSet.Put( rItem );
178 void SvxRTFParser::ReadAttr( int nToken, SfxItemSet* pSet )
180 DBG_ASSERT( pSet, "A SfxItemSet has to be provided as argument!" );
181 bool bFirstToken = true;
182 bool bContinue = true;
183 FontLineStyle eUnderline;
184 FontLineStyle eOverline;
185 FontEmphasisMark eEmphasis;
186 RTF_CharTypeDef eCharType = NOTDEF_CHARTYPE;
187 SvxParaVertAlignItem::Align nFontAlign;
189 bool bChkStkPos = !bNewGroup && !aAttrStack.empty();
191 while( bContinue && IsParserWorking() ) // as long as known Attribute are recognized
193 switch( nToken )
195 case RTF_PARD:
196 RTFPardPlain( true, &pSet );
197 break;
199 case RTF_PLAIN:
200 RTFPardPlain( false, &pSet );
201 break;
203 default:
204 do { // middle checked loop
205 if( !bChkStkPos )
206 break;
208 SvxRTFItemStackType* pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get();
209 if( !pCurrent || (pCurrent->pSttNd->GetIdx() == pInsPos->GetNodeIdx() &&
210 pCurrent->nSttCnt == pInsPos->GetCntIdx() ))
211 break;
213 int nLastToken = GetStackPtr(-1)->nTokenId;
214 if( RTF_PARD == nLastToken || RTF_PLAIN == nLastToken )
215 break;
217 if (pCurrent->aAttrSet.Count() || pCurrent->m_pChildList ||
218 pCurrent->nStyleNo )
220 // Open a new Group
221 std::unique_ptr<SvxRTFItemStackType> pNew(new SvxRTFItemStackType(
222 *pCurrent, *pInsPos, true ));
223 pNew->SetRTFDefaults( GetRTFDefaults() );
225 // "Set" all valid attributes up until this point
226 AttrGroupEnd();
227 pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get(); // can be changed after AttrGroupEnd!
228 pNew->aAttrSet.SetParent( pCurrent ? &pCurrent->aAttrSet : nullptr );
230 aAttrStack.push_back( std::move(pNew) );
231 pCurrent = aAttrStack.back().get();
233 else
234 // continue to use this entry as a new one
235 pCurrent->SetStartPos( *pInsPos );
237 pSet = &pCurrent->aAttrSet;
238 } while( false );
240 switch( nToken )
242 case RTF_INTBL:
243 case RTF_PAGEBB:
244 case RTF_SBYS:
245 case RTF_CS:
246 case RTF_LS:
247 case RTF_ILVL:
248 UnknownAttrToken( nToken );
249 break;
251 case RTF_S:
252 if( bIsInReadStyleTab )
254 if( !bFirstToken )
255 SkipToken();
256 bContinue = false;
258 else
260 sal_uInt16 nStyleNo = -1 == nTokenValue ? 0 : sal_uInt16(nTokenValue);
261 // set StyleNo to the current style on the AttrStack
262 SvxRTFItemStackType* pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get();
263 if( !pCurrent )
264 break;
266 pCurrent->nStyleNo = nStyleNo;
268 break;
270 case RTF_KEEP:
271 if( aPardMap.nSplit )
273 pSet->Put( SvxFormatSplitItem( false, aPardMap.nSplit ));
275 break;
277 case RTF_KEEPN:
278 if( aPardMap.nKeep )
280 pSet->Put( SvxFormatKeepItem( true, aPardMap.nKeep ));
282 break;
284 case RTF_LEVEL:
285 if( aPardMap.nOutlineLvl )
287 pSet->Put( SfxInt16Item( aPardMap.nOutlineLvl,
288 static_cast<sal_uInt16>(nTokenValue) ));
290 break;
292 case RTF_QL:
293 if( aPardMap.nAdjust )
295 pSet->Put( SvxAdjustItem( SvxAdjust::Left, aPardMap.nAdjust ));
297 break;
298 case RTF_QR:
299 if( aPardMap.nAdjust )
301 pSet->Put( SvxAdjustItem( SvxAdjust::Right, aPardMap.nAdjust ));
303 break;
304 case RTF_QJ:
305 if( aPardMap.nAdjust )
307 pSet->Put( SvxAdjustItem( SvxAdjust::Block, aPardMap.nAdjust ));
309 break;
310 case RTF_QC:
311 if( aPardMap.nAdjust )
313 pSet->Put( SvxAdjustItem( SvxAdjust::Center, aPardMap.nAdjust ));
315 break;
317 case RTF_FI:
318 if( aPardMap.nLRSpace )
320 SvxLRSpaceItem aLR(
321 static_cast<const SvxLRSpaceItem&>(pSet->Get(aPardMap.nLRSpace)));
322 sal_uInt16 nSz = 0;
323 if( -1 != nTokenValue )
325 if( IsCalcValue() )
326 CalcValue();
327 nSz = sal_uInt16(nTokenValue);
329 aLR.SetTextFirstLineOffset( nSz );
330 pSet->Put( aLR );
332 break;
334 case RTF_LI:
335 case RTF_LIN:
336 if( aPardMap.nLRSpace )
338 SvxLRSpaceItem aLR(
339 static_cast<const SvxLRSpaceItem&>(pSet->Get(aPardMap.nLRSpace)));
340 sal_uInt16 nSz = 0;
341 if( 0 < nTokenValue )
343 if( IsCalcValue() )
344 CalcValue();
345 nSz = sal_uInt16(nTokenValue);
347 aLR.SetTextLeft( nSz );
348 pSet->Put( aLR );
350 break;
352 case RTF_RI:
353 case RTF_RIN:
354 if( aPardMap.nLRSpace )
356 SvxLRSpaceItem aLR(
357 static_cast<const SvxLRSpaceItem&>(pSet->Get(aPardMap.nLRSpace)));
358 sal_uInt16 nSz = 0;
359 if( 0 < nTokenValue )
361 if( IsCalcValue() )
362 CalcValue();
363 nSz = sal_uInt16(nTokenValue);
365 aLR.SetRight( nSz );
366 pSet->Put( aLR );
368 break;
370 case RTF_SB:
371 if( aPardMap.nULSpace )
373 SvxULSpaceItem aUL(
374 static_cast<const SvxULSpaceItem&>(pSet->Get(aPardMap.nULSpace)));
375 sal_uInt16 nSz = 0;
376 if( 0 < nTokenValue )
378 if( IsCalcValue() )
379 CalcValue();
380 nSz = sal_uInt16(nTokenValue);
382 aUL.SetUpper( nSz );
383 pSet->Put( aUL );
385 break;
387 case RTF_SA:
388 if( aPardMap.nULSpace )
390 SvxULSpaceItem aUL(
391 static_cast<const SvxULSpaceItem&>(pSet->Get(aPardMap.nULSpace)));
392 sal_uInt16 nSz = 0;
393 if( 0 < nTokenValue )
395 if( IsCalcValue() )
396 CalcValue();
397 nSz = sal_uInt16(nTokenValue);
399 aUL.SetLower( nSz );
400 pSet->Put( aUL );
402 break;
404 case RTF_SLMULT:
405 if( aPardMap.nLinespacing && 1 == nTokenValue )
407 // then switches to multi-line!
408 SvxLineSpacingItem aLSpace(
409 static_cast<const SvxLineSpacingItem&>(pSet->Get( aPardMap.nLinespacing,false)));
411 // how much do you get from the line height value?
413 // Proportional-Size:
414 // Ie, the ratio is (n / 240) twips
416 nTokenValue = 240;
417 if( IsCalcValue() )
418 CalcValue();
420 nTokenValue = short( 100 * aLSpace.GetLineHeight() / nTokenValue );
422 aLSpace.SetPropLineSpace( static_cast<sal_uInt16>(nTokenValue) );
423 aLSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
425 pSet->Put( aLSpace );
427 break;
429 case RTF_SL:
430 if( aPardMap.nLinespacing )
432 // Calculate the ratio between the default font and the
433 // specified size. The distance consists of the line height
434 // (100%) and the space above the line (20%).
435 SvxLineSpacingItem aLSpace(0, aPardMap.nLinespacing);
437 nTokenValue = !bTokenHasValue ? 0 : nTokenValue;
438 if (1000 == nTokenValue )
439 nTokenValue = 240;
441 SvxLineSpaceRule eLnSpc;
442 if (nTokenValue < 0)
444 eLnSpc = SvxLineSpaceRule::Fix;
445 nTokenValue = -nTokenValue;
447 else if (nTokenValue == 0)
449 //if \sl0 is used, the line spacing is automatically
450 //determined
451 eLnSpc = SvxLineSpaceRule::Auto;
453 else
454 eLnSpc = SvxLineSpaceRule::Min;
456 if (IsCalcValue())
457 CalcValue();
459 if (eLnSpc != SvxLineSpaceRule::Auto)
460 aLSpace.SetLineHeight( static_cast<sal_uInt16>(nTokenValue) );
462 aLSpace.SetLineSpaceRule(eLnSpc);
463 pSet->Put(aLSpace);
465 break;
467 case RTF_NOCWRAP:
468 if( aPardMap.nForbRule )
470 pSet->Put( SvxForbiddenRuleItem( false,
471 aPardMap.nForbRule ));
473 break;
474 case RTF_NOOVERFLOW:
475 if( aPardMap.nHangPunct )
477 pSet->Put( SvxHangingPunctuationItem( false,
478 aPardMap.nHangPunct ));
480 break;
482 case RTF_ASPALPHA:
483 if( aPardMap.nScriptSpace )
485 pSet->Put( SvxScriptSpaceItem( true,
486 aPardMap.nScriptSpace ));
488 break;
490 case RTF_FAFIXED:
491 case RTF_FAAUTO: nFontAlign = SvxParaVertAlignItem::Align::Automatic;
492 goto SET_FONTALIGNMENT;
493 case RTF_FAHANG: nFontAlign = SvxParaVertAlignItem::Align::Top;
494 goto SET_FONTALIGNMENT;
495 case RTF_FAVAR: nFontAlign = SvxParaVertAlignItem::Align::Bottom;
496 goto SET_FONTALIGNMENT;
497 case RTF_FACENTER: nFontAlign = SvxParaVertAlignItem::Align::Center;
498 goto SET_FONTALIGNMENT;
499 case RTF_FAROMAN: nFontAlign = SvxParaVertAlignItem::Align::Baseline;
500 goto SET_FONTALIGNMENT;
501 SET_FONTALIGNMENT:
502 if( aPardMap.nFontAlign )
504 pSet->Put( SvxParaVertAlignItem( nFontAlign,
505 aPardMap.nFontAlign ));
507 break;
509 case RTF_B:
510 case RTF_AB:
511 if( IsAttrSttPos() ) // not in the text flow?
514 SvxWeightItem aTmpItem(
515 nTokenValue ? WEIGHT_BOLD : WEIGHT_NORMAL,
516 SID_ATTR_CHAR_WEIGHT );
517 SetScriptAttr( eCharType, *pSet, aTmpItem);
519 break;
521 case RTF_CAPS:
522 case RTF_SCAPS:
523 if( aPlainMap.nCaseMap &&
524 IsAttrSttPos() ) // not in the text flow?
526 SvxCaseMap eCaseMap;
527 if( !nTokenValue )
528 eCaseMap = SvxCaseMap::NotMapped;
529 else if( RTF_CAPS == nToken )
530 eCaseMap = SvxCaseMap::Uppercase;
531 else
532 eCaseMap = SvxCaseMap::SmallCaps;
534 pSet->Put( SvxCaseMapItem( eCaseMap, aPlainMap.nCaseMap ));
536 break;
538 case RTF_DN:
539 case RTF_SUB:
540 if( aPlainMap.nEscapement )
542 const sal_uInt16 nEsc = aPlainMap.nEscapement;
543 if( -1 == nTokenValue )
544 nTokenValue = 6; //RTF default \dn value in half-points
545 if( IsCalcValue() )
546 CalcValue();
547 const SvxEscapementItem& rOld =
548 static_cast<const SvxEscapementItem&>(pSet->Get( nEsc,false));
549 sal_Int16 nEs;
550 sal_uInt8 nProp;
551 if( DFLT_ESC_AUTO_SUPER == rOld.GetEsc() )
553 nEs = DFLT_ESC_AUTO_SUB;
554 nProp = rOld.GetProportionalHeight();
556 else
558 nEs = (nToken == RTF_SUB) ? DFLT_ESC_AUTO_SUB : -nTokenValue;
559 nProp = (nToken == RTF_SUB) ? DFLT_ESC_PROP : 100;
561 pSet->Put( SvxEscapementItem( nEs, nProp, nEsc ));
563 break;
565 case RTF_NOSUPERSUB:
566 if( aPlainMap.nEscapement )
568 const sal_uInt16 nEsc = aPlainMap.nEscapement;
569 pSet->Put( SvxEscapementItem( nEsc ));
571 break;
573 case RTF_EXPND:
574 if( aPlainMap.nKering )
576 if( -1 == nTokenValue )
577 nTokenValue = 0;
578 else
579 nTokenValue *= 5;
580 if( IsCalcValue() )
581 CalcValue();
582 pSet->Put( SvxKerningItem( static_cast<short>(nTokenValue), aPlainMap.nKering ));
584 break;
586 case RTF_KERNING:
587 if( aPlainMap.nAutoKerning )
589 if( -1 == nTokenValue )
590 nTokenValue = 0;
591 else
592 nTokenValue *= 10;
593 if( IsCalcValue() )
594 CalcValue();
595 pSet->Put( SvxAutoKernItem( 0 != nTokenValue,
596 aPlainMap.nAutoKerning ));
598 break;
600 case RTF_EXPNDTW:
601 if( aPlainMap.nKering )
603 if( -1 == nTokenValue )
604 nTokenValue = 0;
605 if( IsCalcValue() )
606 CalcValue();
607 pSet->Put( SvxKerningItem( static_cast<short>(nTokenValue), aPlainMap.nKering ));
609 break;
611 case RTF_F:
612 case RTF_AF:
614 const vcl::Font& rSVFont = GetFont( sal_uInt16(nTokenValue) );
615 SvxFontItem aTmpItem( rSVFont.GetFamilyType(),
616 rSVFont.GetFamilyName(), rSVFont.GetStyleName(),
617 rSVFont.GetPitch(), rSVFont.GetCharSet(),
618 SID_ATTR_CHAR_FONT );
619 SetScriptAttr( eCharType, *pSet, aTmpItem );
620 if( RTF_F == nToken )
622 SetEncoding( rSVFont.GetCharSet() );
623 RereadLookahead();
626 break;
628 case RTF_FS:
629 case RTF_AFS:
631 if( -1 == nTokenValue )
632 nTokenValue = 240;
633 else
634 nTokenValue *= 10;
635 // #i66167#
636 // for the SwRTFParser 'IsCalcValue' will be false and for the EditRTFParser
637 // the conversion takes now place in EditRTFParser since for other reasons
638 // the wrong MapUnit might still be use there
639 // if( IsCalcValue() )
640 // CalcValue();
641 SvxFontHeightItem aTmpItem(
642 static_cast<sal_uInt16>(nTokenValue), 100,
643 SID_ATTR_CHAR_FONTHEIGHT );
644 SetScriptAttr( eCharType, *pSet, aTmpItem );
646 break;
648 case RTF_I:
649 case RTF_AI:
650 if( IsAttrSttPos() ) // not in the text flow?
652 SvxPostureItem aTmpItem(
653 nTokenValue ? ITALIC_NORMAL : ITALIC_NONE,
654 SID_ATTR_CHAR_POSTURE );
655 SetScriptAttr( eCharType, *pSet, aTmpItem );
657 break;
659 case RTF_OUTL:
660 if( aPlainMap.nContour &&
661 IsAttrSttPos() ) // not in the text flow?
663 pSet->Put( SvxContourItem(nTokenValue != 0,
664 aPlainMap.nContour ));
666 break;
668 case RTF_SHAD:
669 if( aPlainMap.nShadowed &&
670 IsAttrSttPos() ) // not in the text flow?
672 pSet->Put( SvxShadowedItem(nTokenValue != 0,
673 aPlainMap.nShadowed ));
675 break;
677 case RTF_STRIKE:
678 if( aPlainMap.nCrossedOut &&
679 IsAttrSttPos() ) // not in the text flow?
681 pSet->Put( SvxCrossedOutItem(
682 nTokenValue ? STRIKEOUT_SINGLE : STRIKEOUT_NONE,
683 aPlainMap.nCrossedOut ));
685 break;
687 case RTF_STRIKED:
688 if( aPlainMap.nCrossedOut ) // not in the text flow?
690 pSet->Put( SvxCrossedOutItem(
691 nTokenValue ? STRIKEOUT_DOUBLE : STRIKEOUT_NONE,
692 aPlainMap.nCrossedOut ));
694 break;
696 case RTF_UL:
697 if( !IsAttrSttPos() )
698 break;
699 eUnderline = nTokenValue ? LINESTYLE_SINGLE : LINESTYLE_NONE;
700 goto ATTR_SETUNDERLINE;
702 case RTF_ULD:
703 eUnderline = LINESTYLE_DOTTED;
704 goto ATTR_SETUNDERLINE;
705 case RTF_ULDASH:
706 eUnderline = LINESTYLE_DASH;
707 goto ATTR_SETUNDERLINE;
708 case RTF_ULDASHD:
709 eUnderline = LINESTYLE_DASHDOT;
710 goto ATTR_SETUNDERLINE;
711 case RTF_ULDASHDD:
712 eUnderline = LINESTYLE_DASHDOTDOT;
713 goto ATTR_SETUNDERLINE;
714 case RTF_ULDB:
715 eUnderline = LINESTYLE_DOUBLE;
716 goto ATTR_SETUNDERLINE;
717 case RTF_ULNONE:
718 eUnderline = LINESTYLE_NONE;
719 goto ATTR_SETUNDERLINE;
720 case RTF_ULTH:
721 eUnderline = LINESTYLE_BOLD;
722 goto ATTR_SETUNDERLINE;
723 case RTF_ULWAVE:
724 eUnderline = LINESTYLE_WAVE;
725 goto ATTR_SETUNDERLINE;
726 case RTF_ULTHD:
727 eUnderline = LINESTYLE_BOLDDOTTED;
728 goto ATTR_SETUNDERLINE;
729 case RTF_ULTHDASH:
730 eUnderline = LINESTYLE_BOLDDASH;
731 goto ATTR_SETUNDERLINE;
732 case RTF_ULLDASH:
733 eUnderline = LINESTYLE_LONGDASH;
734 goto ATTR_SETUNDERLINE;
735 case RTF_ULTHLDASH:
736 eUnderline = LINESTYLE_BOLDLONGDASH;
737 goto ATTR_SETUNDERLINE;
738 case RTF_ULTHDASHD:
739 eUnderline = LINESTYLE_BOLDDASHDOT;
740 goto ATTR_SETUNDERLINE;
741 case RTF_ULTHDASHDD:
742 eUnderline = LINESTYLE_BOLDDASHDOTDOT;
743 goto ATTR_SETUNDERLINE;
744 case RTF_ULHWAVE:
745 eUnderline = LINESTYLE_BOLDWAVE;
746 goto ATTR_SETUNDERLINE;
747 case RTF_ULULDBWAVE:
748 eUnderline = LINESTYLE_DOUBLEWAVE;
749 goto ATTR_SETUNDERLINE;
751 case RTF_ULW:
752 eUnderline = LINESTYLE_SINGLE;
754 if( aPlainMap.nWordlineMode )
756 pSet->Put( SvxWordLineModeItem( true, aPlainMap.nWordlineMode ));
758 goto ATTR_SETUNDERLINE;
760 ATTR_SETUNDERLINE:
761 if( aPlainMap.nUnderline )
763 pSet->Put( SvxUnderlineItem( eUnderline, aPlainMap.nUnderline ));
765 break;
767 case RTF_ULC:
768 if( aPlainMap.nUnderline )
770 std::unique_ptr<SvxUnderlineItem> aUL(std::make_unique<SvxUnderlineItem>(LINESTYLE_SINGLE, aPlainMap.nUnderline));
771 const SfxPoolItem* pItem(nullptr);
773 if( SfxItemState::SET == pSet->GetItemState(aPlainMap.nUnderline, false, &pItem ) )
775 // is switched off ?
776 if( LINESTYLE_NONE == static_cast<const SvxUnderlineItem*>(pItem)->GetLineStyle() )
777 break;
779 aUL.reset(static_cast<SvxUnderlineItem*>(pItem->Clone()));
781 else
783 aUL.reset(static_cast<SvxUnderlineItem*>(pSet->Get( aPlainMap.nUnderline, false).Clone()));
786 if(LINESTYLE_NONE == aUL->GetLineStyle())
788 aUL->SetLineStyle(LINESTYLE_SINGLE);
791 aUL->SetColor(GetColor(sal_uInt16(nTokenValue)));
793 pSet->Put(*aUL);
795 break;
797 case RTF_OL:
798 if( !IsAttrSttPos() )
799 break;
800 eOverline = nTokenValue ? LINESTYLE_SINGLE : LINESTYLE_NONE;
801 goto ATTR_SETOVERLINE;
803 case RTF_OLD:
804 eOverline = LINESTYLE_DOTTED;
805 goto ATTR_SETOVERLINE;
806 case RTF_OLDASH:
807 eOverline = LINESTYLE_DASH;
808 goto ATTR_SETOVERLINE;
809 case RTF_OLDASHD:
810 eOverline = LINESTYLE_DASHDOT;
811 goto ATTR_SETOVERLINE;
812 case RTF_OLDASHDD:
813 eOverline = LINESTYLE_DASHDOTDOT;
814 goto ATTR_SETOVERLINE;
815 case RTF_OLDB:
816 eOverline = LINESTYLE_DOUBLE;
817 goto ATTR_SETOVERLINE;
818 case RTF_OLNONE:
819 eOverline = LINESTYLE_NONE;
820 goto ATTR_SETOVERLINE;
821 case RTF_OLTH:
822 eOverline = LINESTYLE_BOLD;
823 goto ATTR_SETOVERLINE;
824 case RTF_OLWAVE:
825 eOverline = LINESTYLE_WAVE;
826 goto ATTR_SETOVERLINE;
827 case RTF_OLTHD:
828 eOverline = LINESTYLE_BOLDDOTTED;
829 goto ATTR_SETOVERLINE;
830 case RTF_OLTHDASH:
831 eOverline = LINESTYLE_BOLDDASH;
832 goto ATTR_SETOVERLINE;
833 case RTF_OLLDASH:
834 eOverline = LINESTYLE_LONGDASH;
835 goto ATTR_SETOVERLINE;
836 case RTF_OLTHLDASH:
837 eOverline = LINESTYLE_BOLDLONGDASH;
838 goto ATTR_SETOVERLINE;
839 case RTF_OLTHDASHD:
840 eOverline = LINESTYLE_BOLDDASHDOT;
841 goto ATTR_SETOVERLINE;
842 case RTF_OLTHDASHDD:
843 eOverline = LINESTYLE_BOLDDASHDOTDOT;
844 goto ATTR_SETOVERLINE;
845 case RTF_OLHWAVE:
846 eOverline = LINESTYLE_BOLDWAVE;
847 goto ATTR_SETOVERLINE;
848 case RTF_OLOLDBWAVE:
849 eOverline = LINESTYLE_DOUBLEWAVE;
850 goto ATTR_SETOVERLINE;
852 case RTF_OLW:
853 eOverline = LINESTYLE_SINGLE;
855 if( aPlainMap.nWordlineMode )
857 pSet->Put( SvxWordLineModeItem( true, aPlainMap.nWordlineMode ));
859 goto ATTR_SETOVERLINE;
861 ATTR_SETOVERLINE:
862 if( aPlainMap.nUnderline )
864 pSet->Put( SvxOverlineItem( eOverline, aPlainMap.nOverline ));
866 break;
868 case RTF_OLC:
869 if( aPlainMap.nOverline )
871 std::unique_ptr<SvxOverlineItem> aOL(std::make_unique<SvxOverlineItem>(LINESTYLE_SINGLE, aPlainMap.nOverline));
872 const SfxPoolItem* pItem(nullptr);
874 if( SfxItemState::SET == pSet->GetItemState(aPlainMap.nOverline, false, &pItem ) )
876 // is switched off ?
877 if( LINESTYLE_NONE == static_cast<const SvxOverlineItem*>(pItem)->GetLineStyle() )
878 break;
880 aOL.reset(static_cast<SvxOverlineItem*>(pItem->Clone()));
882 else
884 aOL.reset(static_cast<SvxOverlineItem*>(pSet->Get( aPlainMap.nOverline, false).Clone()));
887 if(LINESTYLE_NONE == aOL->GetLineStyle())
889 aOL->SetLineStyle(LINESTYLE_SINGLE);
892 aOL->SetColor(GetColor(sal_uInt16(nTokenValue)));
894 pSet->Put(*aOL);
896 break;
898 case RTF_UP:
899 case RTF_SUPER:
900 if( aPlainMap.nEscapement )
902 const sal_uInt16 nEsc = aPlainMap.nEscapement;
903 if( -1 == nTokenValue )
904 nTokenValue = 6; //RTF default \up value in half-points
905 if( IsCalcValue() )
906 CalcValue();
907 const SvxEscapementItem& rOld =
908 static_cast<const SvxEscapementItem&>(pSet->Get( nEsc,false));
909 sal_Int16 nEs;
910 sal_uInt8 nProp;
911 if( DFLT_ESC_AUTO_SUB == rOld.GetEsc() )
913 nEs = DFLT_ESC_AUTO_SUPER;
914 nProp = rOld.GetProportionalHeight();
916 else
918 nEs = (nToken == RTF_SUPER) ? DFLT_ESC_AUTO_SUPER : nTokenValue;
919 nProp = (nToken == RTF_SUPER) ? DFLT_ESC_PROP : 100;
921 pSet->Put( SvxEscapementItem( nEs, nProp, nEsc ));
923 break;
925 case RTF_CF:
926 if( aPlainMap.nColor )
928 pSet->Put( SvxColorItem( GetColor( sal_uInt16(nTokenValue) ),
929 aPlainMap.nColor ));
931 break;
932 //#i12501# While cb is clearly documented in the rtf spec, word
933 //doesn't accept it at all
934 #if 0
935 case RTF_CB:
936 if( aPlainMap.nBgColor )
938 pSet->Put( SvxBrushItem( GetColor( sal_uInt16(nTokenValue) ),
939 aPlainMap.nBgColor ));
941 break;
942 #endif
944 case RTF_LANG:
945 if( aPlainMap.nLanguage )
947 pSet->Put( SvxLanguageItem( LanguageType(nTokenValue),
948 aPlainMap.nLanguage ));
950 break;
952 case RTF_LANGFE:
953 if( aPlainMap.nCJKLanguage )
955 pSet->Put( SvxLanguageItem( LanguageType(nTokenValue),
956 aPlainMap.nCJKLanguage ));
958 break;
959 case RTF_ALANG:
961 SvxLanguageItem aTmpItem( LanguageType(nTokenValue),
962 SID_ATTR_CHAR_LANGUAGE );
963 SetScriptAttr( eCharType, *pSet, aTmpItem );
965 break;
967 case RTF_RTLCH:
968 bIsLeftToRightDef = false;
969 break;
970 case RTF_LTRCH:
971 bIsLeftToRightDef = true;
972 break;
973 case RTF_RTLPAR:
974 if (aPardMap.nDirection)
976 pSet->Put(SvxFrameDirectionItem(SvxFrameDirection::Horizontal_RL_TB,
977 aPardMap.nDirection));
979 break;
980 case RTF_LTRPAR:
981 if (aPardMap.nDirection)
983 pSet->Put(SvxFrameDirectionItem(SvxFrameDirection::Horizontal_LR_TB,
984 aPardMap.nDirection));
986 break;
987 case RTF_LOCH: eCharType = LOW_CHARTYPE; break;
988 case RTF_HICH: eCharType = HIGH_CHARTYPE; break;
989 case RTF_DBCH: eCharType = DOUBLEBYTE_CHARTYPE; break;
992 case RTF_ACCNONE:
993 eEmphasis = FontEmphasisMark::NONE;
994 goto ATTR_SETEMPHASIS;
995 case RTF_ACCDOT:
996 eEmphasis = (FontEmphasisMark::Dot | FontEmphasisMark::PosAbove);
997 goto ATTR_SETEMPHASIS;
999 case RTF_ACCCOMMA:
1000 eEmphasis = (FontEmphasisMark::Accent | FontEmphasisMark::PosAbove);
1001 ATTR_SETEMPHASIS:
1002 if( aPlainMap.nEmphasis )
1004 pSet->Put( SvxEmphasisMarkItem( eEmphasis,
1005 aPlainMap.nEmphasis ));
1007 break;
1009 case RTF_TWOINONE:
1010 if( aPlainMap.nTwoLines )
1012 sal_Unicode cStt, cEnd;
1013 switch ( nTokenValue )
1015 case 1: cStt = '('; cEnd = ')'; break;
1016 case 2: cStt = '['; cEnd = ']'; break;
1017 case 3: cStt = '<'; cEnd = '>'; break;
1018 case 4: cStt = '{'; cEnd = '}'; break;
1019 default: cStt = 0; cEnd = 0; break;
1022 pSet->Put( SvxTwoLinesItem( true, cStt, cEnd,
1023 aPlainMap.nTwoLines ));
1025 break;
1027 case RTF_CHARSCALEX :
1028 if (aPlainMap.nCharScaleX)
1030 //i21372
1031 if (nTokenValue < 1 || nTokenValue > 600)
1032 nTokenValue = 100;
1033 pSet->Put( SvxCharScaleWidthItem( sal_uInt16(nTokenValue),
1034 aPlainMap.nCharScaleX ));
1036 break;
1038 case RTF_HORZVERT:
1039 if( aPlainMap.nHorzVert )
1041 // RTF knows only 90deg
1042 pSet->Put( SvxCharRotateItem( Degree10(900), 1 == nTokenValue,
1043 aPlainMap.nHorzVert ));
1045 break;
1047 case RTF_EMBO:
1048 if (aPlainMap.nRelief)
1050 pSet->Put(SvxCharReliefItem(FontRelief::Embossed,
1051 aPlainMap.nRelief));
1053 break;
1054 case RTF_IMPR:
1055 if (aPlainMap.nRelief)
1057 pSet->Put(SvxCharReliefItem(FontRelief::Engraved,
1058 aPlainMap.nRelief));
1060 break;
1061 case RTF_V:
1062 if (aPlainMap.nHidden)
1064 pSet->Put(SvxCharHiddenItem(nTokenValue != 0,
1065 aPlainMap.nHidden));
1067 break;
1068 case RTF_CHBGFDIAG:
1069 case RTF_CHBGDKVERT:
1070 case RTF_CHBGDKHORIZ:
1071 case RTF_CHBGVERT:
1072 case RTF_CHBGHORIZ:
1073 case RTF_CHBGDKFDIAG:
1074 case RTF_CHBGDCROSS:
1075 case RTF_CHBGCROSS:
1076 case RTF_CHBGBDIAG:
1077 case RTF_CHBGDKDCROSS:
1078 case RTF_CHBGDKCROSS:
1079 case RTF_CHBGDKBDIAG:
1080 case RTF_CHCBPAT:
1081 case RTF_CHCFPAT:
1082 case RTF_CHSHDNG:
1083 if( aPlainMap.nBgColor )
1084 ReadBackgroundAttr( nToken, *pSet );
1085 break;
1087 case BRACELEFT:
1089 // tests on Swg internal tokens
1090 bool bHandled = false;
1091 short nSkip = 0;
1092 if( RTF_IGNOREFLAG != GetNextToken())
1093 nSkip = -1;
1094 else if( (nToken = GetNextToken() ) & RTF_SWGDEFS )
1096 bHandled = true;
1097 switch( nToken )
1099 case RTF_PGDSCNO:
1100 case RTF_PGBRK:
1101 case RTF_SOUTLVL:
1102 UnknownAttrToken( nToken );
1103 // overwrite the closing parenthesis
1104 break;
1106 case RTF_SWG_ESCPROP:
1108 // Store percentage change!
1109 sal_uInt8 nProp = sal_uInt8( nTokenValue / 100 );
1110 short nEsc = 0;
1111 if( 1 == ( nTokenValue % 100 ))
1112 // Recognize own auto-flags!
1113 nEsc = DFLT_ESC_AUTO_SUPER;
1115 if( aPlainMap.nEscapement )
1116 pSet->Put( SvxEscapementItem( nEsc, nProp,
1117 aPlainMap.nEscapement ));
1119 break;
1121 case RTF_HYPHEN:
1123 SvxHyphenZoneItem aHypenZone(
1124 (nTokenValue & 1) != 0,
1125 aPardMap.nHyphenzone );
1126 aHypenZone.SetPageEnd((nTokenValue & 2) != 0);
1128 if( aPardMap.nHyphenzone &&
1129 RTF_HYPHLEAD == GetNextToken() &&
1130 RTF_HYPHTRAIL == GetNextToken() &&
1131 RTF_HYPHMAX == GetNextToken() )
1133 aHypenZone.GetMinLead() =
1134 sal_uInt8(GetStackPtr( -2 )->nTokenValue);
1135 aHypenZone.GetMinTrail() =
1136 sal_uInt8(GetStackPtr( -1 )->nTokenValue);
1137 aHypenZone.GetMaxHyphens() =
1138 sal_uInt8(nTokenValue);
1140 pSet->Put( aHypenZone );
1142 else
1143 SkipGroup(); // at the end of the group
1145 break;
1147 case RTF_SHADOW:
1149 bool bSkip = true;
1150 do { // middle check loop
1151 SvxShadowLocation eSL = SvxShadowLocation( nTokenValue );
1152 if( RTF_SHDW_DIST != GetNextToken() )
1153 break;
1154 sal_uInt16 nDist = sal_uInt16( nTokenValue );
1156 if( RTF_SHDW_STYLE != GetNextToken() )
1157 break;
1159 if( RTF_SHDW_COL != GetNextToken() )
1160 break;
1161 sal_uInt16 nCol = sal_uInt16( nTokenValue );
1163 if( RTF_SHDW_FCOL != GetNextToken() )
1164 break;
1166 Color aColor = GetColor( nCol );
1168 if( aPardMap.nShadow )
1169 pSet->Put( SvxShadowItem( aPardMap.nShadow,
1170 &aColor, nDist, eSL ) );
1172 bSkip = false;
1173 } while( false );
1175 if( bSkip )
1176 SkipGroup(); // at the end of the group
1178 break;
1180 default:
1181 bHandled = false;
1182 if( (nToken & ~(0xff | RTF_SWGDEFS)) == RTF_TABSTOPDEF )
1184 nToken = SkipToken( -2 );
1185 ReadTabAttr( nToken, *pSet );
1188 cmc: #i76140, he who consumed the { must consume the }
1189 We rewound to a state of { being the current
1190 token so it is our responsibility to consume the }
1191 token if we consumed the {. We will not have consumed
1192 the { if it belonged to our caller, i.e. if the { we
1193 are handling is the "firsttoken" passed to us then
1194 the *caller* must consume it, not us. Otherwise *we*
1195 should consume it.
1197 if (nToken == BRACELEFT && !bFirstToken)
1199 nToken = GetNextToken();
1200 SAL_WARN_IF( nToken != BRACERIGHT,
1201 "editeng",
1202 "} did not follow { as expected");
1205 else if( (nToken & ~(0xff| RTF_SWGDEFS)) == RTF_BRDRDEF)
1207 nToken = SkipToken( -2 );
1208 ReadBorderAttr( nToken, *pSet );
1210 else // so no more attribute
1211 nSkip = -2;
1212 break;
1215 #if 1
1217 cmc: #i4727# / #i12713# Who owns this closing bracket?
1218 If we read the opening one, we must read this one, if
1219 other is counting the brackets so as to push/pop off
1220 the correct environment then we will have pushed a new
1221 environment for the start { of this, but will not see
1222 the } and so is out of sync for the rest of the
1223 document.
1225 if (bHandled && !bFirstToken)
1226 GetNextToken();
1227 #endif
1229 else
1230 nSkip = -2;
1232 if( nSkip ) // all completely unknown
1234 if (!bFirstToken)
1235 --nSkip; // BRACELEFT: is the next token
1236 SkipToken( nSkip );
1237 bContinue = false;
1240 break;
1241 default:
1242 if( (nToken & ~0xff ) == RTF_TABSTOPDEF )
1243 ReadTabAttr( nToken, *pSet );
1244 else if( (nToken & ~0xff ) == RTF_BRDRDEF )
1245 ReadBorderAttr( nToken, *pSet );
1246 else if( (nToken & ~0xff ) == RTF_SHADINGDEF )
1247 ReadBackgroundAttr( nToken, *pSet );
1248 else
1250 // unknown token, so token "returned in Parser"
1251 if( !bFirstToken )
1252 SkipToken();
1253 bContinue = false;
1257 if( bContinue )
1259 nToken = GetNextToken();
1261 bFirstToken = false;
1265 void SvxRTFParser::ReadTabAttr( int nToken, SfxItemSet& rSet )
1267 bool bMethodOwnsToken = false; // #i52542# patch from cmc.
1268 // then read all the TabStops
1269 SvxTabStop aTabStop;
1270 SvxTabStopItem aAttr( 0, 0, SvxTabAdjust::Default, aPardMap.nTabStop );
1271 bool bContinue = true;
1272 do {
1273 switch( nToken )
1275 case RTF_TB: // BarTab ???
1276 case RTF_TX:
1278 if( IsCalcValue() )
1279 CalcValue();
1280 aTabStop.GetTabPos() = nTokenValue;
1281 aAttr.Insert( aTabStop );
1282 aTabStop = SvxTabStop(); // all values default
1284 break;
1286 case RTF_TQL:
1287 aTabStop.GetAdjustment() = SvxTabAdjust::Left;
1288 break;
1289 case RTF_TQR:
1290 aTabStop.GetAdjustment() = SvxTabAdjust::Right;
1291 break;
1292 case RTF_TQC:
1293 aTabStop.GetAdjustment() = SvxTabAdjust::Center;
1294 break;
1295 case RTF_TQDEC:
1296 aTabStop.GetAdjustment() = SvxTabAdjust::Decimal;
1297 break;
1299 case RTF_TLDOT: aTabStop.GetFill() = '.'; break;
1300 case RTF_TLHYPH: aTabStop.GetFill() = ' '; break;
1301 case RTF_TLUL: aTabStop.GetFill() = '_'; break;
1302 case RTF_TLTH: aTabStop.GetFill() = '-'; break;
1303 case RTF_TLEQ: aTabStop.GetFill() = '='; break;
1305 case BRACELEFT:
1307 // Swg - control BRACELEFT RTF_IGNOREFLAG RTF_TLSWG BRACERIGHT
1308 short nSkip = 0;
1309 if( RTF_IGNOREFLAG != GetNextToken() )
1310 nSkip = -1;
1311 else if( RTF_TLSWG != ( nToken = GetNextToken() ))
1312 nSkip = -2;
1313 else
1315 aTabStop.GetDecimal() = sal_uInt8(nTokenValue & 0xff);
1316 aTabStop.GetFill() = sal_uInt8((nTokenValue >> 8) & 0xff);
1317 // overwrite the closing parenthesis
1318 if (bMethodOwnsToken)
1319 GetNextToken();
1321 if( nSkip )
1323 SkipToken( nSkip ); // Ignore back again
1324 bContinue = false;
1327 break;
1329 default:
1330 bContinue = false;
1332 if( bContinue )
1334 nToken = GetNextToken();
1335 bMethodOwnsToken = true;
1337 } while( bContinue );
1339 // Fill with defaults is still missing!
1340 rSet.Put( aAttr );
1341 SkipToken();
1344 static void SetBorderLine( int nBorderTyp, SvxBoxItem& rItem,
1345 const SvxBorderLine& rBorder )
1347 switch( nBorderTyp )
1349 case RTF_BOX: // run through all levels
1350 case RTF_BRDRT:
1351 rItem.SetLine( &rBorder, SvxBoxItemLine::TOP );
1352 if( RTF_BOX != nBorderTyp )
1353 return;
1354 [[fallthrough]];
1355 case RTF_BRDRB:
1356 rItem.SetLine( &rBorder, SvxBoxItemLine::BOTTOM );
1357 if( RTF_BOX != nBorderTyp )
1358 return;
1359 [[fallthrough]];
1360 case RTF_BRDRL:
1361 rItem.SetLine( &rBorder, SvxBoxItemLine::LEFT );
1362 if( RTF_BOX != nBorderTyp )
1363 return;
1364 [[fallthrough]];
1365 case RTF_BRDRR:
1366 rItem.SetLine( &rBorder, SvxBoxItemLine::RIGHT );
1367 if( RTF_BOX != nBorderTyp )
1368 return;
1372 void SvxRTFParser::ReadBorderAttr( int nToken, SfxItemSet& rSet,
1373 bool bTableDef )
1375 // then read the border attribute
1376 std::unique_ptr<SvxBoxItem> aAttr(std::make_unique<SvxBoxItem>(aPardMap.nBox));
1377 const SfxPoolItem* pItem(nullptr);
1379 if( SfxItemState::SET == rSet.GetItemState( aPardMap.nBox, false, &pItem ) )
1381 aAttr.reset(static_cast<SvxBoxItem*>(pItem->Clone()));
1384 SvxBorderLine aBrd( nullptr, DEF_LINE_WIDTH_0 ); // Simple plain line
1385 bool bContinue = true;
1386 int nBorderTyp = 0;
1388 tools::Long nWidth = 1;
1389 bool bDoubleWidth = false;
1391 do {
1392 switch( nToken )
1394 case RTF_BOX:
1395 case RTF_BRDRT:
1396 case RTF_BRDRB:
1397 case RTF_BRDRL:
1398 case RTF_BRDRR:
1399 nBorderTyp = nToken;
1400 break;
1402 case RTF_CLBRDRT: // Cell top border
1404 if( bTableDef )
1406 if (nBorderTyp != 0)
1407 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1408 nBorderTyp = RTF_BRDRT;
1410 break;
1412 case RTF_CLBRDRB: // Cell bottom border
1414 if( bTableDef )
1416 if (nBorderTyp != 0)
1417 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1418 nBorderTyp = RTF_BRDRB;
1420 break;
1422 case RTF_CLBRDRL: // Cell left border
1424 if( bTableDef )
1426 if (nBorderTyp != 0)
1427 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1428 nBorderTyp = RTF_BRDRL;
1430 break;
1432 case RTF_CLBRDRR: // Cell right border
1434 if( bTableDef )
1436 if (nBorderTyp != 0)
1437 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1438 nBorderTyp = RTF_BRDRR;
1440 break;
1443 case RTF_BRDRDOT: // dotted border
1444 aBrd.SetBorderLineStyle(SvxBorderLineStyle::DOTTED);
1445 break;
1446 case RTF_BRDRDASH: // dashed border
1447 aBrd.SetBorderLineStyle(SvxBorderLineStyle::DASHED);
1448 break;
1449 case RTF_BRDRHAIR: // hairline border
1451 aBrd.SetBorderLineStyle( SvxBorderLineStyle::SOLID);
1452 aBrd.SetWidth( DEF_LINE_WIDTH_0 );
1454 break;
1455 case RTF_BRDRDB: // Double border
1456 aBrd.SetBorderLineStyle(SvxBorderLineStyle::DOUBLE);
1457 break;
1458 case RTF_BRDRINSET: // inset border
1459 aBrd.SetBorderLineStyle(SvxBorderLineStyle::INSET);
1460 break;
1461 case RTF_BRDROUTSET: // outset border
1462 aBrd.SetBorderLineStyle(SvxBorderLineStyle::OUTSET);
1463 break;
1464 case RTF_BRDRTNTHSG: // ThinThick Small gap
1465 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THINTHICK_SMALLGAP);
1466 break;
1467 case RTF_BRDRTNTHMG: // ThinThick Medium gap
1468 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THINTHICK_MEDIUMGAP);
1469 break;
1470 case RTF_BRDRTNTHLG: // ThinThick Large gap
1471 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THINTHICK_LARGEGAP);
1472 break;
1473 case RTF_BRDRTHTNSG: // ThickThin Small gap
1474 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THICKTHIN_SMALLGAP);
1475 break;
1476 case RTF_BRDRTHTNMG: // ThickThin Medium gap
1477 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THICKTHIN_MEDIUMGAP);
1478 break;
1479 case RTF_BRDRTHTNLG: // ThickThin Large gap
1480 aBrd.SetBorderLineStyle(SvxBorderLineStyle::THICKTHIN_LARGEGAP);
1481 break;
1482 case RTF_BRDREMBOSS: // Embossed border
1483 aBrd.SetBorderLineStyle(SvxBorderLineStyle::EMBOSSED);
1484 break;
1485 case RTF_BRDRENGRAVE: // Engraved border
1486 aBrd.SetBorderLineStyle(SvxBorderLineStyle::ENGRAVED);
1487 break;
1489 case RTF_BRDRS: // single thickness border
1490 bDoubleWidth = false;
1491 break;
1492 case RTF_BRDRTH: // double thickness border width*2
1493 bDoubleWidth = true;
1494 break;
1495 case RTF_BRDRW: // border width <255
1496 nWidth = nTokenValue;
1497 break;
1499 case RTF_BRDRCF: // Border color
1500 aBrd.SetColor( GetColor( sal_uInt16(nTokenValue) ) );
1501 break;
1503 case RTF_BRDRSH: // Shadowed border
1504 rSet.Put( SvxShadowItem( aPardMap.nShadow, nullptr, 60 /*3pt*/,
1505 SvxShadowLocation::BottomRight ) );
1506 break;
1508 case RTF_BRSP: // Spacing to content in twip
1510 switch( nBorderTyp )
1512 case RTF_BRDRB:
1513 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::BOTTOM );
1514 break;
1516 case RTF_BRDRT:
1517 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::TOP );
1518 break;
1520 case RTF_BRDRL:
1521 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::LEFT );
1522 break;
1524 case RTF_BRDRR:
1525 aAttr->SetDistance( static_cast<sal_uInt16>(nTokenValue), SvxBoxItemLine::RIGHT );
1526 break;
1528 case RTF_BOX:
1529 aAttr->SetAllDistances( static_cast<sal_uInt16>(nTokenValue) );
1530 break;
1533 break;
1535 case RTF_BRDRBTW: // Border formatting group
1536 case RTF_BRDRBAR: // Border outside
1537 // TODO unhandled ATM
1538 break;
1540 default:
1541 bContinue = (nToken & ~(0xff| RTF_SWGDEFS)) == RTF_BRDRDEF;
1543 if( bContinue )
1544 nToken = GetNextToken();
1545 } while( bContinue );
1547 // Finally compute the border width
1548 if ( bDoubleWidth ) nWidth *= 2;
1549 aBrd.SetWidth( nWidth );
1551 SetBorderLine( nBorderTyp, *aAttr, aBrd );
1553 rSet.Put( *aAttr );
1554 SkipToken();
1557 static sal_uInt32 CalcShading( sal_uInt32 nColor, sal_uInt32 nFillColor, sal_uInt8 nShading )
1559 nColor = (nColor * nShading) / 100;
1560 nFillColor = (nFillColor * ( 100 - nShading )) / 100;
1561 return nColor + nFillColor;
1564 void SvxRTFParser::ReadBackgroundAttr( int nToken, SfxItemSet& rSet,
1565 bool bTableDef )
1567 // then read the border attribute
1568 bool bContinue = true;
1569 sal_uInt16 nColor = USHRT_MAX, nFillColor = USHRT_MAX;
1570 sal_uInt8 nFillValue = 0;
1572 sal_uInt16 nWh = ( nToken & ~0xff ) == RTF_CHRFMT
1573 ? aPlainMap.nBgColor
1574 : aPardMap.nBrush;
1576 do {
1577 switch( nToken )
1579 case RTF_CLCBPAT:
1580 case RTF_CHCBPAT:
1581 case RTF_CBPAT:
1582 nFillColor = sal_uInt16( nTokenValue );
1583 break;
1585 case RTF_CLCFPAT:
1586 case RTF_CHCFPAT:
1587 case RTF_CFPAT:
1588 nColor = sal_uInt16( nTokenValue );
1589 break;
1591 case RTF_CLSHDNG:
1592 case RTF_CHSHDNG:
1593 case RTF_SHADING:
1594 nFillValue = static_cast<sal_uInt8>( nTokenValue / 100 );
1595 break;
1597 case RTF_CLBGDKHOR:
1598 case RTF_CHBGDKHORIZ:
1599 case RTF_BGDKHORIZ:
1600 case RTF_CLBGDKVERT:
1601 case RTF_CHBGDKVERT:
1602 case RTF_BGDKVERT:
1603 case RTF_CLBGDKBDIAG:
1604 case RTF_CHBGDKBDIAG:
1605 case RTF_BGDKBDIAG:
1606 case RTF_CLBGDKFDIAG:
1607 case RTF_CHBGDKFDIAG:
1608 case RTF_BGDKFDIAG:
1609 case RTF_CLBGDKCROSS:
1610 case RTF_CHBGDKCROSS:
1611 case RTF_BGDKCROSS:
1612 case RTF_CLBGDKDCROSS:
1613 case RTF_CHBGDKDCROSS:
1614 case RTF_BGDKDCROSS:
1615 // dark -> 60%
1616 nFillValue = 60;
1617 break;
1619 case RTF_CLBGHORIZ:
1620 case RTF_CHBGHORIZ:
1621 case RTF_BGHORIZ:
1622 case RTF_CLBGVERT:
1623 case RTF_CHBGVERT:
1624 case RTF_BGVERT:
1625 case RTF_CLBGBDIAG:
1626 case RTF_CHBGBDIAG:
1627 case RTF_BGBDIAG:
1628 case RTF_CLBGFDIAG:
1629 case RTF_CHBGFDIAG:
1630 case RTF_BGFDIAG:
1631 case RTF_CLBGCROSS:
1632 case RTF_CHBGCROSS:
1633 case RTF_BGCROSS:
1634 case RTF_CLBGDCROSS:
1635 case RTF_CHBGDCROSS:
1636 case RTF_BGDCROSS:
1637 // light -> 20%
1638 nFillValue = 20;
1639 break;
1641 default:
1642 if( bTableDef )
1643 bContinue = (nToken & ~(0xff | RTF_TABLEDEF) ) == RTF_SHADINGDEF;
1644 else
1645 bContinue = (nToken & ~0xff) == RTF_SHADINGDEF;
1647 if( bContinue )
1648 nToken = GetNextToken();
1649 } while( bContinue );
1651 Color aCol( COL_WHITE ), aFCol;
1652 if( !nFillValue )
1654 // there was only one of two colors specified or no BrushType
1655 if( USHRT_MAX != nFillColor )
1657 nFillValue = 100;
1658 aCol = GetColor( nFillColor );
1660 else if( USHRT_MAX != nColor )
1661 aFCol = GetColor( nColor );
1663 else
1665 if( USHRT_MAX != nColor )
1666 aCol = GetColor( nColor );
1667 else
1668 aCol = COL_BLACK;
1670 if( USHRT_MAX != nFillColor )
1671 aFCol = GetColor( nFillColor );
1672 else
1673 aFCol = COL_WHITE;
1676 Color aColor;
1677 if( 0 == nFillValue || 100 == nFillValue )
1678 aColor = aCol;
1679 else
1680 aColor = Color(
1681 static_cast<sal_uInt8>(CalcShading( aCol.GetRed(), aFCol.GetRed(), nFillValue )),
1682 static_cast<sal_uInt8>(CalcShading( aCol.GetGreen(), aFCol.GetGreen(), nFillValue )),
1683 static_cast<sal_uInt8>(CalcShading( aCol.GetBlue(), aFCol.GetBlue(), nFillValue )) );
1685 rSet.Put( SvxBrushItem( aColor, nWh ) );
1686 SkipToken();
1690 // pard / plain handling
1691 void SvxRTFParser::RTFPardPlain( bool const bPard, SfxItemSet** ppSet )
1693 if( bNewGroup || aAttrStack.empty() ) // not at the beginning of a new group
1694 return;
1696 SvxRTFItemStackType* pCurrent = aAttrStack.back().get();
1698 int nLastToken = GetStackPtr(-1)->nTokenId;
1699 bool bNewStkEntry = true;
1700 if( RTF_PARD != nLastToken &&
1701 RTF_PLAIN != nLastToken &&
1702 BRACELEFT != nLastToken )
1704 if (pCurrent->aAttrSet.Count() || pCurrent->m_pChildList || pCurrent->nStyleNo)
1706 // open a new group
1707 std::unique_ptr<SvxRTFItemStackType> pNew(new SvxRTFItemStackType( *pCurrent, *pInsPos, true ));
1708 pNew->SetRTFDefaults( GetRTFDefaults() );
1710 // Set all until here valid attributes
1711 AttrGroupEnd();
1712 pCurrent = aAttrStack.empty() ? nullptr : aAttrStack.back().get(); // can be changed after AttrGroupEnd!
1713 pNew->aAttrSet.SetParent( pCurrent ? &pCurrent->aAttrSet : nullptr );
1714 aAttrStack.push_back( std::move(pNew) );
1715 pCurrent = aAttrStack.back().get();
1717 else
1719 // continue to use this entry as new
1720 pCurrent->SetStartPos( *pInsPos );
1721 bNewStkEntry = false;
1725 // now reset all to default
1726 if( bNewStkEntry &&
1727 ( pCurrent->aAttrSet.GetParent() || pCurrent->aAttrSet.Count() ))
1729 const SfxPoolItem *pItem, *pDef;
1730 const sal_uInt16* pPtr;
1731 sal_uInt16 nCnt;
1732 const SfxItemSet* pDfltSet = &GetRTFDefaults();
1733 if( bPard )
1735 pCurrent->nStyleNo = 0;
1736 pPtr = reinterpret_cast<sal_uInt16*>(&aPardMap);
1737 nCnt = sizeof(aPardMap) / sizeof(sal_uInt16);
1739 else
1741 pPtr = reinterpret_cast<sal_uInt16*>(&aPlainMap);
1742 nCnt = sizeof(aPlainMap) / sizeof(sal_uInt16);
1745 for( sal_uInt16 n = 0; n < nCnt; ++n, ++pPtr )
1747 // Item set and different -> Set the Default Pool
1748 if( !*pPtr )
1750 else if (SfxItemPool::IsSlot(*pPtr))
1751 pCurrent->aAttrSet.ClearItem( *pPtr );
1752 else if( IsChkStyleAttr() )
1753 pCurrent->aAttrSet.Put( pDfltSet->Get( *pPtr ) );
1754 else if( !pCurrent->aAttrSet.GetParent() )
1756 if( SfxItemState::SET ==
1757 pDfltSet->GetItemState( *pPtr, false, &pDef ))
1758 pCurrent->aAttrSet.Put( *pDef );
1759 else
1760 pCurrent->aAttrSet.ClearItem( *pPtr );
1762 else if( SfxItemState::SET == pCurrent->aAttrSet.GetParent()->
1763 GetItemState( *pPtr, true, &pItem ) &&
1764 *( pDef = &pDfltSet->Get( *pPtr )) != *pItem )
1765 pCurrent->aAttrSet.Put( *pDef );
1766 else
1768 if( SfxItemState::SET ==
1769 pDfltSet->GetItemState( *pPtr, false, &pDef ))
1770 pCurrent->aAttrSet.Put( *pDef );
1771 else
1772 pCurrent->aAttrSet.ClearItem( *pPtr );
1776 else if( bPard )
1777 pCurrent->nStyleNo = 0; // reset Style number
1779 *ppSet = &pCurrent->aAttrSet;
1781 if (bPard)
1782 return;
1784 //Once we have a default font, then any text without a font specifier is
1785 //in the default font, and thus has the default font charset, otherwise
1786 //we can fall back to the ansicpg set codeset
1787 if (nDfltFont != -1)
1789 const vcl::Font& rSVFont = GetFont(sal_uInt16(nDfltFont));
1790 SetEncoding(rSVFont.GetCharSet());
1792 else
1793 SetEncoding(GetCodeSet());
1796 void SvxRTFParser::SetDefault( int nToken, int nValue )
1798 if( !bNewDoc )
1799 return;
1801 SfxItemSet aTmp( *pAttrPool, aWhichMap.data() );
1802 bool bOldFlag = bIsLeftToRightDef;
1803 bIsLeftToRightDef = true;
1804 switch( nToken )
1806 case RTF_ADEFF:
1807 bIsLeftToRightDef = false;
1808 [[fallthrough]];
1809 case RTF_DEFF:
1811 if( -1 == nValue )
1812 nValue = 0;
1813 const vcl::Font& rSVFont = GetFont( sal_uInt16(nValue) );
1814 SvxFontItem aTmpItem(
1815 rSVFont.GetFamilyType(), rSVFont.GetFamilyName(),
1816 rSVFont.GetStyleName(), rSVFont.GetPitch(),
1817 rSVFont.GetCharSet(), SID_ATTR_CHAR_FONT );
1818 SetScriptAttr( NOTDEF_CHARTYPE, aTmp, aTmpItem );
1820 break;
1822 case RTF_ADEFLANG:
1823 bIsLeftToRightDef = false;
1824 [[fallthrough]];
1825 case RTF_DEFLANG:
1826 // store default Language
1827 if( -1 != nValue )
1829 SvxLanguageItem aTmpItem( LanguageType(nValue), SID_ATTR_CHAR_LANGUAGE );
1830 SetScriptAttr( NOTDEF_CHARTYPE, aTmp, aTmpItem );
1832 break;
1834 case RTF_DEFTAB:
1835 if( aPardMap.nTabStop )
1837 // RTF defines 720 twips as default
1838 bIsSetDfltTab = true;
1839 if( -1 == nValue || !nValue )
1840 nValue = 720;
1842 // who would like to have no twips ...
1843 if( IsCalcValue() )
1845 nTokenValue = nValue;
1846 CalcValue();
1847 nValue = nTokenValue;
1850 // Calculate the ratio of default TabWidth / Tabs and
1851 // calculate the corresponding new number.
1852 // ?? how did one come up with 13 ??
1853 sal_uInt16 nTabCount = (SVX_TAB_DEFDIST * 13 ) / sal_uInt16(nValue);
1855 cmc, make sure we have at least one, or all hell breaks loose in
1856 everybody exporters, #i8247#
1858 if (nTabCount < 1)
1859 nTabCount = 1;
1861 // we want Defaulttabs
1862 SvxTabStopItem aNewTab( nTabCount, sal_uInt16(nValue),
1863 SvxTabAdjust::Default, aPardMap.nTabStop );
1864 while( nTabCount )
1865 const_cast<SvxTabStop&>(aNewTab[ --nTabCount ]).GetAdjustment() = SvxTabAdjust::Default;
1867 pAttrPool->SetPoolDefaultItem( aNewTab );
1869 break;
1871 bIsLeftToRightDef = bOldFlag;
1873 if( aTmp.Count() )
1875 SfxItemIter aIter( aTmp );
1876 const SfxPoolItem* pItem = aIter.GetCurItem();
1879 pAttrPool->SetPoolDefaultItem( *pItem );
1880 pItem = aIter.NextItem();
1881 } while (pItem);
1885 // default: no conversion, leaving everything in twips.
1886 void SvxRTFParser::CalcValue()
1890 // for tokens that are not evaluated in ReadAttr
1891 void SvxRTFParser::UnknownAttrToken( int )
1895 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */