Debian Codegraph @

src/lib/CDRStylesCollector.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libcdr 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 "CDRStylesCollector.h"
11
12 #include "CDRInternalStream.h"
13 #include "libcdr_utils.h"
14
15 #ifndef DUMP_IMAGE
16 #define DUMP_IMAGE 0
17 #endif
18
19
20 libcdr::CDRStylesCollector::CDRStylesCollector(libcdr::CDRParserState &ps) :
21 m_ps(ps), m_page(8.5, 11.0, -4.25, -5.5)
22 {
23 }
24
25 libcdr::CDRStylesCollector::~CDRStylesCollector()
26 {
27 }
28
29 void libcdr::CDRStylesCollector::collectBmp(unsigned imageId, unsigned colorModel, unsigned width, unsigned height, unsigned bpp, const std::vector<unsigned> &palette, const std::vector<unsigned char> &bitmap)
30 {
31 libcdr::CDRInternalStream stream(bitmap);
32 librevenge::RVNGBinaryData image;
33
34 if (height == 0)
35 height = 1;
36
37 auto tmpPixelSize = (unsigned)(height * width);
38 if (tmpPixelSize < (unsigned)height) // overflow
39 return;
40
41 unsigned tmpDIBImageSize = tmpPixelSize * 4;
42 if (tmpPixelSize > tmpDIBImageSize) // overflow !!!
43 return;
44
45 unsigned tmpDIBOffsetBits = 14 + 40;
46 unsigned tmpDIBFileSize = tmpDIBOffsetBits + tmpDIBImageSize;
47 if (tmpDIBImageSize > tmpDIBFileSize) // overflow !!!
48 return;
49
50 // Create DIB file header
51 writeU16(image, 0x4D42); // Type
52 writeU32(image, tmpDIBFileSize); // Size
53 writeU16(image, 0); // Reserved1
54 writeU16(image, 0); // Reserved2
55 writeU32(image, tmpDIBOffsetBits); // OffsetBits
56
57 // Create DIB Info header
58 writeU32(image, 40); // Size
59
60 writeU32(image, width); // Width
61 writeU32(image, height); // Height
62
63 writeU16(image, 1); // Planes
64 writeU16(image, 32); // BitCount
65 writeU32(image, 0); // Compression
66 writeU32(image, tmpDIBImageSize); // SizeImage
67 writeU32(image, 0); // XPelsPerMeter
68 writeU32(image, 0); // YPelsPerMeter
69 writeU32(image, 0); // ColorsUsed
70 writeU32(image, 0); // ColorsImportant
71
72 // Cater for eventual padding
73 unsigned long lineWidth = bitmap.size() / height;
74
75 bool storeBMP = true;
76
77 for (unsigned j = 0; j < height; ++j)
78 {
79 unsigned i = 0;
80 unsigned k = 0;
81 if (colorModel == 6)
82 {
83 while (i <lineWidth && k < width)
84 {
85 unsigned l = 0;
86 unsigned char c = bitmap[j*lineWidth+i];
87 i++;
88 while (k < width && l < 8)
89 {
90 if (c & 0x80)
91 writeU32(image, 0xffffff);
92 else
93 writeU32(image, 0);
94 c <<= 1;
95 l++;
96 k++;
97 }
98 }
99 }
100 else if (colorModel == 5)
101 {
102 while (i <lineWidth && i < width)
103 {
104 unsigned char c = bitmap[j*lineWidth+i];
105 i++;
106 writeU32(image, m_ps.getBMPColor(libcdr::CDRColor((unsigned short)colorModel, c)));
107 }
108 }
109 else if (!palette.empty())
110 {
111 while (i < lineWidth && i < width)
112 {
113 unsigned long c = bitmap[j*lineWidth+i];
114 if (c >= palette.size())
115 c = palette.size() - 1;
116 i++;
117 writeU32(image, m_ps.getBMPColor(libcdr::CDRColor((unsigned short)colorModel, palette[c])));
118 }
119 }
120 else if (bpp == 24 && lineWidth >= 3)
121 {
122 while (i < lineWidth -2 && k < width)
123 {
124 unsigned c = ((unsigned)bitmap[j*lineWidth+i+2] << 16) | ((unsigned)bitmap[j*lineWidth+i+1] << 8) | ((unsigned)bitmap[j*lineWidth+i]);
125 i += 3;
126 writeU32(image, m_ps.getBMPColor(libcdr::CDRColor((unsigned short)colorModel, c)));
127 k++;
128 }
129 }
130 else if (bpp == 32 && lineWidth >= 4)
131 {
132 while (i < lineWidth - 3 && k < width)
133 {
134 unsigned c = (bitmap[j*lineWidth+i+3] << 24) | (bitmap[j*lineWidth+i+2] << 16) | (bitmap[j*lineWidth+i+1] << 8) | (bitmap[j*lineWidth+i]);
135 i += 4;
136 writeU32(image, m_ps.getBMPColor(libcdr::CDRColor((unsigned short)colorModel, c)));
137 k++;
138 }
139 }
140 else
141 storeBMP = false;
142 }
143
144 if (storeBMP)
145 {
146 #if DUMP_IMAGE
147 librevenge::RVNGString filename;
148 filename.sprintf("bitmap%.8x.bmp", imageId);
149 FILE *f = fopen(filename.cstr(), "wb");
150 if (f)
151 {
152 const unsigned char *tmpBuffer = image.getDataBuffer();
153 for (unsigned long k = 0; k < image.size(); k++)
154 fprintf(f, "%c",tmpBuffer[k]);
155 fclose(f);
156 }
157 #endif
158
159 m_ps.m_bmps[imageId] = image;
160 }
161 }
162
163 void libcdr::CDRStylesCollector::collectBmp(unsigned imageId, const std::vector<unsigned char> &bitmap)
164 {
165 librevenge::RVNGBinaryData image(&bitmap[0], bitmap.size());
166 #if DUMP_IMAGE
167 librevenge::RVNGString filename;
168 filename.sprintf("bitmap%.8x.bmp", imageId);
169 FILE *f = fopen(filename.cstr(), "wb");
170 if (f)
171 {
172 const unsigned char *tmpBuffer = image.getDataBuffer();
173 for (unsigned long k = 0; k < image.size(); k++)
174 fprintf(f, "%c",tmpBuffer[k]);
175 fclose(f);
176 }
177 #endif
178
179 m_ps.m_bmps[imageId] = image;
180 }
181
182 void libcdr::CDRStylesCollector::collectPageSize(double width, double height, double offsetX, double offsetY)
183 {
184 if (m_ps.m_pages.empty())
185 m_page = CDRPage(width, height, offsetX, offsetY);
186 else
187 m_ps.m_pages.back() = CDRPage(width, height, offsetX, offsetY);
188 }
189
190 void libcdr::CDRStylesCollector::collectPage(unsigned /* level */)
191 {
192 m_ps.m_pages.push_back(m_page);
193 }
194
195 void libcdr::CDRStylesCollector::collectBmpf(unsigned patternId, unsigned width, unsigned height, const std::vector<unsigned char> &pattern)
196 {
197 m_ps.m_patterns[patternId] = CDRPattern(width, height, pattern);
198 }
199
200 void libcdr::CDRStylesCollector::collectColorProfile(const std::vector<unsigned char> &profile)
201 {
202 if (!profile.empty())
203 m_ps.setColorTransform(profile);
204 }
205
206 void libcdr::CDRStylesCollector::collectPaletteEntry(unsigned colorId, unsigned /* userId */, const libcdr::CDRColor &color)
207 {
208 m_ps.m_documentPalette[colorId] = color;
209 }
210
211 void libcdr::CDRStylesCollector::collectText(unsigned textId, unsigned styleId, const std::vector<unsigned char> &data,
212 const std::vector<unsigned char> &charDescriptions, const std::map<unsigned, CDRStyle> &styleOverrides)
213 {
214 if (data.empty() && styleOverrides.empty())
215 return;
216
217 unsigned char tmpCharDescription = 0;
218 unsigned i = 0;
219 unsigned j = 0;
220 std::vector<unsigned char> tmpTextData;
221 CDRStyle defaultCharStyle, tmpCharStyle;
222 m_ps.getRecursedStyle(defaultCharStyle, styleId);
223
224 CDRTextLine line;
225 for (i=0, j=0; i<charDescriptions.size() && j<data.size(); ++i)
226 {
227 tmpCharStyle = defaultCharStyle;
228 auto iter = styleOverrides.find(tmpCharDescription & 0xfe);
229 if (iter != styleOverrides.end())
230 tmpCharStyle.overrideStyle(iter->second);
231 if (charDescriptions[i] != tmpCharDescription)
232 {
233 librevenge::RVNGString text;
234 if (!tmpTextData.empty())
235 {
236 if (tmpCharDescription & 0x01)
237 appendCharacters(text, tmpTextData);
238 else
239 appendCharacters(text, tmpTextData, tmpCharStyle.m_charSet);
240 }
241 line.append(CDRText(text, tmpCharStyle));
242 tmpTextData.clear();
243 tmpCharDescription = charDescriptions[i];
244
245 }
246 tmpTextData.push_back(data[j++]);
247 if ((tmpCharDescription & 0x01) && (j < data.size()))
248 tmpTextData.push_back(data[j++]);
249 }
250 librevenge::RVNGString text;
251 if (!tmpTextData.empty())
252 {
253 if (tmpCharDescription & 0x01)
254 appendCharacters(text, tmpTextData);
255 else
256 appendCharacters(text, tmpTextData, tmpCharStyle.m_charSet);
257 }
258 line.append(CDRText(text, tmpCharStyle));
259 CDR_DEBUG_MSG(("CDRStylesCollector::collectText - Text: %s\n", text.cstr()));
260
261 std::vector<CDRTextLine> &paragraphVector = m_ps.m_texts[textId];
262 paragraphVector.push_back(line);
263 }
264
265 void libcdr::CDRStylesCollector::collectStld(unsigned id, const CDRStyle &style)
266 {
267 m_ps.m_styles[id] = style;
268 }
269
270 void libcdr::CDRStylesCollector::collectFillStyle(unsigned id, const CDRFillStyle &fillStyle)
271 {
272 m_ps.m_fillStyles[id] = fillStyle;
273 }
274
275 void libcdr::CDRStylesCollector::collectLineStyle(unsigned id, const CDRLineStyle &lineStyle)
276 {
277 m_ps.m_lineStyles[id] = lineStyle;
278 }
279
280 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */