#!/usr/local/bin/ruby
#
#
# ./DomainDemo.rb

require "ClibPDF"

$ymax = 100.0
$ymaxL = 80.0
$ymin = 0.01
$xmax = 3.0
$xmin = 0.03

A = 70.5
X0 = 1.2
Sigma = 0.4

$x = []
$y = []

def generateGaussianData
  xp = 0.03
  xbump = 0.03
  for i in 0..100-1
	$x[i] = xp
	$y[i] = A * Math.exp( - ((xp - X0) / Sigma) * ((xp - X0) / Sigma))
	xp += xbump
  end
end

def plot_Curve(pdf)
  pdf.comments("\n%% plot the curve (linear-linear).\n")
  pdf.setlinewidth(2.0)
  pdf.setrgbcolorStroke(0.0, 0.0, 1.0)
  for i in 0..100-1
	if i==0
	  pdf.moveto($x[i], $y[i])
	else
	  pdf.lineto($x[i], $y[i])
	end
  end
  pdf.stroke
end

def do_LinearLinear(pdf)
	myDomain = ClibPDF::PlotDomain.new( 1.5*ClibPDF::INCH, 1.5*ClibPDF::INCH,
										4.5*ClibPDF::INCH, 3.0*ClibPDF::INCH,
										0.0, $xmax, 0.0, $ymaxL, ClibPDF::LINEAR, ClibPDF::LINEAR, 0)
  oldDomain = pdf.setPlotDomain(myDomain)
  myDomain.fillDomainWithRGBcolor(1.0, 0.9, 0.9)
  myDomain.drawMeshForDomain()

  pdf.setgray(0.0)

  xAxis = ClibPDF::Axis.new( 0.0, 4.5*ClibPDF::INCH, ClibPDF::LINEAR, 0.0, $xmax)
  xAxis.attachAxisToDomain(myDomain, 0.0, -0.2*ClibPDF::INCH)
  xAxis.setAxisNumberFormat("%g", "Helvetica", 16.0)
  xAxis.setAxisLabel("X axis (linear)", "Times-Roman", "MacRomanEncoding", 20.0)
  xAxis.drawAxis()
  xAxis.freeAxis()

  yAxis = ClibPDF::Axis.new(90.0, 3.0*ClibPDF::INCH, ClibPDF::LINEAR, 0.0, $ymaxL)
  yAxis.attachAxisToDomain(myDomain, -0.2*ClibPDF::INCH, 0.0)
  yAxis.setAxisNumberFormat("%2.f", "Helvetica", 16.0)
  yAxis.setAxisLabel("Y axis (linear)", "Times-Roman", "MacRomanEncoding", 20.0)
  yAxis.drawAxis()
  yAxis.freeAxis()

  plot_Curve(pdf)

  pdf.setPlotDomain(oldDomain)
  myDomain.freePlotDomain()
end

def do_LogLog(pdf)
  myDomain = ClibPDF::PlotDomain.new( 1.5*ClibPDF::INCH, 6.0*ClibPDF::INCH,
									  4.5*ClibPDF::INCH, 3.0*ClibPDF::INCH,
									  $xmin, $xmax, $ymin, $ymax,
									  ClibPDF::LOGARITHMIC, ClibPDF::LOGARITHMIC, 0)
  oldDomain = pdf.setPlotDomain(myDomain)
  myDomain.fillDomainWithRGBcolor(1.0, 0.9, 0.9)
  myDomain.drawMeshForDomain()

  pdf.setgray(0.0)
  xAxis = ClibPDF::Axis.new( 0.0, 4.5*ClibPDF::INCH, ClibPDF::LOGARITHMIC, $xmin, $xmax)
  xAxis.setLogAxisNumberSelector(ClibPDF::LOGAXSEL_13)
  xAxis.attachAxisToDomain(myDomain, 0.0, -0.2*ClibPDF::INCH)
  xAxis.setAxisNumberFormat("%g", "Helvetica", 16.0)
  xAxis.setAxisLabel("X axis (logarithmic)", "Times-Roman", "MacRomanEncoding", 20.0)
  xAxis.drawAxis()
  xAxis.freeAxis()

  yAxis = ClibPDF::Axis.new(90.0, 3.0*ClibPDF::INCH, ClibPDF::LOGARITHMIC, $ymin, $ymax)
  yAxis.attachAxisToDomain(myDomain, -0.2*ClibPDF::INCH, 0.0)
  yAxis.setAxisNumberFormat("%g", "Helvetica", 16.0)
  yAxis.setAxisLabel("Y axis (logarithmic)", "Times-Roman", "MacRomanEncoding", 20.0)
  yAxis.drawAxis()
  yAxis.freeAxis()

  pdf.gsave
  myDomain.clipDomain()
  plot_Curve(pdf)
  pdf.grestore

  pdf.setPlotDomain(oldDomain)
  myDomain.freePlotDomain()
end

def do_myTitle(pdf)
	pdf.setrgbcolorFill(0.0, 0.0, 1.0)
	pdf.beginText(0)
	pdf.setFont("Times-Italic", "MacRomanEncoding", 18.0)
	pdf.text(1.5, 9.7, 0.0, "Y = A * exp( - ((X-Xo)/S)^2)")
	pdf.text(1.7, 9.4, 0.0, "where  A=70,  Xo=1.2,  S=0.4")
	pdf.endText
end

pdf = ClibPDF::PDF.open(0)
pdf.enableCompression(ClibPDF::YES)
pdf.init
pdf.pageInit(1, ClibPDF::PORTRAIT, ClibPDF::LETTER, ClibPDF::LETTER)

pdf.setCreator("ClibPDF Domain Demo: DomainDemo.rb")

generateGaussianData
do_LinearLinear(pdf)
do_LogLog(pdf)
do_myTitle(pdf)

pdf.finalizeAll

bufPDF, length = pdf.getBufferForPDF
printf "PDF buffer length= %d\n", length

pdf.savePDFmemoryStreamToFile("DomainDemo.pdf")

#pdf.launchPreview
pdf.close()
