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

require "ClibPDF"

STRWIDTHMAX	= 6.7

textdata = "We the People of the United States, in Order to form a more perfect Union," +
  " establish Justice, insure domestic Tranquility, provide for the common" +
  " defence, promote the general Welfare, and secure the Blessings of Liberty to" +
  " ourselves and our Posterity, do ordain and establish this Constitution for the" +
  " United States of America."

symboldata = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn"

ybump = 0.22
y = 9.75

pdf = ClibPDF::PDF.open(0)
pdf.enableCompression(ClibPDF::YES)
pdf.init
pdf.setTitle("Font Sample")
pdf.setSubject("Font listing for 41 fonts in ClibPDF v2.00")
pdf.pageInit(1, ClibPDF::PORTRAIT, ClibPDF::LETTER, ClibPDF::LETTER)

pdf.setgray(0.2)
pdf.beginText(0)
pdf.setFont("CPDF-SmallCap", "MacRomanEncoding", 24.0)
pdf.text(1.0, 10.4, 0.0, "List of Fonts Supported by ClibPDF v2.00")
pdf.endText

pdf.setlinewidth(10.0)
pdf.setrgbcolor(0.4, 0.4, 1.0)
pdf.moveto(1.0, 10.25)
pdf.lineto(7.8, 10.25)
pdf.moveto(1.0, 0.5)
pdf.lineto(7.8, 0.5)
pdf.stroke

pdf.setgray(0.0)
for i in 0..40
  pdf.beginText(0)
  if i != 12 && i != 13
#	STDERR.print ClibPDF::FONTNAMELIST[i], "\n"
	pdf.setFont(ClibPDF::FONTNAMELIST[i], "MacRomanEncoding", 12.0)
	strbuf = sprintf("%s: %s", ClibPDF::FONTNAMELIST[i], textdata)
	j = 0
	j_p = 0
	while j = strbuf.index(" ", j)
#	  STDERR.print "j: ", strbuf.index(" ", j), "\n"
	  if pdf.stringWidth(strbuf[0..j-1]) > STRWIDTHMAX * ClibPDF::INCH
		pdf.text(1.0, y, 0.0, strbuf[0..j_p])
		break
	  end
	  j_p = j - 1
	  j += 1
	end
	pdf.endText
  else
	pdf.setFont("Times-Roman", "MacRomanEncoding", 12.0)
	strbuf = sprintf("%s:", ClibPDF::FONTNAMELIST[i])
	pdf.text(1.0, y, 0.0, strbuf)
	pdf.endText
	pdf.beginText(0)
	pdf.setFont(ClibPDF::FONTNAMELIST[i], "MacRomanEncoding", 12.0)
	pdf.text(2.05, y, 0.0, symboldata)
	pdf.endText
  end
  y -= ybump
end

y -= ybump * 0.5
pdf.beginText(0)
pdf.setFont("Times-Italic", "MacRomanEncoding", 8.0)
pdf.text(1.0, y, 0.0, "Note: CPDF-Monospace and CPDF-SmallCap will not be rendered correctly by most non-Adobe PDF viewers (e.g., Ghostscript, xpdf, PDFView.app).")
pdf.endText

pdf.beginText(0)
pdf.setFont("CPDF-Monospace", "MacRomanEncoding", 10.0)
pdf.setHorizontalScaling(125.0)
strbuf = sprintf("[Generated by: %s]", ClibPDF.version)
pdf.text(1.0, 0.3, 0.0, strbuf)
pdf.setHorizontalScaling(100.0)
pdf.endText

pdf.finalizeAll
pdf.savePDFmemoryStreamToFile("fontlist.pdf")

#pdf.launchPreview
pdf.close()
