| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) Research In Motion Limited 2010. All rights reserved. | 2 * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
| 18 */ | 18 */ |
| 19 | 19 |
| 20 #include "config.h" | 20 #include "config.h" |
| 21 #include "core/svg/SVGPathByteStreamBuilder.h" | 21 #include "core/svg/SVGPathByteStreamBuilder.h" |
| 22 | 22 |
| 23 #include "core/svg/SVGPathByteStream.h" | 23 #include "core/svg/SVGPathByteStream.h" |
| 24 #include "core/svg/SVGPathData.h" | 24 #include "core/svg/SVGPathData.h" |
| 25 #include "platform/geometry/FloatPoint.h" | 25 #include "platform/geometry/FloatPoint.h" |
| 26 | 26 |
| 27 namespace blink { | 27 namespace blink { |
| 28 | 28 |
| 29 // Helper class that coalesces writes to a SVGPathByteStream to a local buffer. | 29 namespace { |
| 30 class CoalescingBuffer { | 30 |
| 31 class StreamBufferHelper { |
| 31 public: | 32 public: |
| 32 CoalescingBuffer(SVGPathByteStream& byteStream) | 33 StreamBufferHelper(SVGPathByteStream& byteStream, size_t bytesToReserve) |
| 33 : m_currentOffset(0) | 34 : m_buffer(byteStream.appendUninitialized(bytesToReserve)) |
| 34 , m_byteStream(byteStream) | |
| 35 { | 35 { |
| 36 #if ENABLE(ASSERT) |
| 37 m_expectedEnd = m_buffer + bytesToReserve; |
| 38 #endif |
| 36 } | 39 } |
| 37 ~CoalescingBuffer() | 40 ~StreamBufferHelper() { ASSERT(m_expectedEnd == m_buffer); } |
| 38 { | |
| 39 for (size_t i = 0; i < m_currentOffset; ++i) | |
| 40 m_byteStream.append(m_bytes[i]); | |
| 41 } | |
| 42 | 41 |
| 43 template<typename DataType> | 42 template<typename DataType> |
| 44 void writeType(DataType value) | 43 void writeType(DataType value) |
| 45 { | 44 { |
| 46 ByteType<DataType> data; | 45 ByteType<DataType> data; |
| 47 data.value = value; | 46 data.value = value; |
| 48 size_t typeSize = sizeof(ByteType<DataType>); | 47 size_t typeSize = sizeof(ByteType<DataType>); |
| 49 ASSERT(m_currentOffset + typeSize <= sizeof(m_bytes)); | 48 ASSERT(m_currentOffset + typeSize <= sizeof(m_bytes)); |
| 50 memcpy(m_bytes + m_currentOffset, data.bytes, typeSize); | 49 memcpy(m_buffer, data.bytes, typeSize); |
| 51 m_currentOffset += typeSize; | 50 m_buffer += typeSize; |
| 52 } | 51 } |
| 53 | 52 |
| 54 void writeFlag(bool value) { writeType<bool>(value); } | 53 void writeFlag(bool value) { writeType<bool>(value); } |
| 55 void writeFloat(float value) { writeType<float>(value); } | 54 void writeFloat(float value) { writeType<float>(value); } |
| 56 void writeFloatPoint(const FloatPoint& point) | 55 void writeFloatPoint(const FloatPoint& point) |
| 57 { | 56 { |
| 58 writeType<float>(point.x()); | 57 writeType<float>(point.x()); |
| 59 writeType<float>(point.y()); | 58 writeType<float>(point.y()); |
| 60 } | 59 } |
| 61 void writeSegmentType(unsigned short value) { writeType<unsigned short>(valu
e); } | 60 void writeSegmentType(unsigned short value) { writeType<unsigned short>(valu
e); } |
| 62 | 61 |
| 63 private: | 62 private: |
| 64 // Adjust size to fit the largest command (in serialized/byte-stream format)
. | 63 unsigned char* m_buffer; |
| 65 // Currently a cubic segment. | 64 #if ENABLE(ASSERT) |
| 66 size_t m_currentOffset; | 65 const unsigned char* m_expectedEnd; |
| 67 unsigned char m_bytes[sizeof(unsigned short) + sizeof(FloatPoint) * 3]; | 66 #endif |
| 68 SVGPathByteStream& m_byteStream; | |
| 69 }; | 67 }; |
| 70 | 68 |
| 69 } |
| 70 |
| 71 SVGPathByteStreamBuilder::SVGPathByteStreamBuilder(SVGPathByteStream& byteStream
) | 71 SVGPathByteStreamBuilder::SVGPathByteStreamBuilder(SVGPathByteStream& byteStream
) |
| 72 : m_byteStream(byteStream) | 72 : m_byteStream(byteStream) |
| 73 { | 73 { |
| 74 } | 74 } |
| 75 | 75 |
| 76 static const unsigned char commandParamLength[] = { |
| 77 0, |
| 78 /* PathSegClosePath */ 0, |
| 79 /* PathSegMoveToAbs */ sizeof(FloatPoint), |
| 80 /* PathSegMoveToRel */ sizeof(FloatPoint), |
| 81 /* PathSegLineToAbs */ sizeof(FloatPoint), |
| 82 /* PathSegLineToRel */ sizeof(FloatPoint), |
| 83 /* PathSegCurveToCubicAbs */ 3 * sizeof(FloatPoint), |
| 84 /* PathSegCurveToCubicRel */ 3 * sizeof(FloatPoint), |
| 85 /* PathSegCurveToQuadraticAbs */ 2 * sizeof(FloatPoint), |
| 86 /* PathSegCurveToQuadraticRel */ 2 * sizeof(FloatPoint), |
| 87 /* PathSegArcAbs */ 2 * sizeof(FloatPoint) + 1 * sizeof(f
loat) + 2 * sizeof(bool), |
| 88 /* PathSegArcRel */ 2 * sizeof(FloatPoint) + 1 * sizeof(f
loat) + 2 * sizeof(bool), |
| 89 /* PathSegLineToHorizontalAbs */ sizeof(float), |
| 90 /* PathSegLineToHorizontalRel */ sizeof(float), |
| 91 /* PathSegLineToVerticalAbs */ sizeof(float), |
| 92 /* PathSegLineToVerticalRel */ sizeof(float), |
| 93 /* PathSegCurveToCubicSmoothAbs */ 2 * sizeof(FloatPoint), |
| 94 /* PathSegCurveToCubicSmoothRel */ 2 * sizeof(FloatPoint), |
| 95 /* PathSegCurveToQuadraticSmoothAbs */ sizeof(FloatPoint), |
| 96 /* PathSegCurveToQuadraticSmoothRel */ sizeof(FloatPoint) |
| 97 }; |
| 98 |
| 76 void SVGPathByteStreamBuilder::emitSegment(const PathSegmentData& segment) | 99 void SVGPathByteStreamBuilder::emitSegment(const PathSegmentData& segment) |
| 77 { | 100 { |
| 78 CoalescingBuffer buffer(m_byteStream); | 101 ASSERT(segment.command > PathSegUnknown && segment.command <= PathSegCurveTo
QuadraticSmoothRel); |
| 102 size_t commandLength = sizeof(unsigned short) + commandParamLength[segment.c
ommand]; |
| 103 StreamBufferHelper buffer(m_byteStream, commandLength); |
| 79 buffer.writeSegmentType(segment.command); | 104 buffer.writeSegmentType(segment.command); |
| 80 | 105 |
| 81 switch (segment.command) { | 106 switch (segment.command) { |
| 82 case PathSegMoveToRel: | 107 case PathSegMoveToRel: |
| 83 case PathSegMoveToAbs: | 108 case PathSegMoveToAbs: |
| 84 case PathSegLineToRel: | 109 case PathSegLineToRel: |
| 85 case PathSegLineToAbs: | 110 case PathSegLineToAbs: |
| 86 case PathSegCurveToQuadraticSmoothRel: | 111 case PathSegCurveToQuadraticSmoothRel: |
| 87 case PathSegCurveToQuadraticSmoothAbs: | 112 case PathSegCurveToQuadraticSmoothAbs: |
| 88 buffer.writeFloatPoint(segment.targetPoint); | 113 buffer.writeFloatPoint(segment.targetPoint); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 buffer.writeFlag(segment.arcLarge); | 145 buffer.writeFlag(segment.arcLarge); |
| 121 buffer.writeFlag(segment.arcSweep); | 146 buffer.writeFlag(segment.arcSweep); |
| 122 buffer.writeFloatPoint(segment.targetPoint); | 147 buffer.writeFloatPoint(segment.targetPoint); |
| 123 break; | 148 break; |
| 124 default: | 149 default: |
| 125 ASSERT_NOT_REACHED(); | 150 ASSERT_NOT_REACHED(); |
| 126 } | 151 } |
| 127 } | 152 } |
| 128 | 153 |
| 129 } // namespace blink | 154 } // namespace blink |
| OLD | NEW |