-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
FlatIconImporter.cpp
667 lines (590 loc) · 14.6 KB
/
FlatIconImporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
* Copyright 2006, 2023, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
* Zardshard
*/
#include "FlatIconImporter.h"
#include <new>
#include <stdio.h>
#include <Archivable.h>
#include <DataIO.h>
#include <Message.h>
#include "AffineTransformer.h"
#include "AutoDeleter.h"
#include "ContourTransformer.h"
#include "FlatIconFormat.h"
#include "GradientTransformable.h"
#include "Icon.h"
#include "LittleEndianBuffer.h"
#include "PathCommandQueue.h"
#include "PathSourceShape.h"
#include "PerspectiveTransformer.h"
#include "Shape.h"
#include "StrokeTransformer.h"
#include "Style.h"
#include "VectorPath.h"
using std::nothrow;
_USING_ICON_NAMESPACE
// constructor
FlatIconImporter::FlatIconImporter()
#ifdef ICON_O_MATIC
: Importer()
#endif
{
}
// destructor
FlatIconImporter::~FlatIconImporter()
{
}
// Import
status_t
FlatIconImporter::Import(Icon* icon, BPositionIO* stream)
{
#ifdef ICON_O_MATIC
status_t ret = Init(icon);
if (ret < B_OK)
return ret;
#else
status_t ret;
#endif
// seek around in the stream to figure out the size
off_t size = stream->Seek(0, SEEK_END);
if (stream->Seek(0, SEEK_SET) != 0)
return B_ERROR;
// we chicken out on anything larger than 256k
if (size <= 0 || size > 256 * 1024)
return B_BAD_VALUE;
// read the entire stream into a buffer
LittleEndianBuffer buffer(size);
if (!buffer.Buffer())
return B_NO_MEMORY;
if (stream->Read(buffer.Buffer(), size) != size)
return B_ERROR;
ret = _ParseSections(buffer, icon);
return ret;
}
// Import
status_t
FlatIconImporter::Import(Icon* icon, uint8* _buffer, size_t size)
{
#ifdef ICON_O_MATIC
status_t ret = Init(icon);
if (ret < B_OK)
return ret;
#endif
if (!_buffer)
return B_BAD_VALUE;
// attach LittleEndianBuffer to buffer
LittleEndianBuffer buffer(_buffer, size);
return _ParseSections(buffer, icon);
}
// #pragma mark -
// _ParseSections
status_t
FlatIconImporter::_ParseSections(LittleEndianBuffer& buffer, Icon* icon)
{
// test if this is an icon at all
uint32 magic;
if (!buffer.Read(magic) || magic != FLAT_ICON_MAGIC)
return B_BAD_TYPE;
// styles
Container<Style>* styles = icon->Styles();
status_t ret = _ParseStyles(buffer, styles);
if (ret < B_OK) {
printf("FlatIconImporter::_ParseSections() - "
"error parsing styles: %s\n", strerror(ret));
return ret;
}
// paths
Container<VectorPath>* paths = icon->Paths();
ret = _ParsePaths(buffer, paths);
if (ret < B_OK) {
printf("FlatIconImporter::_ParseSections() - "
"error parsing paths: %s\n", strerror(ret));
return ret;
}
// shapes
ret = _ParseShapes(buffer, styles, paths, icon->Shapes());
if (ret < B_OK) {
printf("FlatIconImporter::_ParseSections() - "
"error parsing shapes: %s\n", strerror(ret));
return ret;
}
return B_OK;
}
// _ReadTransformable
static bool
_ReadTransformable(LittleEndianBuffer& buffer, Transformable* transformable)
{
int32 matrixSize = Transformable::matrix_size;
double matrix[matrixSize];
for (int32 i = 0; i < matrixSize; i++) {
float value;
if (!read_float_24(buffer, value))
return false;
matrix[i] = value;
}
transformable->LoadFrom(matrix);
return true;
}
// _ReadTranslation
static bool
_ReadTranslation(LittleEndianBuffer& buffer, Transformable* transformable)
{
BPoint t;
if (read_coord(buffer, t.x) && read_coord(buffer, t.y)) {
transformable->TranslateBy(t);
return true;
}
return false;
}
// _ReadColorStyle
static Style*
_ReadColorStyle(LittleEndianBuffer& buffer, bool alpha, bool gray)
{
rgb_color color;
if (alpha) {
if (gray) {
if (!buffer.Read(color.red)
|| !buffer.Read(color.alpha))
return NULL;
color.green = color.blue = color.red;
} else {
if (!buffer.Read((uint32&)color))
return NULL;
}
} else {
color.alpha = 255;
if (gray) {
if (!buffer.Read(color.red))
return NULL;
color.green = color.blue = color.red;
} else {
if (!buffer.Read(color.red)
|| !buffer.Read(color.green)
|| !buffer.Read(color.blue))
return NULL;
}
}
return new (nothrow) Style(color);
}
// _ReadGradientStyle
static Style*
_ReadGradientStyle(LittleEndianBuffer& buffer)
{
Style* style = new (nothrow) Style();
if (!style)
return NULL;
ObjectDeleter<Style> styleDeleter(style);
uint8 gradientType;
uint8 gradientFlags;
uint8 gradientStopCount;
if (!buffer.Read(gradientType)
|| !buffer.Read(gradientFlags)
|| !buffer.Read(gradientStopCount)) {
return NULL;
}
Gradient gradient(true);
// empty gradient
gradient.SetType((gradients_type)gradientType);
// TODO: support more stuff with flags
// ("inherits transformation" and so on)
if (gradientFlags & GRADIENT_FLAG_TRANSFORM) {
if (!_ReadTransformable(buffer, &gradient))
return NULL;
}
bool alpha = !(gradientFlags & GRADIENT_FLAG_NO_ALPHA);
bool gray = gradientFlags & GRADIENT_FLAG_GRAYS;
for (int32 i = 0; i < gradientStopCount; i++) {
uint8 stopOffset;
rgb_color color;
if (!buffer.Read(stopOffset))
return NULL;
if (alpha) {
if (gray) {
if (!buffer.Read(color.red)
|| !buffer.Read(color.alpha))
return NULL;
color.green = color.blue = color.red;
} else {
if (!buffer.Read((uint32&)color))
return NULL;
}
} else {
color.alpha = 255;
if (gray) {
if (!buffer.Read(color.red))
return NULL;
color.green = color.blue = color.red;
} else {
if (!buffer.Read(color.red)
|| !buffer.Read(color.green)
|| !buffer.Read(color.blue)) {
return NULL;
}
}
}
gradient.AddColor(color, stopOffset / 255.0);
}
style->SetGradient(&gradient);
styleDeleter.Detach();
return style;
}
// _ParseStyles
status_t
FlatIconImporter::_ParseStyles(LittleEndianBuffer& buffer,
Container<Style>* styles)
{
uint8 styleCount;
if (!buffer.Read(styleCount))
return B_ERROR;
for (int32 i = 0; i < styleCount; i++) {
uint8 styleType;
if (!buffer.Read(styleType))
return B_ERROR;
Style* style = NULL;
if (styleType == STYLE_TYPE_SOLID_COLOR) {
// solid color
style = _ReadColorStyle(buffer, true, false);
if (!style)
return B_NO_MEMORY;
} else if (styleType == STYLE_TYPE_SOLID_COLOR_NO_ALPHA) {
// solid color without alpha
style = _ReadColorStyle(buffer, false, false);
if (!style)
return B_NO_MEMORY;
} else if (styleType == STYLE_TYPE_SOLID_GRAY) {
// solid gray plus alpha
style = _ReadColorStyle(buffer, true, true);
if (!style)
return B_NO_MEMORY;
} else if (styleType == STYLE_TYPE_SOLID_GRAY_NO_ALPHA) {
// solid gray without alpha
style = _ReadColorStyle(buffer, false, true);
if (!style)
return B_NO_MEMORY;
} else if (styleType == STYLE_TYPE_GRADIENT) {
// gradient
style = _ReadGradientStyle(buffer);
if (!style)
return B_NO_MEMORY;
} else {
// unkown style type, skip tag
uint16 tagLength;
if (!buffer.Read(tagLength))
return B_ERROR;
buffer.Skip(tagLength);
continue;
}
// add style if we were able to read one
if (style && !styles->AddItem(style)) {
delete style;
return B_NO_MEMORY;
}
}
return B_OK;
}
// read_path_no_curves
static bool
read_path_no_curves(LittleEndianBuffer& buffer, VectorPath* path,
uint8 pointCount)
{
for (uint32 p = 0; p < pointCount; p++) {
BPoint point;
if (!read_coord(buffer, point.x)
|| !read_coord(buffer, point.y))
return false;
if (!path->AddPoint(point))
return false;
}
return true;
}
// read_path_curves
static bool
read_path_curves(LittleEndianBuffer& buffer, VectorPath* path,
uint8 pointCount)
{
for (uint32 p = 0; p < pointCount; p++) {
BPoint point;
if (!read_coord(buffer, point.x)
|| !read_coord(buffer, point.y))
return false;
BPoint pointIn;
if (!read_coord(buffer, pointIn.x)
|| !read_coord(buffer, pointIn.y))
return false;
BPoint pointOut;
if (!read_coord(buffer, pointOut.x)
|| !read_coord(buffer, pointOut.y))
return false;
if (!path->AddPoint(point, pointIn, pointOut, false))
return false;
}
return true;
}
// read_path_with_commands
static bool
read_path_with_commands(LittleEndianBuffer& buffer, VectorPath* path,
uint8 pointCount)
{
PathCommandQueue queue;
return queue.Read(buffer, path, pointCount);
}
// _ParsePaths
status_t
FlatIconImporter::_ParsePaths(LittleEndianBuffer& buffer,
Container<VectorPath>* paths)
{
uint8 pathCount;
if (!buffer.Read(pathCount))
return B_ERROR;
for (int32 i = 0; i < pathCount; i++) {
uint8 pathFlags;
uint8 pointCount;
if (!buffer.Read(pathFlags) || !buffer.Read(pointCount))
return B_ERROR;
VectorPath* path = new (nothrow) VectorPath();
if (!path)
return B_NO_MEMORY;
// chose path reading strategy depending on path flags
bool error = false;
if (pathFlags & PATH_FLAG_NO_CURVES) {
if (!read_path_no_curves(buffer, path, pointCount))
error = true;
} else if (pathFlags & PATH_FLAG_USES_COMMANDS) {
if (!read_path_with_commands(buffer, path, pointCount))
error = true;
} else {
if (!read_path_curves(buffer, path, pointCount))
error = true;
}
if (error) {
delete path;
return B_ERROR;
}
// post process path to clean it up
path->CleanUp();
if (pathFlags & PATH_FLAG_CLOSED)
path->SetClosed(true);
// add path to container
if (!paths->AddItem(path)) {
delete path;
return B_NO_MEMORY;
}
}
return B_OK;
}
// _ReadTransformer
static Transformer*
_ReadTransformer(LittleEndianBuffer& buffer, VertexSource& source, Shape* shape)
{
uint8 transformerType;
if (!buffer.Read(transformerType))
return NULL;
switch (transformerType) {
case TRANSFORMER_TYPE_AFFINE: {
AffineTransformer* affine
= new (nothrow) AffineTransformer(source);
if (!affine)
return NULL;
double matrix[6];
for (int32 i = 0; i < 6; i++) {
float value;
if (!buffer.Read(value)) {
delete affine;
return NULL;
}
matrix[i] = value;
}
affine->load_from(matrix);
return affine;
}
case TRANSFORMER_TYPE_CONTOUR: {
ContourTransformer* contour
= new (nothrow) ContourTransformer(source);
uint8 width;
uint8 lineJoin;
uint8 miterLimit;
if (!contour
|| !buffer.Read(width)
|| !buffer.Read(lineJoin)
|| !buffer.Read(miterLimit)) {
delete contour;
return NULL;
}
contour->width(width - 128.0);
contour->line_join((agg::line_join_e)lineJoin);
contour->miter_limit(miterLimit);
return contour;
}
case TRANSFORMER_TYPE_PERSPECTIVE: {
PerspectiveTransformer* perspective
= new (nothrow) PerspectiveTransformer(source, shape);
if (!perspective)
return NULL;
double matrix[9];
for (int32 i = 0; i < 9; i++) {
float value;
if (!read_float_24(buffer, value)) {
delete perspective;
return NULL;
}
matrix[i] = value;
}
perspective->load_from(matrix);
return perspective;
}
case TRANSFORMER_TYPE_STROKE: {
StrokeTransformer* stroke
= new (nothrow) StrokeTransformer(source);
uint8 width;
uint8 lineOptions;
uint8 miterLimit;
// uint8 shorten;
if (!stroke
|| !buffer.Read(width)
|| !buffer.Read(lineOptions)
|| !buffer.Read(miterLimit)) {
delete stroke;
return NULL;
}
stroke->width(width - 128.0);
uint8 lineJoin = lineOptions & 15;
stroke->line_join((agg::line_join_e)lineJoin);
uint8 lineCap = lineOptions >> 4;
stroke->line_cap((agg::line_cap_e)lineCap);
stroke->miter_limit(miterLimit);
return stroke;
}
default: {
// unkown transformer, skip tag
uint16 tagLength;
if (!buffer.Read(tagLength))
return NULL;
buffer.Skip(tagLength);
return NULL;
}
}
}
// _ReadPathSourceShape
Shape*
FlatIconImporter::_ReadPathSourceShape(LittleEndianBuffer& buffer,
Container<Style>* styles,
Container<VectorPath>* paths)
{
// find out which style this shape uses
uint8 styleIndex;
uint8 pathCount;
if (!buffer.Read(styleIndex) || !buffer.Read(pathCount))
return NULL;
#ifdef ICON_O_MATIC
Style* style = styles->ItemAt(StyleIndexFor(styleIndex));
#else
Style* style = styles->ItemAt(styleIndex);
#endif
if (!style) {
printf("_ReadPathSourceShape() - "
"shape references non-existing style %d\n", styleIndex);
return NULL;
}
// create the shape
PathSourceShape* shape = new (nothrow) PathSourceShape(style);
ObjectDeleter<Shape> shapeDeleter(shape);
if (!shape || shape->InitCheck() < B_OK)
return NULL;
// find out which paths this shape uses
for (uint32 i = 0; i < pathCount; i++) {
uint8 pathIndex;
if (!buffer.Read(pathIndex))
return NULL;
#ifdef ICON_O_MATIC
VectorPath* path = paths->ItemAt(PathIndexFor(pathIndex));
#else
VectorPath* path = paths->ItemAt(pathIndex);
#endif
if (!path) {
printf("_ReadPathSourceShape() - "
"shape references non-existing path %d\n", pathIndex);
continue;
}
shape->Paths()->AddItem(path);
}
// shape flags
uint8 shapeFlags;
if (!buffer.Read(shapeFlags))
return NULL;
shape->SetHinting(shapeFlags & SHAPE_FLAG_HINTING);
if (shapeFlags & SHAPE_FLAG_TRANSFORM) {
// transformation
if (!_ReadTransformable(buffer, shape))
return NULL;
} else if (shapeFlags & SHAPE_FLAG_TRANSLATION) {
// translation
if (!_ReadTranslation(buffer, shape))
return NULL;
}
if (shapeFlags & SHAPE_FLAG_LOD_SCALE) {
// min max visibility scale
uint8 minScale;
uint8 maxScale;
if (!buffer.Read(minScale) || !buffer.Read(maxScale))
return NULL;
shape->SetMinVisibilityScale(minScale / 63.75);
shape->SetMaxVisibilityScale(maxScale / 63.75);
}
// transformers
if (shapeFlags & SHAPE_FLAG_HAS_TRANSFORMERS) {
uint8 transformerCount;
if (!buffer.Read(transformerCount))
return NULL;
for (uint32 i = 0; i < transformerCount; i++) {
Transformer* transformer
= _ReadTransformer(buffer, shape->VertexSource(), shape);
if (transformer && !shape->Transformers()->AddItem(transformer)) {
delete transformer;
return NULL;
}
}
}
shapeDeleter.Detach();
return shape;
}
// _ParseShapes
status_t
FlatIconImporter::_ParseShapes(LittleEndianBuffer& buffer,
Container<Style>* styles,
Container<VectorPath>* paths,
Container<Shape>* shapes)
{
uint8 shapeCount;
if (!buffer.Read(shapeCount))
return B_ERROR;
for (uint32 i = 0; i < shapeCount; i++) {
uint8 shapeType;
if (!buffer.Read(shapeType))
return B_ERROR;
Shape* shape = NULL;
if (shapeType == SHAPE_TYPE_PATH_SOURCE) {
// path source shape
shape = _ReadPathSourceShape(buffer, styles, paths);
if (!shape)
return B_NO_MEMORY;
} else {
// unkown shape type, skip tag
uint16 tagLength;
if (!buffer.Read(tagLength))
return B_ERROR;
buffer.Skip(tagLength);
continue;
}
// add shape if we were able to read one
if (shape && !shapes->AddItem(shape)) {
delete shape;
return B_NO_MEMORY;
}
}
return B_OK;
}