Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / hwpfilter / source / hpara.cxx
blob7363a4b91cf170bbe753a692d19902e39d425ba4
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 .
20 #include <memory>
21 #include "precompile.h"
24 #include <comphelper/newarray.hxx>
26 #include "hwplib.h"
27 #include "hwpfile.h"
28 #include "hpara.h"
29 #include "hbox.h"
30 #include "hutil.h"
32 void LineInfo::Read(HWPFile & hwpf, HWPPara const *pPara)
34 if (!hwpf.Read2b(pos))
35 return;
36 unsigned short tmp16;
37 if (!hwpf.Read2b(tmp16))
38 return;
39 space_width = tmp16;
40 if (!hwpf.Read2b(tmp16))
41 return;
42 height = tmp16;
43 // internal information
44 if (!hwpf.Read2b(tmp16))
45 return;
46 pgy = tmp16;
47 if (!hwpf.Read2b(tmp16))
48 return;
49 sx = tmp16;
50 if (!hwpf.Read2b(tmp16))
51 return;
52 psx = tmp16;
53 if (!hwpf.Read2b(tmp16))
54 return;
55 pex = tmp16;
56 height_sp = 0;
58 if( pex >> 15 & 0x01 )
60 if (pex & 0x01)
61 hwpf.AddPage();
62 pPara->pshape->reserved[0] = sal::static_int_cast<unsigned char>(pex & 0x01);
63 pPara->pshape->reserved[1] = sal::static_int_cast<unsigned char>(pex & 0x02);
67 HWPPara::HWPPara()
68 : _next(nullptr)
69 , reuse_shape(0)
70 , nch(0)
71 , nline(0)
72 , begin_ypos(0)
73 , scflag(0)
74 , contain_cshape(0)
75 , etcflag(0)
76 , ctrlflag(0)
77 , pstyno(0)
78 , cshape(new CharShape)
79 , pshape(new ParaShape)
81 memset(cshape.get(), 0, sizeof(CharShape));
84 HWPPara::~HWPPara()
88 bool HWPPara::Read(HWPFile & hwpf, unsigned char flag)
90 DepthGuard aGuard(hwpf);
91 if (aGuard.toodeep())
92 return false;
93 int ii;
94 scflag = flag;
95 // Paragraph Information
96 hwpf.Read1b(reuse_shape);
97 hwpf.Read2b(&nch, 1);
98 hwpf.Read2b(&nline, 1);
99 hwpf.Read1b(contain_cshape);
100 hwpf.Read1b(etcflag);
101 hwpf.Read4b(&ctrlflag, 1);
102 hwpf.Read1b(pstyno);
104 /* Paragraph representative character */
105 cshape->Read(hwpf);
106 if (nch > 0)
107 hwpf.AddCharShape(cshape);
109 /* Paragraph paragraphs shape */
110 if (nch && !reuse_shape)
112 pshape->Read(hwpf);
113 pshape->cshape = cshape;
114 pshape->pagebreak = etcflag;
117 linfo.reset(::comphelper::newArray_null<LineInfo>(nline));
118 for (ii = 0; ii < nline; ii++)
120 linfo[ii].Read(hwpf, this);
122 if( etcflag & 0x04 ){
123 hwpf.AddColumnInfo();
126 if (nch && !reuse_shape){
127 if( pshape->xColdef->ncols > 1 ) {
128 hwpf.SetColumnDef(pshape->xColdef);
132 if( nline > 0 )
134 begin_ypos = linfo[0].pgy;
136 else
138 begin_ypos = 0;
141 if (contain_cshape)
143 cshapep.resize(nch);
145 for (ii = 0; ii < nch; ii++)
147 cshapep[ii].reset(new CharShape);
148 memset(cshapep[ii].get(), 0, sizeof(CharShape));
150 unsigned char same_cshape(0);
151 hwpf.Read1b(same_cshape);
152 if (!same_cshape)
154 cshapep[ii]->Read(hwpf);
155 if (nch > 1)
156 hwpf.AddCharShape(cshapep[ii]);
158 else if (ii == 0)
159 cshapep[ii] = cshape;
160 else
161 cshapep[ii] = cshapep[ii - 1];
164 // read string
165 ii = 0;
166 while (ii < nch)
168 auto hBox = readHBox(hwpf);
169 if (!hBox)
170 return false;
171 hhstr[ii] = std::move(hBox);
172 if (hhstr[ii]->hh == CH_END_PARA)
173 break;
174 if( hhstr[ii]->hh < CH_END_PARA )
175 pshape->reserved[0] = 0;
176 ii += hhstr[ii]->WSize();
178 return nch && !hwpf.State();
181 CharShape *HWPPara::GetCharShape(int pos)
183 if (contain_cshape == 0)
184 return cshape.get();
185 return cshapep[pos].get();
188 std::unique_ptr<HBox> HWPPara::readHBox(HWPFile & hwpf)
190 std::unique_ptr<HBox> hbox;
192 hchar hh;
193 if (!hwpf.Read2b(hh))
194 return hbox;
196 if (hwpf.State() != HWP_NoError)
197 return hbox;
199 if (hh > 31 || hh == CH_END_PARA)
200 hbox.reset(new HBox(hh));
201 else if (IS_SP_SKIP_BLOCK(hh))
202 hbox.reset(new SkipData(hh));
203 else
205 switch (hh)
207 case CH_FIELD: // 5
208 hbox.reset(new FieldCode);
209 break;
210 case CH_BOOKMARK: // 6
211 hbox.reset(new Bookmark);
212 break;
213 case CH_DATE_FORM: // 7
214 hbox.reset(new DateFormat);
215 break;
216 case CH_DATE_CODE: // 8
217 hbox.reset(new DateCode);
218 break;
219 case CH_TAB: // 9
220 hbox.reset(new Tab);
221 break;
222 case CH_TEXT_BOX: // 10
223 hbox.reset(new TxtBox);
224 break;
225 case CH_PICTURE: // 11
226 hbox.reset(new Picture);
227 break;
228 case CH_LINE: // 14
229 hbox.reset(new Line);
230 break;
231 case CH_HIDDEN: // 15
232 hbox.reset(new Hidden);
233 break;
234 case CH_HEADER_FOOTER: // 16
235 if (!hwpf.already_importing_type(CH_HEADER_FOOTER))
236 hbox.reset(new HeaderFooter);
237 break;
238 case CH_FOOTNOTE: // 17
239 hbox.reset(new Footnote);
240 break;
241 case CH_AUTO_NUM: // 18
242 hbox.reset(new AutoNum);
243 break;
244 case CH_NEW_NUM: // 19
245 hbox.reset(new NewNum);
246 break;
247 case CH_SHOW_PAGE_NUM: // 20
248 hbox.reset(new ShowPageNum);
249 break;
250 case CH_PAGE_NUM_CTRL: // 21
251 hbox.reset(new PageNumCtrl);
252 break;
253 case CH_MAIL_MERGE: // 22
254 hbox.reset(new MailMerge);
255 break;
256 case CH_COMPOSE: // 23
257 hbox.reset(new Compose);
258 break;
259 case CH_HYPHEN: // 24
260 hbox.reset(new Hyphen);
261 break;
262 case CH_TOC_MARK: // 25
263 hbox.reset(new TocMark);
264 break;
265 case CH_INDEX_MARK: // 26
266 hbox.reset(new IndexMark);
267 break;
268 case CH_OUTLINE: // 28
269 hbox.reset(new Outline);
270 break;
271 case CH_KEEP_SPACE: // 30
272 hbox.reset(new KeepSpace);
273 break;
274 case CH_FIXED_SPACE: // 31
275 hbox.reset(new FixedSpace);
276 break;
277 default:
278 break;
282 if (!hbox)
283 return nullptr;
285 hwpf.push_hpara_type(scflag);
286 bool bRead = hbox->Read(hwpf);
287 hwpf.pop_hpara_type();
288 if (!bRead)
290 hbox.reset();
291 return nullptr;
294 if( hh == CH_TEXT_BOX || hh == CH_PICTURE || hh == CH_LINE )
296 FBox *fbox = static_cast<FBox *>(hbox.get());
297 if( ( fbox->style.anchor_type == 1) && ( fbox->pgy >= begin_ypos) )
299 //strange construct to compile without warning
300 int nTemp = fbox->pgy;
301 nTemp -= begin_ypos;
302 fbox->pgy = sal::static_int_cast<short>(nTemp);
305 return hbox;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */