1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
56 /*************************************************************************
58 * For LWP filter architecture prototype
59 ************************************************************************/
62 #include <lwpfrib.hxx>
63 #include "lwphyperlinkmgr.hxx"
64 #include <xfilter/xfhyperlink.hxx>
65 #include <xfilter/xfstylemanager.hxx>
66 #include <xfilter/xftextspan.hxx>
67 #include <xfilter/xftextstyle.hxx>
68 #include <xfilter/xftextcontent.hxx>
69 #include "lwpfribheader.hxx"
70 #include "lwpfribtext.hxx"
71 #include "lwpfribtable.hxx"
72 #include "lwpfribbreaks.hxx"
73 #include "lwpfribframe.hxx"
74 #include "lwpfribsection.hxx"
75 #include "lwpcharacterstyle.hxx"
76 #include "lwpfootnote.hxx"
77 #include "lwpnotes.hxx"
78 #include "lwpfribmark.hxx"
79 #include <lwpglobalmgr.hxx>
81 #include <osl/diagnose.h>
83 LwpFrib::LwpFrib(LwpPara
* pPara
)
90 , m_bRevisionFlag(false)
95 LwpFrib::~LwpFrib() { Deregister(); }
97 LwpFrib
* LwpFrib::CreateFrib(LwpPara
* pPara
, LwpObjectStream
* pObjStrm
, sal_uInt8 fribtag
,
101 std::unique_ptr
<ModifierInfo
> xModInfo
;
102 if (fribtag
& FRIB_TAG_MODIFIER
)
104 xModInfo
.reset(new ModifierInfo
);
105 xModInfo
->CodePage
= 0;
106 xModInfo
->FontID
= 0;
107 xModInfo
->RevisionType
= 0;
108 xModInfo
->RevisionFlag
= false;
109 xModInfo
->HasCharStyle
= false;
110 xModInfo
->HasLangOverride
= false;
111 xModInfo
->HasHighlight
= false;
112 ReadModifiers(pObjStrm
, xModInfo
.get());
116 std::unique_ptr
<LwpFrib
> newFrib
;
117 sal_uInt16 friblen
= pObjStrm
->QuickReaduInt16();
118 sal_uInt8 fribtype
= fribtag
& ~FRIB_TAG_TYPEMASK
;
121 case FRIB_TAG_INVALID
: //fall through
122 case FRIB_TAG_EOP
: //fall through
124 newFrib
.reset(new LwpFrib(pPara
));
128 newFrib
.reset(new LwpFribText(pPara
, (fribtag
& FRIB_TAG_NOUNICODE
) != 0));
132 newFrib
.reset(new LwpFribTable(pPara
));
135 newFrib
.reset(new LwpFribTab(pPara
));
137 case FRIB_TAG_PAGEBREAK
:
138 newFrib
.reset(new LwpFribPageBreak(pPara
));
141 newFrib
.reset(new LwpFribFrame(pPara
));
143 case FRIB_TAG_FOOTNOTE
:
144 newFrib
.reset(new LwpFribFootnote(pPara
));
146 case FRIB_TAG_COLBREAK
:
147 newFrib
.reset(new LwpFribColumnBreak(pPara
));
149 case FRIB_TAG_LINEBREAK
:
150 newFrib
.reset(new LwpFribLineBreak(pPara
));
152 case FRIB_TAG_HARDSPACE
:
153 newFrib
.reset(new LwpFribHardSpace(pPara
));
155 case FRIB_TAG_SOFTHYPHEN
:
156 newFrib
.reset(new LwpFribSoftHyphen(pPara
));
158 case FRIB_TAG_PARANUMBER
:
159 newFrib
.reset(new LwpFribParaNumber(pPara
));
161 case FRIB_TAG_UNICODE
: //fall through
162 case FRIB_TAG_UNICODE2
: //fall through
163 case FRIB_TAG_UNICODE3
: //fall through
164 newFrib
.reset(new LwpFribUnicode(pPara
));
167 newFrib
.reset(new LwpFribNote(pPara
));
169 case FRIB_TAG_SECTION
:
170 newFrib
.reset(new LwpFribSection(pPara
));
172 case FRIB_TAG_PAGENUMBER
:
173 newFrib
.reset(new LwpFribPageNumber(pPara
));
175 case FRIB_TAG_DOCVAR
:
176 newFrib
.reset(new LwpFribDocVar(pPara
));
178 case FRIB_TAG_BOOKMARK
:
179 newFrib
.reset(new LwpFribBookMark(pPara
));
182 newFrib
.reset(new LwpFribField(pPara
));
184 case FRIB_TAG_CHBLOCK
:
185 newFrib
.reset(new LwpFribCHBlock(pPara
));
187 case FRIB_TAG_RUBYMARKER
:
188 newFrib
.reset(new LwpFribRubyMarker(pPara
));
190 case FRIB_TAG_RUBYFRAME
:
191 newFrib
.reset(new LwpFribRubyFrame(pPara
));
195 //Do not know why the fribTag judgment is necessary, to be checked with
196 if (fribtag
& FRIB_TAG_MODIFIER
)
198 newFrib
->SetModifiers(xModInfo
.release());
201 newFrib
->m_nFribType
= fribtype
;
202 newFrib
->m_nEditor
= editID
;
203 newFrib
->Read(pObjStrm
, friblen
);
204 return newFrib
.release();
207 void LwpFrib::Read(LwpObjectStream
* pObjStrm
, sal_uInt16 len
) { pObjStrm
->SeekRel(len
); }
209 void LwpFrib::SetModifiers(ModifierInfo
* pModifiers
)
213 m_pModifiers
.reset(pModifiers
);
215 if (pModifiers
->RevisionFlag
)
217 m_bRevisionFlag
= true;
218 m_nRevisionType
= pModifiers
->RevisionType
;
223 void LwpFrib::RegisterStyle(LwpFoundry
* pFoundry
)
227 if (!m_pModifiers
->FontID
&& !m_pModifiers
->HasCharStyle
&& !m_pModifiers
->HasHighlight
)
232 //we only read four modifiers, in these modifiers,CodePage and LangOverride are not styles,
233 //so we can only handle fontid and charstyle, if others, we should not reg style
235 rtl::Reference
<XFFont
> pFont
;
236 XFTextStyle
* pStyle
= nullptr;
238 XFStyleManager
* pXFStyleManager
= LwpGlobalMgr::GetInstance()->GetXFStyleManager();
239 XFTextStyle
* pNamedStyle
= nullptr;
240 if (m_pModifiers
->HasCharStyle
&& pFoundry
)
242 pNamedStyle
= dynamic_cast<XFTextStyle
*>(
243 pFoundry
->GetStyleManager()->GetStyle(m_pModifiers
->CharStyleID
));
247 LwpCharacterStyle
* pCharStyle
= nullptr;
248 if (m_pModifiers
->FontID
&& pFoundry
)
249 pCharStyle
= dynamic_cast<LwpCharacterStyle
*>(m_pModifiers
->CharStyleID
.obj().get());
252 std::unique_ptr
<XFTextStyle
> pNewStyle(new XFTextStyle());
253 *pNewStyle
= *pNamedStyle
;
255 pNewStyle
->SetStyleName(u
""_ustr
);
256 pFont
= pFoundry
->GetFontManager().CreateOverrideFont(pCharStyle
->GetFinalFontID(),
257 m_pModifiers
->FontID
);
258 pNewStyle
->SetFont(pFont
);
259 IXFStyleRet aNewStyle
= pXFStyleManager
->AddStyle(std::move(pNewStyle
));
260 m_StyleName
= aNewStyle
.m_pStyle
->GetStyleName();
261 pStyle
= dynamic_cast<XFTextStyle
*>(aNewStyle
.m_pStyle
);
262 if (aNewStyle
.m_bOrigDeleted
)
266 m_StyleName
= pNamedStyle
->GetStyleName();
270 if (m_pModifiers
->FontID
&& pFoundry
)
272 std::unique_ptr
<XFTextStyle
> pNewStyle(new XFTextStyle());
273 pFont
= pFoundry
->GetFontManager().CreateFont(m_pModifiers
->FontID
);
274 pNewStyle
->SetFont(pFont
);
275 IXFStyleRet aNewStyle
= pXFStyleManager
->AddStyle(std::move(pNewStyle
));
276 m_StyleName
= aNewStyle
.m_pStyle
->GetStyleName();
277 pStyle
= dynamic_cast<XFTextStyle
*>(aNewStyle
.m_pStyle
);
278 if (aNewStyle
.m_bOrigDeleted
)
283 if (!m_pModifiers
->HasHighlight
)
286 XFColor aColor
= GetHighlightColor(); //right yellow
287 if (pStyle
) //change the style directly
288 pStyle
->GetFont()->SetBackColor(aColor
);
289 else //register a new style
291 std::unique_ptr
<XFTextStyle
> pNewStyle(new XFTextStyle());
293 if (!m_StyleName
.isEmpty())
295 XFTextStyle
* pOldStyle
= pXFStyleManager
->FindTextStyle(m_StyleName
);
296 *pNewStyle
= *pOldStyle
;
297 pNewStyle
->GetFont()->SetBackColor(aColor
);
302 pFont
->SetBackColor(aColor
);
303 pNewStyle
->SetFont(pFont
);
305 m_StyleName
= pXFStyleManager
->AddStyle(std::move(pNewStyle
)).m_pStyle
->GetStyleName();
309 void LwpFrib::ReadModifiers(LwpObjectStream
* pObjStrm
, ModifierInfo
* pModInfo
)
315 // Get the modifier type
316 sal_uInt8 Modifier
= pObjStrm
->QuickReaduInt8(&bFailure
);
320 // Stop when we hit the last modifier
321 if (Modifier
== FRIB_MTAG_NONE
)
324 // Get the modifier length
325 sal_uInt8 len
= pObjStrm
->QuickReaduInt8(&bFailure
);
332 if (len
!= sizeof(pModInfo
->FontID
))
334 OSL_FAIL("FRIB_MTAG_FONT entry wrong size");
335 pObjStrm
->SeekRel(len
);
338 pModInfo
->FontID
= pObjStrm
->QuickReaduInt32();
340 case FRIB_MTAG_CHARSTYLE
:
341 pModInfo
->HasCharStyle
= true;
342 pModInfo
->CharStyleID
.ReadIndexed(pObjStrm
);
344 case FRIB_MTAG_LANGUAGE
:
345 pModInfo
->HasLangOverride
= true;
346 pModInfo
->Language
.Read(pObjStrm
);
348 case FRIB_MTAG_CODEPAGE
:
349 if (len
!= sizeof(pModInfo
->CodePage
))
351 OSL_FAIL("FRIB_MTAG_CODEPAGE entry wrong size");
352 pObjStrm
->SeekRel(len
);
355 pModInfo
->CodePage
= pObjStrm
->QuickReaduInt16();
357 case FRIB_MTAG_ATTRIBUTE
:
358 pModInfo
->aTxtAttrOverride
.Read(pObjStrm
);
359 if (pModInfo
->aTxtAttrOverride
.IsHighlight())
360 pModInfo
->HasHighlight
= true;
362 case FRIB_MTAG_REVISION
:
363 pModInfo
->RevisionType
= pObjStrm
->QuickReaduInt8();
364 pModInfo
->RevisionFlag
= true;
367 pObjStrm
->SeekRel(len
);
370 // TODO: read the modifier data
375 * @descr: Whether there are other fribs following current frib.
376 * @return: True if having following fribs, or false.
378 bool LwpFrib::HasNextFrib() { return GetNext() && GetNext()->GetType() != FRIB_TAG_EOP
; }
380 void LwpFrib::ConvertChars(XFContentContainer
* pXFPara
, const OUString
& text
)
384 OUString strStyleName
= GetStyleName();
385 XFTextSpan
* pSpan
= new XFTextSpan(text
, strStyleName
);
390 XFTextContent
* pSpan
= new XFTextContent();
391 pSpan
->SetText(text
);
396 void LwpFrib::ConvertHyperLink(XFContentContainer
* pXFPara
, const LwpHyperlinkMgr
* pHyperlink
,
397 const OUString
& text
)
399 XFHyperlink
* pHyper
= new XFHyperlink
;
400 pHyper
->SetHRef(pHyperlink
->GetHyperlink());
401 pHyper
->SetText(text
);
402 pHyper
->SetStyleName(GetStyleName());
403 pXFPara
->Add(pHyper
);
407 * @descr: Get the current frib font style
408 * @return: XFFont pointer
410 rtl::Reference
<XFFont
> LwpFrib::GetFont()
412 rtl::Reference
<XFFont
> pFont
;
413 if (m_pModifiers
&& m_pModifiers
->FontID
)
415 LwpFoundry
* pFoundry
= m_pPara
->GetFoundry();
417 pFont
= pFoundry
->GetFontManager().CreateFont(m_pModifiers
->FontID
);
421 XFParaStyle
* pXFParaStyle
= m_pPara
->GetXFParaStyle();
422 pFont
= pXFParaStyle
->GetFont();
427 OUString
LwpFrib::GetEditor()
429 LwpGlobalMgr
* pGlobal
= LwpGlobalMgr::GetInstance();
430 return pGlobal
->GetEditorName(m_nEditor
);
433 XFColor
LwpFrib::GetHighlightColor()
435 LwpGlobalMgr
* pGlobal
= LwpGlobalMgr::GetInstance();
436 return pGlobal
->GetHighlightColor(m_nEditor
);
439 void LwpFrib::Register(std::map
<LwpFrib
*, OUString
>* pFribMap
)
442 throw std::runtime_error("registered already");
443 m_pFribMap
= pFribMap
;
446 void LwpFrib::Deregister()
450 m_pFribMap
->erase(this);
451 m_pFribMap
= nullptr;
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */