diff options
Diffstat (limited to 'src/book.vala')
-rw-r--r-- | src/book.vala | 79 |
1 files changed, 66 insertions, 13 deletions
diff --git a/src/book.vala b/src/book.vala index 97e49a8..db9be0b 100644 --- a/src/book.vala +++ b/src/book.vala @@ -83,12 +83,56 @@ public class Book needs_saving = true; } + public void reverse () + { + var new_pages = new List<Page> (); + foreach (var page in pages) + new_pages.prepend (page); + pages = (owned) new_pages; + + reordered (); + needs_saving = true; + } + + public void combine_sides () + { + var n_front = n_pages - n_pages / 2; + var new_pages = new List<Page> (); + for (var i = 0; i < n_pages; i++) + { + if (i % 2 == 0) + new_pages.append (pages.nth_data (i / 2)); + else + new_pages.append (pages.nth_data (n_front + (i / 2))); + } + pages = (owned) new_pages; + + reordered (); + needs_saving = true; + } + + public void combine_sides_reverse () + { + var new_pages = new List<Page> (); + for (var i = 0; i < n_pages; i++) + { + if (i % 2 == 0) + new_pages.append (pages.nth_data (i / 2)); + else + new_pages.append (pages.nth_data (n_pages - 1 - (i / 2))); + } + pages = (owned) new_pages; + + reordered (); + needs_saving = true; + } + public void delete_page (Page page) { page.pixels_changed.disconnect (page_changed_cb); page.crop_changed.disconnect (page_changed_cb); - page_removed (page); pages.remove (page); + page_removed (page); needs_saving = true; } @@ -153,8 +197,8 @@ public class Book { var page = get_page (i); var image = page.get_image (true); - var width = image.get_width () * 72.0 / page.dpi; - var height = image.get_height () * 72.0 / page.dpi; + var width = image.width * 72.0 / page.dpi; + var height = image.height * 72.0 / page.dpi; surface.set_size (width, height); save_ps_pdf_surface (surface, image, page.dpi); surface.show_page (); @@ -212,6 +256,11 @@ public class Book private void save_pdf (File file, int quality) throws Error { + /* Generate a random ID for this file */ + var id = ""; + for (var i = 0; i < 4; i++) + id += "%08x".printf (Random.next_int ()); + var stream = file.replace (null, false, FileCreateFlags.NONE, null); var writer = new PDFWriter (stream); @@ -227,6 +276,7 @@ public class Book writer.write_string ("<<\n"); writer.write_string ("/Type /Catalog\n"); //FIXMEwriter.write_string ("/Metadata %u 0 R\n".printf (catalog_number + 1)); + //FIXMEwriter.write_string ("/MarkInfo << /Marked true >>"); writer.write_string ("/Pages %u 0 R\n".printf (catalog_number + 1)); //+2 writer.write_string (">>\n"); writer.write_string ("endobj\n"); @@ -264,8 +314,8 @@ public class Book { var page = get_page (i); var image = page.get_image (true); - var width = image.get_width (); - var height = image.get_height (); + var width = image.width; + var height = image.height; unowned uint8[] pixels = image.get_pixels (); var page_width = width * 72.0 / page.dpi; var page_height = height * 72.0 / page.dpi; @@ -284,7 +334,7 @@ public class Book data = new uint8[data_length]; for (var row = 0; row < height; row++) { - var in_offset = row * image.get_rowstride (); + var in_offset = row * image.rowstride; var out_offset = row * width * 3; for (var x = 0; x < width; x++) { @@ -314,7 +364,7 @@ public class Book shift_count = 6; } - var in_offset = row * image.get_rowstride (); + var in_offset = row * image.rowstride; for (var x = 0; x < width; x++) { /* Clear byte */ @@ -359,7 +409,7 @@ public class Book mask = 0x80; } - var in_offset = row * image.get_rowstride (); + var in_offset = row * image.rowstride; for (var x = 0; x < width; x++) { /* Clear byte */ @@ -388,7 +438,7 @@ public class Book data = new uint8 [data_length]; for (var row = 0; row < height; row++) { - var in_offset = row * image.get_rowstride (); + var in_offset = row * image.rowstride; var out_offset = row * width; for (var x = 0; x < width; x++) data[out_offset+x] = pixels[in_offset+x*3]; @@ -457,7 +507,7 @@ public class Book number = writer.start_object (); writer.write_string ("%u 0 obj\n".printf (number)); writer.write_string ("<<\n"); - writer.write_string ("/Length %d\n".printf (command.length + 1)); + writer.write_string ("/Length %d\n".printf (command.length)); writer.write_string (">>\n"); writer.write_string ("stream\n"); writer.write_string (command); @@ -478,19 +528,22 @@ public class Book writer.write_string ("endobj\n"); /* Cross-reference table */ + writer.write_string ("\n"); var xref_offset = writer.offset; writer.write_string ("xref\n"); - writer.write_string ("1 %zu\n".printf (writer.object_offsets.length ())); + writer.write_string ("0 %zu\n".printf (writer.object_offsets.length () + 1)); + writer.write_string ("0000000000 65535 f \n"); foreach (var offset in writer.object_offsets) writer.write_string ("%010zu 00000 n \n".printf (offset)); /* Trailer */ + writer.write_string ("\n"); writer.write_string ("trailer\n"); writer.write_string ("<<\n"); - writer.write_string ("/Size %zu\n".printf (writer.object_offsets.length ())); + writer.write_string ("/Size %zu\n".printf (writer.object_offsets.length () + 1)); writer.write_string ("/Info %u 0 R\n".printf (info_number)); writer.write_string ("/Root %u 0 R\n".printf (catalog_number)); - //FIXME: writer.write_string ("/ID [<...> <...>]\n"); + writer.write_string ("/ID [<%s> <%s>]\n".printf (id, id)); writer.write_string (">>\n"); writer.write_string ("startxref\n"); writer.write_string ("%zu\n".printf (xref_offset)); |