1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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;
50 , error_code(HWP_NoError
)
65 std::list
< ColumnInfo
* >::iterator it_column
= columnlist
.begin();
66 for (; it_column
!= columnlist
.end(); ++it_column
)
69 std::list
< HWPPara
* >::iterator it
= plist
.begin();
70 for (; it
!= plist
.end(); ++it
)
73 std::list
< Table
* >::iterator tbl
= tables
.begin();
74 for (; tbl
!= tables
.end(); ++tbl
)
77 std::list
< HyperText
* >::iterator hyp
= hyperlist
.begin();
78 for (; hyp
!= hyperlist
.end(); ++hyp
)
84 int HWPFile::ReadHwpFile(HStream
* stream
)
86 if (Open(stream
) != HWP_NoError
)
98 int detect_hwp_version(const char *str
)
100 if (memcmp(V20SIGNATURE
, str
, HWPIDLen
) == 0)
102 else if (memcmp(V21SIGNATURE
, str
, HWPIDLen
) == 0)
104 else if (memcmp(V30SIGNATURE
, str
, HWPIDLen
) == 0)
111 int HWPFile::Open(HStream
* stream
)
113 HStreamIODev
*hstreamio
= new HStreamIODev(stream
);
115 if (!hstreamio
->open())
119 return SetState(HWP_EMPTY_FILE
);
122 HIODev
*pPrev
= SetIODevice(hstreamio
);
125 char idstr
[HWPIDLen
];
127 if (ReadBlock(idstr
, HWPIDLen
) <= 0
128 || HWP_V30
!= (version
= detect_hwp_version(idstr
)))
130 return SetState(HWP_UNSUPPORTED_VERSION
);
135 int HWPFile::SetState(int errcode
)
137 error_code
= errcode
;
141 bool HWPFile::Read1b(unsigned char &out
)
143 return hiodev
&& hiodev
->read1b(out
);
146 bool HWPFile::Read1b(char &out
)
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
)
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
;
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
;
263 spNode
->reuse_shape
= 0;
266 spNode
->pshape
.pagebreak
= spNode
->etcflag
;
268 AddParaShape( &spNode
->pshape
);
271 aplist
.back()->SetNext(spNode
);
272 aplist
.push_back(spNode
);
273 spNode
= new HWPPara
;
280 void HWPFile::TagsRead()
290 if (size
<= 0 && tag
> 0){
294 if (tag
== FILETAG_END_OF_COMPRESSED
||
295 tag
== FILETAG_END_OF_UNCOMPRESSED
)
299 case FILETAG_EMBEDDED_PICTURE
:
301 EmPicture
*emb
= new EmPicture(size
);
303 if (emb
->Read(*this))
304 emblist
.push_back(emb
);
309 case FILETAG_OLE_OBJECT
:
312 oledata
= new OlePicture(size
);
313 oledata
->Read(*this);
315 case FILETAG_HYPERTEXT
:
317 if( (size
% 617) != 0 )
321 for( int i
= 0 ; i
< size
/617 ; i
++)
323 HyperText
*hypert
= new HyperText
;
325 hyperlist
.push_back(hypert
);
332 ReadBlock(_hwpInfo
.back_info
.reserved1
, 8);
333 if (!Read4b(_hwpInfo
.back_info
.luminance
))
335 if (!Read4b(_hwpInfo
.back_info
.contrast
))
337 if (!Read1b(_hwpInfo
.back_info
.effect
))
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
;
345 _hwpInfo
.back_info
.flag
= nFlag
>> 8 ;
349 _hwpInfo
.back_info
.range
= nRange
>> 24;
350 ReadBlock(_hwpInfo
.back_info
.reserved3
, 27);
351 if (!Read4b(_hwpInfo
.back_info
.size
))
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;
362 _hwpInfo
.back_info
.type
= 0;
365 _hwpInfo
.back_info
.isset
= true;
369 case FILETAG_PRESENTATION
:
370 case FILETAG_PREVIEW_IMAGE
:
371 case FILETAG_PREVIEW_TEXT
:
379 ColumnDef
*HWPFile::GetColumnDef(int num
)
381 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
383 for(int i
= 0; it
!= columnlist
.end() ; ++it
, i
++){
388 if( it
!= columnlist
.end() )
389 return (*it
)->coldef
;
393 /* @return 인덱스는 1부터 시작한다. */
394 int HWPFile::GetPageMasterNum(int page
)
396 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
399 for( i
= 1 ; it
!= columnlist
.end() ; ++it
, i
++){
400 ColumnInfo
*now
= *it
;
401 if( page
< now
->start_page
)
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
)
417 return it
!= hyperlist
.end() ? *it
: NULL
;
420 EmPicture
*HWPFile::GetEmPicture(Picture
* pic
)
422 char *name
= pic
->picinfo
.picembed
.embname
;
428 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
429 for (; it
!= emblist
.end(); ++it
)
430 if (strcmp(name
, (*it
)->name
) == 0)
435 EmPicture
*HWPFile::GetEmPictureByName(char * name
)
441 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
442 for (; it
!= emblist
.end(); ++it
)
443 if (strcmp(name
, (*it
)->name
) == 0)
449 void HWPFile::AddBox(FBox
* box
)
451 // LATER if we don't use box->next(),
452 // AddBox() and GetBoxHead() are useless;
455 box
->prev
= blist
.back();
456 box
->prev
->next
= box
;
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
++ ){
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
++ ){
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
++ ){
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
++ ){
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
++ ){
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
++ ){
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
++ ){
548 return it
!= tables
.end() ? *it
: NULL
;
551 void HWPFile::AddParaShape(ParaShape
* pshape
)
554 for(int j
= 0 ; j
< MAXTABS
-1 ; j
++)
556 if( j
> 0 && pshape
->tabs
[j
].position
== 0 )
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
) )
564 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
565 (pshape
->tabs
[j
].position
!= 1000 * (j
+ 1)) )
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
);
578 pshape
->index
= value
;
582 void HWPFile::AddCharShape(CharShape
* cshape
)
584 int value
= compareCharShape(cshape
);
587 cshape
->index
= ++ccount
;
588 cslist
.push_back(cshape
);
591 cshape
->index
= value
;
594 void HWPFile::AddColumnInfo()
596 ColumnInfo
*cinfo
= new ColumnInfo(m_nCurrentPage
);
597 columnlist
.push_back(cinfo
);
601 void HWPFile::SetColumnDef(ColumnDef
*coldef
)
603 ColumnInfo
*cinfo
= columnlist
.back();
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();
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
;
662 int HWPFile::compareParaShape(ParaShape
*shape
)
664 int count
= pslist
.size();
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
;
699 HWPFile
*GetCurrentDoc()
701 return HWPFile::cur_doc
;
705 HWPFile
*SetCurrentDoc(HWPFile
* hwpfp
)
707 HWPFile
*org
= HWPFile::cur_doc
;
709 HWPFile::cur_doc
= hwpfp
;
713 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */