/*
 *  OmCoreGraphicsUtils.c
 *  try_empty_window
 *
 *  Created by david van brink on 7/23/10.
 *  Copyright 2010 omino.com. All rights reserved.
 *
 */

#include "OmCoreGraphicsUtils.h"
#include <strings.h>
#include <stdarg.h>
#include <stdio.h>

void GSetFillColor(CGContextRef g,int rgb)
{
    float co[4];
    co[0] = ((rgb >> 16) & 0x00ff) / 255.0;
    co[1] = ((rgb >> 8) & 0x00ff) / 255.0;
    co[2] = ((rgb >> 0) & 0x00ff) / 255.0;
    co[3] = 1.0;
    CGContextSetFillColor(g, co);
}

void GSetStrokeColor(CGContextRef g,int rgb)
{
    float co[4];
    co[0] = ((rgb >> 16) & 0x00ff) / 255.0;
    co[1] = ((rgb >> 8) & 0x00ff) / 255.0;
    co[2] = ((rgb >> 0) & 0x00ff) / 255.0;
    co[3] = 1.0;
    CGContextSetStrokeColor(g, co);
}

static char *fontNames[] = 
{
    "Times New Roman",
    "Helvetica",
    "Courier",
    "Helvetica-Bold"
};

void GSetFont(CGContextRef g,int font,int size)
{
    CGContextSelectFont (g,fontNames[font],size,kCGEncodingMacRoman);
}


CGPoint GDrawString(CGContextRef g,int x,int y,char *s)
{
    CGContextShowTextAtPoint(g, x,y,s,strlen(s));
    CGPoint result = CGContextGetTextPosition(g);
    return result;
}


static char s[1024];

/* fragile temp static result here! hey. */
char *GSPrintf(char *fmt,...)
{
    va_list v;
    
    va_start(v,fmt);
    vsprintf(s,fmt,v);
    return s;
}

CGPoint GPrintf(CGContextRef g,int x,int y,char *fmt,...)
{
    char s[500];
    va_list v;
    
    va_start(v,fmt);
    vsprintf(s,fmt,v);
    return GDrawString(g,x,y,s);
}


void GDrawPip(CGContextRef g,double x,double y,double r,bool oval)
{
    CGRect re = CGRectMake(x - r,y - r,r * 2,r * 2);
    if(oval)
        CGContextAddEllipseInRect(g,re);
    else
        CGContextAddRect(g, re);
    CGContextFillPath(g);
}

void GFillRectR(CGContextRef g,CGRect r,int rgb)
{
    if(rgb != -1)
        GSetFillColor(g, rgb);
    CGContextAddRect(g,r);
    CGContextFillPath(g);
}

void GFrameRectR(CGContextRef g,CGRect r,double lineWidth,int rgb)
{
    CGRect r2 = r;
    r2.origin.x += lineWidth / 2.0;
    r2.origin.y += lineWidth / 2.0;
    r2.size.width -= lineWidth;
    r2.size.height -= lineWidth;
    CGContextSetLineWidth(g, lineWidth);
    if(rgb != -1)
        GSetStrokeColor(g, rgb);
    CGContextAddRect(g,r2);
    CGContextStrokePath(g);
}

void GFillWedge(CGContextRef g,float wideSide,float top,float pointSide,float bottom,int rgb)
{
    CGPoint p1 = CGPointMake(pointSide,(top + bottom) / 2.0);
    CGPoint p2 = CGPointMake(wideSide,top);
    CGPoint p3 = CGPointMake(wideSide,bottom);
    
    CGContextBeginPath (g);
    CGContextMoveToPoint(g, p1.x, p1.y);
    CGContextAddLineToPoint(g, p2.x, p2.y);
    CGContextAddLineToPoint(g, p3.x, p3.y);
    CGContextAddLineToPoint(g, p1.x, p1.y);
    if(rgb >= 0)
        GSetFillColor(g,rgb);
    CGContextFillPath(g);
}


void GInsetRect(CGRect *r,int insetX,int insetY)
{
    r->origin.x += insetX;
    r->size.width -= insetX * 2;
    r->origin.y += insetY;
    r->size.height -= insetY * 2;
}


/* text needs this :( */
void G(CGContextRef g)
{
    CGAffineTransform tt = CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(g, tt);
}

UIColor *GColor(int rgb)
{
    float r = ((rgb >> 16) & 0x000000ff) / 255.0;
    float g = ((rgb >> 8)  & 0x000000ff) / 255.0;
    float b = ((rgb >> 0)  & 0x000000ff) / 255.0;
    UIColor *co = [[UIColor alloc] initWithRed:r green:g blue:b alpha:1];
    [co autorelease];
    return co;
}

// end of message
