| 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 "vraster.h" |
| 20 | #include <cstring> |
| 21 | #include <memory> |
| 22 | #include "config.h" |
| 23 | #include "v_ft_raster.h" |
| 24 | #include "v_ft_stroker.h" |
| 25 | #include "vdebug.h" |
| 26 | #include "vmatrix.h" |
| 27 | #include "vpath.h" |
| 28 | #include "vrle.h" |
| 29 |
| 30 | V_BEGIN_NAMESPACE |
| 31 |
| 32 | template <typename T> |
| 33 | class dyn_array { |
| 34 | public: |
| 35 | explicit dyn_array(size_t size) |
| 36 | : mCapacity(size), mData(std::make_unique<T[]>(mCapacity)) |
| 37 | { |
| 38 | } |
| 39 | void reserve(size_t size) |
| 40 | { |
| 41 | if (mCapacity > size) return; |
| 42 | mCapacity = size; |
| 43 | mData = std::make_unique<T[]>(mCapacity); |
| 44 | } |
| 45 | T * data() const { return mData.get(); } |
| 46 | dyn_array &operator=(dyn_array &&) noexcept = delete; |
| 47 |
| 48 | private: |
| 49 | size_t mCapacity{0}; |
| 50 | std::unique_ptr<T[]> mData{nullptr}; |
| 51 | }; |
| 52 |
| 53 | struct FTOutline { |
| 54 | public: |
| 55 | void reset(); |
| 56 | void grow(size_t, size_t); |
| 57 | void convert(const VPath &path); |
| 58 | void convert(CapStyle, JoinStyle, float, float); |
| 59 | void moveTo(const VPointF &pt); |
| 60 | void lineTo(const VPointF &pt); |
| 61 | void cubicTo(const VPointF &ctr1, const VPointF &ctr2, const VPointF end); |
| 62 | void close(); |
| 63 | void end(); |
| 64 | void transform(const VMatrix &m); |
| 65 | SW_FT_Pos TO_FT_COORD(float x) |
| 66 | { |
| 67 | return SW_FT_Pos(x * 64); |
| 68 | } // to freetype 26.6 coordinate. |
| 69 | SW_FT_Outline ft; |
| 70 | bool closed{false}; |
| 71 | SW_FT_Stroker_LineCap ftCap; |
| 72 | SW_FT_Stroker_LineJoin ftJoin; |
| 73 | SW_FT_Fixed ftWidth; |
| 74 | SW_FT_Fixed ftMeterLimit; |
| 75 | dyn_array<SW_FT_Vector> mPointMemory{100}; |
| 76 | dyn_array<char> mTagMemory{100}; |
| 77 | dyn_array<short> mContourMemory{10}; |
| 78 | dyn_array<char> mContourFlagMemory{10}; |
| 79 | }; |
| 80 |
| 81 | void FTOutline::reset() |
| 82 | { |
| 83 | ft.n_points = ft.n_contours = 0; |
| 84 | ft.flags = 0x0; |
| 85 | } |
| 86 |
| 87 | void FTOutline::grow(size_t points, size_t segments) |
| 88 | { |
| 89 | reset(); |
| 90 | mPointMemory.reserve(points + segments); |
| 91 | mTagMemory.reserve(points + segments); |
| 92 | mContourMemory.reserve(segments); |
| 93 | mContourFlagMemory.reserve(segments); |
| 94 |
| 95 | ft.points = mPointMemory.data(); |
| 96 | ft.tags = mTagMemory.data(); |
| 97 | ft.contours = mContourMemory.data(); |
| 98 | ft.contours_flag = mContourFlagMemory.data(); |
| 99 | } |
| 100 |
| 101 | void FTOutline::convert(const VPath &path) |
| 102 | { |
| 103 | const std::vector<VPath::Element> &elements = path.elements(); |
| 104 | const std::vector<VPointF> & points = path.points(); |
| 105 |
| 106 | grow(points.size(), path.segments()); |
| 107 |
| 108 | size_t index = 0; |
| 109 | for (auto element : elements) { |
| 110 | switch (element) { |
| 111 | case VPath::Element::MoveTo: |
| 112 | moveTo(points[index]); |
| 113 | index++; |
| 114 | break; |
| 115 | case VPath::Element::LineTo: |
| 116 | lineTo(points[index]); |
| 117 | index++; |
| 118 | break; |
| 119 | case VPath::Element::CubicTo: |
| 120 | cubicTo(points[index], points[index + 1], points[index + 2]); |
| 121 | index = index + 3; |
| 122 | break; |
| 123 | case VPath::Element::Close: |
| 124 | close(); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | end(); |
| 129 | } |
| 130 |
| 131 | void FTOutline::convert(CapStyle cap, JoinStyle join, float width, |
| 132 | float meterLimit) |
| 133 | { |
| 134 | // map strokeWidth to freetype. It uses as the radius of the pen not the |
| 135 | // diameter |
| 136 | width = width / 2.0f; |
| 137 | // convert to freetype co-ordinate |
| 138 | // IMP: stroker takes radius in 26.6 co-ordinate |
| 139 | ftWidth = SW_FT_Fixed(width * (1 << 6)); |
| 140 | // IMP: stroker takes meterlimit in 16.16 co-ordinate |
| 141 | ftMeterLimit = SW_FT_Fixed(meterLimit * (1 << 16)); |
| 142 |
| 143 | // map to freetype capstyle |
| 144 | switch (cap) { |
| 145 | case CapStyle::Square: |
| 146 | ftCap = SW_FT_STROKER_LINECAP_SQUARE; |
| 147 | break; |
| 148 | case CapStyle::Round: |
| 149 | ftCap = SW_FT_STROKER_LINECAP_ROUND; |
| 150 | break; |
| 151 | default: |
| 152 | ftCap = SW_FT_STROKER_LINECAP_BUTT; |
| 153 | break; |
| 154 | } |
| 155 | switch (join) { |
| 156 | case JoinStyle::Bevel: |
| 157 | ftJoin = SW_FT_STROKER_LINEJOIN_BEVEL; |
| 158 | break; |
| 159 | case JoinStyle::Round: |
| 160 | ftJoin = SW_FT_STROKER_LINEJOIN_ROUND; |
| 161 | break; |
| 162 | default: |
| 163 | ftJoin = SW_FT_STROKER_LINEJOIN_MITER_FIXED; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 |
| 168 | void FTOutline::moveTo(const VPointF &pt) |
| 169 | { |
| 170 | ft.points[ft.n_points].x = TO_FT_COORD(pt.x()); |
| 171 | ft.points[ft.n_points].y = TO_FT_COORD(pt.y()); |
| 172 | ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON; |
| 173 | if (ft.n_points) { |
| 174 | ft.contours[ft.n_contours] = ft.n_points - 1; |
| 175 | ft.n_contours++; |
| 176 | } |
| 177 | // mark the current contour as open |
| 178 | // will be updated if ther is a close tag at the end. |
| 179 | ft.contours_flag[ft.n_contours] = 1; |
| 180 |
| 181 | ft.n_points++; |
| 182 | } |
| 183 |
| 184 | void FTOutline::lineTo(const VPointF &pt) |
| 185 | { |
| 186 | ft.points[ft.n_points].x = TO_FT_COORD(pt.x()); |
| 187 | ft.points[ft.n_points].y = TO_FT_COORD(pt.y()); |
| 188 | ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON; |
| 189 | ft.n_points++; |
| 190 | } |
| 191 |
| 192 | void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2, |
| 193 | const VPointF ep) |
| 194 | { |
| 195 | ft.points[ft.n_points].x = TO_FT_COORD(cp1.x()); |
| 196 | ft.points[ft.n_points].y = TO_FT_COORD(cp1.y()); |
| 197 | ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC; |
| 198 | ft.n_points++; |
| 199 |
| 200 | ft.points[ft.n_points].x = TO_FT_COORD(cp2.x()); |
| 201 | ft.points[ft.n_points].y = TO_FT_COORD(cp2.y()); |
| 202 | ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC; |
| 203 | ft.n_points++; |
| 204 |
| 205 | ft.points[ft.n_points].x = TO_FT_COORD(ep.x()); |
| 206 | ft.points[ft.n_points].y = TO_FT_COORD(ep.y()); |
| 207 | ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON; |
| 208 | ft.n_points++; |
| 209 | } |
| 210 | void FTOutline::close() |
| 211 | { |
| 212 | // mark the contour as a close path. |
| 213 | ft.contours_flag[ft.n_contours] = 0; |
| 214 |
| 215 | int index; |
| 216 | if (ft.n_contours) { |
| 217 | index = ft.contours[ft.n_contours - 1] + 1; |
| 218 | } else { |
| 219 | index = 0; |
| 220 | } |
| 221 |
| 222 | // make sure atleast 1 point exists in the segment. |
| 223 | if (ft.n_points == index) { |
| 224 | closed = false; |
| 225 | return; |
| 226 | } |
| 227 |
| 228 | ft.points[ft.n_points].x = ft.points[index].x; |
| 229 | ft.points[ft.n_points].y = ft.points[index].y; |
| 230 | ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON; |
| 231 | ft.n_points++; |
| 232 | } |
| 233 |
| 234 | void FTOutline::end() |
| 235 | { |
| 236 | if (ft.n_points) { |
| 237 | ft.contours[ft.n_contours] = ft.n_points - 1; |
| 238 | ft.n_contours++; |
| 239 | } |
| 240 | } |
| 241 |
| 242 | static void rleGenerationCb(int count, const SW_FT_Span *spans, void *user) |
| 243 | { |
| 244 | VRle *rle = static_cast<VRle *>(user); |
| 245 | auto *rleSpan = reinterpret_cast<const VRle::Span *>(spans); |
| 246 | rle->addSpan(rleSpan, count); |
| 247 | } |
| 248 |
| 249 | static void bboxCb(int x, int y, int w, int h, void *user) |
| 250 | { |
| 251 | VRle *rle = static_cast<VRle *>(user); |
| 252 | rle->setBoundingRect({x, y, w, h}); |
| 253 | } |
| 254 |
| 255 | class SharedRle { |
| 256 | public: |
| 257 | SharedRle() = default; |
| 258 | VRle &unsafe() { return _rle; } |
| 259 | void notify() |
| 260 | { |
| 261 | { |
| 262 | std::lock_guard<std::mutex> lock(_mutex); |
| 263 | _ready = true; |
| 264 | } |
| 265 | _cv.notify_one(); |
| 266 | } |
| 267 | VRle &get() |
| 268 | { |
| 269 | if (!_pending) return _rle; |
| 270 |
| 271 | std::unique_lock<std::mutex> lock(_mutex); |
| 272 | while (!_ready) _cv.wait(lock); |
| 273 | _pending = false; |
| 274 | return _rle; |
| 275 | } |
| 276 |
| 277 | void reset() |
| 278 | { |
| 279 | _ready = false; |
| 280 | _pending = true; |
| 281 | } |
| 282 |
| 283 | private: |
| 284 | VRle _rle; |
| 285 | std::mutex _mutex; |
| 286 | std::condition_variable _cv; |
| 287 | bool _ready{true}; |
| 288 | bool _pending{false}; |
| 289 | }; |
| 290 |
| 291 | struct VRleTask { |
| 292 | SharedRle mRle; |
| 293 | VPath mPath; |
| 294 | float mStrokeWidth; |
| 295 | float mMeterLimit; |
| 296 | VRect mClip; |
| 297 | FillRule mFillRule; |
| 298 | CapStyle mCap; |
| 299 | JoinStyle mJoin; |
| 300 | bool mGenerateStroke; |
| 301 |
| 302 | VRle &rle() { return mRle.get(); } |
| 303 |
| 304 | void update(VPath path, FillRule fillRule, const VRect &clip) |
| 305 | { |
| 306 | mRle.reset(); |
| 307 | mPath = std::move(path); |
| 308 | mFillRule = fillRule; |
| 309 | mClip = clip; |
| 310 | mGenerateStroke = false; |
| 311 | } |
| 312 |
| 313 | void update(VPath path, CapStyle cap, JoinStyle join, float width, |
| 314 | float meterLimit, const VRect &clip) |
| 315 | { |
| 316 | mRle.reset(); |
| 317 | mPath = std::move(path); |
| 318 | mCap = cap; |
| 319 | mJoin = join; |
| 320 | mStrokeWidth = width; |
| 321 | mMeterLimit = meterLimit; |
| 322 | mClip = clip; |
| 323 | mGenerateStroke = true; |
| 324 | } |
| 325 | void render(FTOutline &outRef) |
| 326 | { |
| 327 | SW_FT_Raster_Params params; |
| 328 |
| 329 | mRle.unsafe().reset(); |
| 330 |
| 331 | params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA; |
| 332 | params.gray_spans = &rleGenerationCb; |
| 333 | params.bbox_cb = &bboxCb; |
| 334 | params.user = &mRle.unsafe(); |
| 335 | params.source = &outRef.ft; |
| 336 |
| 337 | if (!mClip.empty()) { |
| 338 | params.flags |= SW_FT_RASTER_FLAG_CLIP; |
| 339 |
| 340 | params.clip_box.xMin = mClip.left(); |
| 341 | params.clip_box.yMin = mClip.top(); |
| 342 | params.clip_box.xMax = mClip.right(); |
| 343 | params.clip_box.yMax = mClip.bottom(); |
| 344 | } |
| 345 | // compute rle |
| 346 | sw_ft_grays_raster.raster_render(nullptr, ¶ms); |
| 347 | } |
| 348 |
| 349 | void update(FTOutline &outRef, SW_FT_Stroker &stroker) |
| 350 | { |
| 351 | if (mGenerateStroke) { // Stroke Task |
| 352 | outRef.convert(mPath); |
| 353 | outRef.convert(mCap, mJoin, mStrokeWidth, mMeterLimit); |
| 354 |
| 355 | uint points, contors; |
| 356 |
| 357 | SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, |
| 358 | outRef.ftJoin, outRef.ftMeterLimit); |
| 359 | SW_FT_Stroker_ParseOutline(stroker, &outRef.ft); |
| 360 | SW_FT_Stroker_GetCounts(stroker, &points, &contors); |
| 361 |
| 362 | outRef.grow(points, contors); |
| 363 |
| 364 | SW_FT_Stroker_Export(stroker, &outRef.ft); |
| 365 |
| 366 | } else { // Fill Task |
| 367 | outRef.convert(mPath); |
| 368 | int fillRuleFlag = SW_FT_OUTLINE_NONE; |
| 369 | switch (mFillRule) { |
| 370 | case FillRule::EvenOdd: |
| 371 | fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL; |
| 372 | break; |
| 373 | default: |
| 374 | fillRuleFlag = SW_FT_OUTLINE_NONE; |
| 375 | break; |
| 376 | } |
| 377 | outRef.ft.flags = fillRuleFlag; |
| 378 | } |
| 379 |
| 380 | render(outRef); |
| 381 |
| 382 | mPath = VPath(); |
| 383 |
| 384 | mRle.notify(); |
| 385 | } |
| 386 | }; |
| 387 |
| 388 | struct VRasterizer::VRasterizerImpl { |
| 389 | VRleTask mTask; |
| 390 | FTOutline outlineRef; |
| 391 | SW_FT_Stroker stroker; |
| 392 |
| 393 | VRasterizerImpl() { |
| 394 | SW_FT_Stroker_New(&stroker); |
| 395 | } |
| 396 |
| 397 | ~VRasterizerImpl() { |
| 398 | SW_FT_Stroker_Done(stroker); |
| 399 | } |
| 400 |
| 401 | VRle & rle() { return mTask.rle(); } |
| 402 | VRleTask &task() { return mTask; } |
| 403 | }; |
| 404 |
| 405 | VRle VRasterizer::rle() |
| 406 | { |
| 407 | if (!d) return VRle(); |
| 408 | return d->rle(); |
| 409 | } |
| 410 |
| 411 | void VRasterizer::init() |
| 412 | { |
| 413 | if (!d) d = std::make_shared<VRasterizerImpl>(); |
| 414 | } |
| 415 |
| 416 | void VRasterizer::updateRequest() |
| 417 | { |
| 418 | d->task().update(d->outlineRef, d->stroker); |
| 419 | } |
| 420 |
| 421 | void VRasterizer::rasterize(VPath path, FillRule fillRule, const VRect &clip) |
| 422 | { |
| 423 | init(); |
| 424 | if (path.empty()) { |
| 425 | d->rle().reset(); |
| 426 | return; |
| 427 | } |
| 428 | d->task().update(std::move(path), fillRule, clip); |
| 429 | updateRequest(); |
| 430 | } |
| 431 |
| 432 | void VRasterizer::rasterize(VPath path, CapStyle cap, JoinStyle join, |
| 433 | float width, float meterLimit, const VRect &clip) |
| 434 | { |
| 435 | init(); |
| 436 | if (path.empty() || vIsZero(width)) { |
| 437 | d->rle().reset(); |
| 438 | return; |
| 439 | } |
| 440 | d->task().update(std::move(path), cap, join, width, meterLimit, clip); |
| 441 | updateRequest(); |
| 442 | } |
| 443 |
| 444 | V_END_NAMESPACE |
| 445 |