// This may look like C code, but it's really -*- C++ -*-
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#ifndef WSVG_IMAGE_H_
#define WSVG_IMAGE_H_

#include <Wt/WBrush>
#include <Wt/WFont>
#include <Wt/WPen>
#include <Wt/WPointF>
#include <Wt/WResource>
#include <Wt/WTransform>
#include <Wt/WVectorImage>

namespace Wt {

/*! \class WSvgImage Wt/WSvgImage Wt/WSvgImage
 *  \brief A paint device for rendering using Scalable Vector Graphics (SVG).
 *
 * The %WSvgImage is primarily used by WPaintedWidget to render to the
 * browser in Support Vector Graphics (SVG) format.
 *
 * You may also use the %WSvgImage as an independent resource, for example
 * in conjunction with a WAnchor or WImage, or to make a hard copy of an
 * image in SVG format, using write(std::ostream&).
 *
 * \ingroup painting
 */
class WT_API WSvgImage : public WVectorImage, public WResource
{
public:
  /*! \brief Create an SVG paint device.
   */
  WSvgImage(const WLength& width, const WLength& height, WObject *parent = 0);

  virtual void setChanged(int flags);
  virtual void drawArc(const WRectF& rect, double startAngle, double spanAngle);
  virtual void drawImage(const WRectF& rect, const std::string& imgUri,
			 int imgWidth, int imgHeight, const WRectF& sourceRect);
  virtual void drawLine(double x1, double y1, double x2, double y2);
  virtual void drawPath(const WPainterPath& path);
  virtual void drawText(const WRectF& rect, int flags,
			const WString& text);
  virtual void init();
  virtual void done();

  virtual std::string rendered();

protected:
  virtual const std::string resourceMimeType() const;
  virtual bool streamResourceData(std::ostream& stream,
				  const ArgumentMap& arguments);

private:
  bool        newGroup_;
  bool        newClipPath_;
  bool        busyWithPath_;
  int         currentClipId_;

  static int  nextClipId_;

  WTransform  currentTransform_;
  WBrush      currentBrush_;
  WFont       currentFont_;
  WPen        currentPen_;
  WPointF     pathTranslation_;

  std::string shapes_;

  void finishPath();
  void makeNewGroup();
  std::string fillStyle() const;
  std::string strokeStyle() const;
  std::string fontStyle() const;
  std::string clipPath() const;

  static std::string quote(double s);
  static std::string quote(const std::string& s);

  void drawPlainPath(std::ostream& s, const WPainterPath& path);
};

}

#endif // WSVG_IMAGE_H_
