| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 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 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #include "vbezier.h" |
| 20 |
| 21 | #include <cmath> |
| 22 |
| 23 | #include "vdasher.h" |
| 24 | #include "vline.h" |
| 25 |
| 26 | V_BEGIN_NAMESPACE |
| 27 |
| 28 | VDasher::VDasher(const float *dashArray, size_t size) |
| 29 | { |
| 30 | mDashArray = reinterpret_cast<const VDasher::Dash *>(dashArray); |
| 31 | mArraySize = size / 2; |
| 32 | if (size % 2) mDashOffset = dashArray[size - 1]; |
| 33 | mIndex = 0; |
| 34 | mCurrentLength = 0; |
| 35 | mDiscard = false; |
| 36 | } |
| 37 |
| 38 | void VDasher::moveTo(const VPointF &p) |
| 39 | { |
| 40 | mDiscard = false; |
| 41 | mStartNewSegment = true; |
| 42 | mCurPt = p; |
| 43 | mIndex = 0; |
| 44 |
| 45 | if (!vCompare(mDashOffset, 0.0f)) { |
| 46 | float totalLength = 0.0; |
| 47 | for (size_t i = 0; i < mArraySize; i++) { |
| 48 | totalLength = mDashArray[i].length + mDashArray[i].gap; |
| 49 | } |
| 50 | float normalizeLen = std::fmod(mDashOffset, totalLength); |
| 51 | if (normalizeLen < 0.0f) { |
| 52 | normalizeLen = totalLength + normalizeLen; |
| 53 | } |
| 54 | // now the length is less than total length and +ve |
| 55 | // findout the current dash index , dashlength and gap. |
| 56 | for (size_t i = 0; i < mArraySize; i++) { |
| 57 | if (normalizeLen < mDashArray[i].length) { |
| 58 | mIndex = i; |
| 59 | mCurrentLength = mDashArray[i].length - normalizeLen; |
| 60 | mDiscard = false; |
| 61 | break; |
| 62 | } |
| 63 | normalizeLen -= mDashArray[i].length; |
| 64 | if (normalizeLen < mDashArray[i].gap) { |
| 65 | mIndex = i; |
| 66 | mCurrentLength = mDashArray[i].gap - normalizeLen; |
| 67 | mDiscard = true; |
| 68 | break; |
| 69 | } |
| 70 | normalizeLen -= mDashArray[i].gap; |
| 71 | } |
| 72 | } else { |
| 73 | mCurrentLength = mDashArray[mIndex].length; |
| 74 | } |
| 75 | if (vIsZero(mCurrentLength)) updateActiveSegment(); |
| 76 | } |
| 77 |
| 78 | void VDasher::addLine(const VPointF &p) |
| 79 | { |
| 80 | if (mDiscard) return; |
| 81 |
| 82 | if (mStartNewSegment) { |
| 83 | mResult.moveTo(mCurPt); |
| 84 | mStartNewSegment = false; |
| 85 | } |
| 86 | mResult.lineTo(p); |
| 87 | } |
| 88 |
| 89 | void VDasher::updateActiveSegment() |
| 90 | { |
| 91 | mStartNewSegment = true; |
| 92 |
| 93 | if (mDiscard) { |
| 94 | mDiscard = false; |
| 95 | mIndex = (mIndex + 1) % mArraySize; |
| 96 | mCurrentLength = mDashArray[mIndex].length; |
| 97 | } else { |
| 98 | mDiscard = true; |
| 99 | mCurrentLength = mDashArray[mIndex].gap; |
| 100 | } |
| 101 | if (vIsZero(mCurrentLength)) updateActiveSegment(); |
| 102 | } |
| 103 |
| 104 | void VDasher::lineTo(const VPointF &p) |
| 105 | { |
| 106 | VLine left, right; |
| 107 | VLine line(mCurPt, p); |
| 108 | float length = line.length(); |
| 109 |
| 110 | if (length <= mCurrentLength) { |
| 111 | mCurrentLength -= length; |
| 112 | addLine(p); |
| 113 | } else { |
| 114 | while (length > mCurrentLength) { |
| 115 | length -= mCurrentLength; |
| 116 | line.splitAtLength(mCurrentLength, left, right); |
| 117 |
| 118 | addLine(left.p2()); |
| 119 | updateActiveSegment(); |
| 120 |
| 121 | line = right; |
| 122 | mCurPt = line.p1(); |
| 123 | } |
| 124 | // handle remainder |
| 125 | if (length > 1.0f) { |
| 126 | mCurrentLength -= length; |
| 127 | addLine(line.p2()); |
| 128 | } |
| 129 | } |
| 130 |
| 131 | if (mCurrentLength < 1.0f) updateActiveSegment(); |
| 132 |
| 133 | mCurPt = p; |
| 134 | } |
| 135 |
| 136 | void VDasher::addCubic(const VPointF &cp1, const VPointF &cp2, const VPointF &e) |
| 137 | { |
| 138 | if (mDiscard) return; |
| 139 |
| 140 | if (mStartNewSegment) { |
| 141 | mResult.moveTo(mCurPt); |
| 142 | mStartNewSegment = false; |
| 143 | } |
| 144 | mResult.cubicTo(cp1, cp2, e); |
| 145 | } |
| 146 |
| 147 | void VDasher::cubicTo(const VPointF &cp1, const VPointF &cp2, const VPointF &e) |
| 148 | { |
| 149 | VBezier left, right; |
| 150 | VBezier b = VBezier::fromPoints(mCurPt, cp1, cp2, e); |
| 151 | float bezLen = b.length(); |
| 152 |
| 153 | if (bezLen <= mCurrentLength) { |
| 154 | mCurrentLength -= bezLen; |
| 155 | addCubic(cp1, cp2, e); |
| 156 | } else { |
| 157 | while (bezLen > mCurrentLength) { |
| 158 | bezLen -= mCurrentLength; |
| 159 | b.splitAtLength(mCurrentLength, &left, &right); |
| 160 |
| 161 | addCubic(left.pt2(), left.pt3(), left.pt4()); |
| 162 | updateActiveSegment(); |
| 163 |
| 164 | b = right; |
| 165 | mCurPt = b.pt1(); |
| 166 | } |
| 167 | // handle remainder |
| 168 | if (bezLen > 1.0f) { |
| 169 | mCurrentLength -= bezLen; |
| 170 | addCubic(b.pt2(), b.pt3(), b.pt4()); |
| 171 | } |
| 172 | } |
| 173 |
| 174 | if (mCurrentLength < 1.0f) updateActiveSegment(); |
| 175 |
| 176 | mCurPt = e; |
| 177 | } |
| 178 |
| 179 | VPath VDasher::dashed(const VPath &path) |
| 180 | { |
| 181 | if (path.empty()) return VPath(); |
| 182 |
| 183 | mResult = {}; |
| 184 | mResult.reserve(path.points().size(), path.elements().size()); |
| 185 | mIndex = 0; |
| 186 | const std::vector<VPath::Element> &elms = path.elements(); |
| 187 | const std::vector<VPointF> & pts = path.points(); |
| 188 | const VPointF * ptPtr = pts.data(); |
| 189 |
| 190 | for (auto &i : elms) { |
| 191 | switch (i) { |
| 192 | case VPath::Element::MoveTo: { |
| 193 | moveTo(*ptPtr++); |
| 194 | break; |
| 195 | } |
| 196 | case VPath::Element::LineTo: { |
| 197 | lineTo(*ptPtr++); |
| 198 | break; |
| 199 | } |
| 200 | case VPath::Element::CubicTo: { |
| 201 | cubicTo(*ptPtr, *(ptPtr + 1), *(ptPtr + 2)); |
| 202 | ptPtr += 3; |
| 203 | break; |
| 204 | } |
| 205 | case VPath::Element::Close: { |
| 206 | // The point is already joined to start point in VPath |
| 207 | // no need to do anything here. |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | if (mResult.points().size() > SHRT_MAX) { |
| 213 | mResult.reset(); |
| 214 | } |
| 215 | return std::move(mResult); |
| 216 | } |
| 217 |
| 218 | V_END_NAMESPACE |
| 219 |