tdf#131098 docx export: write fill property of graphic
[LibreOffice.git] / lotuswordpro / source / filter / lwpfrib.cxx
blob50196175cca6622fe65bba560f9c5d6abf9bb0d5
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,
29 * MA 02111-1307 USA
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 /*************************************************************************
57 * @file
58 * For LWP filter architecture prototype
59 ************************************************************************/
61 #include <memory>
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)
84 : m_pFribMap(nullptr)
85 , m_pPara(pPara)
86 , m_pNext(nullptr)
87 , m_nFribType(0)
88 , m_ModFlag(false)
89 , m_nRevisionType(0)
90 , m_bRevisionFlag(false)
91 , m_nEditor(0)
95 LwpFrib::~LwpFrib() { Deregister(); }
97 LwpFrib* LwpFrib::CreateFrib(LwpPara* pPara, LwpObjectStream* pObjStrm, sal_uInt8 fribtag,
98 sal_uInt8 editID)
100 //Read Modifier
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());
115 //Read frib data
116 std::unique_ptr<LwpFrib> newFrib;
117 sal_uInt16 friblen = pObjStrm->QuickReaduInt16();
118 sal_uInt8 fribtype = fribtag & ~FRIB_TAG_TYPEMASK;
119 switch (fribtype)
121 case FRIB_TAG_INVALID: //fall through
122 case FRIB_TAG_EOP: //fall through
123 default:
124 newFrib.reset(new LwpFrib(pPara));
125 break;
126 case FRIB_TAG_TEXT:
128 newFrib.reset(new LwpFribText(pPara, (fribtag & FRIB_TAG_NOUNICODE) != 0));
129 break;
131 case FRIB_TAG_TABLE:
132 newFrib.reset(new LwpFribTable(pPara));
133 break;
134 case FRIB_TAG_TAB:
135 newFrib.reset(new LwpFribTab(pPara));
136 break;
137 case FRIB_TAG_PAGEBREAK:
138 newFrib.reset(new LwpFribPageBreak(pPara));
139 break;
140 case FRIB_TAG_FRAME:
141 newFrib.reset(new LwpFribFrame(pPara));
142 break;
143 case FRIB_TAG_FOOTNOTE:
144 newFrib.reset(new LwpFribFootnote(pPara));
145 break;
146 case FRIB_TAG_COLBREAK:
147 newFrib.reset(new LwpFribColumnBreak(pPara));
148 break;
149 case FRIB_TAG_LINEBREAK:
150 newFrib.reset(new LwpFribLineBreak(pPara));
151 break;
152 case FRIB_TAG_HARDSPACE:
153 newFrib.reset(new LwpFribHardSpace(pPara));
154 break;
155 case FRIB_TAG_SOFTHYPHEN:
156 newFrib.reset(new LwpFribSoftHyphen(pPara));
157 break;
158 case FRIB_TAG_PARANUMBER:
159 newFrib.reset(new LwpFribParaNumber(pPara));
160 break;
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));
165 break;
166 case FRIB_TAG_NOTE:
167 newFrib.reset(new LwpFribNote(pPara));
168 break;
169 case FRIB_TAG_SECTION:
170 newFrib.reset(new LwpFribSection(pPara));
171 break;
172 case FRIB_TAG_PAGENUMBER:
173 newFrib.reset(new LwpFribPageNumber(pPara));
174 break;
175 case FRIB_TAG_DOCVAR:
176 newFrib.reset(new LwpFribDocVar(pPara));
177 break;
178 case FRIB_TAG_BOOKMARK:
179 newFrib.reset(new LwpFribBookMark(pPara));
180 break;
181 case FRIB_TAG_FIELD:
182 newFrib.reset(new LwpFribField(pPara));
183 break;
184 case FRIB_TAG_CHBLOCK:
185 newFrib.reset(new LwpFribCHBlock(pPara));
186 break;
187 case FRIB_TAG_RUBYMARKER:
188 newFrib.reset(new LwpFribRubyMarker(pPara));
189 break;
190 case FRIB_TAG_RUBYFRAME:
191 newFrib.reset(new LwpFribRubyFrame(pPara));
192 break;
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)
211 if (pModifiers)
213 m_pModifiers.reset(pModifiers);
214 m_ModFlag = true;
215 if (pModifiers->RevisionFlag)
217 m_bRevisionFlag = true;
218 m_nRevisionType = pModifiers->RevisionType;
223 void LwpFrib::RegisterStyle(LwpFoundry* pFoundry)
225 if (!m_pModifiers)
226 return;
227 if (!m_pModifiers->FontID && !m_pModifiers->HasCharStyle && !m_pModifiers->HasHighlight)
229 m_ModFlag = false;
230 return;
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
234 //note by ,1-27
235 rtl::Reference<XFFont> pFont;
236 XFTextStyle* pStyle = nullptr;
237 m_StyleName.clear();
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));
245 if (pNamedStyle)
247 LwpCharacterStyle* pCharStyle = nullptr;
248 if (m_pModifiers->FontID && pFoundry)
249 pCharStyle = dynamic_cast<LwpCharacterStyle*>(m_pModifiers->CharStyleID.obj().get());
250 if (pCharStyle)
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)
263 pStyle = nullptr;
265 else
266 m_StyleName = pNamedStyle->GetStyleName();
268 else
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)
279 pStyle = nullptr;
283 if (!m_pModifiers->HasHighlight)
284 return;
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);
299 else
301 pFont = new XFFont;
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)
311 for (;;)
313 bool bFailure;
315 // Get the modifier type
316 sal_uInt8 Modifier = pObjStrm->QuickReaduInt8(&bFailure);
317 if (bFailure)
318 break;
320 // Stop when we hit the last modifier
321 if (Modifier == FRIB_MTAG_NONE)
322 break;
324 // Get the modifier length
325 sal_uInt8 len = pObjStrm->QuickReaduInt8(&bFailure);
326 if (bFailure)
327 break;
329 switch (Modifier)
331 case FRIB_MTAG_FONT:
332 if (len != sizeof(pModInfo->FontID))
334 OSL_FAIL("FRIB_MTAG_FONT entry wrong size");
335 pObjStrm->SeekRel(len);
337 else
338 pModInfo->FontID = pObjStrm->QuickReaduInt32();
339 break;
340 case FRIB_MTAG_CHARSTYLE:
341 pModInfo->HasCharStyle = true;
342 pModInfo->CharStyleID.ReadIndexed(pObjStrm);
343 break;
344 case FRIB_MTAG_LANGUAGE:
345 pModInfo->HasLangOverride = true;
346 pModInfo->Language.Read(pObjStrm);
347 break;
348 case FRIB_MTAG_CODEPAGE:
349 if (len != sizeof(pModInfo->CodePage))
351 OSL_FAIL("FRIB_MTAG_CODEPAGE entry wrong size");
352 pObjStrm->SeekRel(len);
354 else
355 pModInfo->CodePage = pObjStrm->QuickReaduInt16();
356 break;
357 case FRIB_MTAG_ATTRIBUTE:
358 pModInfo->aTxtAttrOverride.Read(pObjStrm);
359 if (pModInfo->aTxtAttrOverride.IsHighlight())
360 pModInfo->HasHighlight = true;
361 break;
362 case FRIB_MTAG_REVISION:
363 pModInfo->RevisionType = pObjStrm->QuickReaduInt8();
364 pModInfo->RevisionFlag = true;
365 break;
366 default:
367 pObjStrm->SeekRel(len);
368 break;
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)
382 if (m_ModFlag)
384 OUString strStyleName = GetStyleName();
385 XFTextSpan* pSpan = new XFTextSpan(text, strStyleName);
386 pXFPara->Add(pSpan);
388 else
390 XFTextContent* pSpan = new XFTextContent();
391 pSpan->SetText(text);
392 pXFPara->Add(pSpan);
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();
416 if (pFoundry)
417 pFont = pFoundry->GetFontManager().CreateFont(m_pModifiers->FontID);
419 else
421 XFParaStyle* pXFParaStyle = m_pPara->GetXFParaStyle();
422 pFont = pXFParaStyle->GetFont();
424 return pFont;
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)
441 if (m_pFribMap)
442 throw std::runtime_error("registered already");
443 m_pFribMap = pFribMap;
446 void LwpFrib::Deregister()
448 if (m_pFribMap)
450 m_pFribMap->erase(this);
451 m_pFribMap = nullptr;
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */