## Copyright (C) 2015 ederag <edera@gmx.fr>
## 
## 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 3 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, see <http://www.gnu.org/licenses/>.


page_output_immediately (true)
page_screen_output(false)

# change this line to point where your cprintf lies
# cprintf can be downloaded from 
# https://sourceforge.net/p/cprintf-m/code
addpath("~/share/prog/octave/cprintf")

function cmp(observed, expected, tolerance, template)
	# if explicitly given, template will be used as 
	# printf(template, "observed", observed)
	# or
	# printf(template, "expected", expected)
	if nargin < 3
		tolerance = 0;
	endif
	if nargin < 4
		template = "  %s: %f\n";
	endif
	
	tolerance_error = max(tolerance);
	tolerance_warning = min(tolerance);
	
	if any(abs(expected - observed) > tolerance_error)
		color_str = "red";
	elseif any(abs(expected - observed) > tolerance_warning)
		color_str = "yellow";
	else
		color_str = "green";
	endif
	ec_printf = @(template, varargin) cprintf(color_str, template, varargin{:});
	ec_printf(template, "observed", observed)
	ec_printf(template, "expected", expected)
endfunction

function waitfor_printout(filename, mtime)
	while (stat(filename).mtime == mtime)
		sleep(0.1) 
		stat(filename).mtime - mtime
	endwhile
endfunction


basename = "try";
filename = [basename ".png"];

#debug_str = "";
close all
graphics_toolkit("gnuplot")
# graphics_toolkit("qt")
# graphics_toolkit("fltk")
printf("graphics toolkit: %s\n", graphics_toolkit())
plot(1:10)
xlabel("x")
ylabel("y")
title("title")
paperposition = get(gcf, "paperposition");

test_all = true;
test_png = !true;
if test_png || test_all
	printf("PNG simplest print...\n")
	mtime = stat(filename).mtime; sleep(1)
	#print(filename, "-debug")
	print(filename)
	waitfor_printout(filename, mtime)
	info = imfinfo(filename);
	tolerance = [0.5, 5];
	resolution = 150;
	# by default, resolution is 150 ppi
	cmp(info.XResolution * 2.54, resolution, tolerance, "  %s: %.1f\n");
	cmp(info.YResolution * 2.54, resolution, tolerance, "  %s: %.1f\n");
	cmp(info.Width, paperposition(3) * resolution, tolerance, "  %s: %.1f\n")
	cmp(info.Height, paperposition(4) * resolution, tolerance, "  %s: %.1f\n")

	printf("PNG with resolution...\n")
	resolution = 300;
	mtime = stat(filename).mtime; sleep(1)
	print(filename, sprintf("-r%d", resolution))
	waitfor_printout(filename, mtime)
	info = imfinfo(filename);
	tolerance = [0.5, 10];
	# by default, resolution is 150 ppi
	cmp(info.XResolution * 2.54, resolution, tolerance, "  %s: %.1f\n")
	cmp(info.YResolution * 2.54, resolution, tolerance, "  %s: %.1f\n")
	cmp(info.Width, paperposition(3) * resolution, tolerance, "  %s: %.1f\n")
	cmp(info.Height, paperposition(4) * resolution, tolerance, "  %s: %.1f\n")

	printf("PNG with -r0...\n")
	mtime = stat(filename).mtime; sleep(1)
	print(filename, "-r0")
	waitfor_printout(filename, mtime)
	info = imfinfo(filename);
	# -r0 => resolution should be screen ppi
	resolution = get(0, "screenpixelsperinch");
	tolerance = [0.5, 5];
	cmp(info.XResolution * 2.54, resolution, tolerance, "  %s: %.1f\n");
	cmp(info.YResolution * 2.54, resolution, tolerance, "  %s: %.1f\n");
	cmp(info.Width, round(paperposition(3) * resolution), tolerance, "  %s: %.1f\n")
	cmp(info.Height, round(paperposition(4) * resolution), tolerance, "  %s: %.1f\n")


	printf("PNG with size...\n")
	sz = [1000, 800];
	mtime = stat(filename).mtime; sleep(1)
	print(filename, sprintf("-S%g,%g", sz))
	waitfor_printout(filename, mtime)
	info = imfinfo(filename);
	tolerance = 0.01;
	cmp(info.Width, sz(1), tolerance, "  %s: %.1f\n")
	cmp(info.Height, sz(2), tolerance, "  %s: %.1f\n")

endif  ## test png


## PDF printout
filename = [basename ".pdf"];

test_pdf = !true;
if test_pdf || test_all
	printf("PDF simplest print... \n")
	mtime = stat(filename).mtime; sleep(1)
	print(filename)
	waitfor_printout(filename, mtime)
	info = imfinfo(filename);
	expected = get(gcf, "papersize");
	observed = [info.Width / 72, info.Height / 72];
	tolerance = 0.1;
	cmp(observed, expected, tolerance, "  %s: %f, %f\n")
endif

test_latex = true;
if test_latex || test_all
	printf("PDF pdflatexstandalone... \n")
	mtime = stat(filename).mtime; sleep(1)
	texfile = [basename ".tex"];
	print(texfile, "-dpdflatexstandalone")
	system(["pdflatex -interaction=nonstopmode " texfile])
	waitfor_printout(filename, mtime)
	info = imfinfo(filename);
	expected = get(gcf, "paperposition")(3:4);
	observed = [info.Width / 72, info.Height / 72];
	tolerance = 0.1;
	cmp(observed, expected, tolerance, "  %s: %f, %f\n")
endif
