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