// Graphics methods for Cairo

#include "abc2ps.h"

#if TARGET_OS_IPHONE || TARGET_OS_MAC

void cgBeginPage( GraphicsContext cx )
{
	CGRect bounds = CGRectMake( 0, 0, cfmt.staffwidth+2*cfmt.leftmargin, cfmt.pageheight );
//  printf("begin\n");
  CGContextBeginPage( cx, &bounds );
  CGContextSaveGState( cx );
	CGContextSetRGBFillColor( cx, 1.0, 1.0, 1.0, 1.0 );
	CGContextFillRect( cx, bounds );
  CGContextTranslateCTM( cx, 0, bounds.size.height );

#if 0//Page debugging
  CGContextTranslateCTM( cx, 0, -cfmt.pageheight );
  CGContextSetRGBFillColor( cx, 0.5, 0.5, 0.5, 0.5 );
  CGContextFillRect( cx, CGRectMake(cfmt.leftmargin, cfmt.topmargin, cfmt.staffwidth, cfmt.pageheight ) );
  CGContextSetRGBStrokeColor( cx, 1.0, 0.0, 0.0, 1.0 );
  CGContextStrokeRect( cx, CGRectMake(cfmt.leftmargin, cfmt.topmargin, cfmt.staffwidth, cfmt.pageheight ) );
  CGContextTranslateCTM( cx, 0, cfmt.pageheight );
  CGContextSetRGBStrokeColor( cx, 0.0, 0.0, 0.0, 1.0 );
  CGContextSetRGBFillColor( cx, 0.0, 0.0, 0.0, 1.0 );
#endif

  CGContextSetRGBFillColor( cx, 0.0, 0.0, 0.0, 1.0 );
  CGContextTranslateCTM( cx, cfmt.leftmargin, -cfmt.topmargin );
}

void cgEndPage( GraphicsContext cx )
{
  CGContextTranslateCTM( cx, -cfmt.leftmargin, 0 );
//  printf("end\n");
  CGContextRestoreGState( cx );
  CGContextEndPage( cx );
  // Doing this outside the page makes the translate for only for non-PDF contexts, which is what we want.
  CGContextTranslateCTM( cx, 0, -cfmt.pageheight );
}

// This is a function whch uses Cocoa to draw text. This works properly with Unicode, unlike CGContextDrawText
// It's not in this file because it uses Objective-C and abc2ps is K&R C.
extern void drawUIText( GraphicsContext context, float x, float y, const char *text, int alignment, bool serif, int size, bool utf8charset );

static void cgAddArc( GraphicsContext cx, float x, float y, float radius, float startAngle, float endAngle )
{
  CGContextAddArc( cx, x, y, radius, startAngle, endAngle, 1 );
}

static int sLastFont = -1;
static int sLastSize = -1;

static void cgSetFont( GraphicsContext cx, int font, int size )
{
  if ( font != sLastFont || size != sLastSize ) {
    sLastFont = font;
    sLastSize = size;
    CGContextSelectFont( cx, font == kSerifFont ? "Times-Roman" :  "Helvetica", size, kCGEncodingMacRoman );
  }
}

static void cgDrawText( GraphicsContext cx, float x, float y, const char *text, int alignment )
{
  drawUIText( cx, x, y, text, alignment, sLastFont == kSerifFont, sLastSize, utf8charset );
}

static void cgMoveToPoint( GraphicsContext cx, float x, float y )
{
  CGContextMoveToPoint( cx, x, y );
}

static void cgAddLineToPoint( GraphicsContext cx, float x, float y )
{
  CGContextAddLineToPoint( cx, x, y );
}

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

static void cgStrokePath( GraphicsContext cx )
{
  CGContextStrokePath( cx );
}

static void cgFillPath( GraphicsContext cx )
{
  CGContextFillPath( cx );
}

static void cgBeginPath( GraphicsContext cx )
{
  CGContextBeginPath( cx );
}

static void cgClosePath( GraphicsContext cx )
{
  CGContextClosePath( cx );
}

static void cgTranslate( GraphicsContext cx, float x, float y )
{
  CGContextTranslateCTM( cx, x, y );
}

static void cgScale( GraphicsContext cx, float x, float y )
{
  CGContextScaleCTM( cx, x, y );
}

static void cgSave( GraphicsContext cx )
{
  CGContextSaveGState( cx );
}

static void cgRestore( GraphicsContext cx )
{
  CGContextRestoreGState( cx );
}

static void cgSetLineWidth( GraphicsContext cx, float x )
{
  CGContextSetLineWidth( cx, x );
}

GraphicsMethods gGraphicsMethods = {
  cgBeginPage,
  cgEndPage,
  cgMoveToPoint,
  cgAddLineToPoint,
  cgAddCurveToPoint,
  cgAddArc,
  cgBeginPath,
  cgClosePath,
  cgSetLineWidth,
  cgFillPath,
  cgStrokePath,
  cgSave,
  cgRestore,
  cgTranslate,
  cgScale,
  cgSetFont,
  cgDrawText,
};

#endif
