Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

211
LINES

< > BotCompany Repo | #1012001 // Java Gnome Test (OK, requires libjava-gnome-java)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 1467K of libraries. Click here for Pure Java version (5161L/35K).

1  
!7
2  
3  
lib 1012002 // gtk-4.1.jar
4  
5  
/*
6  
 * ExampleLinedPaper.java
7  
 *
8  
 * Copyright (c) 2007-2009 Operational Dynamics Consulting Pty Ltd, and Others
9  
 * 
10  
 * The code in this file, and the program it is a part of, are made available
11  
 * to you by the authors under the terms of the "GNU General Public Licence,
12  
 * version 2" See the LICENCE file for the terms governing usage and
13  
 * redistribution.
14  
 * 
15  
 * Copyright (c) 2008      Nathan Strum
16  
 * 
17  
 * The image sketch is included with java-gnome with permission of its author
18  
 * for the express purpose of illustrating this example.
19  
 */
20  
21  
import org.freedesktop.cairo.Context;
22  
import org.freedesktop.cairo.PdfSurface;
23  
import org.freedesktop.cairo.Surface;
24  
import org.gnome.gdk.Pixbuf;
25  
import org.gnome.gtk.Gtk;
26  
import org.gnome.gtk.PaperSize;
27  
import org.gnome.gtk.Unit;
28  
import org.gnome.pango.FontDescription;
29  
import org.gnome.pango.Layout;
30  
import org.gnome.pango.LayoutLine;
31  
import org.gnome.pango.Rectangle;
32  
33  
sS text = "Lorem Ipsum";
34  
35  
/**
36  
 * Some poor kid sitting through latin class doodling on his lined paper.
37  
 * 
38  
 * This is an example of using Pango to render text along side Cairo drawing
39  
 * primitives, and using Cairo's PdfSurface back end to produce a PDF that can
40  
 * subsequently be previewd or printed to paper.
41  
 * 
42  
 * The blue lines actually represent the baseline of the font (the calls to
43  
 * Context's showLayout() draw LayoutLines with their baseline at the current
44  
 * Cairo point); if you go to a high level of zoom you'll see the overshoot
45  
 * that some glyphs are designed with. The red line is the left margin, and
46  
 * unlike the baseline (which is just an arbitrary point midway into the
47  
 * font's extents), nothing bleeds left past the margin line.
48  
 * 
49  
 * @author Andrew Cowie
50  
 * @author Nathan Strum
51  
 */
52  
 
53  
p {
54  
        final Surface surface;
55  
        final Context cr;
56  
        final Layout layout;
57  
        final FontDescription desc;
58  
        final String[] paras;
59  
        final LayoutLine first;
60  
        final PaperSize paper;
61  
        final double pageWidth, pageHeight;
62  
        final double topMargin, leftMargin, rightMargin;
63  
        final Rectangle rect;
64  
        final double[] holes;
65  
        double y, v, b;
66  
        final Pixbuf pixbuf;
67  
68  
        Gtk.init(args);
69  
70  
        paper = PaperSize.getDefault();
71  
        pageWidth = paper.getWidth(Unit.POINTS);
72  
        pageHeight = paper.getHeight(Unit.POINTS);
73  
        topMargin = 25;
74  
        leftMargin = 45;
75  
        rightMargin = 20;
76  
77  
        File pdfFile = programFile("rendered.pdf");
78  
        surface = new PdfSurface(f2s(pdfFile), pageWidth, pageHeight);
79  
        cr = new Context(surface);
80  
81  
        cr.moveTo(leftMargin, topMargin);
82  
83  
        layout = new Layout(cr);
84  
        desc = new FontDescription("Liberation Serif, 12");
85  
        layout.setFontDescription(desc);
86  
87  
        /*
88  
         * Before we start rendering, we need some information about the line
89  
         * height. Given that all lines are going to be rendered in the same
90  
         * font, we can get the metrics of a piece of arbitrary text and then
91  
         * use that height to do the line spacing for both the text and the
92  
         * lines.
93  
         */
94  
95  
        layout.setText("Lorem");
96  
        first = layout.getLineReadonly(0);
97  
        rect = first.getExtentsLogical();
98  
        v = rect.getHeight();
99  
        b = rect.getAscent();
100  
101  
        /*
102  
         * Draw the horizontal rules in blue. These will cunningly be drawn on
103  
         * the font baseline, and given that the LayoutLines below will be
104  
         * drawn with reference to this latitude it will end up looking like
105  
         * the person writing is very good at staying between the lines.
106  
         */
107  
108  
        y = topMargin + b;
109  
        while (y < pageHeight) {
110  
            cr.moveTo(0, y);
111  
            cr.lineTo(pageWidth, y);
112  
113  
            y += v;
114  
        }
115  
116  
        cr.setSource(0, 0, 199.0 / 255.0);
117  
        cr.setLineWidth(0.1);
118  
        cr.stroke();
119  
120  
        /*
121  
         * Draw a vertical red line as the left margin rule.
122  
         */
123  
124  
        cr.moveTo(leftMargin, 0);
125  
        cr.lineTo(leftMargin, pageHeight);
126  
        cr.setSource(255.0 / 255.0, 0.0, 0.0);
127  
        cr.stroke();
128  
129  
        /*
130  
         * Now draw the "holes" making this three-hole punched lined paper.
131  
         * The holes array are fractions of the page height which is where we
132  
         * will draw the circles with arc(). We wil preserve the path so we
133  
         * can use it again to full with white, making it look like the paper
134  
         * was punched out.
135  
         */
136  
137  
        holes = new double[] {
138  
                1.0 / 7.0, 1.0 / 2.0, 6.0 / 7.0
139  
        };
140  
141  
        cr.setLineWidth(1.0);
142  
143  
        for (double hole : holes) {
144  
            cr.arc(leftMargin / 2.0, pageHeight * hole, leftMargin / 4.0, 0.0, 2 * Math.PI);
145  
146  
            cr.setSource(1.0, 1.0, 1.0);
147  
            cr.fillPreserve();
148  
149  
            cr.setSource(0.5, 0.5, 0.5);
150  
            cr.stroke();
151  
        }
152  
153  
        /*
154  
         * And finally we lay out the words. Given some source text, split it
155  
         * up into individual paragraphs. Pango's Layout is capable of doing
156  
         * multiple paragraphs at once, but this allows us to control the
157  
         * spacing between paragraphs.
158  
         */
159  
160  
        paras = text.split("\n");
161  
162  
        /*
163  
         * Set a width for the Layouts. This will kick-off word wrapping. And
164  
         * reset the source colour so that the text will be black.
165  
         */
166  
167  
        layout.setWidth(pageWidth - (leftMargin + rightMargin));
168  
169  
        cr.setSource(0.0, 0.0, 0.0);
170  
171  
        /*
172  
         * We did the lines first so that the typeset text will be over the
173  
         * ruled lines. We go to the trouble of drawing the lines
174  
         * individually, making it easier to keep things aligned with the
175  
         * baselines of the rules that we've already drawn.
176  
         */
177  
178  
        y = topMargin + b;
179  
        for (String para : paras) {
180  
            layout.setText(para);
181  
182  
            for (LayoutLine line : layout.getLinesReadonly()) {
183  
                y += v;
184  
185  
                cr.moveTo(leftMargin, y);
186  
                cr.showLayout(line);
187  
            }
188  
189  
            y += v; // blank line between paras
190  
        }
191  
192  
        /*
193  
         * Of course, what student in latin class is paying attention? None in
194  
         * the history of western civilization, we're quite sure. So to
195  
         * complete our example we have a doodle at the bottom of the page.
196  
         */
197  
198  
        pixbuf = new Pixbuf(f2s(loadImageAsFile(#1009664)));
199  
        cr.setSource(pixbuf, pageWidth - pixbuf.getWidth() - 10, pageHeight - pixbuf.getHeight() + 50);
200  
        cr.paint();
201  
202  
        /*
203  
         * Finally, flush the drawing out to the Surface and through it on out
204  
         * to the PDF document. This is very important! If you don't reach
205  
         * this point the file on disk will be incomplete and won't render in
206  
         * a PDF viewer.
207  
         */
208  
209  
        surface.finish();
210  
        print("Made: " + f2s(pdfFile));
211  
    }

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012001
Snippet name: Java Gnome Test (OK, requires libjava-gnome-java)
Eternal ID of this version: #1012001/10
Text MD5: 5e925ca19bea15f5f1616f55b4592952
Transpilation MD5: 53c7c9422d12eb96e29ea89d0bfcb481
Author: stefan
Category: javax / linux
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-11-15 20:18:00
Source code size: 7121 bytes / 211 lines
Pitched / IR pitched: No / No
Views / Downloads: 429 / 1014
Version history: 9 change(s)
Referenced in: [show references]