update dev300-m58
[ooovba.git] / hwpfilter / source / hwpfile.cpp
bloba33a681355d8a86cb1d21bd65273393c199a412c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hwpfile.cpp,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompile.h"
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include "hwplib.h"
38 #include "hwpfile.h"
39 #include "hiodev.h"
40 #include "hfont.h"
41 #include "hstyle.h"
42 #include "hbox.h"
43 #include "hpara.h"
44 #include "htags.h"
45 #include "hcode.h"
46 #include "hstream.h"
48 #define HWPIDLen 30
49 #define HWPHeadLen 128
50 #define HWPSummaryLen 1008
52 #define V20SIGNATURE "HWP Document File V2.00 \032\1\2\3\4\5"
53 #define V21SIGNATURE "HWP Document File V2.10 \032\1\2\3\4\5"
54 #define V30SIGNATURE "HWP Document File V3.00 \032\1\2\3\4\5"
56 #define FILESTG_SIGNATURE 0xF8995567
57 #define FILESTG_SIGNATURE_NORMAL 0xF8995568
59 HWPFile *HWPFile::cur_doc = 0;
60 static int ccount = 0;
61 static int pcount = 0;
62 static int datecodecount = 0;
64 HWPFile::HWPFile(void)
66 Init();
70 /**
71 * TODO : Ãß°¡µÈ ½ºÅ¸Àϸ®½ºÆ®¿¡ ´ëÇÑ ¸Þ¸ð¸® ÇØÁ¦
73 HWPFile::~HWPFile(void)
75 if (hiodev)
76 delete hiodev;
78 LinkedListIterator < ColumnInfo > it_column(&columnlist);
79 for (; it_column.current(); it_column++)
80 delete it_column.current();
82 LinkedListIterator < HWPPara > it(&plist);
83 for (; it.current(); it++)
84 delete it.current();
86 LinkedListIterator < Table > tbl(&tables);
87 for (; tbl.current(); tbl++)
88 delete tbl.current();
90 LinkedListIterator < HyperText > hyp(&hyperlist);
91 for (; hyp.current(); hyp++)
93 delete hyp.current();
98 void HWPFile::Init(void)
100 version = HWP_V30;
101 info_block_len = 0;
102 compressed = false;
103 encrypted = false;
105 error_code = HWP_NoError;
106 hiodev = 0;
107 oledata = 0;
108 SetCurrentDoc(this);
109 currenthyper = 0;
110 m_nCurrentPage = 1;
111 m_nMaxSettedPage = 0;
115 int HWPFile::ReadHwpFile(HStream & stream)
117 if (Open(stream) != HWP_NoError)
118 return State();
119 // printf("HWPFile::ReadHwpFile\n");
120 InfoRead();
121 // printf("HWPFile::InfoRead Done.\n");
122 FontRead();
123 // printf("HWPFile::FontRead Done.\n");
124 StyleRead();
125 // printf("HWPFile::StyleRead Done.\n");
126 AddColumnInfo();
127 ParaListRead();
128 // printf("HWPFile::ParaListRead Done.\n");
129 TagsRead();
130 //printf("HWPFile::TagsRead Done. State: %d\n", State());
132 return State();
136 static int hwp_version(char *str)
138 if (memcmp(V20SIGNATURE, str, HWPIDLen) == 0)
139 return HWP_V20;
140 else if (memcmp(V21SIGNATURE, str, HWPIDLen) == 0)
141 return HWP_V21;
142 else if (memcmp(V30SIGNATURE, str, HWPIDLen) == 0)
143 return HWP_V30;
144 return 0;
148 // HIODev wrapper
150 int HWPFile::Open(HStream & stream)
152 HStreamIODev *hstreamio;
154 if (0 == (hstreamio = new HStreamIODev(stream)))
156 printf(" hstreamio is not instanciate \n");
157 return SetState(errno);
159 if (!hstreamio->open())
161 delete hstreamio;
163 return SetState(HWP_EMPTY_FILE);
165 SetIODevice(hstreamio);
167 char idstr[HWPIDLen];
169 if (ReadBlock(idstr, HWPIDLen) <= 0
170 || HWP_V30 != (version = hwp_version(idstr)))
172 return SetState(HWP_UNSUPPORTED_VERSION);
174 return HWP_NoError;
178 int HWPFile::State(void) const
180 return error_code;
184 int HWPFile::SetState(int errcode)
186 error_code = errcode;
187 return error_code;
191 int HWPFile::Read1b(void)
193 return hiodev ? hiodev->read1b() : -1;
197 int HWPFile::Read2b(void)
199 return hiodev ? hiodev->read2b() : -1;
203 long HWPFile::Read4b(void)
205 return hiodev ? hiodev->read4b() : -1;
209 int HWPFile::Read1b(void *ptr, size_t nmemb)
211 return hiodev ? hiodev->read1b(ptr, nmemb) : 0;
215 int HWPFile::Read2b(void *ptr, size_t nmemb)
217 return hiodev ? hiodev->read2b(ptr, nmemb) : 0;
221 int HWPFile::Read4b(void *ptr, size_t nmemb)
223 return hiodev ? hiodev->read4b(ptr, nmemb) : 0;
227 size_t HWPFile::ReadBlock(void *ptr, size_t size)
229 return hiodev ? hiodev->readBlock(ptr, size) : 0;
233 size_t HWPFile::SkipBlock(size_t size)
235 return hiodev ? hiodev->skipBlock(size) : 0;
239 bool HWPFile::SetCompressed(bool flag)
241 return hiodev ? hiodev->setCompressed(flag) : false;
245 HIODev *HWPFile::SetIODevice(HIODev * new_hiodev)
247 HIODev *old_hiodev = hiodev;
249 hiodev = new_hiodev;
250 return old_hiodev;
254 // end of HIODev wrapper
256 bool HWPFile::InfoRead(void)
258 return _hwpInfo.Read(*this);
262 bool HWPFile::FontRead(void)
264 return _hwpFont.Read(*this);
268 bool HWPFile::StyleRead(void)
270 return _hwpStyle.Read(*this);
274 bool HWPFile::ParaListRead(void)
276 return ReadParaList(plist);
279 bool HWPFile::ReadParaList(LinkedList < HWPPara > &aplist, unsigned char flag)
281 LinkedListIterator < HWPPara > it(&aplist);
283 HWPPara *spNode = new HWPPara;
284 unsigned char tmp_etcflag;
285 unsigned char prev_etcflag = 0;
286 while (spNode->Read(*this, flag))
288 if( !(spNode->etcflag & 0x04) ){
289 tmp_etcflag = spNode->etcflag;
290 spNode->etcflag = prev_etcflag;
291 prev_etcflag = tmp_etcflag;
293 if (spNode->nch && spNode->reuse_shape)
295 if (aplist.count()){
296 spNode->pshape = aplist.last()->pshape;
298 else{
299 spNode->nch = 0;
300 spNode->reuse_shape = 0;
303 spNode->pshape.pagebreak = spNode->etcflag;
304 if( spNode->nch )
305 AddParaShape( &spNode->pshape );
307 if (aplist.count())
308 aplist.last()->SetNext(spNode);
309 aplist.insert(spNode, -1);
310 spNode = new HWPPara;
312 delete spNode;
314 return true;
318 bool HWPFile::TagsRead(void)
320 ulong tag;
321 long size;
323 while (1)
325 tag = Read4b();
326 size = Read4b();
327 if (size <= 0 && tag > 0){
328 //return false;
329 continue;
332 if (tag == FILETAG_END_OF_COMPRESSED ||
333 tag == FILETAG_END_OF_UNCOMPRESSED)
334 return true;
335 switch (tag)
337 case FILETAG_EMBEDDED_PICTURE:
339 EmPicture *emb = new EmPicture(size);
341 if (true == emb->Read(*this))
342 emblist.insert(emb, -1);
343 else
344 delete emb;
346 break;
347 case FILETAG_OLE_OBJECT:
348 oledata = new OlePicture(size);
349 oledata->Read(*this);
350 break;
351 case FILETAG_HYPERTEXT:
353 if( (size % 617) != 0 )
354 SkipBlock( size );
355 else
356 for( int i = 0 ; i < size/617 ; i++)
358 HyperText *hypert = new HyperText;
359 hypert->Read(*this);
360 hyperlist.insert(hypert, -1);
362 break;
364 case 6:
366 ReadBlock(_hwpInfo.back_info.reserved1, 8);
367 _hwpInfo.back_info.luminance = Read4b();
368 _hwpInfo.back_info.contrast = Read4b();
369 _hwpInfo.back_info.effect = sal::static_int_cast<char>(Read1b());
370 ReadBlock(_hwpInfo.back_info.reserved2, 7);
371 ReadBlock(_hwpInfo.back_info.filename, 260);
372 ReadBlock(_hwpInfo.back_info.color, 3);
373 unsigned short nFlag = sal::static_int_cast<unsigned short>(Read2b());
374 _hwpInfo.back_info.flag = nFlag >> 8 ;
375 int nRange = Read4b();
376 _hwpInfo.back_info.range = nRange >> 24;
377 ReadBlock(_hwpInfo.back_info.reserved3, 27);
378 _hwpInfo.back_info.size = Read4b();
380 _hwpInfo.back_info.data = new char[(unsigned int)_hwpInfo.back_info.size];
381 ReadBlock(_hwpInfo.back_info.data, _hwpInfo.back_info.size);
383 if( _hwpInfo.back_info.size > 0 )
384 _hwpInfo.back_info.type = 2;
385 else if( _hwpInfo.back_info.filename[0] )
386 _hwpInfo.back_info.type = 1;
387 else
388 _hwpInfo.back_info.type = 0;
391 _hwpInfo.back_info.isset = true;
393 break;
395 case FILETAG_PRESENTATION:
396 case FILETAG_PREVIEW_IMAGE:
397 case FILETAG_PREVIEW_TEXT:
398 default:
399 SkipBlock(size);
402 // return false;
406 ColumnDef *HWPFile::GetColumnDef(int num)
408 ColumnInfo *cinfo = columnlist.find(num);
409 if( cinfo )
410 return cinfo->coldef;
411 else
412 return 0;
414 /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
415 int HWPFile::GetPageMasterNum(int page)
417 LinkedListIterator<ColumnInfo> it(&columnlist);
418 //os: unused
419 //ColumnInfo *prev = 0;
420 ColumnInfo *now = 0;
421 int i;
423 for( i = 1 ; it.current() ; it++, i++){
424 now = it.current();
425 if( page < now->start_page )
426 return i-1;
428 return i-1;
431 HyperText *HWPFile::GetHyperText()
433 return hyperlist.find(currenthyper++);
436 EmPicture *HWPFile::GetEmPicture(Picture * pic)
438 char *name = pic->picinfo.picembed.embname;
440 name[0] = 'H';
441 name[1] = 'W';
442 name[2] = 'P';
444 LinkedListIterator < EmPicture > it(&emblist);
445 for (; it.current(); it++)
446 if (strcmp(name, it.current()->name) == 0)
447 return it.current();
448 return 0;
451 EmPicture *HWPFile::GetEmPictureByName(char * name)
453 name[0] = 'H';
454 name[1] = 'W';
455 name[2] = 'P';
457 LinkedListIterator < EmPicture > it(&emblist);
458 for (; it.current(); it++)
459 if (strcmp(name, it.current()->name) == 0)
460 return it.current();
461 return 0;
465 void HWPFile::AddBox(FBox * box)
467 // LATER if we don't use box->next(),
468 // AddBox() and GetBoxHead() are useless;
469 if (blist.count())
471 box->prev = blist.last();
472 box->prev->next = box;
474 else
475 box->prev = 0;
476 blist.insert(box, -1);
480 ParaShape *HWPFile::getParaShape(int index)
482 return pslist.find(index);
486 CharShape *HWPFile::getCharShape(int index)
488 return cslist.find(index);
492 FBoxStyle *HWPFile::getFBoxStyle(int index)
494 return fbslist.find(index);
497 DateCode *HWPFile::getDateCode(int index)
499 return datecodes.find(index);
502 HeaderFooter *HWPFile::getHeaderFooter(int index)
504 return headerfooters.find(index);
507 ShowPageNum *HWPFile::getPageNumber(int index)
509 return pagenumbers.find(index);
512 Table *HWPFile::getTable(int index)
514 return tables.find(index);
517 void HWPFile::AddParaShape(ParaShape * pshape)
519 int nscount = 0;
520 for(int j = 0 ; j < MAXTABS-1 ; j++)
522 if( j > 0 && pshape->tabs[j].position == 0 )
523 break;
524 if( pshape->tabs[0].position == 0 ){
525 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
526 (pshape->tabs[j].position != 1000 *j) )
527 nscount = j;
529 else{
530 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
531 (pshape->tabs[j].position != 1000 * (j + 1)) )
532 nscount = j;
535 if( nscount )
536 pshape->tabs[MAXTABS-1].type = sal::static_int_cast<char>(nscount);
537 int value = compareParaShape(pshape);
538 if( value == 0 || nscount )
540 pshape->index = ++pcount;
541 pslist.insert(pshape, -1);
543 else
544 pshape->index = value;
548 void HWPFile::AddCharShape(CharShape * cshape)
550 int value = compareCharShape(cshape);
551 if( value == 0 )
553 cshape->index = ++ccount;
554 cslist.insert(cshape, -1);
556 else
557 cshape->index = value;
560 void HWPFile::AddColumnInfo()
562 ColumnInfo *cinfo = new ColumnInfo(m_nCurrentPage);
563 columnlist.insert(cinfo, -1);
564 setMaxSettedPage();
567 void HWPFile::SetColumnDef(ColumnDef *coldef)
569 ColumnInfo *cinfo = columnlist.last();
570 if( cinfo->bIsSet )
571 return;
572 cinfo->coldef = coldef;
573 cinfo->bIsSet = true;
576 void HWPFile::AddDateFormat(DateCode * hbox)
578 hbox->key = sal::static_int_cast<char>(++datecodecount);
579 datecodes.insert(hbox, -1);
582 void HWPFile::AddPageNumber(ShowPageNum * hbox)
584 pagenumbers.insert(hbox, -1);
587 void HWPFile::AddHeaderFooter(HeaderFooter * hbox)
589 headerfooters.insert(hbox, -1);
592 void HWPFile::AddTable(Table * hbox)
594 tables.insert(hbox, -1);
597 void HWPFile::AddFBoxStyle(FBoxStyle * fbstyle)
599 fbslist.insert(fbstyle, -1);
602 int HWPFile::compareCharShape(CharShape *shape)
604 int count = cslist.count();
605 if( count > 0 )
607 CharShape *cshape=0;
608 for(int i = 0; i< count; i++)
610 cshape = cslist.find(i);
612 if( shape->size == cshape->size &&
613 shape->font[0] == cshape->font[0] &&
614 shape->ratio[0] == cshape->ratio[0] &&
615 shape->space[0] == cshape->space[0] &&
616 shape->color[1] == cshape->color[1] &&
617 shape->color[0] == cshape->color[0] &&
618 shape->shade == cshape->shade &&
619 shape->attr == cshape->attr )
621 return cshape->index;
625 return 0;
629 int HWPFile::compareParaShape(ParaShape *shape)
631 int count = pslist.count();
632 if( count > 0 )
634 ParaShape *pshape=0;
635 for(int i = 0; i< count; i++)
637 pshape = pslist.find(i);
638 if( shape->left_margin == pshape->left_margin &&
639 shape->right_margin == pshape->right_margin &&
640 shape->pspacing_prev == pshape->pspacing_prev &&
641 shape->pspacing_next == pshape->pspacing_next &&
642 shape->indent == pshape->indent &&
643 shape->lspacing == pshape->lspacing &&
644 shape->arrange_type == pshape->arrange_type &&
645 shape->outline == pshape->outline &&
646 shape->pagebreak == pshape->pagebreak)
648 if( shape->cshape->size == pshape->cshape->size &&
649 shape->cshape->font[0] == pshape->cshape->font[0] &&
650 shape->cshape->ratio[0] == pshape->cshape->ratio[0] &&
651 shape->cshape->space[0] == pshape->cshape->space[0] &&
652 shape->cshape->color[1] == pshape->cshape->color[1] &&
653 shape->cshape->color[0] == pshape->cshape->color[0] &&
654 shape->cshape->shade == pshape->cshape->shade &&
655 shape->cshape->attr == pshape->cshape->attr )
657 return pshape->index;
662 return 0;
666 HWPFile *GetCurrentDoc(void)
668 return HWPFile::cur_doc;
672 HWPFile *SetCurrentDoc(HWPFile * hwpfp)
674 HWPFile *org = HWPFile::cur_doc;
676 HWPFile::cur_doc = hwpfp;
677 return org;