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 #ifndef INCLUDED_HWPFILTER_SOURCE_DRAWING_H
21 #define INCLUDED_HWPFILTER_SOURCE_DRAWING_H
23 #include "precompile.h"
28 #include <osl/diagnose.h>
30 #include <comphelper/newarray.hxx>
48 BEGIN_GRADATION
= 0, LINEAR
, RADIAL
, CONICAL
, SQUARE
,
49 END_GRADATION
, BITMAP_PATTERN
52 #define OBJRET_FILE_OK 0
53 #define OBJRET_FILE_ERROR (-1)
54 #define OBJRET_FILE_NO_PRIVATE_BLOCK (-2)
55 #define OBJRET_FILE_NO_PRIVATE_BLOCK_2 (-3)
57 typedef int (*HWPDOFuncType
) (int, HWPDrawingObject
*, int, void *, int);
59 #define HWPDOFunc(hdo, cmd, argp, argv) \
60 (HWPDOFuncTbl[(hdo)->type]((hdo)->type, (hdo), (cmd), (argp), (argv)))
62 static int HWPDOLineFunc(int, HWPDrawingObject
*, int, void *, int);
63 static int HWPDORectFunc(int, HWPDrawingObject
*, int, void *, int);
64 static int HWPDOEllipseFunc(int, HWPDrawingObject
*, int, void *, int);
65 static int HWPDOArcFunc(int, HWPDrawingObject
*, int, void *, int);
66 static int HWPDOFreeFormFunc(int, HWPDrawingObject
*, int, void *, int);
67 static int HWPDOTextBoxFunc(int, HWPDrawingObject
*, int, void *, int);
68 static int HWPDOEllipse2Func(int, HWPDrawingObject
*, int, void *, int);
69 static int HWPDOArc2Func(int, HWPDrawingObject
*, int, void *, int);
70 static int HWPDOContainerFunc(int, HWPDrawingObject
*, int, void *, int);
71 static HWPPara
*LoadParaList();
73 HWPDOFuncType HWPDOFuncTbl
[] =
88 static HMemIODev
*hmem
= 0;
92 inline bool HAVE_FCOLOR(HWPDrawingObject
* hdo
)
94 return hdo
->property
.fill_color
!= HWPDO_COLOR_NONE
;
98 inline bool HAVE_PATTERN(HWPDrawingObject
* hdo
)
100 return (hdo
->property
.pattern_type
& HWPDO_PAT_TYPE_BITS
)
101 != HWPDO_PAT_SOLID
&& hdo
->property
.pattern_color
!= HWPDO_COLOR_NONE
;
105 inline bool HAVE_GRADATION(HWPDrawingObject
* hdo
)
107 return hdo
->property
.gstyle
> BEGIN_GRADATION
&&
108 hdo
->property
.gstyle
< END_GRADATION
&&
109 hdo
->property
.fromcolor
!= HWPDO_COLOR_NONE
&&
110 hdo
->property
.tocolor
!= HWPDO_COLOR_NONE
;
114 inline bool HAVE_BITMAP_PATTERN(HWPDrawingObject
* hdo
)
116 return hdo
->property
.gstyle
== BITMAP_PATTERN
&&
117 hdo
->property
.szPatternFile
[0];
121 inline bool HAS_PAT(HWPDrawingObject
* hdo
)
123 return HAVE_FCOLOR(hdo
) || HAVE_PATTERN(hdo
) ||
124 HAVE_GRADATION(hdo
) || HAVE_BITMAP_PATTERN(hdo
);
127 static void SetHdoParallRgn(HWPDrawingObject
* hdo
, int width
, int height
)
129 hdo
->property
.parall
.pt
[0].x
= 0;
130 hdo
->property
.parall
.pt
[0].y
= 0;
131 hdo
->property
.parall
.pt
[1].x
= width
;
132 hdo
->property
.parall
.pt
[1].y
= 0;
133 hdo
->property
.parall
.pt
[2].x
= width
;
134 hdo
->property
.parall
.pt
[2].y
= height
;
137 static bool SkipPrivateBlock(int type
)
141 if (type
== OBJRET_FILE_NO_PRIVATE_BLOCK
)
143 if (!hmem
->read4b(n
))
145 if (hmem
->state() || hmem
->skipBlock(n
) != n
)
148 if (!hmem
->read4b(n
))
152 return hmem
->skipBlock(n
) == n
;
155 static int SizeExpected
;
158 static int ReadSizeField(int size
)
161 if (!hmem
->read4b(SizeRead
))
168 static bool SkipUnusedField(void)
170 return (SizeExpected
>= SizeRead
) &&
171 hmem
->skipBlock(SizeRead
- SizeExpected
) != 0;
175 #define HDOFILE_HEADER_SIZE (2*4+16) // 16=sizeof(ZZRect)
176 #define HDOFILE_COMMON_SIZE (7*4+16+44)
178 #define HDOFILE_HAS_NEXT 0x01
179 #define HDOFILE_HAS_CHILD 0x02
181 static bool LoadCommonHeader(HWPDrawingObject
* hdo
, unsigned short * link_info
)
183 uint size
, common_size
;
187 if (!hmem
->read4b(size
))
191 if (size
< HDOFILE_COMMON_SIZE
)
194 common_size
= HDOFILE_COMMON_SIZE
;
195 unsigned short tmp16
;
196 if (!hmem
->read2b(tmp16
))
199 if (!hmem
->read2b(tmp16
))
202 if (!hmem
->read4b(hdo
->offset
.x
))
204 if (!hmem
->read4b(hdo
->offset
.y
))
206 if (!hmem
->read4b(hdo
->extent
.w
))
208 if (!hmem
->read4b(hdo
->extent
.h
))
210 if (!hmem
->read4b(hdo
->offset2
.x
))
212 if (!hmem
->read4b(hdo
->offset2
.y
))
218 if (!hmem
->read4b(hdo
->vrect
.x
))
220 if (!hmem
->read4b(hdo
->vrect
.y
))
222 if (!hmem
->read4b(hdo
->vrect
.w
))
224 if (!hmem
->read4b(hdo
->vrect
.h
))
227 // read bare property 44 bytes
228 if (!hmem
->read4b(hdo
->property
.line_pstyle
))
230 if (!hmem
->read4b(hdo
->property
.line_hstyle
))
232 if (!hmem
->read4b(hdo
->property
.line_tstyle
))
234 if (!hmem
->read4b(hdo
->property
.line_color
))
237 if (!hmem
->read4b(tmp32
))
239 hdo
->property
.line_width
= static_cast<hunit
>(tmp32
);
240 if (!hmem
->read4b(hdo
->property
.fill_color
))
242 if (!hmem
->read4b(hdo
->property
.pattern_type
))
244 if (!hmem
->read4b(hdo
->property
.pattern_color
))
246 if (!hmem
->read4b(tmp32
))
248 hdo
->property
.hmargin
= static_cast<hunit
>(tmp32
);
249 if (!hmem
->read4b(tmp32
))
251 hdo
->property
.vmargin
= static_cast<hunit
>(tmp32
);
252 if (!hmem
->read4b(hdo
->property
.flag
))
254 // read rotation property 32 bytes
255 if ((size
>= common_size
+ 32)
256 && (hdo
->property
.flag
& HWPDO_FLAG_ROTATION
))
258 if (!hmem
->read4b(hdo
->property
.rot_originx
))
260 if (!hmem
->read4b(hdo
->property
.rot_originy
))
262 for (int ii
= 0; ii
< 3; ++ii
)
264 if (!hmem
->read4b(hdo
->property
.parall
.pt
[ii
].x
))
266 if (!hmem
->read4b(hdo
->property
.parall
.pt
[ii
].y
))
272 SetHdoParallRgn(hdo
, hdo
->extent
.w
, hdo
->extent
.h
);
274 // read gradient property 28 bytes
275 if ((size
>= common_size
+ 28) &&
276 (hdo
->property
.flag
& HWPDO_FLAG_GRADATION
))
278 if (!hmem
->read4b(hdo
->property
.fromcolor
))
280 if (!hmem
->read4b(hdo
->property
.tocolor
))
282 if (!hmem
->read4b(hdo
->property
.gstyle
))
284 if (!hmem
->read4b(hdo
->property
.angle
))
286 if (!hmem
->read4b(hdo
->property
.center_x
))
288 if (!hmem
->read4b(hdo
->property
.center_y
))
290 if (!hmem
->read4b(hdo
->property
.nstep
))
295 // read bitmap property 278 bytes
296 if ((size
>= common_size
+ 278) && \
297 (hdo
->property
.flag
& HWPDO_FLAG_BITMAP
))
299 if (!hmem
->read4b(hdo
->property
.offset1
.x
))
301 if (!hmem
->read4b(hdo
->property
.offset1
.y
))
303 if (!hmem
->read4b(hdo
->property
.offset2
.x
))
305 if (!hmem
->read4b(hdo
->property
.offset2
.y
))
307 if (!hmem
->readBlock(hdo
->property
.szPatternFile
, 261))
309 if (!hmem
->read1b(hdo
->property
.pictype
))
313 if( ( size
>= common_size
+ 3 ) && ( hdo
->property
.flag
& HWPDO_FLAG_WATERMARK
) )
314 //if( ( size >= common_size ) && ( hdo->property.flag >> 20 & 0x01 ) )
316 if (size
- common_size
>= 5)
319 if (!hmem
->read1b(tmp8
))
321 hdo
->property
.luminance
= tmp8
;
322 if (!hmem
->read1b(tmp8
))
324 hdo
->property
.contrast
= tmp8
;
325 if (!hmem
->read1b(tmp8
))
327 hdo
->property
.greyscale
= tmp8
;
333 hdo
->property
.luminance
= 0;
334 hdo
->property
.contrast
= 0;
335 hdo
->property
.greyscale
= 0;
337 hdo
->property
.pPara
= 0L;
339 if( ( size
> common_size
) && (hdo
->property
.flag
& HWPDO_FLAG_AS_TEXTBOX
) )
342 hdo
->property
.pPara
= LoadParaList();
343 if( hdo
->property
.pPara
)
349 if (size
<= common_size
)
351 return hmem
->skipBlock(size
- common_size
) != 0;
354 static HWPDrawingObject
*LoadDrawingObject(void)
356 fprintf(stderr
, "LoadDrawingObject\n");
358 HWPDrawingObject
*hdo
, *head
, *prev
;
361 unsigned short link_info
;
366 hdo
= new HWPDrawingObject
;
367 if (!LoadCommonHeader(hdo
, &link_info
))
371 if (hdo
->type
< 0 || hdo
->type
>= HWPDO_NITEMS
)
373 hdo
->type
= HWPDO_RECT
;
374 if (!SkipPrivateBlock(OBJRET_FILE_NO_PRIVATE_BLOCK
))
381 switch (res
= HWPDOFunc(hdo
, OBJFUNC_LOAD
, NULL
, 0))
383 case OBJRET_FILE_ERROR
:
387 case OBJRET_FILE_NO_PRIVATE_BLOCK
:
388 case OBJRET_FILE_NO_PRIVATE_BLOCK_2
:
389 if (!SkipPrivateBlock(res
))
394 if (link_info
& HDOFILE_HAS_CHILD
)
396 hdo
->child
= LoadDrawingObject();
397 if (hdo
->child
== NULL
)
408 while (link_info
& HDOFILE_HAS_NEXT
);
412 // drawing object can be list.
413 // hdo = current item, head = list;
417 if (hdo
->type
< 0 || hdo
->type
>= HWPDO_NITEMS
)
419 hdo
->type
= HWPDO_RECT
;
422 HWPDOFunc(hdo
, OBJFUNC_FREE
, NULL
, 0);
435 static bool LoadDrawingObjectBlock(Picture
* pic
)
438 if (!hmem
->read4b(size
))
441 if (hmem
->state() || size
< HDOFILE_HEADER_SIZE
)
444 if (!hmem
->read4b(pic
->picinfo
.picdraw
.zorder
))
446 if (!hmem
->read4b(pic
->picinfo
.picdraw
.mbrcnt
))
448 if (!hmem
->read4b(pic
->picinfo
.picdraw
.vrect
.x
))
450 if (!hmem
->read4b(pic
->picinfo
.picdraw
.vrect
.y
))
452 if (!hmem
->read4b(pic
->picinfo
.picdraw
.vrect
.w
))
454 if (!hmem
->read4b(pic
->picinfo
.picdraw
.vrect
.h
))
457 if (size
> HDOFILE_HEADER_SIZE
&&
458 !hmem
->skipBlock(size
- HDOFILE_HEADER_SIZE
))
461 pic
->picinfo
.picdraw
.hdo
= LoadDrawingObject();
462 if (pic
->picinfo
.picdraw
.hdo
== 0)
467 // object manipulation function
469 HWPDODefaultFunc(int , HWPDrawingObject
* , int cmd
, void *, int)
471 if (cmd
== OBJFUNC_LOAD
)
472 return OBJRET_FILE_NO_PRIVATE_BLOCK
;
473 return OBJRET_FILE_OK
;
477 HWPDOLineFunc(int type
, HWPDrawingObject
* hdo
, int cmd
, void *argp
, int argv
)
479 int ret
= OBJRET_FILE_OK
;
483 if (ReadSizeField(4) < 4)
484 return OBJRET_FILE_ERROR
;
485 if (!hmem
->read4b(hdo
->u
.line_arc
.flip
))
486 return OBJRET_FILE_ERROR
;
488 return OBJRET_FILE_ERROR
;
489 if (!SkipUnusedField())
490 return OBJRET_FILE_ERROR
;
491 ret
= OBJRET_FILE_NO_PRIVATE_BLOCK_2
;
494 ret
= HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
504 HWPDORectFunc(int type
, HWPDrawingObject
* hdo
, int cmd
, void *argp
, int argv
)
506 return HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
513 HWPDOEllipseFunc(int type
, HWPDrawingObject
* hdo
,
514 int cmd
, void *argp
, int argv
)
516 return HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
519 #define WTMM(x) ((double)(x) / 1800. * 25.4)
521 HWPDOEllipse2Func(int type
, HWPDrawingObject
* hdo
,
522 int cmd
, void *argp
, int argv
)
527 if (ReadSizeField(16) < 16)
528 return OBJRET_FILE_ERROR
;
529 if (!hmem
->read4b(hdo
->u
.arc
.radial
[0].x
))
530 return OBJRET_FILE_ERROR
;
531 if (!hmem
->read4b(hdo
->u
.arc
.radial
[0].y
))
532 return OBJRET_FILE_ERROR
;
533 if (!hmem
->read4b(hdo
->u
.arc
.radial
[1].x
))
534 return OBJRET_FILE_ERROR
;
535 if (!hmem
->read4b(hdo
->u
.arc
.radial
[1].y
))
536 return OBJRET_FILE_ERROR
;
537 if (ReadSizeField(0) < 0)
538 return OBJRET_FILE_ERROR
;
541 return HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
543 return OBJRET_FILE_OK
;
550 HWPDOArcFunc(int type
, HWPDrawingObject
* hdo
, int cmd
, void *argp
, int argv
)
555 if (ReadSizeField(4) < 4)
556 return OBJRET_FILE_ERROR
;
557 if (!hmem
->read4b(hdo
->u
.line_arc
.flip
))
558 return OBJRET_FILE_ERROR
;
560 return OBJRET_FILE_ERROR
;
561 if (!SkipUnusedField())
562 return OBJRET_FILE_ERROR
;
565 return HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
567 return OBJRET_FILE_OK
;
572 HWPDOArc2Func(int type
, HWPDrawingObject
* hdo
, int cmd
, void *argp
, int argv
)
574 int ret
= OBJRET_FILE_OK
;
578 ret
= OBJRET_FILE_NO_PRIVATE_BLOCK
;
581 ret
= HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
589 HWPDOFreeFormFunc(int type
, HWPDrawingObject
* hdo
,
590 int cmd
, void *argp
, int argv
)
596 hdo
->u
.freeform
.pt
= 0;
597 if (ReadSizeField(4) < 4)
598 return OBJRET_FILE_ERROR
;
599 if (!hmem
->read4b(hdo
->u
.freeform
.npt
))
600 return OBJRET_FILE_ERROR
;
602 return OBJRET_FILE_ERROR
;
603 if (!SkipUnusedField())
604 return OBJRET_FILE_ERROR
;
606 int size
= hdo
->u
.freeform
.npt
* sizeof(ZZPoint
);
608 if (ReadSizeField(size
) < size
)
609 return OBJRET_FILE_ERROR
;
610 if (hdo
->u
.freeform
.npt
)
613 ::comphelper::newArray_null
<ZZPoint
>(hdo
->u
.freeform
.npt
);
614 if (hdo
->u
.freeform
.pt
== NULL
)
616 hdo
->u
.freeform
.npt
= 0;
617 return OBJRET_FILE_ERROR
;
619 for (int ii
= 0; ii
< hdo
->u
.freeform
.npt
; ++ii
)
621 bool bFailure
= false;
622 if (!hmem
->read4b(hdo
->u
.freeform
.pt
[ii
].x
))
624 if (!hmem
->read4b(hdo
->u
.freeform
.pt
[ii
].y
))
630 delete[]hdo
->u
.freeform
.pt
;
631 hdo
->u
.freeform
.npt
= 0;
632 return OBJRET_FILE_ERROR
;
636 if (!SkipUnusedField())
637 return OBJRET_FILE_ERROR
;
638 return OBJRET_FILE_OK
;
641 if (hdo
->u
.freeform
.pt
)
642 delete[]hdo
->u
.freeform
.pt
;
645 return HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
647 return OBJRET_FILE_OK
;
653 static void FreeParaList(HWPPara
* para
)
656 FreeParaList(para
->Next());
661 static HWPPara
*LoadParaList()
666 HWPFile
*hwpf
= GetCurrentDoc();
667 HIODev
*hio
= hwpf
->SetIODevice(hmem
);
669 std::list
< HWPPara
* > plist
;
671 hwpf
->ReadParaList(plist
);
672 hwpf
->SetIODevice(hio
);
674 return plist
.size()? plist
.front() : 0;
679 HWPDOTextBoxFunc(int type
, HWPDrawingObject
* hdo
,
680 int cmd
, void *argp
, int argv
)
685 if (ReadSizeField(0) < 0 || !SkipUnusedField())
686 return OBJRET_FILE_ERROR
;
687 if (ReadSizeField(0) < 0)
688 return OBJRET_FILE_ERROR
;
689 hdo
->u
.textbox
.h
= LoadParaList();
690 return hdo
->u
.textbox
.h
? OBJRET_FILE_OK
: OBJRET_FILE_ERROR
;
692 if (hdo
->u
.textbox
.h
)
694 FreeParaList(hdo
->u
.textbox
.h
);
695 hdo
->u
.textbox
.h
= NULL
;
699 return HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
701 return OBJRET_FILE_OK
;
707 HWPDOContainerFunc(int type
, HWPDrawingObject
* hdo
,
708 int cmd
, void *argp
, int argv
)
710 return HWPDODefaultFunc(type
, hdo
, cmd
, argp
, argv
);
714 HWPDrawingObject::HWPDrawingObject()
716 memset(this, 0, sizeof(HWPDrawingObject
));
721 HWPDrawingObject::~HWPDrawingObject()
729 HWPDOFunc(this, OBJFUNC_FREE
, NULL
, 0);
733 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */