summaryrefslogtreecommitdiff
path: root/hwpfilter/source/hpara.cxx
blob: 6b43c3bbf827335437edd2ee4ea579086b987dca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <memory>
#include "precompile.h"


#include <comphelper/newarray.hxx>

#include "hwplib.h"
#include "hwpfile.h"
#include "hpara.h"
#include "hbox.h"
#include "hutil.h"

void LineInfo::Read(HWPFile & hwpf, HWPPara const *pPara)
{
    if (!hwpf.Read2b(pos))
        return;
    unsigned short tmp16;
    if (!hwpf.Read2b(tmp16))
        return;
    space_width = tmp16;
    if (!hwpf.Read2b(tmp16))
        return;
    height = tmp16;
// internal information
    if (!hwpf.Read2b(tmp16))
        return;
    pgy = tmp16;
    if (!hwpf.Read2b(tmp16))
        return;
    sx = tmp16;
    if (!hwpf.Read2b(tmp16))
        return;
    psx = tmp16;
    if (!hwpf.Read2b(tmp16))
        return;
    pex = tmp16;
    height_sp = 0;

    if( pex >> 15 & 0x01 )
    {
        if (pex & 0x01)
            hwpf.AddPage();
        pPara->pshape->reserved[0] = sal::static_int_cast<unsigned char>(pex & 0x01);
        pPara->pshape->reserved[1] = sal::static_int_cast<unsigned char>(pex & 0x02);
    }
}

HWPPara::HWPPara()
    : _next(nullptr)
    , reuse_shape(0)
    , nch(0)
    , nline(0)
    , begin_ypos(0)
    , scflag(0)
    , contain_cshape(0)
    , etcflag(0)
    , ctrlflag(0)
    , pstyno(0)
    , cshape(new CharShape)
    , pshape(new ParaShape)
{
    memset(cshape.get(), 0, sizeof(CharShape));
}

HWPPara::~HWPPara()
{
}

bool HWPPara::Read(HWPFile & hwpf, unsigned char flag)
{
    int ii;
    scflag = flag;
// Paragraph Information
    hwpf.Read1b(&reuse_shape, 1);
    hwpf.Read2b(&nch, 1);
    hwpf.Read2b(&nline, 1);
    hwpf.Read1b(&contain_cshape, 1);
    hwpf.Read1b(&etcflag, 1);
    hwpf.Read4b(&ctrlflag, 1);
    hwpf.Read1b(&pstyno, 1);

/* Paragraph representative character */
    cshape->Read(hwpf);
    if (nch > 0)
        hwpf.AddCharShape(cshape);

/* Paragraph paragraphs shape  */
    if (nch && !reuse_shape)
    {
        pshape->Read(hwpf);
        pshape->cshape = cshape;
        pshape->pagebreak = etcflag;
    }

    linfo.reset(::comphelper::newArray_null<LineInfo>(nline));
    for (ii = 0; ii < nline; ii++)
    {
        linfo[ii].Read(hwpf, this);
    }
    if( etcflag & 0x04 ){
         hwpf.AddColumnInfo();
    }

    if (nch && !reuse_shape){
         if( pshape->coldef.ncols > 1 ) {
             hwpf.SetColumnDef(&(pshape->coldef));
         }
    }

    if( nline > 0 )
    {
        begin_ypos = linfo[0].pgy;
    }
    else
    {
        begin_ypos = 0;
    }

    if (contain_cshape)
    {
        cshapep.resize(nch);

        for (ii = 0; ii < nch; ii++)
        {
            cshapep[ii].reset(new CharShape);
            memset(cshapep[ii].get(), 0, sizeof(CharShape));

            unsigned char same_cshape(0);
            hwpf.Read1b(&same_cshape, 1);
            if (!same_cshape)
            {
                cshapep[ii]->Read(hwpf);
                if (nch > 1)
                    hwpf.AddCharShape(cshapep[ii]);
            }
            else if (ii == 0)
                cshapep[ii] = cshape;
            else
                cshapep[ii] = cshapep[ii - 1];
        }
    }
// read string
    hhstr.resize(nch);
    ii = 0;
    while (ii < nch)
    {
        hhstr[ii] = readHBox(hwpf);
        if (!hhstr[ii])
            return false;
        if (hhstr[ii]->hh == CH_END_PARA)
            break;
          if( hhstr[ii]->hh < CH_END_PARA )
                pshape->reserved[0] = 0;
        ii += hhstr[ii]->WSize();
    }
    return nch && !hwpf.State();
}

