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