Merge lp:~firgeis/pantheon-print/pantheon-print into lp:~elementary-apps/pantheon-print/trunk

Proposed by Ezequiel Lewin
Status: Merged
Approved by: Cody Garver
Approved revision: 10
Merged at revision: 9
Proposed branch: lp:~firgeis/pantheon-print/pantheon-print
Merge into: lp:~elementary-apps/pantheon-print/trunk
Diff against target: 241 lines (+94/-102)
2 files modified
CMakeLists.txt (+1/-1)
src/pantheon-print.vala (+93/-101)
To merge this branch: bzr merge lp:~firgeis/pantheon-print/pantheon-print
Reviewer Review Type Date Requested Status
Cody Garver (community) Needs Fixing
Review via email: mp+211177@code.launchpad.net

Commit message

- Refactored original code for readability
- Removed file extension check, now reads and outputs all valid UTF-8 from file
- Fixed a bug where it would halt on ampersand glyphs
- CMake change adds an option for DEBUG to output vala debug symbols

Description of the change

- Refactored original code for readability
- Removed file extension check, now reads and outputs all valid UTF-8 from file
- Fixed a bug where it would halt on ampersand glyphs
- CMake change adds an option for DEBUG to output vala debug symbols

To post a comment you must log in.
Revision history for this message
Cody Garver (codygarver) wrote :

Hey, I'm excited to see this merge, good work.

However, it does not comply with the elementary code style. Please fix it using instruction from this document: http://elementaryos.org/docs/code/code-style

review: Needs Fixing
10. By Ezequiel Lewin

* Restylized code

Revision history for this message
Ezequiel Lewin (firgeis) wrote :

