+// Adds an object to the Document, clipping it to m_clip_rect.
+// Helper function called by Document::Add()
+int Document::AddClip(ObjectType type, const Rect& bounds, unsigned data_index, const Rect& clip_rect)
+{
+ PROFILE_SCOPE("Document::AddAndClip");
+ switch (type)
+ {
+ case RECT_FILLED:
+ case RECT_OUTLINE:
+ case PATH:
+ {
+ Rect obj_bounds = clip_rect.Clip(bounds);
+ m_objects.bounds.push_back(obj_bounds);
+ m_objects.types.push_back(type);
+ m_objects.data_indices.push_back(data_index);
+ return 1;
+ }
+ case BEZIER:
+ {
+ // If we're entirely within the clipping rect, no clipping need occur.
+ if (clip_rect.Contains(bounds))
+ {
+ m_objects.bounds.push_back(bounds);
+ m_objects.types.push_back(type);
+ m_objects.data_indices.push_back(data_index);
+ return 1;
+ }
+ Rect clip_bezier_bounds = TransformRectCoordinates(bounds, clip_rect);
+ std::vector<Bezier> new_curves = m_objects.beziers[data_index].ClipToRectangle(clip_bezier_bounds);
+ for (size_t i = 0; i < new_curves.size(); ++i)
+ {
+ Bezier new_curve_data = new_curves[i].ToAbsolute(bounds);
+ Rect new_bounds = new_curve_data.SolveBounds();
+ new_curve_data = new_curve_data.ToRelative(new_bounds);
+ unsigned index = AddBezierData(new_curve_data);
+ m_objects.bounds.push_back(new_bounds);
+ m_objects.types.push_back(BEZIER);
+ m_objects.data_indices.push_back(index);
+ }
+ return new_curves.size();
+ }
+ default:
+ m_objects.bounds.push_back(bounds);
+ m_objects.types.push_back(type);
+ m_objects.data_indices.push_back(data_index);
+ return 1;
+ }
+ return 0;
+}