/*
 * GIMP Speedline Plug-in -- This is a plug-in for The GIMP 2
 * Copyright (C) 2013 OHTSUKA, Yoshio <ohtsuka.yoshio@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */
 
#include <cairo.h>
#include "speedline_line.h"

LineObj *
line_new (PluginVals *vals)
{
	gdouble resx, resy;
	LineObj *line = g_new0 (LineObj, 1);

	gimp_image_get_resolution (vals->image_ID, &resx, &resy);

	line->antialias = vals->line_antialias;

	line->color = vals->line_color;

	line->pitch      = gimp_units_to_pixels (vals->line_pitch,      vals->area_unit, resx);
	line->width      = gimp_units_to_pixels (vals->line_width,      vals->area_unit, resx);
	line->in_margin  = gimp_units_to_pixels (vals->line_in_margin,  vals->area_unit, resx);
	line->in_length  = gimp_units_to_pixels (vals->line_in_length,  vals->area_unit, resx);
	line->out_margin = gimp_units_to_pixels (vals->line_out_margin, vals->area_unit, resx);
	line->out_length = gimp_units_to_pixels (vals->line_out_length, vals->area_unit, resx);

	line->pitch_rand      = vals->line_pitch_rand;
	line->width_rand      = vals->line_width_rand;
	line->in_margin_rand  = vals->line_in_margin_rand;
	line->in_length_rand  = vals->line_in_length_rand;
	line->out_margin_rand = vals->line_out_margin_rand;
	line->out_length_rand = vals->line_out_length_rand;

	line->randobj_pitch      = g_rand_new_with_seed (vals->line_pitch_rand_seed);
	line->randobj_width      = g_rand_new_with_seed (vals->line_width_rand_seed);
	line->randobj_in_margin  = g_rand_new_with_seed (vals->line_in_margin_rand_seed);
	line->randobj_in_length  = g_rand_new_with_seed (vals->line_in_length_rand_seed);
	line->randobj_out_margin = g_rand_new_with_seed (vals->line_out_margin_rand_seed);
	line->randobj_out_length = g_rand_new_with_seed (vals->line_out_length_rand_seed);

	return line;
}

//draw a line at normalized coordinates; IN(0,0) -> OUT(0,length)
void
line_draw (LineObj	*line, 
		   cairo_t	*cr, 
		   double	length)
{
	double w = line->width - (line->width * g_rand_double (line->randobj_width) * line->width_rand / 100.0);
	double in_margin  = line->in_margin  - (line->in_margin  * g_rand_double (line->randobj_in_margin)  * line->in_margin_rand  / 100.0);
	double in_length  = line->in_length  - (line->in_length  * g_rand_double (line->randobj_in_length)  * line->in_length_rand  / 100.0);
	double out_margin = line->out_margin - (line->out_margin * g_rand_double (line->randobj_out_margin) * line->out_margin_rand / 100.0);
	double out_length = line->out_length - (line->out_length * g_rand_double (line->randobj_out_length) * line->out_length_rand / 100.0);

	cairo_save (cr);
	{
		cairo_set_source_rgba (cr,
			line->color.r,
			line->color.g,
			line->color.b,
			line->color.a);

		switch (line->antialias) {
			case ANTIALIAS_TYPE_NONE:
				cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
				break;
			case ANTIALIAS_TYPE_FAST:
				cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT | CAIRO_ANTIALIAS_GRAY);
				break;
			case ANTIALIAS_TYPE_BEST:
				cairo_set_antialias (cr, CAIRO_ANTIALIAS_SUBPIXEL);
				break;
		}

		cairo_move_to (cr, 0       , in_margin);
		cairo_line_to (cr, w / -2.0, in_margin + in_length);
		cairo_line_to (cr, w / -2.0, length - out_margin - out_length);
		cairo_line_to (cr, 0       , length - out_margin);
		cairo_line_to (cr, w /  2.0, length - out_margin - out_length);
		cairo_line_to (cr, w /  2.0, in_margin + in_length);
		cairo_close_path (cr);
		cairo_fill (cr);
	}
	cairo_restore (cr);
}

void
line_destroy (LineObj *line)
{
	if (line->randobj_pitch)      g_free (line->randobj_pitch);
	if (line->randobj_width)      g_free (line->randobj_width);
	if (line->randobj_in_margin)  g_free (line->randobj_in_margin);
	if (line->randobj_in_length)  g_free (line->randobj_in_length);
	if (line->randobj_out_margin) g_free (line->randobj_out_margin);
	if (line->randobj_out_length) g_free (line->randobj_out_length);

	line->randobj_pitch      = NULL;
	line->randobj_width      = NULL;
	line->randobj_in_margin  = NULL;
	line->randobj_in_length  = NULL;
	line->randobj_out_margin = NULL;
	line->randobj_out_length = NULL;

	if (line) g_free (line);
}

/* vim: set fdm=marker ts=4 sw=4:*/