Let me know if further changes are needed

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2013-06-09 20:53:01 +0000
3+++ CMakeLists.txt 2014-04-03 19:06:45 +0000
4@@ -32,7 +32,7 @@
5 PACKAGES
6 gtk+-3.0
7 OPTIONS
8- --vapidir=${CMAKE_CURRENT_SOURCE_DIR}/vapi/
9+ --vapidir=${CMAKE_CURRENT_SOURCE_DIR}/vapi/ -g --save-temps
10 )
11 add_executable (pantheon-print ${VALA_C})
12 install(TARGETS pantheon-print RUNTIME DESTINATION bin)
13
14=== modified file 'src/pantheon-print.vala'
15--- src/pantheon-print.vala 2012-04-21 12:36:30 +0000
16+++ src/pantheon-print.vala 2014-04-03 19:06:45 +0000
17@@ -1,5 +1,6 @@
18 //
19 // Copyright (C) 2012 Andrea Basso
20+// Copyright (C) 2014 Ezequiel Lewin
21 //
22 // This program uses code originally written by Tarot Osuji for Leafpad http://tarot.freeshell.org/leafpad/
23 //
24@@ -18,125 +19,116 @@
25 //
26
27 public class CustomOperation : Gtk.PrintOperation {
28- const int FONT_SIZE = 12;
29-
30- bool is_text = false;
31- string content = "";
32- int[] page_breaks;
33- Cairo.Context context = null;
34- Pango.Layout layout = null;
35- Pango.FontDescription desc;
36- Gtk.PaperSize paper_size;
37-
38- int height;
39- int width;
40- int line_count;
41-
42- public CustomOperation (string[] args, Gtk.Window main_window) {
43-
44- var file = File.new_for_commandline_arg (args[1]);
45-
46- try {
47- var file_info = file.query_info ("standard::content-type", 0, null);
48- is_text = "text" in file_info.get_content_type ();
49- } catch (Error e) {
50- printerr ("Error: %s\n", e.message);
51- }
52-
53- if (file.query_exists () && is_text) {
54+ const int FONT_SIZE = 12;
55+ string content = "";
56+ int[] page_breaks;
57+ Cairo.Context context = null;
58+ Pango.Layout layout = null;
59+ Pango.FontDescription desc;
60+ int height;
61+ int width;
62+ int line_count;
63+
64+ public CustomOperation (string[] args, Gtk.Window main_window) {
65+ var file = File.new_for_commandline_arg (args[1]);
66+
67+ if (file.query_exists ()) {
68 try {
69 var dis = new DataInputStream (file.read ());
70 string line;
71
72 while ((line = dis.read_line (null)) != null)
73- content += line + "\n";
74+ if (line.validate())
75+ content += line + "\n";
76 } catch (Error e) {
77 error ("%s", e.message);
78 }
79- }
80-
81+ }
82+
83+ content = content.replace("&", "&");
84+
85 var setup = new Gtk.PageSetup ();
86+
87 setup.set_top_margin (15, Gtk.Unit.MM);
88 setup.set_bottom_margin (15, Gtk.Unit.MM);
89 setup.set_right_margin (20, Gtk.Unit.MM);
90 setup.set_left_margin (20, Gtk.Unit.MM);
91- set_default_page_setup (setup);
92-
93- begin_print.connect ((print_context) => {
94- layout = print_context.create_pango_layout ();
95- layout.set_wrap (Pango.WrapMode.WORD_CHAR);
96- desc = new Pango.FontDescription ();
97- desc.set_family ("Open Sans");
98- desc.set_absolute_size (FONT_SIZE*Pango.SCALE);
99- layout.set_font_description (desc);
100-
101- width = (int)print_context.get_width ();
102- layout.set_width (Pango.SCALE*width);
103- height = (int)print_context.get_height ();
104-
105- layout.set_markup (content, -1);
106-
107- line_count = layout.get_line_count ();
108- Pango.LayoutLine layout_line;
109- double page_height = 0;
110- for (int line = 0; line < line_count; ++line) {
111- Pango.Rectangle ink_rect, logical_rect;
112-
113- layout_line = layout.get_line (line);
114- layout_line.get_extents (out ink_rect, out logical_rect);
115-
116- double line_height = logical_rect.height / Pango.SCALE;
117-
118- if (page_height + line_height > height) {
119- page_breaks += line;
120- page_height = 0;
121- }
122-
123- page_height += line_height;
124- }
125-
126- set_n_pages (page_breaks.length + 1);
127- });
128-
129- draw_page.connect ( (operation, print_context, page_num)=> {
130- context = print_context.get_cairo_context ();
131- context.set_source_rgb (0, 0, 0);
132- layout.set_font_description (desc);
133-
134- var draw_layout = print_context.create_pango_layout ();
135- draw_layout.set_wrap (Pango.WrapMode.WORD_CHAR);
136- draw_layout.set_text (content, -1);
137-
138- int layout_width, layout_height;
139- layout.get_size (out layout_width, out layout_height);
140-
141- context.move_to (height - layout_height/Pango.SCALE, - 72 / 2.54);
142- Pango.cairo_show_layout (context, layout);
143-
144- int line_num = 0;
145- var line_per_page = height / FONT_SIZE;
146-
147- if (line_count > line_per_page * (page_num + 1))
148- line_num = line_per_page * (page_num + 1);
149- else
150- line_num = line_count;
151-
152- int j = 0;
153-
154- for (int i = line_per_page * page_num; i < line_num; i++) {
155- var line = layout.get_line (i);
156- context.move_to (0, FONT_SIZE * (j + 1));
157- Pango.cairo_show_layout_line (context, line);
158- j++;
159+ set_default_page_setup (setup);
160+ begin_print.connect (beginprint);
161+ draw_page.connect (drawpage);
162+ }
163+
164+ void beginprint (Gtk.PrintContext print_context) {
165+ layout = print_context.create_pango_layout ();
166+ layout.set_wrap (Pango.WrapMode.WORD_CHAR);
167+
168+ desc = new Pango.FontDescription ();
169+ desc.set_family ("Open Sans");
170+ desc.set_absolute_size (FONT_SIZE*Pango.SCALE);
171+ layout.set_font_description (desc);
172+
173+ width = (int)print_context.get_width ();
174+ layout.set_width (Pango.SCALE*width);
175+ height = (int)print_context.get_height ();
176+
177+ layout.set_markup (content, -1);
178+ line_count = layout.get_line_count ();
179+ Pango.LayoutLine layout_line;
180+
181+ double page_height = 0;
182+
183+ for (int line = 0; line < line_count; ++line) {
184+ Pango.Rectangle ink_rect, logical_rect;
185+ layout_line = layout.get_line (line);
186+ layout_line.get_extents (out ink_rect, out logical_rect);
187+
188+ double line_height = logical_rect.height / Pango.SCALE;
189+
190+ if (page_height + line_height > height) {
191+ page_breaks += line;
192+ page_height = 0;
193 }
194- });
195- }
196+
197+ page_height += line_height;
198+ }
199+
200+ set_n_pages (page_breaks.length + 1);
201+ }
202+
203+ void drawpage(Gtk.PrintContext print_context, int page_num) {
204+ context = print_context.get_cairo_context ();
205+ context.set_source_rgb (0, 0, 0);
206+
207+ int layout_width, layout_height;
208+
209+ layout.get_size (out layout_width, out layout_height);
210+ context.move_to(layout_width / 2, (height - layout_height/Pango.SCALE) / 2);
211+ Pango.cairo_show_layout (context, layout);
212+
213+ int line_num = 0;
214+ var line_per_page = height / FONT_SIZE;
215+
216+ if (line_count > line_per_page * (page_num + 1))
217+ line_num = line_per_page * (page_num + 1);
218+ else
219+ line_num = line_count;
220+
221+ int j = 0;
222+
223+ for (int i = line_per_page * page_num; i < line_num; i++) {
224+ var line = layout.get_line (i);
225+ context.move_to (0, FONT_SIZE * (j + 1));
226+ Pango.cairo_show_layout_line (context, line);
227+ j++;
228+ }
229+ }
230+
231 }
232
233 public static void main (string[] args) {
234 Gtk.init (ref args);
235- var main_window = new Gtk.Window ();
236-
237+ var main_window = new Gtk.Window ();
238 var operation = new CustomOperation (args, main_window);
239+
240 operation.run (Gtk.PrintOperationAction.PRINT_DIALOG, main_window);
241 }

Subscribers

People subscribed via source and target branches