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

require "ClibPDF"

WIDTH = 6.0
HEIGHT = 4.0
XBASE = 1.5
YBASE = 4.0
NCYCLES = 10.0
WEBSITE_STR	= "http://www.fastio.com/"
EMAIL_STR = "clibpdf@fastio.com"
PAGE2TEXT = "page2.txt"
sinewave = []

def random01
  v = rand(2**31)
  v /= (2**31).to_f
#  STDERR.print v, "\n"
  return v
end

def do_page_2(pdf)
  pdf.pageInit(2, ClibPDF::PORTRAIT, ClibPDF::LETTER, ClibPDF::LETTER)
  pdf.beginText(0)
  pdf.setFont("Helvetica", "MacRomanEncoding", 7.0)
  pdf.setHorizontalScaling(130.0)
  pdf.setTextLeading(11.0)
  pdf.setTextPosition(1.0, 10.0)
  pdf.textShow("ClibPDF Library Reference Manual")

  fp = open(PAGE2TEXT, "r")
  while linebuf = fp.gets
	pdf.textCRLFshow(linebuf)
  end
  fp.close
  pdf.endText
end

srand(23456)
pdf = ClibPDF::PDF.open(0)
pdf.enableCompression(ClibPDF::YES)
pdf.init

pdf.setCreator("FastIO Systems: cover.rb")
pdf.setTitle("ClibPDF Reference Manual")
pdf.setSubject("ANSI C Library for Direct PDF Generation")
pdf.setKeywords("ClibPDF, ANSI C Library, Acrobat, PDF, Dynamic Web, Graph, Plot")

pdf.pageInit(1, ClibPDF::PORTRAIT, ClibPDF::LETTER, ClibPDF::LETTER)

pdf.setgrayStroke(0.0)
pdf.setlinewidth(0.2)

pdf.gsave()
pdf.newpath
pdf.rect(XBASE, YBASE, WIDTH, HEIGHT)
pdf.clip
pdf.newpath

pdf.comments("\n%% random pie/pacmans.\n")

for i in 0..160-1
  radius = 0.2 + random01
  eangle = 360.0 * random01
  sangle = 360.0 * random01
  xr = WIDTH * random01
  yr = HEIGHT * random01
  r = 0.7 * random01 + 0.3
  g = 0.7 * random01 + 0.3
  b = 0.7 * random01 + 0.3
  pdf.setrgbcolorFill(r, g, b)
  xorig = XBASE + xr
  yorig = YBASE + yr
  pdf.moveto(xorig, yorig)
  pdf.arc(xorig, yorig, radius, sangle, eangle, 0)
  pdf.closepath
  pdf.fillAndStroke
end

pdf.grestore

pdf.rect(XBASE, YBASE, WIDTH, HEIGHT)
pdf.setlinewidth(0.8)
pdf.stroke

pdf.setrgbcolorFill(0.0, 0.0, 1.0)
pdf.beginText(0)
pdf.setFont("Times-BoldItalic", "MacRomanEncoding", 38.0)
pdf.textAligned(4.5, 9.0, 0.0, ClibPDF::TEXTPOS_LM, "ClibPDF Reference Manual")
pdf.endText

pdf.setrgbcolorFill(0.4, 0.3, 0.2)
pdf.beginText(0)
pdf.setFont("Times-Roman", "MacRomanEncoding", 20.0)
pdf.textAligned(4.5, 8.4, 0.0, ClibPDF::TEXTPOS_LM, "ANSI C Library for Direct PDF Generation")
pdf.endText

pdf.setgray(0.0)
pdf.beginText(0)
pdf.setFont("Helvetica-Bold", "MacRomanEncoding", 18.0)
pdf.setHorizontalScaling(140.0)
pdf.setCharacterSpacing(1.0)
pdf.textAligned(4.5, 2.8, 0.0, ClibPDF::TEXTPOS_LM, "FastIO Systems")
pdf.endText

pdf.setgray(0.0)
pdf.beginText(0)
pdf.setFont("Helvetica", "MacRomanEncoding", 14.0)
pdf.setHorizontalScaling(120.0)
pdf.setCharacterSpacing(0.5)
pdf.textAligned(4.5, 2.4, 0.0, ClibPDF::TEXTPOS_LM, WEBSITE_STR)
pdf.textAligned(4.5, 2.1, 0.0, ClibPDF::TEXTPOS_LM, EMAIL_STR)
strwidth1 = pdf.stringWidth(WEBSITE_STR)
#STDERR.print "strwidth1: ", strwidth1, "\n"
strwidth2 = pdf.stringWidth(EMAIL_STR)
pdf.endText

pdf.rawSetActionURL(4.5 * ClibPDF::INCH - strwidth1/2.0, 2.4 * ClibPDF::INCH - 3.0,
						4.5 * ClibPDF::INCH + strwidth1/2.0, 2.4 * ClibPDF::INCH + 12.0,
						WEBSITE_STR, NIL)
pdf.rawSetActionURL(4.5 * ClibPDF::INCH - strwidth2/2.0, 2.1 * ClibPDF::INCH - 3.0,
						4.5 * ClibPDF::INCH + strwidth2/2.0, 2.1 * ClibPDF::INCH + 12.0,
						"mailto:clibpdf@fastio.com", NIL)

for i in 0..1024-1
  angle = i.to_f / 512.0 * Math::PI * 2.0 * NCYCLES
  if i < 512
	sinewave[i] = 127.0 + 60.0 * Math.sin(angle)
  else
	sinewave[i] = 127.0 - 60.0 * Math.sin(angle)
  end
end

pdf.placeInLineImage(sinewave, XBASE, 1.0, 0.0, WIDTH * 72.0, 0.2 * 72.0,
					 512, 2, 8, ClibPDF::CS_GRAY, 1)
pdf.rect(XBASE, 1.0, WIDTH, 0.2)
pdf.setgray(0.0)
pdf.setlinewidth(0.2)
pdf.stroke

do_page_2(pdf)

pdf.finalizeAll
pdf.savePDFmemoryStreamToFile("cover.pdf")

pdf.close
#pdf.launchPreview
