X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fdocument.cpp;h=4a5214a9d6ca447c14bdba1079fc0dbb30a0369b;hp=256950b527dda0a4848a2bca4c06b36f3c406549;hb=f0b6c9b6b95fde134927c395afbfbbbc057868e6;hpb=f28146cf72fc68c8d3690814b1f89d47b8c4e7b9 diff --git a/src/document.cpp b/src/document.cpp index 256950b..4a5214a 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -173,7 +173,7 @@ QuadTreeIndex Document::GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren PROFILE_SCOPE("Document::GenQuadChild()"); QuadTreeIndex new_index = m_quadtree.nodes.size(); Debug("-------------- Generating Quadtree Node %d (parent %d, type %d) ----------------------", new_index, parent, type); - m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, parent, type, 0, 0, -1}); + m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, parent, type, 0, 0, -1, true}); m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size(); for (QuadTreeIndex overlay = parent; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay) @@ -215,7 +215,7 @@ void Document::OverlayQuadChildren(QuadTreeIndex orig_parent, QuadTreeIndex pare PROFILE_SCOPE("Document::OverlayQuadChildren()"); QuadTreeIndex new_index = m_quadtree.nodes.size(); Debug("-------------- Generating Quadtree Node %d (orig %d parent %d, type %d) ----------------------", new_index, orig_parent, parent, type); - m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, orig_parent, type, 0, 0, -1}); + m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, orig_parent, type, 0, 0, -1, true}); m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size(); for (unsigned i = m_quadtree.nodes[parent].object_dirty; i < m_quadtree.nodes[parent].object_end; ++i) @@ -272,7 +272,7 @@ void Document::OverlayQuadParent(QuadTreeIndex orig_child, QuadTreeIndex child, { PROFILE_SCOPE("Document::OverlayQuadParent()"); QuadTreeIndex new_index = m_quadtree.nodes.size(); - m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0, -1}); + m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0, -1, true}); m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size(); m_quadtree.nodes[new_index].object_dirty = m_objects.bounds.size(); @@ -280,7 +280,10 @@ void Document::OverlayQuadParent(QuadTreeIndex orig_child, QuadTreeIndex child, { for (unsigned i = m_quadtree.nodes[overlay].object_begin; i < m_quadtree.nodes[overlay].object_end; ++i) { - m_objects.bounds.push_back(TransformFromQuadChild(m_objects.bounds[i], type)); + Rect new_bounds = TransformFromQuadChild(m_objects.bounds[i], type); + // If the object is too small to be seen, discard it. + if (!new_bounds.w || !new_bounds.h) continue; + m_objects.bounds.push_back(new_bounds); m_objects.types.push_back(m_objects.types[i]); m_objects.data_indices.push_back(m_objects.data_indices[i]); m_count++; @@ -295,6 +298,7 @@ void Document::OverlayQuadParent(QuadTreeIndex orig_child, QuadTreeIndex child, QuadTreeIndex prev_overlay = orig_node; while (m_quadtree.nodes[prev_overlay].next_overlay != -1) prev_overlay = m_quadtree.nodes[prev_overlay].next_overlay; m_quadtree.nodes[prev_overlay].next_overlay = new_index; + Debug("OverlayQuadParent(%d, %d, %d) = %d", orig_child, child, type, new_index); // Recurse into any extant parent. if (m_quadtree.nodes[orig_node].parent != -1) @@ -313,7 +317,7 @@ void Document::PropagateQuadChanges(QuadTreeIndex node) if (m_quadtree.nodes[overlay].object_dirty == m_quadtree.nodes[overlay].object_end) continue; // Recurse into our parent, should we have any. if (m_quadtree.nodes[node].parent != -1) - OverlayQuadParent(node, overlay, m_quadtree.nodes[overlay].child_type); + OverlayQuadParent(node, overlay, m_quadtree.nodes[node].child_type); // Recurse into any extant children. if (m_quadtree.nodes[node].top_left != -1) OverlayQuadChildren(node, overlay, QTC_TOP_LEFT); @@ -332,14 +336,17 @@ void Document::PropagateQuadChanges(QuadTreeIndex node) QuadTreeIndex Document::GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren type) { QuadTreeIndex new_index = m_quadtree.nodes.size(); - m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0, -1}); + m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0, -1, true}); m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size(); for (QuadTreeIndex overlay = child; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay) { for (unsigned i = m_quadtree.nodes[overlay].object_begin; i < m_quadtree.nodes[overlay].object_end; ++i) { - m_objects.bounds.push_back(TransformFromQuadChild(m_objects.bounds[i], type)); + Rect new_bounds = TransformFromQuadChild(m_objects.bounds[i], type); + // If the object is too small to be seen, discard it. + if (!new_bounds.w || !new_bounds.h) continue; + m_objects.bounds.push_back(new_bounds); m_objects.types.push_back(m_objects.types[i]); m_objects.data_indices.push_back(m_objects.data_indices[i]); m_count++; @@ -457,22 +464,78 @@ unsigned Document::AddBezier(const Bezier & bezier) unsigned index = AddBezierData(data); return Add(BEZIER, bounds, index); } +// 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 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; +} unsigned Document::Add(ObjectType type, const Rect & bounds, unsigned data_index, QuadTreeIndex qti) { PROFILE_SCOPE("Document::Add"); Rect new_bounds = bounds; #ifndef QUADTREE_DISABLED + int num_added = 1; if (qti == -1) qti = m_current_insert_node; if (qti != -1) { - // I am ashamed, yes. + // Move the object to the quadtree node it should be in. m_quadtree.GetCanonicalCoords(qti, new_bounds.x, new_bounds.y, this); + Rect cliprect = Rect(0,0,1,1); + num_added = AddClip(type, new_bounds, data_index, cliprect); } + else #endif - m_objects.types.push_back(type); - m_objects.bounds.push_back(new_bounds); - m_objects.data_indices.push_back(data_index); + { + m_objects.types.push_back(type); + m_objects.bounds.push_back(new_bounds); + m_objects.data_indices.push_back(data_index); + } + m_document_dirty = true; #ifndef QUADTREE_DISABLED if (qti != -1) { @@ -481,27 +544,30 @@ unsigned Document::Add(ObjectType type, const Rect & bounds, unsigned data_index { if (m_count == m_quadtree.nodes[new_qti].object_end+1) { - m_quadtree.nodes[new_qti].object_end++; - goto done; + m_quadtree.nodes[new_qti].object_end += num_added; + m_quadtree.nodes[new_qti].render_dirty = true; + new_qti = -1; + break; } new_qti = m_quadtree.nodes[new_qti].next_overlay; } + if (new_qti != -1) { QuadTreeIndex overlay = m_quadtree.nodes.size(); Debug("Adding new overlay, nqti = %d, overlay = %d", new_qti, overlay); - m_quadtree.nodes.push_back(m_quadtree.nodes[qti]); + m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0, -1}); m_quadtree.nodes[overlay].object_begin = m_count; // All objects are dirty. m_quadtree.nodes[overlay].object_dirty = m_count; - m_quadtree.nodes[overlay].object_end = m_count+1; + m_quadtree.nodes[overlay].object_end = m_count+num_added; m_quadtree.nodes[overlay].next_overlay = -1; + m_quadtree.nodes[overlay].render_dirty = true; m_quadtree.nodes[new_qti].next_overlay = overlay; new_qti = overlay; } -done: // matches is not amused, but sulix is nice and moved it inside the #ifdef for him. - m_count++; } - return m_count-1; + m_count += num_added; + return m_count-num_added; #else // words fail me (still not amused) return (m_count++); #endif