Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* libcdr
3 : * Version: MPL 1.1 / GPLv2+ / LGPLv2+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2011 Fridrich Strba <fridrich.strba@bluewin.ch>
17 : *
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
25 : * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 : * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
27 : * instead of those above.
28 : */
29 :
30 : #include <libwpd-stream/libwpd-stream.h>
31 : #include "libcdr_utils.h"
32 : #include "CommonParser.h"
33 :
34 : #ifndef M_PI
35 : #define M_PI 3.14159265358979323846
36 : #endif
37 :
38 0 : libcdr::CommonParser::CommonParser(libcdr::CDRCollector *collector)
39 0 : : m_collector(collector), m_precision(libcdr::PRECISION_UNKNOWN) {}
40 :
41 0 : libcdr::CommonParser::~CommonParser()
42 : {
43 0 : }
44 :
45 0 : double libcdr::CommonParser::readCoordinate(WPXInputStream *input, bool bigEndian)
46 : {
47 0 : if (m_precision == PRECISION_UNKNOWN)
48 0 : throw UnknownPrecisionException();
49 0 : else if (m_precision == PRECISION_16BIT)
50 0 : return (double)readS16(input, bigEndian) / 1000.0;
51 0 : return (double)readS32(input, bigEndian) / 254000.0;
52 : }
53 :
54 0 : unsigned libcdr::CommonParser::readUnsigned(WPXInputStream *input, bool bigEndian)
55 : {
56 0 : if (m_precision == PRECISION_UNKNOWN)
57 0 : throw UnknownPrecisionException();
58 0 : else if (m_precision == PRECISION_16BIT)
59 0 : return (unsigned)readU16(input, bigEndian);
60 0 : return readU32(input, bigEndian);
61 : }
62 :
63 0 : int libcdr::CommonParser::readInteger(WPXInputStream *input, bool bigEndian)
64 : {
65 0 : if (m_precision == PRECISION_UNKNOWN)
66 0 : throw UnknownPrecisionException();
67 0 : else if (m_precision == PRECISION_16BIT)
68 0 : return (int)readS16(input, bigEndian);
69 0 : return readS32(input, bigEndian);
70 : }
71 :
72 0 : double libcdr::CommonParser::readAngle(WPXInputStream *input, bool bigEndian)
73 : {
74 0 : if (m_precision == PRECISION_UNKNOWN)
75 0 : throw UnknownPrecisionException();
76 0 : else if (m_precision == PRECISION_16BIT)
77 0 : return M_PI * (double)readS16(input, bigEndian) / 1800.0;
78 0 : return M_PI * (double)readS32(input, bigEndian) / 180000000.0;
79 : }
80 :
81 0 : void libcdr::CommonParser::outputPath(const std::vector<std::pair<double, double> > &points,
82 : const std::vector<unsigned char> &types)
83 : {
84 0 : bool isClosedPath = false;
85 0 : std::vector<std::pair<double, double> >tmpPoints;
86 0 : for (unsigned k=0; k<points.size(); k++)
87 : {
88 0 : const unsigned char &type = types[k];
89 0 : if (type & 0x08)
90 0 : isClosedPath = true;
91 : else
92 0 : isClosedPath = false;
93 0 : if (!(type & 0x10) && !(type & 0x20))
94 : {
95 : // cont angle
96 : }
97 0 : else if (type & 0x10)
98 : {
99 : // cont smooth
100 : }
101 0 : else if (type & 0x20)
102 : {
103 : // cont symmetrical
104 : }
105 0 : if (!(type & 0x40) && !(type & 0x80))
106 : {
107 0 : tmpPoints.clear();
108 0 : m_collector->collectMoveTo(points[k].first, points[k].second);
109 : }
110 0 : else if ((type & 0x40) && !(type & 0x80))
111 : {
112 0 : tmpPoints.clear();
113 0 : m_collector->collectLineTo(points[k].first, points[k].second);
114 0 : if (isClosedPath)
115 0 : m_collector->collectClosePath();
116 : }
117 0 : else if (!(type & 0x40) && (type & 0x80))
118 : {
119 0 : if (tmpPoints.size() >= 2)
120 0 : m_collector->collectCubicBezier(tmpPoints[0].first, tmpPoints[0].second,
121 0 : tmpPoints[1].first, tmpPoints[1].second,
122 0 : points[k].first, points[k].second);
123 : else
124 0 : m_collector->collectLineTo(points[k].first, points[k].second);
125 0 : if (isClosedPath)
126 0 : m_collector->collectClosePath();
127 0 : tmpPoints.clear();
128 : }
129 0 : else if((type & 0x40) && (type & 0x80))
130 : {
131 0 : tmpPoints.push_back(points[k]);
132 : }
133 0 : }
134 0 : }
135 :
136 :
137 : /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|