From: David Gow Date: Fri, 17 Oct 2014 09:58:29 +0000 (+0800) Subject: Some code cleanup, quadtree fixes, non-quadtree build fixes. X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=commitdiff_plain;h=d463f1ebcf3057162a1e9e51579a022c98754fd3 Some code cleanup, quadtree fixes, non-quadtree build fixes. --- diff --git a/src/document.cpp b/src/document.cpp index 256950b..5b04bd2 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -457,22 +457,83 @@ 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; + int num_added = 1; + bool still_to_add = true; #ifndef QUADTREE_DISABLED 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); + // If an object spans multiple quadtree nodes... + if (!cliprect.Contains(new_bounds)) + { + num_added = AddClip(type, new_bounds, data_index, cliprect); + still_to_add = false; + } } #endif - m_objects.types.push_back(type); - m_objects.bounds.push_back(new_bounds); - m_objects.data_indices.push_back(data_index); + if (still_to_add) + { + m_objects.types.push_back(type); + m_objects.bounds.push_back(new_bounds); + m_objects.data_indices.push_back(data_index); + } #ifndef QUADTREE_DISABLED if (qti != -1) { @@ -481,7 +542,7 @@ 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++; + m_quadtree.nodes[new_qti].object_end += num_added; goto done; } new_qti = m_quadtree.nodes[new_qti].next_overlay; @@ -493,15 +554,15 @@ unsigned Document::Add(ObjectType type, const Rect & bounds, unsigned data_index 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[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++; + m_count += num_added; } - return m_count-1; + return m_count-num_added; #else // words fail me (still not amused) return (m_count++); #endif diff --git a/src/document.h b/src/document.h index fd6e574..f98833a 100644 --- a/src/document.h +++ b/src/document.h @@ -29,11 +29,14 @@ namespace IPDF class Document { public: - Document(const std::string & filename = "", const std::string & font_filename = "fonts/DejaVuSansMono.ttf") : m_objects(), m_current_insert_node(-1), m_count(0), m_font_data(NULL), m_font() + Document(const std::string & filename = "", const std::string & font_filename = "fonts/DejaVuSansMono.ttf") : m_objects(), m_count(0), m_font_data(NULL), m_font() { Load(filename); if (font_filename != "") SetFont(font_filename); +#ifndef QUADTREE_DISABLED + m_current_insert_node = -1; +#endif } virtual ~Document() { @@ -54,6 +57,7 @@ namespace IPDF unsigned AddPath(unsigned start_index, unsigned end_index, const Colour & shading=Colour(0.6,0.6,0.6,1), const Colour & stroke=Colour(0,0,0,0)); unsigned AddBezier(const Bezier & bezier); + int AddClip(ObjectType type, const Rect & bounds, unsigned data_index, const Rect & clip_rect); unsigned Add(ObjectType type, const Rect & bounds, unsigned data_index = 0, QuadTreeIndex qtnode = -1); unsigned AddBezierData(const Bezier & bezier); unsigned AddPathData(const Path & path); diff --git a/src/rect.h b/src/rect.h index 0188446..78c5a70 100644 --- a/src/rect.h +++ b/src/rect.h @@ -38,6 +38,11 @@ namespace IPDF if (y > other.y + other.h) return false; return true; } + + inline bool Contains(const TRect& other) const + { + return PointIn(other.x, other.y) && PointIn(other.x + other.w, other.y + other.h); + } template TRect Convert() const {return TRect(B(x), B(y), B(w), B(h));} @@ -49,6 +54,31 @@ namespace IPDF h = T(equ.h); return *this; } + + // Clips "other" to "this" + inline TRect Clip(const TRect& other) const + { + TRect clipped = other; + if (clipped.x < x) + { + clipped.w += clipped.x - x; + clipped.x = x; + } + if (clipped.y < y) + { + clipped.h += clipped.y - y; + clipped.y = 0; + } + if (clipped.x + clipped.w > x + w) + { + clipped.w += ((x + w) - (clipped.x + clipped.w)); + } + if (clipped.y + clipped.h > y + h) + { + clipped.h += ((y + h) - (clipped.y + clipped.h)); + } + return clipped; + } };