2 #include"v_ft_raster.h"
3 #include"v_ft_stroker.h"
16 FTOutline():mMemory(nullptr){}
17 ~FTOutline(){delete[] mMemory;}
18 void releaseMemory() {
19 if (mMemory) delete [] mMemory;
26 void convert(const VPath &path);
27 void convert(CapStyle, JoinStyle, float, float);
28 void moveTo(const VPointF &pt);
29 void lineTo(const VPointF &pt);
30 void cubicTo(const VPointF &ctr1, const VPointF &ctr2, const VPointF end);
33 void transform(const VMatrix &m);
35 SW_FT_Vector *mMemory{nullptr};
39 SW_FT_Stroker_LineCap ftCap;
40 SW_FT_Stroker_LineJoin ftJoin;
46 void FTOutline::reset()
48 ft.n_points = ft.n_contours = 0;
52 void FTOutline::grow(int points, int segments)
55 if (mPointSize >= points && mSegmentSize >= segments)
63 mSegmentSize = segments;
65 int point_size = (points + segments);
66 int contour_size = ((sizeof(short) * segments) / sizeof(SW_FT_Vector)) + 1;
67 int tag_size = ((sizeof(char) * (points + segments)) / sizeof(SW_FT_Vector)) + 1;
70 * Optimization, instead of allocating 3 different buffer
71 * allocate one big buffer and divide the buffer into 3 different
74 mMemory = new SW_FT_Vector[point_size + contour_size + tag_size];
75 ft.points = reinterpret_cast<SW_FT_Vector *>(mMemory);
76 ft.tags = reinterpret_cast<char *>(mMemory + point_size);
77 ft.contours = reinterpret_cast<short *>(mMemory + point_size + tag_size);
80 void FTOutline::convert(const VPath &path)
82 const std::vector<VPath::Element> &elements = path.elements();
83 const std::vector<VPointF> &points = path.points();
85 grow(points.size(), path.segments());
88 for(auto element : elements) {
90 case VPath::Element::MoveTo:
91 moveTo(points[index]);
94 case VPath::Element::LineTo:
95 lineTo(points[index]);
98 case VPath::Element::CubicTo:
99 cubicTo(points[index], points[index+1], points[index+2]);
102 case VPath::Element::Close:
112 void FTOutline::convert(CapStyle cap, JoinStyle join,
113 float width, float meterLimit)
115 ftClosed = (SW_FT_Bool) closed;
117 // map strokeWidth to freetype. It uses as the radius of the pen not the diameter
119 // convert to freetype co-ordinate
120 ftWidth = int(width * 64);
121 ftMeterLimit = int(meterLimit * 64);
123 // map to freetype capstyle
126 case CapStyle::Square:
127 ftCap = SW_FT_STROKER_LINECAP_SQUARE;
129 case CapStyle::Round:
130 ftCap = SW_FT_STROKER_LINECAP_ROUND;
133 ftCap = SW_FT_STROKER_LINECAP_BUTT;
138 case JoinStyle::Bevel:
139 ftJoin = SW_FT_STROKER_LINEJOIN_BEVEL;
141 case JoinStyle::Round:
142 ftJoin = SW_FT_STROKER_LINEJOIN_ROUND;
145 ftJoin = SW_FT_STROKER_LINEJOIN_MITER;
151 #define TO_FT_COORD(x) ((x) * 64) // to freetype 26.6 coordinate.
153 void FTOutline::moveTo(const VPointF &pt)
155 ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
156 ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
157 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
159 ft.contours[ft.n_contours] = ft.n_points - 1;
166 void FTOutline::lineTo(const VPointF &pt)
168 ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
169 ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
170 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
175 void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2, const VPointF ep)
177 ft.points[ft.n_points].x = TO_FT_COORD(cp1.x());
178 ft.points[ft.n_points].y = TO_FT_COORD(cp1.y());
179 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
182 ft.points[ft.n_points].x = TO_FT_COORD(cp2.x());
183 ft.points[ft.n_points].y = TO_FT_COORD(cp2.y());
184 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
187 ft.points[ft.n_points].x = TO_FT_COORD(ep.x());
188 ft.points[ft.n_points].y = TO_FT_COORD(ep.y());
189 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
193 void FTOutline::close()
197 index = ft.contours[ft.n_contours - 1] + 1;
202 // make sure atleast 1 point exists in the segment.
203 if (ft.n_points == index) {
208 ft.points[ft.n_points].x = ft.points[index].x;
209 ft.points[ft.n_points].y = ft.points[index].y;
210 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
215 void FTOutline::end()
218 ft.contours[ft.n_contours] = ft.n_points - 1;
230 rleGenerationCb( int count, const SW_FT_Span* spans,void *user)
232 VRle *rle = (VRle *) user;
233 VRle::Span *rleSpan = (VRle::Span *)spans;
234 rle->addSpan(rleSpan, count);
240 receiver = sender.get_future();
242 std::promise<VRle> sender;
243 std::future<VRle> receiver;
251 VRle operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
254 VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
256 if (stroke) { //Stroke Task
257 outRef.convert(path);
258 outRef.convert(cap, join, width, meterLimit);
262 SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, outRef.ftJoin, outRef.ftMeterLimit);
263 SW_FT_Stroker_ParseOutline(stroker, &outRef.ft, !outRef.ftClosed);
264 SW_FT_Stroker_GetCounts(stroker,&points, &contors);
266 FTOutline strokeOutline;
267 strokeOutline.grow(points, contors);
269 SW_FT_Stroker_Export(stroker, &strokeOutline.ft);
272 SW_FT_Raster_Params params;
274 params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA ;
275 params.gray_spans = &rleGenerationCb;
277 params.source = &strokeOutline;
279 sw_ft_grays_raster.raster_render(nullptr, ¶ms);
283 outRef.convert(path);
284 int fillRuleFlag = SW_FT_OUTLINE_NONE;
286 case FillRule::EvenOdd:
287 fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL;
290 fillRuleFlag = SW_FT_OUTLINE_NONE;
293 outRef.ft.flags = fillRuleFlag;
295 SW_FT_Raster_Params params;
297 params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA ;
298 params.gray_spans = &rleGenerationCb;
300 params.source = &outRef.ft;
302 sw_ft_grays_raster.raster_render(nullptr, ¶ms);
307 class RleTaskScheduler {
308 const unsigned _count{std::thread::hardware_concurrency()};
309 std::vector<std::thread> _threads;
310 std::vector<TaskQueue<RleTask>> _q{_count};
311 std::atomic<unsigned> _index{0};
313 void run(unsigned i) {
315 * initalize per thread objects.
317 FTOutline outlineRef;
318 SW_FT_Stroker stroker;
319 SW_FT_Stroker_New(&stroker);
323 RleTask *task = nullptr;
325 for (unsigned n = 0; n != _count * 32; ++n) {
326 if (_q[(i + n) % _count].try_pop(task)) break;
328 if (!task && !_q[i].pop(task)) break;
330 task->sender.set_value((*task)(outlineRef, stroker));
335 SW_FT_Stroker_Done(stroker);
340 for (unsigned n = 0; n != _count; ++n) {
341 _threads.emplace_back([&, n] { run(n); });
345 ~RleTaskScheduler() {
349 for (auto& e : _threads)
353 std::future<VRle> async(RleTask *task) {
354 auto receiver = std::move(task->receiver);
357 for (unsigned n = 0; n != _count; ++n) {
358 if (_q[(i + n) % _count].try_push(task)) return std::move(receiver);
361 _q[i % _count].push(task);
363 return std::move(receiver);
366 std::future<VRle> strokeRle(const VPath &path,
371 RleTask *task = new RleTask();
377 task->meterLimit = meterLimit;
381 std::future<VRle> fillRle(const VPath &path, FillRule fillRule) {
382 RleTask *task = new RleTask();
384 task->fillRule = fillRule;
385 task->stroke = false;
390 static RleTaskScheduler raster_scheduler;
401 VRaster::generateFillInfo(const VPath &path, FillRule fillRule)
403 if (path.isEmpty()) {
404 std::promise<VRle> promise;
405 promise.set_value(VRle());
406 return promise.get_future();
408 return std::move(raster_scheduler.fillRle(path, fillRule));
412 VRaster::generateStrokeInfo(const VPath &path, CapStyle cap, JoinStyle join,
413 float width, float meterLimit)
415 if (path.isEmpty()) {
416 std::promise<VRle> promise;
417 promise.set_value(VRle());
418 return promise.get_future();
420 return std::move(raster_scheduler.strokeRle(path, cap, join, width, meterLimit));