Debian Codegraph @

src/lib/QXP1Parser.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp project.
4 *
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/.
8 */
9
10 #include "QXP1Parser.h"
11
12 #include "QXP1Header.h"
13 #include "QXPCollector.h"
14
15 namespace libqxp
16 {
17
18 using librevenge::RVNGInputStream;
19 using std::make_shared;
20 using std::shared_ptr;
21
22 namespace
23 {
24
25 double getShade(const unsigned shadeId)
26 {
27 if (shadeId < 3)
28 return 0.1 * shadeId;
29 else if (shadeId < 6)
30 return 0.2 * (shadeId - 1);
31 else
32 return 1.0;
33 }
34
35 }
36
37 QXP1Parser::QXP1Parser(const std::shared_ptr<librevenge::RVNGInputStream> &input, librevenge::RVNGDrawingInterface *painter, const std::shared_ptr<QXP1Header> &header)
38 : QXPParser(input, painter, header)
39 , m_header(header)
40 {
41 }
42
43 void QXP1Parser::adjust(double &pos, unsigned adjustment)
44 {
45 pos += double(adjustment - 0x8000) / 0x10000;
46 }
47
48 bool QXP1Parser::parseDocument(const std::shared_ptr<librevenge::RVNGInputStream> &docStream, QXPCollector &)
49 {
50 parseCharFormats(docStream);
51 parseParagraphFormats(docStream);
52 return true;
53 }
54
55 bool QXP1Parser::parsePages(const std::shared_ptr<librevenge::RVNGInputStream> &stream, QXPCollector &collector)
56 {
57 Page page;
58 page.pageSettings.resize(1);
59 page.pageSettings[0].offset.bottom = m_header->pageHeight();
60 page.pageSettings[0].offset.right = m_header->pageWidth();
61
62 for (unsigned i = 0; i != m_header->pages(); ++i)
63 {
64 const bool empty = parsePage(stream);
65 collector.startPage(page);
66 bool last = !empty;
67 while (!last)
68 {
69 last = parseObject(stream, collector);
70 }
71 collector.endPage();
72 }
73
74 return false;
75 }
76
77 CharFormat QXP1Parser::parseCharFormat(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
78 {
79 CharFormat result;
80
81 skip(stream, 2);
82
83 const int fontIndex = readS16(stream, true);
84 (void) fontIndex;
85 result.fontName = "Helvetica"; // TODO: How to determine font name? Is the list of fonts fixed?
86
87 result.fontSize = readU16(stream, true) / 4.0;
88
89 const unsigned flags = readU16(stream, true);
90 convertCharFormatFlags(flags, result);
91
92 skip(stream, 2);
93
94 const unsigned colorId = readU8(stream);
95 const unsigned shadeId = readU8(stream);
96 result.color = getColor(colorId).applyShade(getShade(shadeId));
97
98 return result;
99 }
100
101 ParagraphFormat QXP1Parser::parseParagraphFormat(const std::shared_ptr<librevenge::RVNGInputStream> &stream)
102 {
103 ParagraphFormat result;
104
105 skip(stream, 3);
106 result.alignment = readHorAlign(stream);
107 skip(stream, 2);
108 result.margin.left = readFraction(stream, true);
109 result.firstLineIndent = readFraction(stream, true);
110 result.margin.right = readFraction(stream, true);
111 result.leading = readFraction(stream, true);
112 result.margin.top = readFraction(stream, true);
113 result.margin.bottom = readFraction(stream, true);
114
115 for (unsigned i = 0; i < 20; ++i)
116 {
117 TabStop tabStop;
118
119 const uint8_t type = readU8(stream);
120 tabStop.type = convertTabStopType(type);
121 tabStop.fillChar.append(char(readU8(stream)));
122 tabStop.position = readFraction(stream, true);
123
124 if (tabStop.isDefined())
125 result.tabStops.push_back(tabStop);
126 }
127
128 return result;
129 }
130
131 std::shared_ptr<HJ> QXP1Parser::parseHJ(const std::shared_ptr<librevenge::RVNGInputStream> &)
132 {
133 return std::shared_ptr<HJ>();
134 }
135
136 bool QXP1Parser::parsePage(const std::shared_ptr<librevenge::RVNGInputStream> &input)
137 {
138 skip(input, 15);
139 const unsigned empty = readU8(input);
140 switch (empty)
141 {
142 case 1:
143 return false;
144 case 2:
145 return true;
146 default:
147 QXP_DEBUG_MSG(("QXP1Parser::parsePage: unknown 'is empty' value %d, cannot continue\n", empty));
148 throw ParseError();
149 }
150 }
151
152 bool QXP1Parser::parseObject(const std::shared_ptr<librevenge::RVNGInputStream> &input, QXPCollector &collector)
153 {
154 const unsigned type = readU8(input);
155
156 bool transparent = false;
157 const unsigned transVal = readU8(input);
158 switch (transVal)
159 {
160 default:
161 QXP_DEBUG_MSG(("QXP1Parser::parseObject: unexpected 'is transparent' value: %d\n", transVal));
162 QXP_FALLTHROUGH;
163 case 1:
164 transparent = true;
165 break;
166 case 0:
167 transparent = false;
168 break;
169 }
170
171 unsigned contentIndex = readU16(input);
172 skip(input, 2);
173
174 Rect bbox;
175 parseCoordPair(input, bbox.left, bbox.top, bbox.right, bbox.bottom);
176
177 const unsigned textOffset = readU32(input, true) >> 8;
178 skip(input, 8);
179 const unsigned linkId = readU32(input, true);
180 const unsigned shadeId = readU8(input);
181 const unsigned colorId = readU8(input);
182 const auto &color = getColor(colorId).applyShade(getShade(shadeId));
183
184 switch (type)
185 {
186 case 0:
187 case 1:
188 parseLine(input, collector, bbox, color, transparent);
189 break;
190 case 3:
191 parseText(input, collector, bbox, color, transparent, contentIndex, textOffset, linkId);
192 break;
193 case 4:
194 case 5:
195 case 6:
196 parsePicture(input, collector, bbox, color, transparent);
197 break;
198 default:
199 QXP_DEBUG_MSG(("QXP1Parser::parseObject: unknown object type %d, cannot continue\n", type));
200 throw ParseError();
201 }
202
203 const unsigned lastObject = readU8(input);
204 switch (lastObject)
205 {
206 case 0:
207 case 1:
208 return false;
209 case 2:
210 return true;
211 default:
212 QXP_DEBUG_MSG(("QXP1Parser::parseObject: unknown 'last object' value %d, cannot continue\n", lastObject));
213 throw ParseError();
214 }
215 }
216
217 void QXP1Parser::parseLine(const std::shared_ptr<librevenge::RVNGInputStream> &input, QXPCollector &collector, const Rect &bbox, const Color &color, bool transparent)
218 {
219 (void) collector;
220 (void) bbox;
221 (void) color;
222 (void) transparent;
223
224 skip(input, 25);
225 }
226
227 void QXP1Parser::parseText(const std::shared_ptr<librevenge::RVNGInputStream> &input, QXPCollector &collector, const Rect &bbox, const Color &color, bool transparent, unsigned content, unsigned textOffset, unsigned linkID)
228 {
229 (void) collector;
230 (void) bbox;
231 (void) color;
232 (void) transparent;
233 (void) content;
234 (void) textOffset;
235 (void) linkID;
236
237 skip(input, 40);
238 }
239
240 void QXP1Parser::parsePicture(const std::shared_ptr<librevenge::RVNGInputStream> &input, QXPCollector &collector, const Rect &bbox, const Color &color, bool transparent)
241 {
242 (void) collector;
243 (void) bbox;
244 (void) color;
245 (void) transparent;
246
247 skip(input, 54);
248 }
249
250 void QXP1Parser::parseCoordPair(const std::shared_ptr<librevenge::RVNGInputStream> &input, double &x1, double &y1, double &x2, double &y2)
251 {
252 y1 = readU16(input, true);
253 x1 = readU16(input, true);
254 y2 = readU16(input, true);
255 x2 = readU16(input, true);
256 const unsigned y1Adj = readU16(input, true);
257 const unsigned x1Adj = readU16(input, true);
258 const unsigned y2Adj = readU16(input, true);
259 const unsigned x2Adj = readU16(input, true);
260 QXP1Parser::adjust(y1, y1Adj);
261 QXP1Parser::adjust(x1, x1Adj);
262 QXP1Parser::adjust(y2, y2Adj);
263 QXP1Parser::adjust(x2, x2Adj);
264 }
265
266 }
267
268 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */