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;
42 SW_FT_Fixed ftMeterLimit;
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 //IMP: stroker takes radius in 26.6 co-ordinate
121 ftWidth = SW_FT_Fixed(width * (1<<6));
122 //IMP: stroker takes meterlimit in 16.16 co-ordinate
123 ftMeterLimit = SW_FT_Fixed(meterLimit * (1<<16));
125 // map to freetype capstyle
128 case CapStyle::Square:
129 ftCap = SW_FT_STROKER_LINECAP_SQUARE;
131 case CapStyle::Round:
132 ftCap = SW_FT_STROKER_LINECAP_ROUND;
135 ftCap = SW_FT_STROKER_LINECAP_BUTT;
140 case JoinStyle::Bevel:
141 ftJoin = SW_FT_STROKER_LINEJOIN_BEVEL;
143 case JoinStyle::Round:
144 ftJoin = SW_FT_STROKER_LINEJOIN_ROUND;
147 ftJoin = SW_FT_STROKER_LINEJOIN_MITER;
153 #define TO_FT_COORD(x) ((x) * 64) // to freetype 26.6 coordinate.
155 void FTOutline::moveTo(const VPointF &pt)
157 ft.points[ft.n_points].x = TO_FT_COORD(pt.x());
158 ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
159 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
161 ft.contours[ft.n_contours] = ft.n_points - 1;
168 void FTOutline::lineTo(const VPointF &pt)
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;
177 void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2, const VPointF ep)
179 ft.points[ft.n_points].x = TO_FT_COORD(cp1.x());
180 ft.points[ft.n_points].y = TO_FT_COORD(cp1.y());
181 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
184 ft.points[ft.n_points].x = TO_FT_COORD(cp2.x());
185 ft.points[ft.n_points].y = TO_FT_COORD(cp2.y());
186 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_CUBIC;
189 ft.points[ft.n_points].x = TO_FT_COORD(ep.x());
190 ft.points[ft.n_points].y = TO_FT_COORD(ep.y());
191 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
195 void FTOutline::close()
199 index = ft.contours[ft.n_contours - 1] + 1;
204 // make sure atleast 1 point exists in the segment.
205 if (ft.n_points == index) {
210 ft.points[ft.n_points].x = ft.points[index].x;
211 ft.points[ft.n_points].y = ft.points[index].y;
212 ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
217 void FTOutline::end()
220 ft.contours[ft.n_contours] = ft.n_points - 1;
232 rleGenerationCb( int count, const SW_FT_Span* spans,void *user)
234 VRle *rle = (VRle *) user;
235 VRle::Span *rleSpan = (VRle::Span *)spans;
236 rle->addSpan(rleSpan, count);
242 receiver = sender.get_future();
244 std::promise<VRle> sender;
245 std::future<VRle> receiver;
253 VRle operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
256 VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
258 if (stroke) { //Stroke Task
259 outRef.convert(path);
260 outRef.convert(cap, join, width, meterLimit);
264 SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, outRef.ftJoin, outRef.ftMeterLimit);
265 SW_FT_Stroker_ParseOutline(stroker, &outRef.ft, !outRef.ftClosed);
266 SW_FT_Stroker_GetCounts(stroker,&points, &contors);
268 FTOutline strokeOutline;
269 strokeOutline.grow(points, contors);
271 SW_FT_Stroker_Export(stroker, &strokeOutline.ft);
274 SW_FT_Raster_Params params;
276 params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA ;
277 params.gray_spans = &rleGenerationCb;
279 params.source = &strokeOutline;
281 sw_ft_grays_raster.raster_render(nullptr, ¶ms);
285 outRef.convert(path);
286 int fillRuleFlag = SW_FT_OUTLINE_NONE;
288 case FillRule::EvenOdd:
289 fillRuleFlag = SW_FT_OUTLINE_EVEN_ODD_FILL;
292 fillRuleFlag = SW_FT_OUTLINE_NONE;
295 outRef.ft.flags = fillRuleFlag;
297 SW_FT_Raster_Params params;
299 params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA ;
300 params.gray_spans = &rleGenerationCb;
302 params.source = &outRef.ft;
304 sw_ft_grays_raster.raster_render(nullptr, ¶ms);
309 class RleTaskScheduler {
310 const unsigned _count{std::thread::hardware_concurrency()};
311 std::vector<std::thread> _threads;
312 std::vector<TaskQueue<RleTask>> _q{_count};
313 std::atomic<unsigned> _index{0};
315 void run(unsigned i) {
317 * initalize per thread objects.
319 FTOutline outlineRef;
320 SW_FT_Stroker stroker;
321 SW_FT_Stroker_New(&stroker);
325 RleTask *task = nullptr;
327 for (unsigned n = 0; n != _count * 32; ++n) {
328 if (_q[(i + n) % _count].try_pop(task)) break;
330 if (!task && !_q[i].pop(task)) break;
332 task->sender.set_value((*task)(outlineRef, stroker));
337 SW_FT_Stroker_Done(stroker);
342 for (unsigned n = 0; n != _count; ++n) {
343 _threads.emplace_back([&, n] { run(n); });
347 ~RleTaskScheduler() {
351 for (auto& e : _threads)
355 std::future<VRle> async(RleTask *task) {
356 auto receiver = std::move(task->receiver);
359 for (unsigned n = 0; n != _count; ++n) {
360 if (_q[(i + n) % _count].try_push(task)) return receiver;
363 _q[i % _count].push(task);
368 std::future<VRle> strokeRle(const VPath &path,
373 RleTask *task = new RleTask();
379 task->meterLimit = meterLimit;
383 std::future<VRle> fillRle(const VPath &path, FillRule fillRule) {
384 RleTask *task = new RleTask();
386 task->fillRule = fillRule;
387 task->stroke = false;
392 static RleTaskScheduler raster_scheduler;
403 VRaster::generateFillInfo(const VPath &path, FillRule fillRule)
405 if (path.isEmpty()) {
406 std::promise<VRle> promise;
407 promise.set_value(VRle());
408 return promise.get_future();
410 return raster_scheduler.fillRle(path, fillRule);
414 VRaster::generateStrokeInfo(const VPath &path, CapStyle cap, JoinStyle join,
415 float width, float meterLimit)
417 if (path.isEmpty()) {
418 std::promise<VRle> promise;
419 promise.set_value(VRle());
420 return promise.get_future();
422 return raster_scheduler.strokeRle(path, cap, join, width, meterLimit);