2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "core/svg/SVGPathByteStreamBuilder.h"
23 #include "core/svg/SVGPathByteStream.h"
24 #include "core/svg/SVGPathSeg.h"
25 #include "platform/geometry/FloatPoint.h"
29 // Helper class that coalesces writes to a SVGPathByteStream to a local buffer.
30 class CoalescingBuffer
{
32 CoalescingBuffer(SVGPathByteStream
& byteStream
)
34 , m_byteStream(byteStream
)
39 for (size_t i
= 0; i
< m_currentOffset
; ++i
)
40 m_byteStream
.append(m_bytes
[i
]);
43 template<typename DataType
>
44 void writeType(DataType value
)
46 ByteType
<DataType
> data
;
48 size_t typeSize
= sizeof(ByteType
<DataType
>);
49 ASSERT(m_currentOffset
+ typeSize
<= sizeof(m_bytes
));
50 memcpy(m_bytes
+ m_currentOffset
, data
.bytes
, typeSize
);
51 m_currentOffset
+= typeSize
;
54 void writeFlag(bool value
) { writeType
<bool>(value
); }
55 void writeFloat(float value
) { writeType
<float>(value
); }
56 void writeFloatPoint(const FloatPoint
& point
)
58 writeType
<float>(point
.x());
59 writeType
<float>(point
.y());
61 void writeSegmentType(unsigned short value
) { writeType
<unsigned short>(value
); }
64 // Adjust size to fit the largest command (in serialized/byte-stream format).
65 // Currently a cubic segment.
66 size_t m_currentOffset
;
67 unsigned char m_bytes
[sizeof(unsigned short) + sizeof(FloatPoint
) * 3];
68 SVGPathByteStream
& m_byteStream
;
71 SVGPathByteStreamBuilder::SVGPathByteStreamBuilder(SVGPathByteStream
& byteStream
)
72 : m_byteStream(byteStream
)
76 void SVGPathByteStreamBuilder::emitSegment(const PathSegmentData
& segment
)
78 CoalescingBuffer
buffer(m_byteStream
);
79 buffer
.writeSegmentType(segment
.command
);
81 switch (segment
.command
) {
82 case PathSegMoveToRel
:
83 case PathSegMoveToAbs
:
84 case PathSegLineToRel
:
85 case PathSegLineToAbs
:
86 case PathSegCurveToQuadraticSmoothRel
:
87 case PathSegCurveToQuadraticSmoothAbs
:
88 buffer
.writeFloatPoint(segment
.targetPoint
);
90 case PathSegLineToHorizontalRel
:
91 case PathSegLineToHorizontalAbs
:
92 buffer
.writeFloat(segment
.targetPoint
.x());
94 case PathSegLineToVerticalRel
:
95 case PathSegLineToVerticalAbs
:
96 buffer
.writeFloat(segment
.targetPoint
.y());
98 case PathSegClosePath
:
100 case PathSegCurveToCubicRel
:
101 case PathSegCurveToCubicAbs
:
102 buffer
.writeFloatPoint(segment
.point1
);
103 buffer
.writeFloatPoint(segment
.point2
);
104 buffer
.writeFloatPoint(segment
.targetPoint
);
106 case PathSegCurveToCubicSmoothRel
:
107 case PathSegCurveToCubicSmoothAbs
:
108 buffer
.writeFloatPoint(segment
.point2
);
109 buffer
.writeFloatPoint(segment
.targetPoint
);
111 case PathSegCurveToQuadraticRel
:
112 case PathSegCurveToQuadraticAbs
:
113 buffer
.writeFloatPoint(segment
.point1
);
114 buffer
.writeFloatPoint(segment
.targetPoint
);
118 buffer
.writeFloatPoint(segment
.point1
);
119 buffer
.writeFloat(segment
.point2
.x());
120 buffer
.writeFlag(segment
.arcLarge
);
121 buffer
.writeFlag(segment
.arcSweep
);
122 buffer
.writeFloatPoint(segment
.targetPoint
);
125 ASSERT_NOT_REACHED();