nss: upgrade to release 3.73
[LibreOffice.git] / hwpfilter / source / drawing.h
blob935b33171c15a5e196999852ac074888c2ee6b63
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 #ifndef INCLUDED_HWPFILTER_SOURCE_DRAWING_H
21 #define INCLUDED_HWPFILTER_SOURCE_DRAWING_H
23 #include "precompile.h"
25 #include <math.h>
27 #include <osl/diagnose.h>
29 #include <comphelper/newarray.hxx>
31 #include "hwplib.h"
32 #include "hwpfile.h"
33 #include "hiodev.h"
34 #include "hbox.h"
35 #include "drawdef.h"
37 enum
39 OBJFUNC_LOAD,
40 OBJFUNC_FREE,
41 OBJFUNC_DISPLAY,
42 OBJFUNC_NITEM
45 enum
47 BEGIN_GRADATION = 0, LINEAR, RADIAL, CONICAL, SQUARE,
48 END_GRADATION, BITMAP_PATTERN
51 #define OBJRET_FILE_OK 0
52 #define OBJRET_FILE_ERROR (-1)
53 #define OBJRET_FILE_NO_PRIVATE_BLOCK (-2)
54 #define OBJRET_FILE_NO_PRIVATE_BLOCK_2 (-3)
56 typedef int (*HWPDOFuncType) (int, HWPDrawingObject *, int, void *, int);
58 #define HWPDOFunc(hdo, cmd, argp, argv) \
59 (HWPDOFuncTbl[(hdo)->type]((hdo)->type, (hdo), (cmd), (argp), (argv)))
61 static int HWPDOLineFunc(int, HWPDrawingObject *, int, void *, int);
62 static int HWPDORectFunc(int, HWPDrawingObject *, int, void *, int);
63 static int HWPDOEllipseFunc(int, HWPDrawingObject *, int, void *, int);
64 static int HWPDOArcFunc(int, HWPDrawingObject *, int, void *, int);
65 static int HWPDOFreeFormFunc(int, HWPDrawingObject *, int, void *, int);
66 static int HWPDOTextBoxFunc(int, HWPDrawingObject *, int, void *, int);
67 static int HWPDOEllipse2Func(int, HWPDrawingObject *, int, void *, int);
68 static int HWPDOArc2Func(int, HWPDrawingObject *, int, void *, int);
69 static int HWPDOContainerFunc(int, HWPDrawingObject *, int, void *, int);
70 static HWPPara *LoadParaList();
72 HWPDOFuncType HWPDOFuncTbl[] =
74 HWPDOContainerFunc,
75 HWPDOLineFunc,
76 HWPDORectFunc,
77 HWPDOEllipseFunc,
78 HWPDOArcFunc,
79 HWPDOFreeFormFunc,
80 HWPDOTextBoxFunc,
81 HWPDOFreeFormFunc,
82 HWPDOEllipse2Func,
83 HWPDOArc2Func,
84 HWPDOFreeFormFunc,
87 static HIODev *hmem = nullptr;
89 static int count = 0;
91 static void SetHdoParallRgn(HWPDrawingObject * hdo, int width, int height)
93 hdo->property.parall.pt[0].x = 0;
94 hdo->property.parall.pt[0].y = 0;
95 hdo->property.parall.pt[1].x = width;
96 hdo->property.parall.pt[1].y = 0;
97 hdo->property.parall.pt[2].x = width;
98 hdo->property.parall.pt[2].y = height;
101 static bool SkipPrivateBlock(int type)
103 int n;
105 if (type == OBJRET_FILE_NO_PRIVATE_BLOCK)
107 if (!hmem->read4b(n))
108 return false;
109 if (hmem->state() || hmem->skipBlock(n) != static_cast<size_t>(n))
110 return false;
112 if (!hmem->read4b(n))
113 return false;
114 if (hmem->state())
115 return false;
116 return hmem->skipBlock(n) == static_cast<size_t>(n);
119 static int SizeExpected;
120 static int SizeRead;
122 static int ReadSizeField(int size)
124 SizeExpected = size;
125 if (!hmem->read4b(SizeRead))
126 return -1;
127 if (hmem->state())
128 return -1;
129 return SizeRead;
132 static bool SkipUnusedField(void)
134 return (SizeExpected >= SizeRead) &&
135 hmem->skipBlock(SizeRead - SizeExpected) != 0;
139 #define HDOFILE_HEADER_SIZE (2*4+16) // 16=sizeof(ZZRect)
140 #define HDOFILE_COMMON_SIZE (7*4+16+44)
142 #define HDOFILE_HAS_NEXT 0x01
143 #define HDOFILE_HAS_CHILD 0x02
145 static bool LoadCommonHeader(HWPDrawingObject * hdo, unsigned short * link_info)
147 uint size, common_size;
149 if (!hmem)
150 return false;
151 if (!hmem->read4b(size))
152 return false;
153 if (hmem->state())
154 return false;
155 if (size < HDOFILE_COMMON_SIZE)
156 return false;
158 common_size = HDOFILE_COMMON_SIZE;
159 unsigned short tmp16;
160 if (!hmem->read2b(tmp16))
161 return false;
162 hdo->type = tmp16;
163 if (!hmem->read2b(tmp16))
164 return false;
165 *link_info = tmp16;
166 if (!hmem->read4b(hdo->offset.x))
167 return false;
168 if (!hmem->read4b(hdo->offset.y))
169 return false;
170 if (!hmem->read4b(hdo->extent.w))
171 return false;
172 if (!hmem->read4b(hdo->extent.h))
173 return false;
174 if (!hmem->read4b(hdo->offset2.x))
175 return false;
176 if (!hmem->read4b(hdo->offset2.y))
177 return false;
179 if (hmem->state())
180 return false;
182 if (!hmem->read4b(hdo->vrect.x))
183 return false;
184 if (!hmem->read4b(hdo->vrect.y))
185 return false;
186 if (!hmem->read4b(hdo->vrect.w))
187 return false;
188 if (!hmem->read4b(hdo->vrect.h))
189 return false;
191 // read bare property 44 bytes
192 if (!hmem->read4b(hdo->property.line_pstyle))
193 return false;
194 if (!hmem->read4b(hdo->property.line_hstyle))
195 return false;
196 if (!hmem->read4b(hdo->property.line_tstyle))
197 return false;
198 if (!hmem->read4b(hdo->property.line_color))
199 return false;
200 unsigned int tmp32;
201 if (!hmem->read4b(tmp32))
202 return false;
203 hdo->property.line_width = static_cast<hunit>(tmp32);
204 if (!hmem->read4b(hdo->property.fill_color))
205 return false;
206 if (!hmem->read4b(hdo->property.pattern_type))
207 return false;
208 if (!hmem->read4b(hdo->property.pattern_color))
209 return false;
210 if (!hmem->read4b(tmp32))
211 return false;
212 hdo->property.hmargin = static_cast<hunit>(tmp32);
213 if (!hmem->read4b(tmp32))
214 return false;
215 hdo->property.vmargin = static_cast<hunit>(tmp32);
216 if (!hmem->read4b(hdo->property.flag))
217 return false;
218 // read rotation property 32 bytes
219 if ((size >= common_size + 32)
220 && (hdo->property.flag & HWPDO_FLAG_ROTATION))
222 if (!hmem->read4b(hdo->property.rot_originx))
223 return false;
224 if (!hmem->read4b(hdo->property.rot_originy))
225 return false;
226 for (int ii = 0; ii < 3; ++ii)
228 if (!hmem->read4b(hdo->property.parall.pt[ii].x))
229 return false;
230 if (!hmem->read4b(hdo->property.parall.pt[ii].y))
231 return false;
233 common_size += 32;
235 else
236 SetHdoParallRgn(hdo, hdo->extent.w, hdo->extent.h);
238 // read gradient property 28 bytes
239 if ((size >= common_size + 28) &&
240 (hdo->property.flag & HWPDO_FLAG_GRADATION))
242 if (!hmem->read4b(hdo->property.fromcolor))
243 return false;
244 if (!hmem->read4b(hdo->property.tocolor))
245 return false;
246 if (!hmem->read4b(hdo->property.gstyle))
247 return false;
248 if (!hmem->read4b(hdo->property.angle))
249 return false;
250 if (!hmem->read4b(hdo->property.center_x))
251 return false;
252 if (!hmem->read4b(hdo->property.center_y))
253 return false;
254 if (!hmem->read4b(hdo->property.nstep))
255 return false;
256 common_size += 28;
259 // read bitmap property 278 bytes
260 if ((size >= common_size + 278) && \
261 (hdo->property.flag & HWPDO_FLAG_BITMAP))
263 if (!hmem->read4b(hdo->property.offset1.x))
264 return false;
265 if (!hmem->read4b(hdo->property.offset1.y))
266 return false;
267 if (!hmem->read4b(hdo->property.offset2.x))
268 return false;
269 if (!hmem->read4b(hdo->property.offset2.y))
270 return false;
271 if (!hmem->readBlock(hdo->property.szPatternFile, 261))
272 return false;
273 if (!hmem->read1b(hdo->property.pictype))
274 return false;
275 common_size += 278;
277 if( ( size >= common_size + 3 ) && ( hdo->property.flag & HWPDO_FLAG_WATERMARK ) )
278 //if( ( size >= common_size ) && ( hdo->property.flag >> 20 & 0x01 ) )
280 if (size - common_size >= 5)
281 hmem->skipBlock(2);
282 unsigned char tmp8;
283 if (!hmem->read1b(tmp8))
284 return false;
285 hdo->property.luminance = tmp8;
286 if (!hmem->read1b(tmp8))
287 return false;
288 hdo->property.contrast = tmp8;
289 if (!hmem->read1b(tmp8))
290 return false;
291 hdo->property.greyscale = tmp8;
293 common_size += 5;
295 else
297 hdo->property.luminance = 0;
298 hdo->property.contrast = 0;
299 hdo->property.greyscale = 0;
301 hdo->property.pPara = nullptr;
303 if( ( size > common_size ) && (hdo->property.flag & HWPDO_FLAG_AS_TEXTBOX) )
305 hmem->skipBlock(8);
306 hdo->property.pPara = LoadParaList();
307 if( hdo->property.pPara )
308 return true;
309 else
310 return false;
313 if (size <= common_size)
314 return true;
315 return hmem->skipBlock(size - common_size ) != 0;
318 static std::unique_ptr<HWPDrawingObject> LoadDrawingObject(void)
320 HWPDrawingObject *prev = nullptr;
321 std::unique_ptr<HWPDrawingObject> hdo, head;
323 unsigned short link_info;
327 hdo.reset(new HWPDrawingObject);
328 if (!LoadCommonHeader(hdo.get(), &link_info))
330 goto error;
332 if (hdo->type < 0 || hdo->type >= HWPDO_NITEMS)
334 hdo->type = HWPDO_RECT;
335 if (!SkipPrivateBlock(OBJRET_FILE_NO_PRIVATE_BLOCK))
337 goto error;
340 else
342 switch (int res = HWPDOFunc(hdo.get(), OBJFUNC_LOAD, nullptr, 0))
344 case OBJRET_FILE_ERROR:
345 goto error;
346 case OBJRET_FILE_OK:
347 break;
348 case OBJRET_FILE_NO_PRIVATE_BLOCK:
349 case OBJRET_FILE_NO_PRIVATE_BLOCK_2:
350 if (!SkipPrivateBlock(res))
351 goto error;
352 break;
355 if (link_info & HDOFILE_HAS_CHILD)
357 hdo->child = LoadDrawingObject();
358 if (hdo->child == nullptr)
360 goto error;
363 if (prev == nullptr)
365 head = std::move(hdo);
366 prev = head.get();
368 else
370 prev->next = std::move(hdo);
371 prev = prev->next.get();
374 while (link_info & HDOFILE_HAS_NEXT);
376 return head;
378 error:
379 // drawing object can be list.
380 // hdo = current item, head = list;
382 if (hdo->type < 0 || hdo->type >= HWPDO_NITEMS)
384 hdo->type = HWPDO_RECT;
386 HWPDOFunc(hdo.get(), OBJFUNC_FREE, nullptr, 0);
387 hdo.reset();
389 if( prev )
391 prev->next = nullptr;
392 return head;
394 else
395 return nullptr;
399 static bool LoadDrawingObjectBlock(Picture * pic)
401 int size;
402 if (!hmem->read4b(size))
403 return false;
405 if (hmem->state() || size < HDOFILE_HEADER_SIZE)
406 return false;
408 if (!hmem->read4b(pic->picinfo.picdraw.zorder))
409 return false;
410 if (!hmem->read4b(pic->picinfo.picdraw.mbrcnt))
411 return false;
412 if (!hmem->read4b(pic->picinfo.picdraw.vrect.x))
413 return false;
414 if (!hmem->read4b(pic->picinfo.picdraw.vrect.y))
415 return false;
416 if (!hmem->read4b(pic->picinfo.picdraw.vrect.w))
417 return false;
418 if (!hmem->read4b(pic->picinfo.picdraw.vrect.h))
419 return false;
421 if (size > HDOFILE_HEADER_SIZE &&
422 !hmem->skipBlock(size - HDOFILE_HEADER_SIZE))
423 return false;
425 pic->picinfo.picdraw.hdo = LoadDrawingObject().release();
426 if (pic->picinfo.picdraw.hdo == nullptr)
427 return false;
428 return true;
431 // object manipulation function
432 static int
433 HWPDODefaultFunc(int cmd)
435 if (cmd == OBJFUNC_LOAD)
436 return OBJRET_FILE_NO_PRIVATE_BLOCK;
437 return OBJRET_FILE_OK;
440 static int
441 HWPDOLineFunc(int /*type*/, HWPDrawingObject * hdo, int cmd, void * /*argp*/, int /*argv*/)
443 int ret = OBJRET_FILE_OK;
444 switch (cmd)
446 case OBJFUNC_LOAD:
447 if (ReadSizeField(4) < 4)
448 return OBJRET_FILE_ERROR;
449 if (!hmem->read4b(hdo->u.line_arc.flip))
450 return OBJRET_FILE_ERROR;
451 if (hmem->state())
452 return OBJRET_FILE_ERROR;
453 if (!SkipUnusedField())
454 return OBJRET_FILE_ERROR;
455 ret = OBJRET_FILE_NO_PRIVATE_BLOCK_2;
456 break;
457 default:
458 ret = HWPDODefaultFunc(cmd);
459 break;
461 return ret;
465 // rectangle
467 static int
468 HWPDORectFunc(int /*type*/, HWPDrawingObject * /*hdo*/, int cmd, void * /*argp*/, int /*argv*/)
470 return HWPDODefaultFunc(cmd);
474 // ellipse
476 static int
477 HWPDOEllipseFunc(int /*type*/, HWPDrawingObject * /*hdo*/,
478 int cmd, void * /*argp*/, int /*argv*/)
480 return HWPDODefaultFunc(cmd);
483 #define WTMM(x) ((double)(x) / 1800. * 25.4)
484 static int
485 HWPDOEllipse2Func(int /*type*/, HWPDrawingObject * hdo,
486 int cmd, void * /*argp*/, int /*argv*/)
488 switch (cmd)
490 case OBJFUNC_LOAD:
491 if (ReadSizeField(16) < 16)
492 return OBJRET_FILE_ERROR;
493 if (!hmem->read4b(hdo->u.arc.radial[0].x))
494 return OBJRET_FILE_ERROR;
495 if (!hmem->read4b(hdo->u.arc.radial[0].y))
496 return OBJRET_FILE_ERROR;
497 if (!hmem->read4b(hdo->u.arc.radial[1].x))
498 return OBJRET_FILE_ERROR;
499 if (!hmem->read4b(hdo->u.arc.radial[1].y))
500 return OBJRET_FILE_ERROR;
501 if (ReadSizeField(0) < 0)
502 return OBJRET_FILE_ERROR;
503 break;
504 default:
505 return HWPDODefaultFunc(cmd);
507 return OBJRET_FILE_OK;
511 // arc
513 static int
514 HWPDOArcFunc(int /*type*/, HWPDrawingObject * hdo, int cmd, void * /*argp*/, int /*argv*/)
516 switch (cmd)
518 case OBJFUNC_LOAD:
519 if (ReadSizeField(4) < 4)
520 return OBJRET_FILE_ERROR;
521 if (!hmem->read4b(hdo->u.line_arc.flip))
522 return OBJRET_FILE_ERROR;
523 if (hmem->state())
524 return OBJRET_FILE_ERROR;
525 if (!SkipUnusedField())
526 return OBJRET_FILE_ERROR;
527 break;
528 default:
529 return HWPDODefaultFunc(cmd);
531 return OBJRET_FILE_OK;
535 static int
536 HWPDOArc2Func(int /*type*/, HWPDrawingObject * /*hdo*/, int cmd, void * /*argp*/, int /*argv*/)
538 int ret = OBJRET_FILE_OK;
539 switch (cmd)
541 case OBJFUNC_LOAD:
542 ret = OBJRET_FILE_NO_PRIVATE_BLOCK;
543 break;
544 default:
545 ret = HWPDODefaultFunc(cmd);
546 break;
548 return ret;
552 static int
553 HWPDOFreeFormFunc(int /*type*/, HWPDrawingObject * hdo,
554 int cmd, void * /*argp*/, int /*argv*/)
556 switch (cmd)
558 case OBJFUNC_LOAD:
560 hdo->u.freeform.pt = nullptr;
561 if (ReadSizeField(4) < 4)
562 return OBJRET_FILE_ERROR;
563 if (!hmem->read4b(hdo->u.freeform.npt))
564 return OBJRET_FILE_ERROR;
565 if (hmem->state())
566 return OBJRET_FILE_ERROR;
567 if (!SkipUnusedField())
568 return OBJRET_FILE_ERROR;
570 int size = hdo->u.freeform.npt * sizeof(ZZPoint);
572 if (ReadSizeField(size) < size)
573 return OBJRET_FILE_ERROR;
574 if (hdo->u.freeform.npt)
576 hdo->u.freeform.pt =
577 ::comphelper::newArray_null<ZZPoint>(hdo->u.freeform.npt);
578 if (hdo->u.freeform.pt == nullptr)
580 hdo->u.freeform.npt = 0;
581 return OBJRET_FILE_ERROR;
583 for (int ii = 0; ii < hdo->u.freeform.npt; ++ii)
585 bool bFailure = false;
586 if (!hmem->read4b(hdo->u.freeform.pt[ii].x))
587 bFailure = true;
588 if (!hmem->read4b(hdo->u.freeform.pt[ii].y))
589 bFailure = true;
590 if (hmem->state())
591 bFailure = true;
592 if (bFailure)
594 delete[]hdo->u.freeform.pt;
595 hdo->u.freeform.npt = 0;
596 return OBJRET_FILE_ERROR;
600 if (!SkipUnusedField())
601 return OBJRET_FILE_ERROR;
602 return OBJRET_FILE_OK;
604 case OBJFUNC_FREE:
605 if (hdo->u.freeform.pt)
606 delete[]hdo->u.freeform.pt;
607 break;
608 default:
609 return HWPDODefaultFunc(cmd);
611 return OBJRET_FILE_OK;
615 // text box
617 static void FreeParaList(HWPPara * para)
619 if (para->Next())
620 FreeParaList(para->Next());
621 delete para;
625 static HWPPara *LoadParaList()
627 if (!hmem)
628 return nullptr;
630 HWPFile *hwpf = GetCurrentDoc();
631 std::unique_ptr<HIODev> hio = hwpf->SetIODevice(std::unique_ptr<HIODev>(hmem));
633 std::vector< HWPPara* > plist;
635 hwpf->ReadParaList(plist);
636 std::unique_ptr<HIODev> orighmem = hwpf->SetIODevice(std::move(hio));
637 hmem = orighmem.release();
639 return plist.size()? plist.front() : nullptr;
643 static int
644 HWPDOTextBoxFunc(int /*type*/, HWPDrawingObject * hdo,
645 int cmd, void * /*argp*/, int /*argv*/)
647 switch (cmd)
649 case OBJFUNC_LOAD:
650 if (ReadSizeField(0) < 0 || !SkipUnusedField())
651 return OBJRET_FILE_ERROR;
652 if (ReadSizeField(0) < 0)
653 return OBJRET_FILE_ERROR;
654 hdo->u.textbox.h = LoadParaList();
655 return hdo->u.textbox.h ? OBJRET_FILE_OK : OBJRET_FILE_ERROR;
656 case OBJFUNC_FREE:
657 if (hdo->u.textbox.h)
659 FreeParaList(hdo->u.textbox.h);
660 hdo->u.textbox.h = nullptr;
662 break;
663 default:
664 return HWPDODefaultFunc(cmd);
666 return OBJRET_FILE_OK;
671 static int
672 HWPDOContainerFunc(int /*type*/, HWPDrawingObject * /*hdo*/,
673 int cmd, void * /*argp*/, int /*argv*/)
675 return HWPDODefaultFunc(cmd);
679 HWPDrawingObject::HWPDrawingObject():
680 type(0), offset{0, 0}, offset2{0, 0}, extent{0, 0}, vrect{0, 0, 0, 0}
682 memset(&property, 0, sizeof property);
683 memset(&u, 0, sizeof u);
684 index = ++count;
688 HWPDrawingObject::~HWPDrawingObject()
690 if (property.pPara)
691 FreeParaList(property.pPara);
693 HWPDOFunc(this, OBJFUNC_FREE, nullptr, 0);
695 #endif
697 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */