package cairo;
import java.io.IOException;
import org.freedesktop.cairo.Context;
import org.freedesktop.cairo.PdfSurface;
import org.freedesktop.cairo.Surface;
import org.gnome.gdk.Pixbuf;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.PaperSize;
import org.gnome.gtk.Unit;
import org.gnome.pango.FontDescription;
import org.gnome.pango.Layout;
import org.gnome.pango.LayoutLine;
import org.gnome.pango.Rectangle;
import static java.lang.Math.PI;
import static textview.LoremIpsum.text;
/**
* Some poor kid sitting through latin class doodling on his lined paper.
*
* This is an example of using Pango to render text along side Cairo drawing
* primitives, and using Cairo's PdfSurface back end to produce a PDF that can
* subsequently be previewd or printed to paper.
*
* The blue lines actually represent the baseline of the font (the calls to
* Context's showLayout() draw LayoutLines with their baseline at the current
* Cairo point); if you go to a high level of zoom you'll see the overshoot
* that some glyphs are designed with. The red line is the left margin, and
* unlike the baseline (which is just an arbitrary point midway into the
* font's extents), nothing bleeds left past the margin line.
*
* @author Andrew Cowie
* @author Nathan Strum
*/
public class ExampleLinedPaper
{
public static void main(String[] args) throws IOException {
final Surface surface;
final Context cr;
final Layout layout;
final FontDescription desc;
final String[] paras;
final LayoutLine first;
final PaperSize paper;
final double pageWidth, pageHeight;
final double topMargin, leftMargin, rightMargin;
final Rectangle rect;
final double[] holes;
double y, v, b;
final Pixbuf pixbuf;
Gtk.init(args);
paper = PaperSize.getDefault();
pageWidth = paper.getWidth(Unit.POINTS);
pageHeight = paper.getHeight(Unit.POINTS);
topMargin = 25;
leftMargin = 45;
rightMargin = 20;
surface = new PdfSurface("doc/examples/cairo/ExampleLinedPaper.pdf", pageWidth, pageHeight);
cr = new Context(surface);
cr.moveTo(leftMargin, topMargin);
layout = new Layout(cr);
desc = new FontDescription("Liberation Serif, 12");
layout.setFontDescription(desc);
layout.setText("Lorem");
first = layout.getLineReadonly(0);
rect = first.getExtentsLogical();
v = rect.getHeight();
b = rect.getAscent();
y = topMargin + b;
while (y < pageHeight) {
cr.moveTo(0, y);
cr.lineTo(pageWidth, y);
y += v;
}
cr.setSource(0, 0, 199.0 / 255.0);
cr.setLineWidth(0.1);
cr.stroke();
cr.moveTo(leftMargin, 0);
cr.lineTo(leftMargin, pageHeight);
cr.setSource(255.0 / 255.0, 0.0, 0.0);
cr.stroke();
holes = new double[] {
1.0 / 7.0, 1.0 / 2.0, 6.0 / 7.0
};
cr.setLineWidth(1.0);
for (double hole : holes) {
cr.arc(leftMargin / 2.0, pageHeight * hole, leftMargin / 4.0, 0.0, 2 * PI);
cr.setSource(1.0, 1.0, 1.0);
cr.fillPreserve();
cr.setSource(0.5, 0.5, 0.5);
cr.stroke();
}
paras = text.split("\n");
layout.setWidth(pageWidth - (leftMargin + rightMargin));
cr.setSource(0.0, 0.0, 0.0);
y = topMargin + b;
for (String para : paras) {
layout.setText(para);
for (LayoutLine line : layout.getLinesReadonly()) {
y += v;
cr.moveTo(leftMargin, y);
cr.showLayout(line);
}
y += v;
}
pixbuf = new Pixbuf("doc/examples/cairo/incoming-sketch.png");
cr.setSource(pixbuf, pageWidth - pixbuf.getWidth() - 10, pageHeight - pixbuf.getHeight() + 50);
cr.paint();
surface.finish();
}
}