Chris@0: # Information Chris@0: # Chris@0: # PDF_EPS class from Valentin Schmidt ported to ruby by Thiago Jackiw (tjackiw@gmail.com) Chris@0: # working for Mingle LLC (www.mingle.com) Chris@0: # Release Date: July 13th, 2006 Chris@0: # Chris@0: # Description Chris@0: # Chris@0: # This script allows to embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files. Chris@0: # Only vector drawing is supported, not text or bitmap. Although the script was successfully Chris@0: # tested with various AI format versions, best results are probably achieved with files that Chris@0: # were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2). Chris@0: # Chris@0: # ImageEps(string file, float x, float y [, float w [, float h [, string link [, boolean useBoundingBox]]]]) Chris@0: # Chris@0: # Same parameters as for regular FPDF::Image() method, with an additional one: Chris@0: # Chris@0: # useBoundingBox: specifies whether to position the bounding box (true) or the complete canvas (false) Chris@0: # at location (x,y). Default value is true. Chris@0: # Chris@0: # First added to the Ruby FPDF distribution in 1.53c Chris@0: # Chris@0: # Usage is as follows: Chris@0: # Chris@0: # require 'fpdf' Chris@0: # require 'fpdf_eps' Chris@0: # pdf = FPDF.new Chris@0: # pdf.extend(PDF_EPS) Chris@0: # pdf.ImageEps(...) Chris@0: # Chris@0: # This allows it to be combined with other extensions, such as the bookmark Chris@0: # module. Chris@0: Chris@0: module PDF_EPS Chris@0: def ImageEps(file, x, y, w=0, h=0, link='', use_bounding_box=true) Chris@0: data = nil Chris@0: if File.exists?(file) Chris@0: File.open(file, 'rb') do |f| Chris@0: data = f.read() Chris@0: end Chris@0: else Chris@0: Error('EPS file not found: '+file) Chris@0: end Chris@0: Chris@0: # Find BoundingBox param Chris@0: regs = data.scan(/%%BoundingBox: [^\r\n]*/m) Chris@0: regs << regs[0].gsub(/%%BoundingBox: /, '') Chris@0: if regs.size > 1 Chris@0: tmp = regs[1].to_s.split(' ') Chris@0: @x1 = tmp[0].to_i Chris@0: @y1 = tmp[1].to_i Chris@0: @x2 = tmp[2].to_i Chris@0: @y2 = tmp[3].to_i Chris@0: else Chris@0: Error('No BoundingBox found in EPS file: '+file) Chris@0: end Chris@0: f_start = data.index('%%EndSetup') Chris@0: f_start = data.index('%%EndProlog') if f_start === false Chris@0: f_start = data.index('%%BoundingBox') if f_start === false Chris@0: Chris@0: data = data.slice(f_start, data.length) Chris@0: Chris@0: f_end = data.index('%%PageTrailer') Chris@0: f_end = data.index('showpage') if f_end === false Chris@0: data = data.slice(0, f_end) if f_end Chris@0: Chris@0: # save the current graphic state Chris@0: out('q') Chris@0: Chris@0: k = @k Chris@0: Chris@0: # Translate Chris@0: if use_bounding_box Chris@0: dx = x*k-@x1 Chris@0: dy = @hPt-@y2-y*k Chris@0: else Chris@0: dx = x*k Chris@0: dy = -y*k Chris@0: end Chris@0: tm = [1,0,0,1,dx,dy] Chris@0: out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm', Chris@0: tm[0], tm[1], tm[2], tm[3], tm[4], tm[5])) Chris@0: Chris@0: if w > 0 Chris@0: scale_x = w/((@x2-@x1)/k) Chris@0: if h > 0 Chris@0: scale_y = h/((@y2-@y1)/k) Chris@0: else Chris@0: scale_y = scale_x Chris@0: h = (@y2-@y1)/k * scale_y Chris@0: end Chris@0: else Chris@0: if h > 0 Chris@0: scale_y = $h/((@y2-@y1)/$k) Chris@0: scale_x = scale_y Chris@0: w = (@x2-@x1)/k * scale_x Chris@0: else Chris@0: w = (@x2-@x1)/k Chris@0: h = (@y2-@y1)/k Chris@0: end Chris@0: end Chris@0: Chris@0: if !scale_x.nil? Chris@0: # Scale Chris@0: tm = [scale_x,0,0,scale_y,0,@hPt*(1-scale_y)] Chris@0: out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm', Chris@0: tm[0], tm[1], tm[2], tm[3], tm[4], tm[5])) Chris@0: end Chris@0: Chris@0: data.split(/\r\n|[\r\n]/).each do |line| Chris@0: next if line == '' || line[0,1] == '%' Chris@0: len = line.length Chris@0: # next if (len > 2 && line[len-2,len] != ' ') Chris@0: cmd = line[len-2,len].strip Chris@0: case cmd Chris@0: when 'm', 'l', 'v', 'y', 'c', 'k', 'K', 'g', 'G', 's', 'S', 'J', 'j', 'w', 'M', 'd': Chris@0: out(line) Chris@0: Chris@0: when 'L': Chris@0: line[len-1,len]='l' Chris@0: out(line) Chris@0: Chris@0: when 'C': Chris@0: line[len-1,len]='c' Chris@0: out(line) Chris@0: Chris@0: when 'f', 'F': Chris@0: out('f*') Chris@0: Chris@0: when 'b', 'B': Chris@0: out(cmd + '*') Chris@0: end Chris@0: end Chris@0: Chris@0: # restore previous graphic state Chris@0: out('Q') Chris@0: Link(x,y,w,h,link) if link Chris@0: end Chris@0: end