fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / hwpfilter / source / hwpfile.cxx
blobdb7ed1156b081329e714864f86cc8868d96456b1
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 "precompile.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include "hwplib.h"
27 #include "hwpfile.h"
28 #include "hiodev.h"
29 #include "hfont.h"
30 #include "hstyle.h"
31 #include "hbox.h"
32 #include "hpara.h"
33 #include "htags.h"
34 #include "hcode.h"
35 #include "hstream.hxx"
37 #include <osl/diagnose.h>
39 HWPFile *HWPFile::cur_doc = 0;
40 static int ccount = 0;
41 static int pcount = 0;
42 static int datecodecount = 0;
44 HWPFile::HWPFile()
45 : version(HWP_V30)
46 , compressed(false)
47 , encrypted(false)
48 , linenumber(0)
49 , info_block_len(0)
50 , error_code(HWP_NoError)
51 , oledata(0)
52 , m_nCurrentPage(1)
53 , m_nMaxSettedPage(0)
54 , hiodev(0)
55 , currenthyper(0)
57 SetCurrentDoc(this);
60 HWPFile::~HWPFile()
62 delete oledata;
63 delete hiodev;
65 std::list < ColumnInfo* >::iterator it_column = columnlist.begin();
66 for (; it_column != columnlist.end(); ++it_column)
67 delete *it_column;
69 std::list < HWPPara* >::iterator it = plist.begin();
70 for (; it != plist.end(); ++it)
71 delete *it;
73 std::list < Table* >::iterator tbl = tables.begin();
74 for (; tbl != tables.end(); ++tbl)
75 delete *tbl;
77 std::list < HyperText* >::iterator hyp = hyperlist.begin();
78 for (; hyp != hyperlist.end(); ++hyp)
80 delete *hyp;
84 int HWPFile::ReadHwpFile(HStream * stream)
86 if (Open(stream) != HWP_NoError)
87 return State();
88 InfoRead();
89 FontRead();
90 StyleRead();
91 AddColumnInfo();
92 ParaListRead();
93 TagsRead();
95 return State();
98 int detect_hwp_version(const char *str)
100 if (memcmp(V20SIGNATURE, str, HWPIDLen) == 0)
101 return HWP_V20;
102 else if (memcmp(V21SIGNATURE, str, HWPIDLen) == 0)
103 return HWP_V21;
104 else if (memcmp(V30SIGNATURE, str, HWPIDLen) == 0)
105 return HWP_V30;
106 return 0;
109 // HIODev wrapper
111 int HWPFile::Open(HStream * stream)
113 HStreamIODev *hstreamio = new HStreamIODev(stream);
115 if (!hstreamio->open())
117 delete hstreamio;
119 return SetState(HWP_EMPTY_FILE);
122 HIODev *pPrev = SetIODevice(hstreamio);
123 delete pPrev;
125 char idstr[HWPIDLen];
127 if (ReadBlock(idstr, HWPIDLen) <= 0
128 || HWP_V30 != (version = detect_hwp_version(idstr)))
130 return SetState(HWP_UNSUPPORTED_VERSION);
132 return HWP_NoError;
135 int HWPFile::SetState(int errcode)
137 error_code = errcode;
138 return error_code;
141 bool HWPFile::Read1b(unsigned char &out)
143 return hiodev && hiodev->read1b(out);
146 bool HWPFile::Read1b(char &out)
148 unsigned char tmp8;
149 if (!Read1b(tmp8))
150 return false;
151 out = tmp8;
152 return true;
155 bool HWPFile::Read2b(unsigned short &out)
157 return hiodev && hiodev->read2b(out);
160 bool HWPFile::Read4b(unsigned int &out)
162 return hiodev && hiodev->read4b(out);
165 bool HWPFile::Read4b(int &out)
167 unsigned int tmp32;
168 if (!Read4b(tmp32))
169 return false;
170 out = tmp32;
171 return true;
174 int HWPFile::Read1b(void *ptr, size_t nmemb)
176 return hiodev ? hiodev->read1b(ptr, nmemb) : 0;
179 int HWPFile::Read2b(void *ptr, size_t nmemb)
181 return hiodev ? hiodev->read2b(ptr, nmemb) : 0;
185 int HWPFile::Read4b(void *ptr, size_t nmemb)
187 return hiodev ? hiodev->read4b(ptr, nmemb) : 0;
191 size_t HWPFile::ReadBlock(void *ptr, size_t size)
193 return hiodev ? hiodev->readBlock(ptr, size) : 0;
197 size_t HWPFile::SkipBlock(size_t size)
199 return hiodev ? hiodev->skipBlock(size) : 0;
203 bool HWPFile::SetCompressed(bool flag)
205 return hiodev && hiodev->setCompressed(flag);
209 HIODev *HWPFile::SetIODevice(HIODev * new_hiodev)
211 HIODev *old_hiodev = hiodev;
213 hiodev = new_hiodev;
215 return old_hiodev;
219 // end of HIODev wrapper
221 bool HWPFile::InfoRead()
223 return _hwpInfo.Read(*this);
227 bool HWPFile::FontRead()
229 return _hwpFont.Read(*this);
233 bool HWPFile::StyleRead()
235 return _hwpStyle.Read(*this);
239 bool HWPFile::ParaListRead()
241 return ReadParaList(plist);
244 bool HWPFile::ReadParaList(std::list < HWPPara* > &aplist, unsigned char flag)
246 HWPPara *spNode = new HWPPara;
247 unsigned char tmp_etcflag;
248 unsigned char prev_etcflag = 0;
249 while (spNode->Read(*this, flag))
251 if( !(spNode->etcflag & 0x04) ){
252 tmp_etcflag = spNode->etcflag;
253 spNode->etcflag = prev_etcflag;
254 prev_etcflag = tmp_etcflag;
256 if (spNode->nch && spNode->reuse_shape)
258 if (!aplist.empty()){
259 spNode->pshape = aplist.back()->pshape;
261 else{
262 spNode->nch = 0;
263 spNode->reuse_shape = 0;
266 spNode->pshape.pagebreak = spNode->etcflag;
267 if( spNode->nch )
268 AddParaShape( &spNode->pshape );
270 if (!aplist.empty())
271 aplist.back()->SetNext(spNode);
272 aplist.push_back(spNode);
273 spNode = new HWPPara;
275 delete spNode;
277 return true;
280 void HWPFile::TagsRead()
282 while (true)
284 uint tag;
285 if (!Read4b(tag))
286 return;
287 int size;
288 if (!Read4b(size))
289 return;
290 if (size <= 0 && tag > 0){
291 continue;
294 if (tag == FILETAG_END_OF_COMPRESSED ||
295 tag == FILETAG_END_OF_UNCOMPRESSED)
296 return;
297 switch (tag)
299 case FILETAG_EMBEDDED_PICTURE:
301 EmPicture *emb = new EmPicture(size);
303 if (emb->Read(*this))
304 emblist.push_back(emb);
305 else
306 delete emb;
308 break;
309 case FILETAG_OLE_OBJECT:
310 if (oledata)
311 delete oledata;
312 oledata = new OlePicture(size);
313 oledata->Read(*this);
314 break;
315 case FILETAG_HYPERTEXT:
317 if( (size % 617) != 0 )
318 SkipBlock( size );
319 else
321 for( int i = 0 ; i < size/617 ; i++)
323 HyperText *hypert = new HyperText;
324 hypert->Read(*this);
325 hyperlist.push_back(hypert);
328 break;
330 case 6:
332 ReadBlock(_hwpInfo.back_info.reserved1, 8);
333 if (!Read4b(_hwpInfo.back_info.luminance))
334 return;
335 if (!Read4b(_hwpInfo.back_info.contrast))
336 return;
337 if (!Read1b(_hwpInfo.back_info.effect))
338 return;
339 ReadBlock(_hwpInfo.back_info.reserved2, 7);
340 ReadBlock(_hwpInfo.back_info.filename, 260);
341 ReadBlock(_hwpInfo.back_info.color, 3);
342 unsigned short nFlag;
343 if (!Read2b(nFlag))
344 return;
345 _hwpInfo.back_info.flag = nFlag >> 8 ;
346 int nRange;
347 if (!Read4b(nRange))
348 return;
349 _hwpInfo.back_info.range = nRange >> 24;
350 ReadBlock(_hwpInfo.back_info.reserved3, 27);
351 if (!Read4b(_hwpInfo.back_info.size))
352 return;
354 _hwpInfo.back_info.data = new char[(unsigned int)_hwpInfo.back_info.size];
355 ReadBlock(_hwpInfo.back_info.data, _hwpInfo.back_info.size);
357 if( _hwpInfo.back_info.size > 0 )
358 _hwpInfo.back_info.type = 2;
359 else if( _hwpInfo.back_info.filename[0] )
360 _hwpInfo.back_info.type = 1;
361 else
362 _hwpInfo.back_info.type = 0;
365 _hwpInfo.back_info.isset = true;
367 break;
369 case FILETAG_PRESENTATION:
370 case FILETAG_PREVIEW_IMAGE:
371 case FILETAG_PREVIEW_TEXT:
372 default:
373 SkipBlock(size);
379 ColumnDef *HWPFile::GetColumnDef(int num)
381 std::list<ColumnInfo*>::iterator it = columnlist.begin();
383 for(int i = 0; it != columnlist.end() ; ++it, i++){
384 if( i == num )
385 break;
388 if( it != columnlist.end() )
389 return (*it)->coldef;
390 else
391 return 0;
393 /* @return 인덱스는 1부터 시작한다. */
394 int HWPFile::GetPageMasterNum(int page)
396 std::list<ColumnInfo*>::iterator it = columnlist.begin();
397 int i;
399 for( i = 1 ; it != columnlist.end() ; ++it, i++){
400 ColumnInfo *now = *it;
401 if( page < now->start_page )
402 return i-1;
404 return i-1;
407 HyperText *HWPFile::GetHyperText()
409 std::list<HyperText*>::iterator it = hyperlist.begin();
411 for( int i = 0; it != hyperlist.end(); ++it, i++ ){
412 if( i == currenthyper )
413 break;
416 currenthyper++;
417 return it != hyperlist.end() ? *it : NULL;
420 EmPicture *HWPFile::GetEmPicture(Picture * pic)
422 char *name = pic->picinfo.picembed.embname;
424 name[0] = 'H';
425 name[1] = 'W';
426 name[2] = 'P';
428 std::list < EmPicture* >::iterator it = emblist.begin();
429 for (; it != emblist.end(); ++it)
430 if (strcmp(name, (*it)->name) == 0)
431 return *it;
432 return 0;
435 EmPicture *HWPFile::GetEmPictureByName(char * name)
437 name[0] = 'H';
438 name[1] = 'W';
439 name[2] = 'P';
441 std::list < EmPicture* >::iterator it = emblist.begin();
442 for (; it != emblist.end(); ++it)
443 if (strcmp(name, (*it)->name) == 0)
444 return *it;
445 return 0;
449 void HWPFile::AddBox(FBox * box)
451 // LATER if we don't use box->next(),
452 // AddBox() and GetBoxHead() are useless;
453 if (!blist.empty())
455 box->prev = blist.back();
456 box->prev->next = box;
458 else
459 box->prev = 0;
460 blist.push_back(box);
464 ParaShape *HWPFile::getParaShape(int index)
466 std::list<ParaShape*>::iterator it = pslist.begin();
468 for( int i = 0; it != pslist.end(); ++it, i++ ){
469 if( i == index )
470 break;
473 return it != pslist.end() ? *it : NULL;
477 CharShape *HWPFile::getCharShape(int index)
479 std::list<CharShape*>::iterator it = cslist.begin();
481 for( int i = 0; it != cslist.end(); ++it, i++ ){
482 if( i == index )
483 break;
486 return it != cslist.end() ? *it : 0;
490 FBoxStyle *HWPFile::getFBoxStyle(int index)
492 std::list<FBoxStyle*>::iterator it = fbslist.begin();
494 for( int i = 0; it != fbslist.end(); ++it, i++ ){
495 if( i == index )
496 break;
499 return it != fbslist.end() ? *it : 0;
502 DateCode *HWPFile::getDateCode(int index)
504 std::list<DateCode*>::iterator it = datecodes.begin();
506 for( int i = 0; it != datecodes.end(); ++it, i++ ){
507 if( i == index )
508 break;
511 return it != datecodes.end() ? *it : NULL;
514 HeaderFooter *HWPFile::getHeaderFooter(int index)
516 std::list<HeaderFooter*>::iterator it = headerfooters.begin();
518 for( int i = 0; it != headerfooters.end(); ++it, i++ ){
519 if( i == index )
520 break;
523 return it != headerfooters.end() ? *it : NULL;
526 ShowPageNum *HWPFile::getPageNumber(int index)
528 std::list<ShowPageNum*>::iterator it = pagenumbers.begin();
530 for( int i = 0; it != pagenumbers.end(); ++it, i++ ){
531 if( i == index )
532 break;
535 return it != pagenumbers.end() ? *it : NULL;
539 Table *HWPFile::getTable(int index)
541 std::list<Table*>::iterator it = tables.begin();
543 for( int i = 0; it != tables.end(); ++it, i++ ){
544 if( i == index )
545 break;
548 return it != tables.end() ? *it : NULL;
551 void HWPFile::AddParaShape(ParaShape * pshape)
553 int nscount = 0;
554 for(int j = 0 ; j < MAXTABS-1 ; j++)
556 if( j > 0 && pshape->tabs[j].position == 0 )
557 break;
558 if( pshape->tabs[0].position == 0 ){
559 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
560 (pshape->tabs[j].position != 1000 *j) )
561 nscount = j;
563 else{
564 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
565 (pshape->tabs[j].position != 1000 * (j + 1)) )
566 nscount = j;
569 if( nscount )
570 pshape->tabs[MAXTABS-1].type = sal::static_int_cast<char>(nscount);
571 int value = compareParaShape(pshape);
572 if( value == 0 || nscount )
574 pshape->index = ++pcount;
575 pslist.push_back(pshape);
577 else
578 pshape->index = value;
582 void HWPFile::AddCharShape(CharShape * cshape)
584 int value = compareCharShape(cshape);
585 if( value == 0 )
587 cshape->index = ++ccount;
588 cslist.push_back(cshape);
590 else
591 cshape->index = value;
594 void HWPFile::AddColumnInfo()
596 ColumnInfo *cinfo = new ColumnInfo(m_nCurrentPage);
597 columnlist.push_back(cinfo);
598 setMaxSettedPage();
601 void HWPFile::SetColumnDef(ColumnDef *coldef)
603 ColumnInfo *cinfo = columnlist.back();
604 if( cinfo->bIsSet )
605 return;
606 cinfo->coldef = coldef;
607 cinfo->bIsSet = true;
610 void HWPFile::AddDateFormat(DateCode * hbox)
612 hbox->key = sal::static_int_cast<char>(++datecodecount);
613 datecodes.push_back(hbox);
616 void HWPFile::AddPageNumber(ShowPageNum * hbox)
618 pagenumbers.push_back(hbox);
621 void HWPFile::AddHeaderFooter(HeaderFooter * hbox)
623 headerfooters.push_back(hbox);
626 void HWPFile::AddTable(Table * hbox)
628 tables.push_back(hbox);
631 void HWPFile::AddFBoxStyle(FBoxStyle * fbstyle)
633 fbslist.push_back(fbstyle);
636 int HWPFile::compareCharShape(CharShape *shape)
638 int count = cslist.size();
639 if( count > 0 )
641 for(int i = 0; i< count; i++)
643 CharShape *cshape = getCharShape(i);
645 if( shape->size == cshape->size &&
646 shape->font[0] == cshape->font[0] &&
647 shape->ratio[0] == cshape->ratio[0] &&
648 shape->space[0] == cshape->space[0] &&
649 shape->color[1] == cshape->color[1] &&
650 shape->color[0] == cshape->color[0] &&
651 shape->shade == cshape->shade &&
652 shape->attr == cshape->attr )
654 return cshape->index;
658 return 0;
662 int HWPFile::compareParaShape(ParaShape *shape)
664 int count = pslist.size();
665 if( count > 0 )
667 for(int i = 0; i< count; i++)
669 ParaShape *pshape = getParaShape(i);
670 if( shape->left_margin == pshape->left_margin &&
671 shape->right_margin == pshape->right_margin &&
672 shape->pspacing_prev == pshape->pspacing_prev &&
673 shape->pspacing_next == pshape->pspacing_next &&
674 shape->indent == pshape->indent &&
675 shape->lspacing == pshape->lspacing &&
676 shape->arrange_type == pshape->arrange_type &&
677 shape->outline == pshape->outline &&
678 shape->pagebreak == pshape->pagebreak)
680 if( shape->cshape && pshape->cshape &&
681 shape->cshape->size == pshape->cshape->size &&
682 shape->cshape->font[0] == pshape->cshape->font[0] &&
683 shape->cshape->ratio[0] == pshape->cshape->ratio[0] &&
684 shape->cshape->space[0] == pshape->cshape->space[0] &&
685 shape->cshape->color[1] == pshape->cshape->color[1] &&
686 shape->cshape->color[0] == pshape->cshape->color[0] &&
687 shape->cshape->shade == pshape->cshape->shade &&
688 shape->cshape->attr == pshape->cshape->attr )
690 return pshape->index;
695 return 0;
699 HWPFile *GetCurrentDoc()
701 return HWPFile::cur_doc;
705 HWPFile *SetCurrentDoc(HWPFile * hwpfp)
707 HWPFile *org = HWPFile::cur_doc;
709 HWPFile::cur_doc = hwpfp;
710 return org;
713 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */