From: Sam Moore Date: Thu, 7 Aug 2014 02:42:02 +0000 (+0800) Subject: Cubic Beziers and more SVG stuff X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=457682a07cf1346aecfec22798e5a49a16db3c1b;p=ipdf%2Fcode.git Cubic Beziers and more SVG stuff Cubic Beziers! Actually transform the coordinates properly! Sort of maybe kind of render shape.svg correctly* * Shading and stroke width will be available in the premium edition, order today for $99.99 --- diff --git a/src/bezier.h b/src/bezier.h index 3a8e80d..0e9217f 100644 --- a/src/bezier.h +++ b/src/bezier.h @@ -9,21 +9,25 @@ namespace IPDF extern int BinomialCoeff(int n, int k); extern Real Bernstein(int k, int n, const Real & u); - /** A _quadratic_ bezier. **/ + /** A _cubic_ bezier. **/ struct Bezier { Real x0; Real y0; Real x1; Real y1; Real x2; Real y2; + Real x3; Real y3; Bezier() = default; // Needed so we can fread/fwrite this struct... for now. - Bezier(Real _x0, Real _y0, Real _x1, Real _y1, Real _x2, Real _y2) : x0(_x0), y0(_y0), x1(_x1), y1(_y1), x2(_x2), y2(_y2) {} + Bezier(Real _x0, Real _y0, Real _x1, Real _y1, Real _x2, Real _y2, Real _x3, Real _y3) : x0(_x0), y0(_y0), x1(_x1), y1(_y1), x2(_x2), y2(_y2), x3(_x3), y3(_y3) {} + + Bezier(Real _x0, Real _y0, Real _x1, Real _y1, Real _x2, Real _y2) : x0(_x0), y0(_y0), x1(_x1), y1(_y1), x2(_x2), y2(_y2), x3(_x2), y3(_y2) {} + std::string Str() const { std::stringstream s; - s << "Bezier{" << Float(x0) << "," << Float(y0) << " -> " << Float(x1) << "," << Float(y1) << " -> " << Float(x2) << "," << Float(y2) << "}"; + s << "Bezier{" << Float(x0) << "," << Float(y0) << " -> " << Float(x1) << "," << Float(y1) << " -> " << Float(x2) << "," << Float(y2) << " -> " << Float(x3) << "," << Float(y3) << "}"; return s.str(); } - Bezier(const Bezier & cpy, const Rect & t = Rect(0,0,1,1)) : x0(cpy.x0), y0(cpy.y0), x1(cpy.x1), y1(cpy.y1), x2(cpy.x2),y2(cpy.y2) + Bezier(const Bezier & cpy, const Rect & t = Rect(0,0,1,1)) : x0(cpy.x0), y0(cpy.y0), x1(cpy.x1), y1(cpy.y1), x2(cpy.x2),y2(cpy.y2), x3(cpy.x3), y3(cpy.y3) { x0 *= t.w; y0 *= t.h; @@ -31,24 +35,28 @@ namespace IPDF y1 *= t.h; x2 *= t.w; y2 *= t.h; + x3 *= t.w; + y3 *= t.h; x0 += t.x; y0 += t.y; x1 += t.x; y1 += t.y; x2 += t.x; y2 += t.y; + x3 += t.x; + y3 += t.y; } - Rect ToRect() {return Rect(x0,y0,x2-x0,y2-y0);} + Rect ToRect() {return Rect(x0,y0,x3-x0,y3-y0);} /** Evaluate the Bezier at parametric parameter u, puts resultant point in (x,y) **/ void Evaluate(Real & x, Real & y, const Real & u) { - Real coeff[3]; - for (unsigned i = 0; i < 3; ++i) - coeff[i] = Bernstein(i,2,u); - x = x0*coeff[0] + x1*coeff[1] + x2*coeff[2]; - y = y0*coeff[0] + y1*coeff[1] + y2*coeff[2]; + Real coeff[4]; + for (unsigned i = 0; i < 4; ++i) + coeff[i] = Bernstein(i,3,u); + x = x0*coeff[0] + x1*coeff[1] + x2*coeff[2] + x3*coeff[3]; + y = y0*coeff[0] + y1*coeff[1] + y2*coeff[2] + y3*coeff[3]; } }; diff --git a/src/document.cpp b/src/document.cpp index 7c49074..892f77a 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -225,6 +225,9 @@ bool Document::operator==(const Document & equ) const #include "../contrib/pugixml-1.4/src/pugixml.hpp" #include "../contrib/pugixml-1.4/src/pugixml.cpp" +/** + * Load an SVG into a rectangle + */ void Document::LoadSVG(const string & filename, const Rect & bounds) { using namespace pugi; @@ -306,7 +309,7 @@ static string & GetToken(const string & d, string & token, unsigned & i) while (i < d.size()) { - if (d[i] == ',' || isalpha(d[i]) || iswspace(d[i])) + if (d[i] == ',' || (isalpha(d[i]) && d[i] != 'e') || iswspace(d[i])) { if (token.size() == 0 && !iswspace(d[i])) { @@ -325,17 +328,20 @@ static string & GetToken(const string & d, string & token, unsigned & i) // Seriously this isn't really very DOM-like at all is it? void Document::AddPathFromString(const string & d, const Rect & bounds) { - Real x[3] = {0,0,0}; - Real y[3] = {0,0,0}; + Real x[4] = {0,0,0,0}; + Real y[4] = {0,0,0,0}; string token(""); string command("m"); + Real x0(0); + Real y0(0); + unsigned i = 0; unsigned prev_i = 0; - Real x0; - Real y0; - bool started = false; + + bool start = false; + while (i < d.size() && GetToken(d, token, i).size() > 0) { if (isalpha(token[0])) @@ -344,87 +350,111 @@ void Document::AddPathFromString(const string & d, const Rect & bounds) { i = prev_i; // hax if(command == "") - command = "l"; + command = "L"; } + + bool relative = islower(command[0]); - if (command == "m") + if (command == "m" || command == "M") { Debug("Construct moveto command"); - x[0] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; + Real dx = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; assert(GetToken(d,token,i) == ","); - y[0] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; - if (!started) - { - x0 = x[0]; - y0 = y[0]; - started = true; - } + Real dy = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; + + x[0] = (relative) ? x[0] + dx : dx; + y[0] = (relative) ? y[0] + dy : dy; + + + Debug("mmoveto %f,%f", Float(x[0]),Float(y[0])); - command = "l"; + command = (command == "m") ? "l" : "L"; } - else if (command == "c") + else if (command == "c" || command == "C" || command == "q" || command == "Q") { Debug("Construct curveto command"); - x[0] = strtod(GetToken(d,token,i).c_str(),NULL)/bounds.w; + Real dx = strtod(GetToken(d,token,i).c_str(),NULL)/bounds.w; assert(GetToken(d,token,i) == ","); - y[0] = strtod(GetToken(d,token,i).c_str(),NULL)/bounds.h; + Real dy = strtod(GetToken(d,token,i).c_str(),NULL)/bounds.h; - x[1] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; - assert(GetToken(d,token,i) == ","); - y[1] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; + x[1] = (relative) ? x[0] + dx : dx; + y[1] = (relative) ? y[0] + dy : dy; - x[2] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; + dx = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; assert(GetToken(d,token,i) == ","); - y[2] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; + dy = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; - unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2])); - Add(BEZIER,bounds,index); + x[2] = (relative) ? x[0] + dx : dx; + y[2] = (relative) ? y[0] + dy : dy; + if (command != "q" && command != "Q") + { + dx = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; + assert(GetToken(d,token,i) == ","); + dy = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; + x[3] = (relative) ? x[0] + dx : dx; + y[3] = (relative) ? y[0] + dy : dy; + } + else + { + x[3] = x[2]; + y[3] = y[2]; + } - Debug("[%u] curveto %f,%f %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]),Float(x[2]),Float(y[2])); + unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3])); + Add(BEZIER,Rect(0,0,1,1),index); - x[0] = x[2]; - y[0] = y[2]; + + Debug("[%u] curveto %f,%f %f,%f %f,%f", index, Float(x[1]),Float(y[1]),Float(x[2]),Float(y[2]),Float(x[3]),Float(y[3])); + + x[0] = x[3]; + y[0] = y[3]; } - else if (command == "l") + else if (command == "l" || command == "L") { Debug("Construct lineto command"); - x[1] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; + Real dx = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.w; assert(GetToken(d,token,i) == ","); - y[1] = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; + Real dy = strtod(GetToken(d,token,i).c_str(),NULL) / bounds.h; + + x[1] = (relative) ? x0 + dx : dx; + y[1] = (relative) ? y0 + dy : dy; x[2] = x[1]; y[2] = y[1]; - - Rect segment_bounds(x[0], y[0], x[2] - x[0], y[2] - y[0]); - unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2])); - Add(BEZIER,bounds,index); + x[3] = x[1]; + y[3] = y[1]; + + unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3])); + Add(BEZIER,Rect(0,0,1,1),index); Debug("[%u] lineto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1])); - x[0] = x[2]; - y[0] = y[2]; + x[0] = x[3]; + y[0] = y[3]; } - else if (command == "z") + else if (command == "z" || command == "Z") { Debug("Construct returnto command"); x[1] = x0; y[1] = y0; x[2] = x0; y[2] = y0; + x[3] = x0; + y[3] = y0; - unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2])); - Add(BEZIER,bounds,index); + unsigned index = AddBezierData(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3])); + Add(BEZIER,Rect(0,0,1,1),index); Debug("[%u] returnto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1])); - x[0] = x[2]; - y[0] = y[2]; + x[0] = x[3]; + y[0] = y[3]; command = "m"; } else @@ -432,6 +462,13 @@ void Document::AddPathFromString(const string & d, const Rect & bounds) Warn("Unrecognised command \"%s\", set to \"m\"", command.c_str()); command = "m"; } + + if (!start) + { + x0 = x[0]; + y0 = y[0]; + start = true; + } prev_i = i; } } diff --git a/src/objectrenderer.cpp b/src/objectrenderer.cpp index d23cd98..cb96c30 100644 --- a/src/objectrenderer.cpp +++ b/src/objectrenderer.cpp @@ -241,15 +241,12 @@ void BezierRenderer::RenderUsingCPU(const Objects & objects, const View & view, Real x[2]; Real y[2]; control.Evaluate(x[0], y[0], Real(0)); - Debug("target is (%lu, %lu)", target.w, target.h); - int64_t blen = 1; - if ((control.x1 != control.x2 || control.y1 != control.y2) - && (control.x1 != control.x0 || control.y1 != control.y0)) - { - blen = min(max((int64_t)2, (int64_t)(target.w/view.GetBounds().w)), (int64_t)100); - } + //Debug("target is (%lu, %lu)", target.w, target.h); + int64_t blen = 100; + //blen = min(max((int64_t)2, (int64_t)(target.w/view.GetBounds().w)), (int64_t)100); + Real invblen(1); invblen /= blen; - Debug("Using %li lines, inverse %f", blen, Double(invblen)); + //Debug("Using %li lines, inverse %f", blen, Double(invblen)); for (int64_t j = 1; j <= blen; ++j) { control.Evaluate(x[j % 2],y[j % 2], invblen*j); @@ -288,7 +285,8 @@ void BezierRenderer::PrepareBezierGPUBuffer(const Objects& objects) GPUBezierCoeffs coeffs = { Float(bez->x0), Float(bez->y0), Float(bez->x1), Float(bez->y1), - Float(bez->x2), Float(bez->y2) + Float(bez->x2), Float(bez->y2), + Float(bez->x3), Float(bez->y3) }; builder.Add(coeffs); } @@ -425,7 +423,7 @@ void ObjectRenderer::RenderLineOnCPU(int64_t x0, int64_t y0, int64_t x1, int64_t if (neg_m) --y; else ++y; p += two_dxdy; } - } while (++x < x_end); + } while (++x <= x_end); } } diff --git a/src/objectrenderer.h b/src/objectrenderer.h index 602b461..697cf5b 100644 --- a/src/objectrenderer.h +++ b/src/objectrenderer.h @@ -121,6 +121,7 @@ namespace IPDF float x0, y0; float x1, y1; float x2, y2; + float x3, y3; }; GLuint m_bezier_buffer_texture; diff --git a/src/shaders/bezier_texbuf_geom.glsl b/src/shaders/bezier_texbuf_geom.glsl index bb9a219..281627d 100644 --- a/src/shaders/bezier_texbuf_geom.glsl +++ b/src/shaders/bezier_texbuf_geom.glsl @@ -19,9 +19,11 @@ void main() { int bezierid = texelFetch(bezier_id_buffer_texture, objectid[0]).r; vec2 boundssize = gl_in[1].gl_Position.xy - gl_in[0].gl_Position.xy; - vec2 coeff0 = texelFetch(bezier_buffer_texture, bezierid*3).rg; - vec2 coeff1 = texelFetch(bezier_buffer_texture, bezierid*3+1).rg; - vec2 coeff2 = texelFetch(bezier_buffer_texture, bezierid*3+2).rg; + vec2 coeff0 = texelFetch(bezier_buffer_texture, bezierid*4).rg; + vec2 coeff1 = texelFetch(bezier_buffer_texture, bezierid*4+1).rg; + vec2 coeff2 = texelFetch(bezier_buffer_texture, bezierid*4+2).rg; + vec2 coeff3 = texelFetch(bezier_buffer_texture, bezierid*4+3).rg; + vec2 boundspxsize = pixsize[0]; int blen = clamp(int(abs(boundspxsize.x)),2,100); float invblen = 1.0f/float(blen); @@ -29,10 +31,11 @@ void main() { float t = i * invblen; float oneminust = 1.0f - t; - float bernstein0 = t*t; - float bernstein1 = 2*t*oneminust; - float bernstein2 = oneminust*oneminust; - gl_Position = vec4((coeff0*bernstein0 + coeff1*bernstein1 + coeff2*bernstein2) * boundssize + gl_in[0].gl_Position.xy, 0.0, 1.0); + float bernstein0 = t*t*t; + float bernstein1 = 3*t*t*oneminust; + float bernstein2 = 3*t*oneminust*oneminust; + float bernstein3 = oneminust*oneminust*oneminust; + gl_Position = vec4((coeff0*bernstein0 + coeff1*bernstein1 + coeff2*bernstein2 + coeff3*bernstein3) * boundssize + gl_in[0].gl_Position.xy, 0.0, 1.0); EmitVertex(); } diff --git a/src/svg-tests/glyphs.svg b/src/svg-tests/glyphs.svg new file mode 100644 index 0000000..e258dee --- /dev/null +++ b/src/svg-tests/glyphs.svg @@ -0,0 +1,136 @@ + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/svg-tests/koch1.svg b/src/svg-tests/koch1.svg new file mode 120000 index 0000000..2305547 --- /dev/null +++ b/src/svg-tests/koch1.svg @@ -0,0 +1 @@ +/home/sam/ipdf/sam/figures/koch1.svg \ No newline at end of file diff --git a/src/svg-tests/rabbit_simple.svg b/src/svg-tests/rabbit_simple.svg new file mode 120000 index 0000000..30fed1a --- /dev/null +++ b/src/svg-tests/rabbit_simple.svg @@ -0,0 +1 @@ +/home/sam/ipdf/sam/figures/rabbit_simple.svg \ No newline at end of file diff --git a/src/svg-tests/shape.svg b/src/svg-tests/shape.svg new file mode 120000 index 0000000..62d71a8 --- /dev/null +++ b/src/svg-tests/shape.svg @@ -0,0 +1 @@ +/home/sam/ipdf/sam/figures/shape.svg \ No newline at end of file diff --git a/src/view.cpp b/src/view.cpp index 496b3b0..4bbe5f0 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -330,7 +330,7 @@ void View::PrepareRender() m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling? // (Also, Wow I just actually used std::vector::at()) // (Also, I just managed to make it throw an exception because I'm a moron) - Debug("Object of type %d", type); + //Debug("Object of type %d", type); } // Finish the buffers