CharShape *HWPPara::GetCharShape(int pos)
{
    if (contain_cshape == 0)
        return cshape.get();
    return cshapep[pos].get();
}

std::unique_ptr<HBox> HWPPara::readHBox(HWPFile & hwpf)
{
    std::unique_ptr<HBox> hbox;

    hchar hh;
    if (!hwpf.Read2b(hh))
        return hbox;

    if (hwpf.State() != HWP_NoError)
        return hbox;

    if (hh > 31 || hh == CH_END_PARA)
        hbox.reset(new HBox(hh));
    else if (IS_SP_SKIP_BLOCK(hh))
        hbox.reset(new SkipData(hh));
    else
    {
        switch (hh)
        {
            case CH_FIELD:                        // 5
                hbox.reset(new FieldCode);
                break;
            case CH_BOOKMARK:                     // 6
                hbox.reset(new Bookmark);
                break;
            case CH_DATE_FORM:                    // 7
                hbox.reset(new DateFormat);
                break;
            case CH_DATE_CODE:                    // 8
                hbox.reset(new DateCode);
                break;
            case CH_TAB:                          // 9
                hbox.reset(new Tab);
                break;
            case CH_TEXT_BOX:                     // 10
                hbox.reset(new TxtBox);
                break;
            case CH_PICTURE:                      // 11
                hbox.reset(new Picture);
                break;
            case CH_LINE:                         // 14
                hbox.reset(new Line);
                break;
            case CH_HIDDEN:                       // 15
                hbox.reset(new Hidden);
                break;
            case CH_HEADER_FOOTER:                // 16
                hbox.reset(new HeaderFooter);
                break;
            case CH_FOOTNOTE:                     // 17
                hbox.reset(new Footnote);
                break;
            case CH_AUTO_NUM:                     // 18
                hbox.reset(new AutoNum);
                break;
            case CH_NEW_NUM:                      // 19
                hbox.reset(new NewNum);
                break;
            case CH_SHOW_PAGE_NUM:                // 20
                hbox.reset(new ShowPageNum);
                break;
            case CH_PAGE_NUM_CTRL:                // 21
                hbox.reset(new PageNumCtrl);
                break;
            case CH_MAIL_MERGE:                   // 22
                hbox.reset(new MailMerge);
                break;
            case CH_COMPOSE:                      // 23
                hbox.reset(new Compose);
                break;
            case CH_HYPHEN:                       // 24
                hbox.reset(new Hyphen);
                break;
            case CH_TOC_MARK:                     // 25
                hbox.reset(new TocMark);
                break;
            case CH_INDEX_MARK:                   // 26
                hbox.reset(new IndexMark);
                break;
            case CH_OUTLINE:                      // 28
                hbox.reset(new Outline);
                break;
            case CH_KEEP_SPACE:                   // 30
                hbox.reset(new KeepSpace);
                break;
            case CH_FIXED_SPACE:                  // 31
                hbox.reset(new FixedSpace);
                break;
            default:
                break;
        }
    }
    if (!hbox || !hbox->Read(hwpf))
    {
        hbox.reset();
        return hbox;
    }
    if( hh == CH_TEXT_BOX || hh == CH_PICTURE || hh == CH_LINE )
    {
        FBox *fbox = static_cast<FBox *>(hbox.get());
        if( ( fbox->style.anchor_type == 1) && ( fbox->pgy >= begin_ypos) )
        {
            //strange construct to compile without warning
            int nTemp = fbox->pgy;
            nTemp -= begin_ypos;
            fbox->pgy = sal::static_int_cast<short>(nTemp);
        }
    }
    return hbox;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */