#ifndef DOC_SVG_H
#define DOC_SVG_H
#include <vector>
//#include <ctype.h>
#include "tinyxml.h"
#include "DocStyle.h"
#include "DocTemplate.h"
#include "DocOutput.h"
#include "hpdf.h"
class SVGOperation
{
public:
enum PathOperation {
MoveToAbs,
LineToAbs,
HorizontalLineToAbs,
VerticalLineToAbs,
CurveToAbs,
SmoothCurveToAbs,
QuadraticBezierCurveToAbs,
SmoothQuadraticBezierCurveToAbs,
EllipticalArcToAbs,
ClosePath,
MoveToRel,
LineToRel,
HorizontalLineToRel,
VerticalLineToRel,
CurveToRel,
SmoothCurveToRel,
QuadraticBezierCurveToRel,
SmoothQuadraticBezierCurveToRel,
EllipticalArcToRel,
AltClosePath,
BadOperation,
BeginPathOperation,
EndPathOperation
};
static const char PathOperationChar[];
static const int PathOperationArgs[];
static PathOperation getOperationForChar(char a_ch) {
for (int i = 0; i < BadOperation; i++)
if (PathOperationChar[i] == a_ch)
return (PathOperation)i;
return BadOperation;
}
int getArgCount() {
return PathOperationArgs[m_type];
}
void dump() {
fprintf(stderr, " OP: -%c- ARGS: ", PathOperationChar[m_type]);
for (int i = 0; i < PathOperationArgs[m_type]; i++)
fprintf(stderr, " -%f- ", m_values[i]);
fprintf(stderr, "\n");
}
void output(HPDF_Page page, float curPos[2], bool fill, PathOperation prevOp) {
float pageHeight = HPDF_Page_GetHeight(page);
float x = m_values[0];
float y = m_values[1];
switch (m_type) {
case MoveToAbs:
curPos[0] = x; curPos[1] = y;
HPDF_Page_MoveTo(page, curPos[0], pageHeight - curPos[1]);
fprintf(stderr, "move to: %f %f\n", curPos[0], curPos[1]);
break;
case LineToAbs:
curPos[0] = x; curPos[1] = y;
HPDF_Page_LineTo(page, curPos[0], pageHeight - curPos[1]);
fprintf(stderr, "line to: %f %f\n", curPos[0], curPos[1]);
break;
case HorizontalLineToAbs:
curPos[0] = x;
HPDF_Page_LineTo(page, curPos[0], pageHeight - curPos[1]);
break;
case VerticalLineToAbs:
curPos[1] = x;
HPDF_Page_LineTo(page, curPos[0], pageHeight - curPos[1]);
break;
case CurveToAbs:
HPDF_Page_CurveTo(page,
m_values[0], pageHeight - m_values[1],
m_values[2], pageHeight - m_values[3],
m_values[4], pageHeight - m_values[5]);
curPos[0] = m_values[4];
curPos[1] = m_values[5];
break;
case SmoothCurveToAbs:
HPDF_Page_CurveTo2(page,
m_values[0], pageHeight - m_values[1],
m_values[2], pageHeight - m_values[3]);
curPos[0] = m_values[2];
curPos[1] = m_values[3];
break;
case QuadraticBezierCurveToAbs:
//HPDF_Page_CurveTo2(HPDF_Page, HPDF_REAL x2, HPDF_REAL y2, HPDF_REAL x3, HPDF_REAL y3);
//HPDF_Page_CurveTo3(HPDF_Page, HPDF_REAL x1, HPDF_REAL y1, HPDF_REAL x3, HPDF_REAL y3);
// HPDF_Page_CurveTo3 // ??
HPDF_Page_CurveTo2(page,
m_values[0], pageHeight - m_values[1],
m_values[2], pageHeight - m_values[3]);
curPos[0] = m_values[2];
curPos[1] = m_values[3];
break;
case SmoothQuadraticBezierCurveToAbs:
// No idea, only has 2 args, I guess an x and y, but how does this make a curve?
break;
case EllipticalArcToAbs:
// SVG has 7 args, PDF has 5!
//HPDF_Page_Arc (HPDF_Page page, HPDF_REAL x, HPDF_REAL y,
// HPDF_REAL ray, HPDF_REAL ang1, HPDF_REAL ang2);
break;
case MoveToRel:
curPos[0] += x; curPos[1] += y;
HPDF_Page_MoveTo(page, curPos[0], pageHeight - curPos[1]);
break;
case LineToRel:
if (prevOp == BeginPathOperation) {
curPos[0] = x; curPos[1] = y;
} else {
curPos[0] += x; curPos[1] += y;
}
HPDF_Page_LineTo(page, curPos[0], pageHeight - curPos[1]);
break;
case HorizontalLineToRel:
curPos[0] += x;
HPDF_Page_LineTo(page, curPos[0], pageHeight - curPos[1]);
break;
case VerticalLineToRel:
curPos[1] += x;
HPDF_Page_LineTo(page, curPos[0], pageHeight - curPos[1]);
break;
case CurveToRel:
// TODO: check if these are cumulatively relative, or all rel to current pos
HPDF_Page_CurveTo(page,
curPos[0] + m_values[0], pageHeight - curPos[1] - m_values[1],
curPos[0] + m_values[2], pageHeight - curPos[1] - m_values[3],
curPos[0] + m_values[4], pageHeight - curPos[1] - m_values[5]);
curPos[0] += m_values[4];
curPos[1] += m_values[5];
break;
case SmoothCurveToRel:
// TODO: check if these are cumulatively relative, or all rel to current pos
HPDF_Page_CurveTo2(page,
curPos[0] + m_values[0], pageHeight - curPos[1] - m_values[1],
curPos[0] + m_values[2], pageHeight - curPos[1] - m_values[3]);
curPos[0] += m_values[2];
curPos[1] += m_values[3];
break;
case QuadraticBezierCurveToRel:
fprintf(stderr, "TODO: %s %i\n", __FILE__, __LINE__);
break;
case SmoothQuadraticBezierCurveToRel:
fprintf(stderr, "TODO: %s %i\n", __FILE__, __LINE__);
break;
case EllipticalArcToRel:
fprintf(stderr, "TODO: %s %i\n", __FILE__, __LINE__);
break;
case ClosePath:
case AltClosePath:
default:
// TODO: actually this means to draw a line back to the first point in the path
/*
if (fill)
HPDF_Page_FillStroke(page);
else
HPDF_Page_Stroke(page);
*/
break;
}
}
PathOperation m_type;
double m_values[7];
};
class DocSVG : public TiXmlVisitor
{
public:
DocSVG();
virtual ~DocSVG();
/// Visit a document.
virtual bool VisitEnter( const TiXmlDocument& doc );
/// Visit a document.
virtual bool VisitExit( const TiXmlDocument& doc );
/// Visit a declaration
virtual bool Visit( const TiXmlDeclaration& declaration );
/// Visit a stylesheet reference
virtual bool Visit( const TiXmlStylesheetReference& stylesheet );
/// Visit a comment node
virtual bool Visit( const TiXmlComment& comment );
/// Visit an unknow node
virtual bool Visit( const TiXmlUnknown& unknown );
/// Visit an element.
virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
/// Visit an element.
virtual bool VisitExit( const TiXmlElement& element );
/// Visit a text node
virtual bool Visit( const TiXmlText& text );
void DumpOperations()
{
for (int i = 0; i < operations.size(); i++)
operations[i].dump();
}
void WriteTo(DocOutputDevice* outputDoc)
{
DocOutputPage* outputPage = outputDoc->newPage();
HPDF_Page page = outputPage->page();
float curPos[2] = { 10.0, 10.0 };
//fprintf(stderr, "doc wxh: %f x %f\n", HPDF_Page_GetWidth(page), HPDF_Page_GetHeight(page));
HPDF_Page_EndText(page);
HPDF_Page_SetLineWidth(page, 2.0);
HPDF_Page_SetRGBStroke(page, 0.85f, 0.5f, 0.5f); // TODO: set this via API
//HPDF_Page_SetRGBFill(page, 0.95f, 0.95f, 0.95f);
HPDF_Page_MoveTo(page, 0.0, HPDF_Page_GetHeight(page));
/*
HPDF_Page_Rectangle(page, x1, pageHeight - y1 - h, w, h);
HPDF_Page_Circle(page, x, pageHeight - y, radius);
*/
SVGOperation::PathOperation prevOp = SVGOperation::BadOperation;
for (int i = 0; i < operations.size(); i++) {
operations[i].output(page, curPos, false, prevOp);
prevOp = operations[i].m_type;
}
HPDF_Page_Stroke(page); // Need to close
HPDF_Page_BeginText(page);
delete outputPage;
}
private:
void ParsePath(const char* a_pathData)
{
SVGOperation currentOp;
currentOp.m_type = SVGOperation::BeginPathOperation;
operations.push_back(currentOp);
currentOp.m_type = SVGOperation::MoveToAbs;
int val = 0;
int i = 0;
const char* pathPtr = a_pathData;
while (*pathPtr) {
char* end = 0;
double v = strtod(pathPtr, &end);
if ( end != pathPtr ) {
currentOp.m_values[val] = v;
val++;
if (val == currentOp.getArgCount()) {
operations.push_back(currentOp);
val = 0;
}
pathPtr = end;
} else {
if (SVGOperation::getOperationForChar(*pathPtr) != SVGOperation::BadOperation)
{
if (val != 0) {
fprintf(stderr, "Broken SVG path data, expecting more numbers, got: -%c-\n", *pathPtr);
break;
}
if (currentOp.getArgCount() == 0) // eg: ClosePath
operations.push_back(currentOp);
currentOp.m_type = SVGOperation::getOperationForChar(*pathPtr);
}
pathPtr++;
}
}
if (val == currentOp.getArgCount()) {
operations.push_back(currentOp);
} else if (val) {
fprintf(stderr, "Broken SVG path data, expecting more numbers\n");
}
currentOp.m_type = SVGOperation::EndPathOperation;
operations.push_back(currentOp);
}
std::vector<SVGOperation> operations;
};
#endif // DOC_SVG_H