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

require "ClibPDF"

NPAGES = 200

def show_textStuff(pdf, yloc, page, chapter, section, subsection)
  ybump = 0.3

  pdf.beginText(0)
  pdf.setFont("Times-Roman", "MacRomanEncoding", 24.0)
  strbuf,  = sprintf("Page: %d", page)
  pdf.text(1.0, yloc, 0.0, strbuf)
  yloc -= ybump
  strbuf = sprintf("Chapter: %d", chapter)
  pdf.text(1.0, yloc, 0.0, strbuf)
  yloc -= ybump
  strbuf = sprintf("Section: %d.%d", chapter, section)
  pdf.text(1.0, yloc, 0.0, strbuf)
  yloc -= ybump
  strbuf = sprintf("Subsection: %d.%d.%d", chapter, section, subsection)
  pdf.text(1.0, yloc, 0.0, strbuf)
  yloc -= ybump

  pdf.endText
end

lastchap = 0
lastsect = 0
lastsubsect = 0
same_sub = 0
p1=0.0; p2=0.0; p3=0.0; p4=0.0
vP = [ClibPDF::PM_OUTLINES, 0, 0, 0, 0, 0, 0, 0]

pdf = ClibPDF::PDF.open(0, NPAGES + 50, -1, -1, -1, 3000)
pdf.enableCompression(ClibPDF::YES)
pdf.setOutputFilename("outline.pdf")
pdf.init

pdf.setViewerPreferences(vP)

act_dict = "/Type /Action /S /URI /URI (http://www.fastio.com/example/arctest.pdf)"
lastChapOL = pdf.addOutlineAction(NIL, ClibPDF::OL_SAME, ClibPDF::OL_OPEN, act_dict, "FastIO Arc Example (URI)")

act_dict = "/Type /Action /S /GoToR /D [0 /XYZ null null null] /F (./domain.pdf) /NewWindow true"
lastChapOL = pdf.addOutlineAction(lastChapOL, ClibPDF::OL_SAME, ClibPDF::OL_OPEN, act_dict, "Domain Example (local)")

act_dict = "/Type /Action /S /URI /URI (http://partners.adobe.com/supportservice/devrelations/PDFS/TN/PDFSPEC.PDF)"
lastChapOL = pdf.addOutlineAction(lastChapOL, ClibPDF::OL_SAME, ClibPDF::OL_OPEN, act_dict, "PDF Reference v1.3 at Adobe")


for page in 1..NPAGES
  STDERR.printf "Page %d  \r", page
#  IO.flush
  pdf.pageInit(page, ClibPDF::PORTRAIT, ClibPDF::LETTER, ClibPDF::LETTER)
  chapter = (page-1) / 20 + 1
  section = ((page-1) % 20) / 4 + 1
  subsection = ((page-1) % 4) * 2 + 1

  show_textStuff(pdf, 9.5, page, chapter, section, subsection)

  p1 = 0.0
  p2 = 11.0*72.0
  p3 = 0.0

  if chapter != lastchap
	strbuf = sprintf "Chapter %d", chapter
	lastChapOL = pdf.addOutlineEntry(lastChapOL, ClibPDF::OL_SAME, ClibPDF::OL_OPEN, page, strbuf,
									  ClibPDF::DEST_XYZ, p1, p2, p3, p4)
	lastchap = chapter
	lastSectOL = lastChapOL
  end
  if section != lastsect
	sprintf(strbuf, "Section %d.%d", chapter, section)
	if section == 1
	  same_sub = ClibPDF::OL_SUBENT
	else
	  same_sub = ClibPDF::OL_SAME
	end
	  lastSectOL = pdf.addOutlineEntry(lastSectOL, same_sub, ClibPDF::OL_CLOSED, page, strbuf,
										ClibPDF::DEST_XYZ, p1, p2, p3, p4)
	  lastsect = section
	  lastSubsectOL = lastSectOL
  end
  if subsection != lastsubsect
	sprintf(strbuf, "Subsection %d.%d.%d", chapter, section, subsection)
	if subsection == 1
	  same_sub = ClibPDF::OL_SUBENT
	else
	  same_sub = ClibPDF::OL_SAME
	end
	lastSubsectOL = pdf.addOutlineEntry(lastSubsectOL, same_sub, ClibPDF::OL_CLOSED, page, strbuf,
										 ClibPDF::DEST_XYZ, p1, p2, p3, p4)
	lastsubsect = subsection
  end

  subsection += 1
  show_textStuff(pdf, 4.5, page, chapter, section, subsection)

  p2 = 5.0 * 72.0
  if subsection != lastsubsect
	sprintf(strbuf, "Subsection %d.%d.%d", chapter, section, subsection)
	if subsection == 1
	  same_sub = ClibPDF::OL_SUBENT
	else
	  same_sub = ClibPDF::OL_SAME
	end
	lastSubsectOL = pdf.addOutlineEntry(lastSubsectOL, same_sub, ClibPDF::OL_OPEN, page, strbuf,
										 ClibPDF::DEST_XYZ, p1, p2, p3, p4)
	lastsubsect = subsection
  end

  pdf.finalizePage(page)

end

STDERR.print "\nNow writing PDF...\n"
pdf.finalizeAll()

#pdf.launchPreview()
pdf.close()
