| 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 | //if the dash array contains ZERO length |
| 37 | // segments or ZERO lengths gaps we could |
| 38 | // optimize those usecase. |
| 39 | for (size_t i = 0; i < mArraySize; i++) { |
| 40 | if (!vCompare(mDashArray[i].length, 0.0f)) |
| 41 | mNoLength = false; |
| 42 | if (!vCompare(mDashArray[i].gap, 0.0f)) |
| 43 | mNoGap = false; |
| 44 | } |
| 45 | } |
| 46 |
| 47 | void VDasher::moveTo(const VPointF &p) |
| 48 | { |
| 49 | mDiscard = false; |
| 50 | mStartNewSegment = true; |
| 51 | mCurPt = p; |
| 52 | mIndex = 0; |
| 53 |
| 54 | if (!vCompare(mDashOffset, 0.0f)) { |
| 55 | float totalLength = 0.0; |
| 56 | for (size_t i = 0; i < mArraySize; i++) { |
| 57 | totalLength = mDashArray[i].length + mDashArray[i].gap; |
| 58 | } |
| 59 | float normalizeLen = std::fmod(mDashOffset, totalLength); |
| 60 | if (normalizeLen < 0.0f) { |
| 61 | normalizeLen = totalLength + normalizeLen; |
| 62 | } |
| 63 | // now the length is less than total length and +ve |
| 64 | // findout the current dash index , dashlength and gap. |
| 65 | for (size_t i = 0; i < mArraySize; i++) { |
| 66 | if (normalizeLen < mDashArray[i].length) { |
| 67 | mIndex = i; |
| 68 | mCurrentLength = mDashArray[i].length - normalizeLen; |
| 69 | mDiscard = false; |
| 70 | break; |
| 71 | } |
| 72 | normalizeLen -= mDashArray[i].length; |
| 73 | if (normalizeLen < mDashArray[i].gap) { |
| 74 | mIndex = i; |
| 75 | mCurrentLength = mDashArray[i].gap - normalizeLen; |
| 76 | mDiscard = true; |
| 77 | break; |
| 78 | } |
| 79 | normalizeLen -= mDashArray[i].gap; |
| 80 | } |
| 81 | } else { |
| 82 | mCurrentLength = mDashArray[mIndex].length; |
| 83 | } |
| 84 | if (vIsZero(mCurrentLength)) updateActiveSegment(); |
| 85 | } |
| 86 |
| 87 | void VDasher::addLine(const VPointF &p) |
| 88 | { |
| 89 | if (mDiscard) return; |
| 90 |
| 91 | if (mStartNewSegment) { |
| 92 | mResult.moveTo(mCurPt); |
| 93 | mStartNewSegment = false; |
| 94 | } |
| 95 | mResult.lineTo(p); |
| 96 | } |
| 97 |
| 98 | void VDasher::updateActiveSegment() |
| 99 | { |
| 100 | mStartNewSegment = true; |
| 101 |
| 102 | if (mDiscard) { |
| 103 | mDiscard = false; |
| 104 | mIndex = (mIndex + 1) % mArraySize; |
| 105 | mCurrentLength = mDashArray[mIndex].length; |
| 106 | } else { |
| 107 | mDiscard = true; |
| 108 | mCurrentLength = mDashArray[mIndex].gap; |
| 109 | } |
| 110 | if (vIsZero(mCurrentLength)) updateActiveSegment(); |
| 111 | } |
| 112 |
| 113 | void VDasher::lineTo(const VPointF &p) |
| 114 | { |
| 115 | VLine left, right; |
| 116 | VLine line(mCurPt, p); |
| 117 | float length = line.length(); |
| 118 |
| 119 | if (length <= mCurrentLength) { |
| 120 | mCurrentLength -= length; |
| 121 | addLine(p); |
| 122 | } else { |
| 123 | while (length > mCurrentLength) { |
| 124 | length -= mCurrentLength; |
| 125 | line.splitAtLength(mCurrentLength, left, right); |
| 126 |
| 127 | addLine(left.p2()); |
| 128 | updateActiveSegment(); |
| 129 |
| 130 | line = right; |
| 131 | mCurPt = line.p1(); |
| 132 | } |
| 133 | // handle remainder |
| 134 | if (length > 1.0f) { |
| 135 | mCurrentLength -= length; |
| 136 | addLine(line.p2()); |
| 137 | } |
| 138 | } |
| 139 |
| 140 | if (mCurrentLength < 1.0f) updateActiveSegment(); |
| 141 |
| 142 | mCurPt = p; |
| 143 | } |
| 144 |
| 145 | void VDasher::addCubic(const VPointF &cp1, const VPointF &cp2, const VPointF &e) |
| 146 | { |
| 147 | if (mDiscard) return; |
| 148 |
| 149 | if (mStartNewSegment) { |
| 150 | mResult.moveTo(mCurPt); |
| 151 | mStartNewSegment = false; |
| 152 | } |
| 153 | mResult.cubicTo(cp1, cp2, e); |
| 154 | } |
| 155 |
| 156 | void VDasher::cubicTo(const VPointF &cp1, const VPointF &cp2, const VPointF &e) |
| 157 | { |
| 158 | VBezier left, right; |
| 159 | VBezier b = VBezier::fromPoints(mCurPt, cp1, cp2, e); |
| 160 | float bezLen = b.length(); |
| 161 |
| 162 | if (bezLen <= mCurrentLength) { |
| 163 | mCurrentLength -= bezLen; |
| 164 | addCubic(cp1, cp2, e); |
| 165 | } else { |
| 166 | while (bezLen > mCurrentLength) { |
| 167 | bezLen -= mCurrentLength; |
| 168 | b.splitAtLength(mCurrentLength, &left, &right); |
| 169 |
| 170 | addCubic(left.pt2(), left.pt3(), left.pt4()); |
| 171 | updateActiveSegment(); |
| 172 |
| 173 | b = right; |
| 174 | mCurPt = b.pt1(); |
| 175 | } |
| 176 | // handle remainder |
| 177 | if (bezLen > 1.0f) { |
| 178 | mCurrentLength -= bezLen; |
| 179 | addCubic(b.pt2(), b.pt3(), b.pt4()); |
| 180 | } |
| 181 | } |
| 182 |
| 183 | if (mCurrentLength < 1.0f) updateActiveSegment(); |
| 184 |
| 185 | mCurPt = e; |
| 186 | } |
| 187 |
| 188 | VPath VDasher::dashed(const VPath &path) |
| 189 | { |
| 190 | if (mNoLength && mNoGap) return path; |
| 191 |
| 192 | if (path.empty() || mNoLength) return VPath(); |
| 193 |
| 194 | if (mNoGap) return path; |
| 195 |
| 196 | mResult = {}; |
| 197 | mResult.reserve(path.points().size(), path.elements().size()); |
| 198 | mIndex = 0; |
| 199 | const std::vector<VPath::Element> &elms = path.elements(); |
| 200 | const std::vector<VPointF> & pts = path.points(); |
| 201 | const VPointF * ptPtr = pts.data(); |
| 202 |
| 203 | for (auto &i : elms) { |
| 204 | switch (i) { |
| 205 | case VPath::Element::MoveTo: { |
| 206 | moveTo(*ptPtr++); |
| 207 | break; |
| 208 | } |
| 209 | case VPath::Element::LineTo: { |
| 210 | lineTo(*ptPtr++); |
| 211 | break; |
| 212 | } |
| 213 | case VPath::Element::CubicTo: { |
| 214 | cubicTo(*ptPtr, *(ptPtr + 1), *(ptPtr + 2)); |
| 215 | ptPtr += 3; |
| 216 | break; |
| 217 | } |
| 218 | case VPath::Element::Close: { |
| 219 | // The point is already joined to start point in VPath |
| 220 | // no need to do anything here. |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | if (mResult.points().size() > SHRT_MAX) { |
| 226 | mResult.reset(); |
| 227 | } |
| 228 | return std::move(mResult); |
| 229 | } |
| 230 |
| 231 | V_END_NAMESPACE |
| 232 |