/** ******************************************************************************* * @file parserSVG/Path.cpp * @brief basic path parsing (Sources) * @author Edouard DUPIN * @date 20/03/2012 * @par Project * parserSVG * * @par Copyright * Copyright 2011 Edouard DUPIN, all right reserved * * This software is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY. * * Licence summary : * You can modify and redistribute the sources code and binaries. * You can send me the bug-fix * * Term of the licence in in the file licence.txt. * ******************************************************************************* */ #include #include #include #include #include #include #include svg::Path::Path(PaintState parentPaintState) : svg::Base(parentPaintState) { } svg::Path::~Path(void) { } // return the next char position ... (after 'X' or NULL) const char * extractCmd(const char * input, char& cmd, etk::Vector& outputList) { if (*input == '\0') { return NULL; } outputList.Clear(); cmd = '\0'; const char * outputPointer = NULL; if (!( (input[0] <= 'Z' && input[0] >= 'A') || (input[0] <= 'z' && input[0] >= 'a') ) ) { SVG_ERROR("Error in the SVG Path : \"" << input << "\""); return NULL; } cmd = input[0]; SVG_VERBOSE("Find command : " << cmd); if (input[1] == '\0') { return &input[1]; } int32_t iii=1; // extract every float separated by a ' ' or a ',' float element; char spacer[10]; int32_t nbElementRead; while( sscanf(&input[iii], "%1[, ]%f%n", spacer, &element, &nbElementRead) == 2 || sscanf(&input[iii], "%f%n", &element, &nbElementRead) == 1) { SVG_VERBOSE("Find element : " << element); outputList.PushBack(element); iii += nbElementRead; } outputPointer = &input[iii]; while(*outputPointer!= '\0' && *outputPointer == ' ') { outputPointer++; } //outputPointer++; return outputPointer; } bool svg::Path::Parse(TiXmlNode * node, agg::trans_affine& parentTrans, etk::Vector2D& sizeMax) { ParseTransform(node); ParsePaintAttr(node); // add the property of the parrent modifications ... m_transformMatrix *= parentTrans; const char *elementXML = node->ToElement()->Attribute("d"); if (NULL == elementXML) { SVG_ERROR("(l "<Row()<<") path: missing 'p' attribute"); return false; } SVG_VERBOSE("Parse Path : \"" << elementXML << "\""); char command; etk::Vector listDot; for( const char *sss=extractCmd(elementXML, command, listDot); NULL != sss; sss=extractCmd(sss, command, listDot) ) { pathBasic_ts pathElement; memset(&pathElement, 0, 1*sizeof(pathBasic_ts)); switch(command) { case 'M': // Move To (absolute) case 'L': // Line To (absolute) case 'V': // Vertical Line To (absolute) case 'H': // Horizantal Line To (absolute) case 'Q': // Quadratic Bezier curve (absolute) case 'T': // smooth quadratic Bezier curve to (absolute) case 'C': // curve to (absolute) case 'S': // smooth curve to (absolute) case 'A': // elliptical Arc (absolute) case 'Z': // closepath (absolute) pathElement.relative = false; break; default : // else (relative) pathElement.relative = true; break; } switch(command) { case 'M': // Move To (absolute) case 'm': // Move To (relative) // 2 Elements ... if(listDot.Size()%2 != 0) { SVG_WARNING("the PATH command "<< command << " has not the good number of element = " << listDot.Size() ); break; } pathElement.cmd = svg::PATH_ENUM_MOVETO; if (listDot.Size() >= 2) { pathElement.element[0] = listDot[0]; pathElement.element[1] = listDot[1]; m_listElement.PushBack(pathElement); } pathElement.cmd = svg::PATH_ENUM_LINETO; for(int32_t iii=2; iiicolor(agg::rgba8(m_paint.fill.r, m_paint.fill.g, m_paint.fill.b, m_paint.fill.a)); agg::path_storage path; path.start_new_path(); for(int32_t iii=0; iii curve(path); if (m_paint.fill.a != 0x00) { agg::conv_transform, agg::trans_affine> trans(curve, mtx); // set the filling mode : myRenderer.m_rasterizer.filling_rule((m_paint.flagEvenOdd)?agg::fill_even_odd:agg::fill_non_zero); myRenderer.m_rasterizer.add_path(trans); agg::render_scanlines(myRenderer.m_rasterizer, myRenderer.m_scanLine, *myRenderer.m_renderArea); } if (m_paint.strokeWidth > 0 && m_paint.stroke.a!=0x00 ) { myRenderer.m_renderArea->color(agg::rgba8(m_paint.stroke.r, m_paint.stroke.g, m_paint.stroke.b, m_paint.stroke.a)); // Drawing as an outline agg::conv_stroke > myPolygonStroke(curve); myPolygonStroke.width(m_paint.strokeWidth); agg::conv_transform >, agg::trans_affine> transStroke(myPolygonStroke, mtx); // set the filling mode : myRenderer.m_rasterizer.filling_rule(agg::fill_non_zero); myRenderer.m_rasterizer.add_path(transStroke); agg::render_scanlines(myRenderer.m_rasterizer, myRenderer.m_scanLine, *myRenderer.m_renderArea); } } void svg::Path::AbstractMoveTo(agg::path_storage& path, bool rel, double x, double y) { if(true == rel) { path.rel_to_abs(&x, &y); } path.move_to(x, y); } void svg::Path::AbstractLineTo(agg::path_storage& path, bool rel, double x, double y) { if(true == rel) { path.rel_to_abs(&x, &y); } path.line_to(x, y); } void svg::Path::AbstractHLineTo(agg::path_storage& path, bool rel, double x) { double x2 = 0.0; double y2 = 0.0; if(0!=path.total_vertices()) { path.vertex(path.total_vertices() - 1, &x2, &y2); if(true == rel) { x += x2; } path.line_to(x, y2); } } void svg::Path::AbstractVLineTo(agg::path_storage& path, bool rel, double y) { double x2 = 0.0; double y2 = 0.0; if(path.total_vertices()) { path.vertex(path.total_vertices() - 1, &x2, &y2); if(true == rel) { y += y2; } path.line_to(x2, y); } } void svg::Path::AbstractCurve3(agg::path_storage& path, bool rel, double x1, double y1, double x, double y) { if(true == rel) { path.rel_to_abs(&x1, &y1); path.rel_to_abs(&x, &y); } path.curve3(x1, y1, x, y); } void svg::Path::AbstractCurve3(agg::path_storage& path, bool rel, double x, double y) { if(true == rel) { path.curve3_rel(x, y); } else { path.curve3(x, y); } } void svg::Path::AbstractCurve4(agg::path_storage& path, bool rel, double x1, double y1, double x2, double y2, double x, double y) { if(true == rel) { path.rel_to_abs(&x1, &y1); path.rel_to_abs(&x2, &y2); path.rel_to_abs(&x, &y); } path.curve4(x1, y1, x2, y2, x, y); } void svg::Path::AbstractCurve4(agg::path_storage& path, bool rel, double x2, double y2, double x, double y) { if(true == rel) { path.curve4_rel(x2, y2, x, y); } else { path.curve4(x2, y2, x, y); } } void svg::Path::AbstractCloseSubpath(agg::path_storage& path) { path.end_poly(agg::path_flags_close); }