update dev300-m58
[ooovba.git] / sw / source / filter / rtf / rtffld.cxx
blobfd5cec1233fa6edf1567847b1a2eb2fbbe564b4c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtffld.cxx,v $
10 * $Revision: 1.33 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
35 #include <ctype.h>
36 #include <hintids.hxx>
38 #ifndef _COM_SUN_STAR_I18N_SCRIPTTYPE_HDL_
39 #include <com/sun/star/i18n/ScriptType.hdl>
40 #endif
41 #ifndef _GRAPH_HXX //autogen
42 #include <vcl/graph.hxx>
43 #endif
44 #include <svtools/urihelper.hxx>
45 #include <svtools/rtftoken.h>
46 #include <svtools/zforlist.hxx>
47 #include <svx/fontitem.hxx>
48 #include <svx/fhgtitem.hxx>
49 #include <svx/langitem.hxx>
50 #include <svx/brkitem.hxx>
51 #include <fmtfld.hxx>
52 #include <fmtinfmt.hxx>
53 #include <swtypes.hxx>
54 #include <doc.hxx>
55 #include <pam.hxx>
56 #include <ndtxt.hxx>
57 #include <shellio.hxx>
58 #include <fldbas.hxx>
59 #include <swparrtf.hxx>
60 #include <txatbase.hxx>
61 #include <dbfld.hxx>
62 #include <usrfld.hxx>
63 #include <docufld.hxx>
64 #include <flddat.hxx>
65 #include <charfmt.hxx>
66 #ifndef _fmtruby_HXX
67 #include <fmtruby.hxx>
68 #endif
69 #include <breakit.hxx>
70 #include <reffld.hxx>
71 #include <SwStyleNameMapper.hxx>
72 #include <svx/charhiddenitem.hxx>
75 // bestimme, ob es sich um ein IMPORT/TOC - Feld handelt.
76 // return: 0 - weder noch,
77 // 1 - TOC
78 // 2 - IMPORT
79 // 3 - INDEX
80 enum RTF_FLD_TYPES {
81 RTFFLD_UNKNOWN = 0,
82 RTFFLD_TOC,
83 RTFFLD_IMPORT,
84 RTFFLD_INDEX,
85 RTFFLD_SYMBOL,
86 RTFFLD_PAGE,
87 RTFFLD_NUMPAGES,
88 RTFFLD_DATE,
89 RTFFLD_TIME,
90 RTFFLD_DATA,
91 RTFFLD_MERGEFLD,
92 RTFFLD_HYPERLINK,
93 RTFFLD_REF,
94 RTFFLD_PAGEREF,
95 RTFFLD_EQ,
96 RTFFLD_INCLUDETEXT
99 static RTF_FLD_TYPES _WhichFld( String& rName, String& rNext )
101 // Strings sind PascalStrings; Laenge steht an 1. Stellen, dadurch wird
102 // sich der Aufruf von strlen erspart!!!
103 sal_Char __READONLY_DATA sTOC[]= "\x03""toc";
104 sal_Char __READONLY_DATA sIMPORT[]= "\x06""import";
105 sal_Char __READONLY_DATA sINDEX[]= "\x05""index";
106 sal_Char __READONLY_DATA sSYMBOL[]= "\x06""symbol";
107 sal_Char __READONLY_DATA sPAGE[]= "\x04""page";
108 sal_Char __READONLY_DATA sNUMPAGES[]= "\x08""numpages";
109 sal_Char __READONLY_DATA sDATE[]= "\x04""date";
110 sal_Char __READONLY_DATA sTIME[]= "\x04""time";
111 sal_Char __READONLY_DATA sDATA[]= "\x04""data";
112 sal_Char __READONLY_DATA sMERGEFLD[]= "\x0A""mergefield";
113 sal_Char __READONLY_DATA sIMPORT2[]= "\x0E""includepicture";
114 sal_Char __READONLY_DATA sHYPERLINK[]= "\x09""hyperlink";
115 sal_Char __READONLY_DATA sREF[]= "\x03""ref";
116 sal_Char __READONLY_DATA sPAGEREF[]= "\x07""pageref";
117 sal_Char __READONLY_DATA sEQ[]= "\x02""eq";
118 sal_Char __READONLY_DATA sINCLUDETEXT[]="\x0B""includetext";
120 struct _Dummy_RTF_FLD_TYPES
122 RTF_FLD_TYPES eFldType;
123 const sal_Char* pFldNm;
125 __READONLY_DATA _Dummy_RTF_FLD_TYPES aFldNmArr[RTFFLD_INCLUDETEXT + 1] = {
126 {RTFFLD_TOC, sTOC},
127 {RTFFLD_IMPORT, sIMPORT},
128 {RTFFLD_INDEX, sINDEX},
129 {RTFFLD_SYMBOL, sSYMBOL},
130 {RTFFLD_PAGE, sPAGE},
131 {RTFFLD_NUMPAGES, sNUMPAGES},
132 {RTFFLD_DATE, sDATE},
133 {RTFFLD_TIME, sTIME},
134 {RTFFLD_DATA, sDATA},
135 {RTFFLD_MERGEFLD, sMERGEFLD},
136 {RTFFLD_IMPORT, sIMPORT2},
137 {RTFFLD_HYPERLINK, sHYPERLINK},
138 {RTFFLD_REF, sREF},
139 {RTFFLD_PAGEREF, sPAGEREF},
140 {RTFFLD_EQ, sEQ},
141 {RTFFLD_INCLUDETEXT, sINCLUDETEXT}
145 if( !rName.Len() )
146 return RTFFLD_UNKNOWN;
148 String sNm( rName );
149 sNm = sNm.EraseLeadingChars().GetToken(0, ' ');
150 ASSERT( sNm.Len(), "Feldname hat keine Laenge!" );
151 if( !sNm.Len() )
152 return RTFFLD_UNKNOWN;
154 xub_StrLen nTokenStt = rName.Search( sNm );
155 sNm.ToLowerAscii();
157 for (size_t n = 0; n < sizeof(aFldNmArr) / sizeof(aFldNmArr[0]); ++n)
159 const sal_Char* pCmp = aFldNmArr[n].pFldNm;
160 int nLen = *pCmp++;
161 xub_StrLen nFndPos = sNm.SearchAscii( pCmp );
162 if( STRING_NOTFOUND != nFndPos &&
163 ( !nFndPos || !isalpha(sNm.GetChar( static_cast< xub_StrLen >(nFndPos-1) )) ) &&
164 ( nFndPos+nLen == sNm.Len() || !isalpha(sNm.GetChar( static_cast< xub_StrLen >(nFndPos+nLen) ) ) ) )
166 // rName = sNm.Copy( nFndPos, nLen );
167 rName = rName.Copy( nFndPos, static_cast< xub_StrLen >(nLen) );
168 nFndPos += nTokenStt + static_cast< xub_StrLen >(nLen);
169 while( rNext.GetChar( nFndPos ) == ' ' ) ++nFndPos;
170 rNext.Erase( 0, nFndPos );
171 rNext.EraseTrailingChars();
172 return aFldNmArr[n].eFldType;
175 return RTFFLD_UNKNOWN; // nichts gefunden.
178 static USHORT CheckNumberFmtStr( const String& rNStr )
180 const static sal_Char* aNumberTypeTab[] =
182 "\x0A""ALPHABETIC", /* CHARS_UPPER_LETTER*/
183 "\x0A""alphabetic", /* CHARS_LOWER_LETTER*/
184 "\x05""ROMAN", /* ROMAN_UPPER */
185 "\x05""roman", /* ROMAN_LOWER */
186 "\x06""ARABIC", /* ARABIC */
187 "\x04""NONE", /* NUMBER_NONE */
188 "\x04""CHAR", /* CHAR_SPECIAL */
189 "\x04""PAGE" /* PAGEDESC */
192 ASSERT(sizeof(aNumberTypeTab) / sizeof(sal_Char *)
193 >= SVX_NUM_PAGEDESC - SVX_NUM_CHARS_UPPER_LETTER, "impossible");
195 for (USHORT n = SVX_NUM_CHARS_UPPER_LETTER; n <= SVX_NUM_PAGEDESC; ++n)
197 const sal_Char* pCmp = aNumberTypeTab[n - SVX_NUM_CHARS_UPPER_LETTER];
198 int nLen = *pCmp++;
199 if( rNStr.EqualsAscii( pCmp, 0, static_cast< xub_StrLen >(nLen) ))
200 return static_cast< USHORT >(2 <= n ? n : (n + SVX_NUM_CHARS_UPPER_LETTER_N));
202 return SVX_NUM_PAGEDESC; // default-Wert
205 class RtfFieldSwitch
207 String sParam;
208 xub_StrLen nCurPos;
209 public:
210 RtfFieldSwitch( const String& rParam );
211 sal_Unicode GetSwitch( String& rParam );
213 BOOL IsAtEnd() const { return nCurPos >= sParam.Len(); }
214 xub_StrLen GetCurPos() const { return nCurPos; }
215 void Erase( xub_StrLen nEndPos ) { sParam.Erase( 0, nEndPos ); }
216 void Insert( const String& rIns ) { sParam.Insert( rIns, 0 ); }
217 const String& GetStr() const { return sParam; }
220 RtfFieldSwitch::RtfFieldSwitch( const String& rParam )
221 : sParam( rParam ), nCurPos( 0 )
223 sParam.EraseTrailingChars().EraseLeadingChars();
226 sal_Unicode RtfFieldSwitch::GetSwitch( String& rParam )
228 // beginnt ein Schalter?
229 sal_Unicode c, cKey = 0;
230 if( '\\' == (c = sParam.GetChar( nCurPos )) )
232 if( '\\' == ( c = sParam.GetChar( ++nCurPos )) )
233 c = sParam.GetChar( ++nCurPos );
235 cKey = c;
237 while( ++nCurPos < sParam.Len() &&
238 ' ' == ( c = sParam.GetChar( nCurPos )) )
242 // dann alles in Hochkommatas oder bis zum naechsten // als
243 // Param returnen
244 USHORT nOffset;
245 if( '"' != c && '\'' != c )
246 c = '\\', nOffset = 0;
247 else
248 nOffset = 1;
250 sParam.Erase( 0, nCurPos + nOffset );
251 rParam = sParam.GetToken( 0, c );
252 sParam.Erase( 0, rParam.Len() + nOffset ).EraseLeadingChars();
253 if( '\\' == c )
254 rParam.EraseTrailingChars();
255 nCurPos = 0;
257 return cKey;
260 struct RTF_EquationData
262 String sFontName, sUp, sDown, sText;
263 sal_Int32 nJustificationCode, nFontSize, nUp, nDown, nStyleNo;
265 inline RTF_EquationData()
266 : nJustificationCode(0), nFontSize(0), nUp(0), nDown(0),
267 nStyleNo( -1 )
271 xub_StrLen lcl_FindEndBracket( const String& rStr )
273 xub_StrLen nEnd = rStr.Len(), nRet = STRING_NOTFOUND, nPos = 0;
274 int nOpenCnt = 1;
275 sal_Unicode cCh;
276 for( ; nPos < nEnd; ++nPos )
277 if( ')' == (cCh = rStr.GetChar( nPos )) && !--nOpenCnt )
279 nRet = nPos;
280 break;
282 else if( '(' == cCh )
283 ++nOpenCnt;
285 return nRet;
288 void lcl_ScanEquationField( const String& rStr, RTF_EquationData& rData,
289 sal_Unicode nSttKey )
291 int nSubSupFlag(0);
292 RtfFieldSwitch aRFS( rStr );
293 while( !aRFS.IsAtEnd() )
295 String sParam;
296 sal_Unicode cKey = aRFS.GetSwitch( sParam );
297 if( 1 == nSubSupFlag )
298 ++nSubSupFlag;
299 else if( 1 < nSubSupFlag )
300 nSubSupFlag = 0;
302 BOOL bCheckBracket = FALSE;
303 switch( cKey )
305 case 0:
306 switch( nSttKey )
308 case 'u': rData.sUp += sParam; break;
309 case 'd': rData.sDown += sParam; break;
310 default: rData.sText += sParam; break;
312 break;
314 case '*':
315 if( sParam.Len() )
317 if( sParam.EqualsIgnoreCaseAscii( "jc", 0, 2 ) )
318 rData.nJustificationCode = sParam.Copy( 2 ).ToInt32();
319 else if( sParam.EqualsIgnoreCaseAscii( "hps", 0, 3 ) )
320 rData.nFontSize= sParam.Copy( 3 ).ToInt32();
321 else if( sParam.EqualsIgnoreCaseAscii( "Font:", 0, 5 ) )
322 rData.sFontName = sParam.Copy( 5 );
323 else if( sParam.EqualsIgnoreCaseAscii( "cs", 0, 2 ) )
324 rData.nStyleNo = sParam.Copy( 2 ).ToInt32();
326 break;
327 case 's' :
328 ++nSubSupFlag;
329 break;
331 case 'u':
332 if( sParam.Len() && 'p' == sParam.GetChar( 0 ) &&
333 2 == nSubSupFlag )
335 rData.nUp = sParam.Copy( 1 ).ToInt32();
336 bCheckBracket = TRUE;
338 break;
340 case 'd':
341 if( sParam.Len() && 'o' == sParam.GetChar( 0 ) &&
342 2 == nSubSupFlag )
344 rData.nDown = sParam.Copy( 1 ).ToInt32();
345 bCheckBracket = TRUE;
347 break;
349 default:
350 bCheckBracket = TRUE;
351 cKey = 0;
352 break;
355 if( bCheckBracket && sParam.Len() )
357 xub_StrLen nEnd, nStt = sParam.Search( '(' ),
358 nLen = sParam.Len();
359 if( STRING_NOTFOUND != nStt )
361 sParam.Erase( 0, nStt + 1 ) += aRFS.GetStr();
362 if( STRING_NOTFOUND !=
363 (nEnd = ::lcl_FindEndBracket( sParam )) )
365 // end in the added string?
366 if( (nLen - nStt - 1 ) < nEnd )
367 aRFS.Erase( nEnd + 1 - (nLen - nStt - 1));
368 else
370 // not all handled here, so set new into the RFS
371 aRFS.Insert( sParam.Copy( nEnd + 1,
372 nLen - nStt - nEnd - 2 ));
373 sal_Unicode cCh;
374 if( aRFS.GetStr().Len() &&
375 ( ',' == (cCh = aRFS.GetStr().GetChar(0)) ||
376 ';' == cCh ))
377 aRFS.Erase( 1 );
380 ::lcl_ScanEquationField( sParam.Copy( 0, nEnd ),
381 rData, cKey );
388 int SwRTFParser::MakeFieldInst( String& rFieldStr )
390 // sicher den Original-String fuer die FeldNamen (User/Datenbank)
391 String aSaveStr( rFieldStr );
392 SwFieldType * pFldType;
393 int nRet = _WhichFld(rFieldStr, aSaveStr);
395 //Strip Mergeformat from fields
396 xub_StrLen nPos=0;
397 while (STRING_NOTFOUND != ( nPos = aSaveStr.SearchAscii("\\*", nPos)))
399 xub_StrLen nStartDel = nPos;
400 nPos += 2;
401 while (aSaveStr.GetChar(nPos) == ' ')
402 ++nPos;
403 if (aSaveStr.EqualsIgnoreCaseAscii("MERGEFORMAT", nPos, 11))
405 xub_StrLen nNoDel = (nPos + 11 ) - nStartDel;
406 aSaveStr.Erase(nStartDel, nNoDel);
407 nPos -= (nStartDel - nPos);
411 nPos = 0;
412 switch (nRet)
414 case RTFFLD_INCLUDETEXT:
415 break;
416 case RTFFLD_IMPORT:
418 //JP 11.03.96: vertraegt sich nicht so ganz mit Internet!
419 // if( STRING_NOTFOUND != ( nPos = aSaveStr.Search( '.' )))
420 // aSaveStr.Erase( nPos+4 );
422 aSaveStr.EraseLeadingAndTrailingChars();
423 if( aSaveStr.Len() )
425 sal_Unicode c = aSaveStr.GetChar( 0 );
426 if( '"' == c || '\'' == c )
428 aSaveStr.Erase( 0, 1 );
429 aSaveStr = aSaveStr.GetToken( 0, c );
432 rFieldStr = URIHelper::SmartRel2Abs(
433 INetURLObject(GetBaseURL()), aSaveStr,
434 URIHelper::GetMaybeFileHdl() );
436 // SkipGroup(); // ueberlese den Rest
438 break;
440 case RTFFLD_NUMPAGES:
442 SwDocStatField aFld( (SwDocStatFieldType*)pDoc->GetSysFldType( RES_DOCSTATFLD ),
443 DS_PAGE, SVX_NUM_ARABIC );
444 if( STRING_NOTFOUND != ( nPos = aSaveStr.SearchAscii( "\\*" )) )
446 nPos += 2;
447 while( aSaveStr.GetChar(nPos) == ' ' ) nPos++;
448 aSaveStr.Erase( 0, nPos );
450 // steht jetzt geanu auf dem Format-Namen
451 aFld.ChangeFormat( CheckNumberFmtStr( aSaveStr ));
453 pDoc->Insert( *pPam, SwFmtFld( aFld ), 0 );
454 SkipGroup();
456 break;
458 case RTFFLD_PAGE:
460 pFldType = pDoc->GetSysFldType( RES_PAGENUMBERFLD );
461 SwPageNumberField aPF( (SwPageNumberFieldType*)pFldType,
462 PG_RANDOM, SVX_NUM_ARABIC);
463 if( STRING_NOTFOUND != ( nPos = aSaveStr.SearchAscii( "\\*" )) )
465 nPos += 2;
466 while( aSaveStr.GetChar(nPos) == ' ' ) nPos++;
467 aSaveStr.Erase( 0, nPos );
469 // steht jetzt geanu auf dem Format-Namen
470 aPF.ChangeFormat( CheckNumberFmtStr( aSaveStr ));
472 pDoc->Insert( *pPam, SwFmtFld( aPF ), 0 );
473 SkipGroup(); // ueberlese den Rest
475 break;
476 case RTFFLD_DATE:
477 case RTFFLD_TIME:
479 if( STRING_NOTFOUND == ( nPos = aSaveStr.SearchAscii( "\\@" )) )
481 // es fehlt die Format - Angabe: defaulten auf Datum
482 pFldType = pDoc->GetSysFldType( RES_DATETIMEFLD );
483 pDoc->Insert( *pPam, SwFmtFld( SwDateTimeField(
484 (SwDateTimeFieldType*)pFldType, DATEFLD )), 0);
486 else
488 // versuche aus dem Formatstring zu erkennen, ob es ein
489 // Datum oder Zeit oder Datum & Zeit Field ist
490 // nur das Format interressiert
491 aSaveStr.Erase( 0, aSaveStr.Search( '\"' )+1 );
492 // alles hinter dem Format interressiert auch nicht mehr.
493 aSaveStr.Erase( aSaveStr.Search( '\"' ) );
494 aSaveStr.SearchAndReplaceAscii( "AM", aEmptyStr );
495 aSaveStr.SearchAndReplaceAscii( "PM", aEmptyStr );
497 // #117892# M.M. Put the word date and time formatter stuff in a common area
498 // and get the rtf filter to use it
499 SwField *pFld = 0;
500 short nNumFmtType = NUMBERFORMAT_UNDEFINED;
501 ULONG nFmtIdx = NUMBERFORMAT_UNDEFINED;
503 USHORT rLang(0);
504 RES_CHRATR eLang = maPageDefaults.mbRTLdoc ? RES_CHRATR_CTL_LANGUAGE : RES_CHRATR_LANGUAGE;
505 const SvxLanguageItem *pLang = (SvxLanguageItem*)&pDoc->GetAttrPool().GetDefaultItem( static_cast< USHORT >(eLang) );
506 rLang = pLang ? pLang->GetValue() : LANGUAGE_ENGLISH_US;
508 SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
509 bool bHijri = false;
511 if( pFormatter )
513 nFmtIdx = sw::ms::MSDateTimeFormatToSwFormat(aSaveStr, pFormatter, rLang, bHijri);
514 if (nFmtIdx)
515 nNumFmtType = pFormatter->GetType(nFmtIdx);
518 pFldType = pDoc->GetSysFldType( RES_DATETIMEFLD );
520 if(nNumFmtType & NUMBERFORMAT_DATE)
521 pFld = new SwDateTimeField( (SwDateTimeFieldType*)pFldType, DATEFLD, nFmtIdx );
522 else if(nNumFmtType == NUMBERFORMAT_TIME)
523 pFld = new SwDateTimeField( (SwDateTimeFieldType*)pFldType, TIMEFLD, nFmtIdx );
525 if( pFld )
527 pDoc->Insert( *pPam, SwFmtFld( *pFld ), 0);
528 delete pFld;
531 SkipGroup(); // ueberlese den Rest
533 break;
535 case RTFFLD_DATA:
537 // Datenbank-FileName: nur der Filename interressiert
538 // Zur Zeit werden nur SDF-Files verarbeitet, also suche nach
539 // der Extension
541 // im SWG geben die DATA Felder den Namen der Datenbank
542 // an. Dieser kann als Field oder als DBInfo interpretiert
543 // werden:
544 // \\data -> Datenbank-Name als Field
545 // DATA -> Datenbank-Info
546 BOOL bField = rFieldStr.GetChar( 0 ) != 'D';
548 // nur der Name interressiert
549 if( STRING_NOTFOUND != (nPos = aSaveStr.Search( '.' )) )
550 aSaveStr.Erase( nPos );
551 SwDBData aData;
552 aData.sDataSource = aSaveStr;
553 if( bField )
555 pFldType = pDoc->GetSysFldType( RES_DBNAMEFLD );
556 pDoc->Insert( *pPam, SwFmtFld( SwDBNameField(
557 (SwDBNameFieldType*)pFldType, SwDBData() ) ), 0);
559 else
560 pDoc->ChgDBData( aData ); // MS: Keine DBInfo verwenden
561 SkipGroup(); // ueberlese den Rest
563 break;
564 case RTFFLD_MERGEFLD:
566 // ein Datenbank - Feld: nur der Name interressiert
567 // bis zum Ende vom String ist das der Feldname
568 SwDBFieldType aTmp( pDoc, aSaveStr, SwDBData() ); //
569 SwDBField aDBFld( (SwDBFieldType*)pDoc->InsertFldType( aTmp ));
571 aDBFld.ChangeFormat( UF_STRING );
572 pDoc->Insert( *pPam, SwFmtFld( aDBFld ), 0);
573 SkipGroup(); // ueberlese den Rest
575 break;
577 case RTFFLD_SYMBOL:
579 // loesche fuehrende Blanks
580 if( IsNewGroup() )
581 GetAttrSet();
582 SetNewGroup( TRUE );
584 SfxItemSet& rSet = GetAttrSet();
586 BOOL bCharIns = FALSE;
587 RtfFieldSwitch aRFS( aSaveStr );
588 while( !aRFS.IsAtEnd() )
590 String sParam;
591 sal_Unicode cKey = aRFS.GetSwitch( sParam );
592 if( sParam.Len() )
593 switch( cKey )
595 case 0:
596 if( !bCharIns )
598 sal_Unicode nChar = (sal_Unicode)sParam.ToInt32();
599 if( nChar )
601 pDoc->Insert( *pPam, nChar );
602 bCharIns = TRUE;
605 break;
607 case 'f': case 'F':
608 // Font setzen
610 SvxRTFFontTbl& rTbl = GetFontTbl();
611 for( Font* pFont = rTbl.First(); pFont;
612 pFont = rTbl.Next() )
613 if( pFont->GetName() == sParam )
615 rSet.Put( SvxFontItem(
616 pFont->GetFamily(),
617 sParam,
618 pFont->GetStyleName(),
619 pFont->GetPitch(),
620 pFont->GetCharSet(),
621 RES_CHRATR_FONT ));
622 break;
625 break;
626 case 'h': case 'H':
627 //??
628 break;
629 case 's': case 'S':
630 // Fontsize setzen
632 const USHORT nVal = (USHORT)(sParam.ToInt32() * 20);
633 rSet.Put( SvxFontHeightItem( nVal,
634 100, RES_CHRATR_FONTSIZE ));
636 break;
640 if( !IsNewGroup() ) AttrGroupEnd();
641 SetNewGroup( FALSE );
643 SkipGroup(); // ueberlese den Rest
645 break;
647 case RTFFLD_HYPERLINK:
648 rFieldStr.Erase();
649 if( aSaveStr.Len() )
651 // return String ist URL, # Mark, \1 Frame
652 String sMark, sFrame;
653 RtfFieldSwitch aRFS( aSaveStr );
654 while( !aRFS.IsAtEnd() )
656 String sParam;
657 sal_Unicode cKey = aRFS.GetSwitch( sParam );
658 if( sParam.Len() )
659 switch( cKey )
661 case 0:
662 if( !rFieldStr.Len() )
663 rFieldStr = URIHelper::SmartRel2Abs(
664 INetURLObject(GetBaseURL()), sParam,
665 URIHelper::GetMaybeFileHdl() );
666 break;
668 case 'l': case 'L': sMark = sParam; break;
669 case 't': case 'T': sFrame = sParam; break;
673 if( sMark.Len() )
674 ( rFieldStr += INET_MARK_TOKEN ) += sMark;
675 if( sFrame.Len() )
676 ( rFieldStr += '\1' ) += sFrame;
678 break;
680 case RTFFLD_EQ:
681 rFieldStr.Erase();
682 if( aSaveStr.Len() )
684 RTF_EquationData aData;
685 ::lcl_ScanEquationField( aSaveStr, aData, 0 );
687 // is it a ruby attr?
688 if( aData.sText.Len() && aData.sFontName.Len() &&
689 aData.nFontSize && aData.sUp.Len() && !aData.sDown.Len() )
691 //Translate and apply
692 switch( aData.nJustificationCode )
694 case 0: aData.nJustificationCode = 1; break;
695 case 1: aData.nJustificationCode = 3; break;
696 case 2: aData.nJustificationCode = 4; break;
697 case 4: aData.nJustificationCode = 2; break;
698 // case 3:
699 default: aData.nJustificationCode = 0; break;
702 SwFmtRuby aRuby( aData.sUp );
703 SwCharFmt * pCharFmt = -1 != aData.nStyleNo
704 ? aCharFmtTbl.Get( aData.nStyleNo )
705 : 0;
707 if( !pCharFmt )
709 //Make a guess at which of asian of western we should be setting
710 USHORT nScript;
711 if (pBreakIt->GetBreakIter().is())
712 nScript = pBreakIt->GetBreakIter()->getScriptType( aData.sUp, 0);
713 else
714 nScript = i18n::ScriptType::ASIAN;
716 USHORT nFntHWhich = GetWhichOfScript( RES_CHRATR_FONTSIZE, nScript ),
717 nFntWhich = GetWhichOfScript( RES_CHRATR_FONT, nScript );
719 //Check to see if we already have a ruby charstyle that this fits
720 for(USHORT i=0; i < aRubyCharFmts.Count(); ++i )
722 SwCharFmt *pFmt = (SwCharFmt *)aRubyCharFmts[i];
723 const SvxFontHeightItem &rF = (const SvxFontHeightItem &)
724 pFmt->GetFmtAttr( nFntHWhich );
725 if( rF.GetHeight() == USHORT(aData.nFontSize * 10 ))
727 const SvxFontItem &rFI = (const SvxFontItem &)
728 pFmt->GetFmtAttr( nFntWhich );
729 if( rFI.GetFamilyName().Equals( aData.sFontName ))
731 pCharFmt = pFmt;
732 break;
737 //Create a new char style if necessary
738 if( !pCharFmt )
740 String sNm;
741 //Take this as the base name
742 SwStyleNameMapper::FillUIName( RES_POOLCHR_RUBYTEXT, sNm );
743 sNm += String::CreateFromInt32( aRubyCharFmts.Count() + 1 );
744 pCharFmt = pDoc->MakeCharFmt( sNm,
745 ( SwCharFmt*)pDoc->GetDfltCharFmt() );
747 SvxFontHeightItem aHeightItem( aData.nFontSize * 10, 100, RES_CHRATR_FONTSIZE );
748 aHeightItem.SetWhich( nFntHWhich );
750 SvxFontItem aFontItem( FAMILY_DONTKNOW, aData.sFontName,
751 aEmptyStr, PITCH_DONTKNOW, RTL_TEXTENCODING_DONTKNOW, nFntWhich );
753 pCharFmt->SetFmtAttr( aHeightItem );
754 pCharFmt->SetFmtAttr( aFontItem );
755 void* p = pCharFmt;
756 aRubyCharFmts.Insert( p, aRubyCharFmts.Count() );
760 //Set the charstyle and justification
761 aRuby.SetCharFmtName( pCharFmt->GetName() );
762 aRuby.SetCharFmtId( pCharFmt->GetPoolFmtId() );
763 aRuby.SetAdjustment( (USHORT)aData.nJustificationCode );
765 // im FieldStr steht der anzuzeigenden Text, im
766 pDoc->Insert( *pPam, aData.sText, true );
767 pPam->SetMark();
768 pPam->GetMark()->nContent -= aData.sText.Len();
769 pDoc->Insert( *pPam, aRuby, nsSetAttrMode::SETATTR_DONTEXPAND );
770 pPam->DeleteMark();
772 // or a combined character field?
773 else if( aData.sUp.Len() && aData.sDown.Len() &&
774 !aData.sText.Len() && !aData.sFontName.Len() &&
775 !aData.nFontSize )
777 String sFld( aData.sUp );
778 sFld += aData.sDown;
779 SwCombinedCharField aFld((SwCombinedCharFieldType*)pDoc->
780 GetSysFldType( RES_COMBINED_CHARS ), sFld );
781 pDoc->Insert( *pPam, SwFmtFld( aFld ), 0);
784 SkipGroup(); // ueberlese den Rest
786 break;
788 case RTFFLD_PAGEREF:
790 String sOrigBkmName;
791 RtfFieldSwitch aRFS( aSaveStr );
792 while( !aRFS.IsAtEnd() )
794 String sParam;
795 sal_Unicode cKey = aRFS.GetSwitch( sParam );
796 switch( cKey )
798 // In the case of pageref the only parameter we are
799 // interested in, is the name of the bookmark
800 case 0:
801 if( !sOrigBkmName.Len() ) // get name of bookmark
802 sOrigBkmName = sParam;
803 break;
806 SwGetRefField aFld(
807 (SwGetRefFieldType*)pDoc->GetSysFldType( RES_GETREFFLD ),
808 sOrigBkmName,REF_BOOKMARK,0,REF_PAGE);
810 if(!bNestedField)
811 pDoc->Insert( *pPam, SwFmtFld( aFld ), 0 );
812 else
813 bNestedField = false;
815 break;
817 case RTFFLD_REF:
819 String sOrigBkmName;
820 bool bChapterNr = false;
821 bool bAboveBelow = false;
823 RtfFieldSwitch aRFS( aSaveStr );
824 while( !aRFS.IsAtEnd() )
826 String sParam;
827 sal_Unicode cKey = aRFS.GetSwitch( sParam );
828 switch( cKey )
830 case 0:
831 if( !sOrigBkmName.Len() ) // get name of bookmark
832 sOrigBkmName = sParam;
833 break;
835 case 'n':
836 case 'r':
837 case 'w':
838 bChapterNr = true; // activate flag 'Chapter Number'
839 break;
841 case 'p':
842 bAboveBelow = true;
843 break;
846 if (!bAboveBelow || bChapterNr)
848 if (bChapterNr)
850 SwGetRefField aFld(
851 (SwGetRefFieldType*)pDoc->GetSysFldType( RES_GETREFFLD ),
852 sOrigBkmName,REF_BOOKMARK,0,REF_CHAPTER);
853 pDoc->Insert( *pPam, SwFmtFld( aFld ), 0 );
855 else
857 SwGetRefField aFld(
858 (SwGetRefFieldType*)pDoc->GetSysFldType( RES_GETREFFLD ),
859 sOrigBkmName,REF_BOOKMARK,0,REF_CONTENT);
860 pDoc->Insert( *pPam, SwFmtFld( aFld ), 0 );
864 if( bAboveBelow )
866 SwGetRefField aFld( (SwGetRefFieldType*)
867 pDoc->GetSysFldType( RES_GETREFFLD ), sOrigBkmName, REF_BOOKMARK, 0,
868 REF_UPDOWN );
869 pDoc->Insert(*pPam, SwFmtFld(aFld), 0);
872 break;
874 case RTFFLD_TOC:
875 case RTFFLD_INDEX:
876 break;
878 default:
880 // keines von den bekannten Feldern, also eine neues UserField
881 aSaveStr.EraseLeadingChars().EraseTrailingChars();
882 SwUserFieldType aTmp( pDoc, aSaveStr );
883 SwUserField aUFld( (SwUserFieldType*)pDoc->InsertFldType( aTmp ));
884 aUFld.ChangeFormat( UF_STRING );
885 pDoc->Insert( *pPam, SwFmtFld( aUFld ), 0);
886 nRet = RTFFLD_UNKNOWN;
888 break;
890 return nRet;
894 void SwRTFParser::ReadXEField()
896 bReadSwFly = false; //#it may be that any uses of this need to be removed and replaced
897 int nNumOpenBrakets = 1;
898 String sFieldStr;
899 BYTE cCh;
901 int nToken;
902 while (nNumOpenBrakets && IsParserWorking())
904 switch (nToken = GetNextToken())
906 case '}':
908 --nNumOpenBrakets;
910 if( sFieldStr.Len())
912 String sXE(sFieldStr);
913 sXE.Insert('\"', 0);
914 sXE.Append('\"');
916 // we have to make sure the hidden text flag is not on
917 // otherwise the index will not see this index mark
918 SfxItemSet& rSet = GetAttrSet();
919 const SfxPoolItem* pItem;
920 if( SFX_ITEM_SET == rSet.GetItemState( RES_CHRATR_HIDDEN, sal_True, &pItem ) )
922 SvxCharHiddenItem aCharHidden(*(SvxCharHiddenItem*)pItem);
923 aCharHidden.SetValue(FALSE);
924 rSet.Put(aCharHidden);
927 sw::ms::ImportXE(*pDoc, *pPam, sXE);
929 sFieldStr.Erase();
932 break;
934 case '{':
935 if( RTF_IGNOREFLAG != GetNextToken() )
936 SkipToken( -1 );
937 // Unknown und alle bekannten nicht ausgewerteten Gruppen
938 // sofort ueberspringen
939 else if( RTF_UNKNOWNCONTROL != GetNextToken() )
940 SkipToken( -2 );
941 else
943 // gleich herausfiltern
944 ReadUnknownData();
945 if( '}' != GetNextToken() )
946 eState = SVPAR_ERROR;
947 break;
949 ++nNumOpenBrakets;
950 break;
952 case RTF_U:
954 if( nTokenValue )
955 sFieldStr += (sal_Unicode)nTokenValue;
956 else
957 sFieldStr += aToken;
959 break;
961 case RTF_LINE: cCh = '\n'; goto INSINGLECHAR;
962 case RTF_TAB: cCh = '\t'; goto INSINGLECHAR;
963 case RTF_SUBENTRYINDEX: cCh = ':'; goto INSINGLECHAR;
964 case RTF_EMDASH: cCh = 151; goto INSINGLECHAR;
965 case RTF_ENDASH: cCh = 150; goto INSINGLECHAR;
966 case RTF_BULLET: cCh = 149; goto INSINGLECHAR;
967 case RTF_LQUOTE: cCh = 145; goto INSINGLECHAR;
968 case RTF_RQUOTE: cCh = 146; goto INSINGLECHAR;
969 case RTF_LDBLQUOTE: cCh = 147; goto INSINGLECHAR;
970 case RTF_RDBLQUOTE: cCh = 148; goto INSINGLECHAR;
971 INSINGLECHAR:
972 sFieldStr += ByteString::ConvertToUnicode( cCh,
973 RTL_TEXTENCODING_MS_1252 );
974 break;
976 // kein Break, aToken wird als Text gesetzt
977 case RTF_TEXTTOKEN:
978 sFieldStr += aToken;
979 break;
981 case RTF_BKMK_KEY:
982 case RTF_TC:
983 case RTF_NEXTFILE:
984 case RTF_TEMPLATE:
985 case RTF_SHPRSLT:
986 SkipGroup();
987 break;
989 case RTF_PAR:
990 sFieldStr.Append('\x0a');
991 break;
992 default:
993 SvxRTFParser::NextToken( nToken );
994 break;
998 SkipToken( -1 ); // die schliesende Klammer wird "oben" ausgewertet
1002 void SwRTFParser::ReadField()
1004 bReadSwFly = false; //#it may be that any uses of this need to be removed and replaced
1005 int nRet = 0;
1006 int nNumOpenBrakets = 1; // die erste wurde schon vorher erkannt !!
1007 int bFldInst = FALSE, bFldRslt = FALSE;
1008 String sFieldStr, sFieldNm;
1009 BYTE cCh;
1011 int nToken;
1012 while (nNumOpenBrakets && IsParserWorking())
1014 switch (nToken = GetNextToken())
1016 case '}':
1018 --nNumOpenBrakets;
1019 if( 1 != nNumOpenBrakets || !bFldInst )
1020 break;
1022 if( !bFldRslt )
1024 // FieldInst vollstaendig eingelesen, was ist es denn?
1025 nRet = MakeFieldInst( sFieldStr );
1026 switch ( nRet )
1028 case RTFFLD_INCLUDETEXT:
1029 case RTFFLD_TOC:
1030 case RTFFLD_INDEX:
1031 // erstmal Index/Inhaltsverzeichniss ueberspringen
1032 // und als normalen Text einfuegen. Spaeter mal auch dem
1033 // SwPaM darum aufspannen.
1034 return ;
1036 case RTFFLD_IMPORT:
1037 case RTFFLD_HYPERLINK:
1038 sFieldNm = sFieldStr;
1039 break;
1041 sFieldStr.Erase();
1043 else if (RTFFLD_UNKNOWN == nRet)
1045 // FieldResult wurde eingelesen
1046 if (SwTxtNode* pTxtNd = pPam->GetPoint()->nNode.GetNode().GetTxtNode())
1048 SwTxtAttr* pFldAttr = pTxtNd->GetTxtAttr(
1049 pPam->GetPoint()->nContent.GetIndex()-1 );
1051 if (pFldAttr)
1053 const SwField *pFld = pFldAttr->GetFld().GetFld();
1054 SwFieldType *pTyp = pFld ? pFld->GetTyp() : 0;
1055 ASSERT(pTyp->Which() == RES_USERFLD, "expected a user field");
1056 if (pTyp->Which() == RES_USERFLD)
1058 SwUserFieldType *pUsrTyp = (SwUserFieldType*)pTyp;
1059 pUsrTyp->SetContent(sFieldStr);
1064 else if( sFieldNm.Len() )
1066 switch ( nRet )
1068 case RTFFLD_IMPORT:
1069 // Grafik einfuegen
1070 InsPicture( sFieldNm );
1071 nRet = INT_MAX;
1072 break;
1073 case RTFFLD_HYPERLINK:
1074 if( sFieldStr.Len() )
1076 if(sNestedFieldStr.Len())
1077 sFieldStr.Insert(sNestedFieldStr);
1079 sNestedFieldStr.Erase();
1080 // im FieldStr steht der anzuzeigenden Text, im
1081 pDoc->Insert( *pPam, sFieldStr, true );
1083 String sTarget( sFieldNm.GetToken( 1, '\1' ));
1084 if( sTarget.Len() )
1085 sFieldNm.Erase( sFieldNm.Len() - sTarget.Len() -1 );
1087 // oder ueber den Stack setzen??
1088 pPam->SetMark();
1089 pPam->GetMark()->nContent -= sFieldStr.Len();
1090 pDoc->Insert( *pPam,
1091 SwFmtINetFmt( sFieldNm, sTarget ),
1092 nsSetAttrMode::SETATTR_DONTEXPAND );
1093 pPam->DeleteMark();
1096 break;
1099 else if(bNestedField)
1101 if(nRet == RTFFLD_PAGEREF)
1103 // #17371 Nasty hack to get a pageref within a hyperlink working
1104 sNestedFieldStr = sFieldStr;
1110 break;
1112 case '{':
1113 if( RTF_IGNOREFLAG != GetNextToken() )
1114 SkipToken( -1 );
1115 // Unknown und alle bekannten nicht ausgewerteten Gruppen
1116 // sofort ueberspringen
1117 else if( RTF_UNKNOWNCONTROL != GetNextToken() )
1118 SkipToken( -2 );
1119 else
1121 // gleich herausfiltern
1122 ReadUnknownData();
1123 if( '}' != GetNextToken() )
1124 eState = SVPAR_ERROR;
1125 break;
1127 ++nNumOpenBrakets;
1128 break;
1130 case RTF_DATAFIELD:
1131 SkipGroup();
1132 break;
1134 case RTF_FIELD:
1135 bNestedField = true;
1136 ReadField();
1137 break;
1139 case RTF_FLDINST:
1140 bFldInst = TRUE;
1141 break;
1143 case RTF_FLDRSLT:
1144 bFldRslt = TRUE;
1145 break;
1147 case RTF_U:
1149 if( nTokenValue )
1150 sFieldStr += (sal_Unicode)nTokenValue;
1151 else
1152 sFieldStr += aToken;
1154 break;
1156 case RTF_LINE: cCh = '\n'; goto INSINGLECHAR;
1157 case RTF_TAB: cCh = '\t'; goto INSINGLECHAR;
1158 case RTF_SUBENTRYINDEX: cCh = ':'; goto INSINGLECHAR;
1159 case RTF_EMDASH: cCh = 151; goto INSINGLECHAR;
1160 case RTF_ENDASH: cCh = 150; goto INSINGLECHAR;
1161 case RTF_BULLET: cCh = 149; goto INSINGLECHAR;
1162 case RTF_LQUOTE: cCh = 145; goto INSINGLECHAR;
1163 case RTF_RQUOTE: cCh = 146; goto INSINGLECHAR;
1164 case RTF_LDBLQUOTE: cCh = 147; goto INSINGLECHAR;
1165 case RTF_RDBLQUOTE: cCh = 148; goto INSINGLECHAR;
1166 INSINGLECHAR:
1167 sFieldStr += ByteString::ConvertToUnicode( cCh,
1168 RTL_TEXTENCODING_MS_1252 );
1169 break;
1171 // kein Break, aToken wird als Text gesetzt
1172 case RTF_TEXTTOKEN:
1173 sFieldStr += aToken;
1174 break;
1176 case RTF_PICT: // Pic-Daten einlesen!
1177 if( RTFFLD_IMPORT == nRet )
1179 Graphic aGrf;
1180 SvxRTFPictureType aPicType;
1181 if( ReadBmpData( aGrf, aPicType ) )
1183 InsPicture( sFieldNm, &aGrf, &aPicType );
1184 nRet = INT_MAX;
1186 SkipGroup();
1188 break;
1190 case RTF_BKMK_KEY:
1191 case RTF_XE:
1192 case RTF_TC:
1193 case RTF_NEXTFILE:
1194 case RTF_TEMPLATE:
1195 case RTF_SHPRSLT:
1196 SkipGroup();
1197 break;
1199 case RTF_CS:
1200 // we write every time "EQ "
1201 if( bFldInst && 0 == sFieldStr.SearchAscii( "EQ " ))
1203 // insert behind the EQ the "\*cs<NO> " string. This is utilize
1204 // in the MakeFieldInst
1205 String sTmp;
1206 (sTmp.AssignAscii( "\\* cs" )
1207 += String::CreateFromInt32( nTokenValue )) += ' ';
1208 sFieldStr.Insert( sTmp, 3 );
1210 break;
1211 case RTF_FFNAME:
1212 case RTF_FORMFIELD:
1213 break;
1214 case RTF_PAR:
1215 sFieldStr.Append('\x0a');
1216 break;
1217 default:
1218 SvxRTFParser::NextToken( nToken );
1219 break;
1223 // Grafik einfuegen
1224 if (RTFFLD_IMPORT == nRet && sFieldNm.Len())
1225 InsPicture( sFieldNm );
1227 SkipToken( -1 ); // die schliesende Klammer wird "oben" ausgewertet
1230 /* vi:set tabstop=4 shiftwidth=4 expandtab: */