mirror of
				https://github.com/johnwhitington/cpdf-source.git
				synced 2025-06-05 22:09:39 +02:00 
			
		
		
		
	fit working
This commit is contained in:
		
							
								
								
									
										31
									
								
								cpdf.ml
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								cpdf.ml
									
									
									
									
									
								
							| @@ -2767,7 +2767,7 @@ let combine_pdf_rests pdf a b = | |||||||
|           Pdf.Dictionary (unknown_keys_a @ unknown_keys_b @ combined_known_entries) |           Pdf.Dictionary (unknown_keys_a @ unknown_keys_b @ combined_known_entries) | ||||||
|  |  | ||||||
| (* Calculate the transformation matrices for a single imposed output page. *) | (* Calculate the transformation matrices for a single imposed output page. *) | ||||||
| let impose_transforms n fx fy columns rtl btt center margin spacing linewidth mediabox len = | let impose_transforms fx fy columns rtl btt center margin spacing linewidth mediabox len = | ||||||
|   let width, height = |   let width, height = | ||||||
|     match Pdf.parse_rectangle mediabox with |     match Pdf.parse_rectangle mediabox with | ||||||
|       xmin, ymin, xmax, ymax -> xmax -. xmin, ymax -. ymin |       xmin, ymin, xmax, ymax -> xmax -. xmin, ymax -. ymin | ||||||
| @@ -2809,11 +2809,11 @@ let impose_transforms n fx fy columns rtl btt center margin spacing linewidth me | |||||||
|  |  | ||||||
| (* Combine two pages into one throughout the document. The pages have already | (* Combine two pages into one throughout the document. The pages have already | ||||||
| had their objects renumbered so as not to clash. *) | had their objects renumbered so as not to clash. *) | ||||||
| let impose_pages n x y columns rtl btt center margin spacing linewidth mediabox' fast pdf = function | let impose_pages x y columns rtl btt center margin spacing linewidth mediabox' fast pdf = function | ||||||
|   | [] -> assert false |   | [] -> assert false | ||||||
|   | (h::_) as pages -> |   | (h::_) as pages -> | ||||||
|      let transforms =  |      let transforms =  | ||||||
|        impose_transforms n x y columns rtl btt center margin spacing linewidth h.Pdfpage.mediabox (length pages) |        impose_transforms x y columns rtl btt center margin spacing linewidth h.Pdfpage.mediabox (length pages) | ||||||
|      in |      in | ||||||
|        (* Change the pattern matrices before combining resources *) |        (* Change the pattern matrices before combining resources *) | ||||||
|        let pages, h = |        let pages, h = | ||||||
| @@ -2865,24 +2865,31 @@ let impose_pages n x y columns rtl btt center margin spacing linewidth mediabox' | |||||||
|           Pdfpage.resources = resources'; |           Pdfpage.resources = resources'; | ||||||
|           Pdfpage.rest = rest'} |           Pdfpage.rest = rest'} | ||||||
|  |  | ||||||
| (* FIXME impose to use cropbox by default? See what happens with cropbox above. Clarify? Also point to hardbox. *) |  | ||||||
| let impose ~x ~y ~fit ~columns ~rtl ~btt ~center ~margin ~spacing ~linewidth ~fast pdf = | let impose ~x ~y ~fit ~columns ~rtl ~btt ~center ~margin ~spacing ~linewidth ~fast pdf = | ||||||
|   (*Printf.printf |   (*Printf.printf | ||||||
|     "impose: x = %f, y = %f, fit = %b, columns = %b, rtl = %b, btt = %b, center = %b, margin = %f, spacing = %f, linewidth = %f, fast = %b\n" |     "impose: x = %f, y = %f, fit = %b, columns = %b, rtl = %b, btt = %b, | ||||||
|  |     center = %b, margin = %f, spacing = %f, linewidth = %f, fast = %b\n" | ||||||
|     x y fit columns rtl btt center margin spacing linewidth fast;*) |     x y fit columns rtl btt center margin spacing linewidth fast;*) | ||||||
|   let endpage = Pdfpage.endpage pdf in |   let endpage = Pdfpage.endpage pdf in | ||||||
|  |   let firstpage = hd (Pdfpage.pages_of_pagetree pdf) in | ||||||
|  |   let _, _, w, h = Pdf.parse_rectangle firstpage.Pdfpage.mediabox in | ||||||
|   let ix = int_of_float x in |   let ix = int_of_float x in | ||||||
|   let iy = int_of_float y in |   let iy = int_of_float y in | ||||||
|   let n, ix, iy = |   let n, ix, iy = | ||||||
|  |       if fit then | ||||||
|  |         (* +. 0.001 ensures a page always fits on itself, or on another page of same height or width. *) | ||||||
|  |         let across = int_of_float (floor (x /. w +. 0.001)) in | ||||||
|  |         let down = int_of_float (floor (y /. h +. 0.001)) in | ||||||
|  |           if across < 1 || down < 1 then error "Not even a single page would fit." else | ||||||
|  |           (across * down, across, down) | ||||||
|  |       else | ||||||
|         if ix = 0 && iy = 0 then error "impose-xy: both dimensions cannot be zero" else |         if ix = 0 && iy = 0 then error "impose-xy: both dimensions cannot be zero" else | ||||||
|         if ix = 0 then (endpage, endpage, 1) |         if ix = 0 then (endpage, endpage, 1) | ||||||
|         else if iy = 0 then (endpage, 1, endpage) |         else if iy = 0 then (endpage, 1, endpage) | ||||||
|         else (ix * iy, ix, iy) |         else (ix * iy, ix, iy) | ||||||
|   in |   in | ||||||
|   let firstpage = hd (Pdfpage.pages_of_pagetree pdf) in |  | ||||||
|   let mediabox' = |   let mediabox' = | ||||||
|     if fit then Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real x; Pdf.Real y] else |     if fit then Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real x; Pdf.Real y] else | ||||||
|       let _, _, w, h = Pdf.parse_rectangle firstpage.Pdfpage.mediabox in |  | ||||||
|       if x = 0.0 then Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real (w *. float_of_int endpage); Pdf.Real h] |       if x = 0.0 then Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real (w *. float_of_int endpage); Pdf.Real h] | ||||||
|       else if y = 0.0 then Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real w; Pdf.Real (h *. float_of_int endpage)] |       else if y = 0.0 then Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real w; Pdf.Real (h *. float_of_int endpage)] | ||||||
|       else Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real (w *. x); Pdf.Real (h *. y)] |       else Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real (w *. x); Pdf.Real (h *. y)] | ||||||
| @@ -2892,11 +2899,15 @@ let impose ~x ~y ~fit ~columns ~rtl ~btt ~center ~margin ~spacing ~linewidth ~fa | |||||||
|   let pages = Pdfpage.pages_of_pagetree pdf in |   let pages = Pdfpage.pages_of_pagetree pdf in | ||||||
|   let pagesets = splitinto n pages in |   let pagesets = splitinto n pages in | ||||||
|   let renumbered = map (Pdfpage.renumber_pages pdf) pagesets in |   let renumbered = map (Pdfpage.renumber_pages pdf) pagesets in | ||||||
|   let pages' = map (impose_pages n (float_of_int ix) (float_of_int iy) columns rtl btt center margin spacing linewidth mediabox' fast pdf) renumbered in |   let pages' = | ||||||
|  |      map | ||||||
|  |        (impose_pages (float_of_int ix) (float_of_int iy) columns rtl btt center margin spacing linewidth mediabox' fast pdf) | ||||||
|  |        renumbered | ||||||
|  |   in | ||||||
|   let changes = map (fun x -> (x, (x + (n - 1)) / n)) pagenums in |   let changes = map (fun x -> (x, (x + (n - 1)) / n)) pagenums in | ||||||
|     Pdfpage.change_pages ~changes true pdf pages' |     Pdfpage.change_pages ~changes true pdf pages' | ||||||
|  |  | ||||||
| (* Legacy -twoup-stack. Imposes 2x1 on a page twice the size then rotates. *) | (* Legacy -twoup-stack. Impose 2x1 on a page twice the size then rotate. *) | ||||||
| let twoup_stack fast pdf = | let twoup_stack fast pdf = | ||||||
|   let pdf = |   let pdf = | ||||||
|     impose |     impose | ||||||
| @@ -2906,7 +2917,7 @@ let twoup_stack fast pdf = | |||||||
|    let all = ilist 1 (Pdfpage.endpage pdf) in |    let all = ilist 1 (Pdfpage.endpage pdf) in | ||||||
|     upright ~fast all (rotate_pdf ~-90 pdf all) |     upright ~fast all (rotate_pdf ~-90 pdf all) | ||||||
|  |  | ||||||
| (* Legacy -two-up. Rotates the pages and shrinks them so as to fit 2x1 on a page the same size. *) | (* Legacy -two-up. Rotate the pages and shrinks them so as to fit 2x1 on a page the same size. *) | ||||||
| let twoup fast pdf = | let twoup fast pdf = | ||||||
|   let firstpage = hd (Pdfpage.pages_of_pagetree pdf) in |   let firstpage = hd (Pdfpage.pages_of_pagetree pdf) in | ||||||
|   let width, height = |   let width, height = | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user