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

#include <Wt/Render/WTextRenderer>
#include <Wt/WPdfImage>

namespace Wt {
  namespace Render {

/*! \class WPdfRenderer Wt/Render/WPdfRenderer Wt/Render/WPdfRenderer
 *  \brief An XHTML to PDF renderer.
 *
 * This class implements an XHTML to PDF renderer. The rendering engine
 * supports only a subset of XHTML. See the documentation of WTextRenderer
 * for more information.
 *
 * \if cpp
 * The renderer renders to a libharu PDF document (using WPdfImage).
 * \endif
 * \if java
 * The renderer renders to a PDFJet PDF document (using WPdfImage).
 * \endif
 *
 * By default it uses a pixel resolution of 72 DPI, which is the
 * default for a WPdfImage, but differs from the default used by most
 * browsers (which is 96 DPI and has nothing to do with the actual
 * screen resolution). The pixel resolution can be configured using
 * setDpi(). Increasing the resolution has the effect of scaling down
 * the rendering. This can be used in conjunction with setFontScale() to
 * scale the font size differently than other content.
 *
 * \if cpp
 * Usage example:
 *
 * \code
 * extern "C" {
 *   HPDF_STATUS HPDF_UseUTFEncodings(HPDF_Doc pdf);
 * }
 *	
 * HPDF_Doc pdf = HPDF_New(error_handler, 0);
 * HPDF_UseUTFEncodings(pdf); // enables UTF-8 encoding with true type fonts
 * HPDF_Page page = HPDF_AddPage(pdf);
 * HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
 *
 * Render::WPdfRenderer renderer(pdf, page);
 * renderer.setMargin(2.54);
 * renderer.setDpi(96);
 *
 * renderer.render("<p style=\"background-color: #c11\">Hello, world !</p>");
 *
 * HPDF_SaveToFile(pdf, "hello.pdf");
 * HPDF_Free(pdf);
 * \endcode
 * \endif
 *
 * \ingroup render
 */
class WT_API WPdfRenderer : public WTextRenderer
{
public:
  /*! \brief Creates a new PDF renderer.
   *
   * The PDF renderer will render on the given \p pdf (starting).
   * If the \p page is not \c 0, then rendering will happen on this
   * first page (and its page sizes will be taken into account).
   *
   * Default margins are 0, and the default DPI is 72.
   */
  WPdfRenderer(HPDF_Doc pdf, HPDF_Page page = 0);

  virtual ~WPdfRenderer();

  /*! \brief Sets the page margins.
   *
   * This sets page margins, in \p cm, for one or more \p sides.
   */
  void setMargin(double cm, WFlags<Side> sides = All);

  /*! \brief Sets the resolution.
   *
   * The resolution used between CSS pixels and actual page
   * dimensions. Note that his does not have an effect on the <i>de
   * facto</i> standard CSS resolution of 96 DPI that is used to
   * convert between physical WLength units (like <em>cm</em>,
   * <em>inch</em> and <em>point</em>) and pixels. Instead it has the
   * effect of scaling down or up the rendered XHTML on the page.
   *
   * The dpi setting also affects the pageWidth(), pageHeight(), and
   * margin() pixel calculations.
   *
   * The default resolution is 72 DPI.
   */
  void setDpi(int dpi);

  /*! \brief Adds a font collection.
   *
   * \sa WPdfImage::addFontCollection()
   */
  void addFontCollection(const std::string& directory, bool recursive=true);

  /*! \brief Sets the current page.
   */
  void setCurrentPage(HPDF_Page page);

  /*! \brief Returns the current page.
   *
   * This returns the page last created using createPage(), or the page
   * set with setCurrentPage() page.
   */
  HPDF_Page currentPage() const { return page_; }

  virtual double pageWidth(int page) const;
  virtual double pageHeight(int page) const;

  virtual double margin(Side side) const;

  virtual WPaintDevice *startPage(int page);
  virtual void endPage(WPaintDevice *device);
  virtual WPainter *getPainter(WPaintDevice *device);

  /*! \brief Creates a new page.
   *
   * The default implementation creates a new page with the same dimensions
   * as the previous page.
   *
   * You may want to specialize this method to add e.g.~headers and footers.
   */
  virtual HPDF_Page createPage(int page);

private:
  struct FontCollection {
    std::string directory;
    bool recursive;
  };

  std::vector<FontCollection> fontCollections_;

  HPDF_Doc  pdf_;
  HPDF_Page page_;

  double margin_[4];
  int dpi_;
  WPainter *painter_;
};

  }
}

#endif // WPDF_RENDER_
