// Graphics methods for Cairo

#include "abc2ps.h"
#include <string.h>
#include <math.h>

#if ABC_USE_CAIRO
void cairoBeginPage( GraphicsContext cx )
{
  cairo_translate( cx, cfmt.leftmargin, 0 );
}

void cairoEndPage( GraphicsContext cx )
{
  cairo_translate( cx, -cfmt.leftmargin, 0 );
}

static void cairoMoveTo( GraphicsContext cx, float x, float y )
{
  cairo_move_to( cx, x, y );
}

static void cairoLineTo( GraphicsContext cx, float x, float y )
{
  cairo_line_to( cx, x, y );
}

static void cairoCurveTo( GraphicsContext cx, float x1, float y1, float x2, float y2, float x3, float y3 )
{
  cairo_curve_to( cx, x1, y1, x2, y2, x3, y3 );
}

static void cairoArc( GraphicsContext cx, float x, float y, float radius, float startAngle, float endAngle )
{
  cairo_arc( cx, x, y, radius, startAngle, endAngle );
}

static void cairoTranslate( GraphicsContext cx, float x, float y )
{
  cairo_translate( cx, x, y );
}

static void cairoScale( GraphicsContext cx, float x, float y )
{
  cairo_scale( cx, x, y );
}

static void cairoSetLineWidth( GraphicsContext cx, float x )
{
  cairo_set_line_width( cx, x );
}

static void cairoSetFont( GraphicsContext cx, int font, int size )
{
  static cairo_font_face_t* sSerifFace;
  if ( font == kSerifFont ) {
    if ( !sSerifFace )
      sSerifFace = cairo_toy_font_face_create( "Times-Roman", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL );
    cairo_set_font_face( cx, sSerifFace );
  } else
    cairo_set_font_face( cx, NULL );
  cairo_set_font_size( cx, size );
}

static void cairoDrawText( GraphicsContext cx, float x, float y, const char *text, int alignment )
{
  cairo_text_extents_t extents;
  cairo_text_extents( cx, text, &extents );
  if ( alignment < 0 )
    x -= extents.width;
  else if ( alignment == 0 )
    x -= extents.width/2;
  y += extents.height;
  cairo_save( cx );
  cairo_translate( cx, x, y-extents.height );
  cairo_scale( cx, 1, -1 );
  cairo_move_to( cx, 0, 0 );
  cairo_show_text( cx, text );
  cairo_restore( cx );
}

GraphicsMethods gGraphicsMethods = {
  cairoBeginPage,
  cairoEndPage,
  cairoMoveTo,
  cairoLineTo,
  cairoCurveTo,
  cairoArc,
  cairo_new_path,
  cairo_close_path,
  cairoSetLineWidth,
  cairo_fill,
  cairo_stroke,
  cairo_save,
  cairo_restore,
  cairoTranslate,
  cairoScale,
  cairoSetFont,
  cairoDrawText,
};
#endif

