annotate vendor/plugins/rfpdf/lib/fpdf/fpdf_eps.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents cbce1fd3b1b7
children
rev   line source
Chris@0 1 # Information
Chris@0 2 #
Chris@0 3 # PDF_EPS class from Valentin Schmidt ported to ruby by Thiago Jackiw (tjackiw@gmail.com)
Chris@0 4 # working for Mingle LLC (www.mingle.com)
Chris@0 5 # Release Date: July 13th, 2006
Chris@0 6 #
Chris@0 7 # Description
Chris@0 8 #
Chris@0 9 # This script allows to embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
Chris@0 10 # Only vector drawing is supported, not text or bitmap. Although the script was successfully
Chris@0 11 # tested with various AI format versions, best results are probably achieved with files that
Chris@0 12 # were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2).
Chris@0 13 #
Chris@0 14 # ImageEps(string file, float x, float y [, float w [, float h [, string link [, boolean useBoundingBox]]]])
Chris@0 15 #
Chris@0 16 # Same parameters as for regular FPDF::Image() method, with an additional one:
Chris@0 17 #
Chris@0 18 # useBoundingBox: specifies whether to position the bounding box (true) or the complete canvas (false)
Chris@0 19 # at location (x,y). Default value is true.
Chris@0 20 #
Chris@0 21 # First added to the Ruby FPDF distribution in 1.53c
Chris@0 22 #
Chris@0 23 # Usage is as follows:
Chris@0 24 #
Chris@0 25 # require 'fpdf'
Chris@0 26 # require 'fpdf_eps'
Chris@0 27 # pdf = FPDF.new
Chris@0 28 # pdf.extend(PDF_EPS)
Chris@0 29 # pdf.ImageEps(...)
Chris@0 30 #
Chris@0 31 # This allows it to be combined with other extensions, such as the bookmark
Chris@0 32 # module.
Chris@0 33
Chris@0 34 module PDF_EPS
Chris@0 35 def ImageEps(file, x, y, w=0, h=0, link='', use_bounding_box=true)
Chris@0 36 data = nil
Chris@0 37 if File.exists?(file)
Chris@0 38 File.open(file, 'rb') do |f|
Chris@0 39 data = f.read()
Chris@0 40 end
Chris@0 41 else
Chris@0 42 Error('EPS file not found: '+file)
Chris@0 43 end
Chris@0 44
Chris@0 45 # Find BoundingBox param
Chris@0 46 regs = data.scan(/%%BoundingBox: [^\r\n]*/m)
Chris@0 47 regs << regs[0].gsub(/%%BoundingBox: /, '')
Chris@0 48 if regs.size > 1
Chris@0 49 tmp = regs[1].to_s.split(' ')
Chris@0 50 @x1 = tmp[0].to_i
Chris@0 51 @y1 = tmp[1].to_i
Chris@0 52 @x2 = tmp[2].to_i
Chris@0 53 @y2 = tmp[3].to_i
Chris@0 54 else
Chris@0 55 Error('No BoundingBox found in EPS file: '+file)
Chris@0 56 end
Chris@0 57 f_start = data.index('%%EndSetup')
Chris@0 58 f_start = data.index('%%EndProlog') if f_start === false
Chris@0 59 f_start = data.index('%%BoundingBox') if f_start === false
Chris@0 60
Chris@0 61 data = data.slice(f_start, data.length)
Chris@0 62
Chris@0 63 f_end = data.index('%%PageTrailer')
Chris@0 64 f_end = data.index('showpage') if f_end === false
Chris@0 65 data = data.slice(0, f_end) if f_end
Chris@0 66
Chris@0 67 # save the current graphic state
Chris@0 68 out('q')
Chris@0 69
Chris@0 70 k = @k
Chris@0 71
Chris@0 72 # Translate
Chris@0 73 if use_bounding_box
Chris@0 74 dx = x*k-@x1
Chris@0 75 dy = @hPt-@y2-y*k
Chris@0 76 else
Chris@0 77 dx = x*k
Chris@0 78 dy = -y*k
Chris@0 79 end
Chris@0 80 tm = [1,0,0,1,dx,dy]
Chris@0 81 out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm',
Chris@0 82 tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]))
Chris@0 83
Chris@0 84 if w > 0
Chris@0 85 scale_x = w/((@x2-@x1)/k)
Chris@0 86 if h > 0
Chris@0 87 scale_y = h/((@y2-@y1)/k)
Chris@0 88 else
Chris@0 89 scale_y = scale_x
Chris@0 90 h = (@y2-@y1)/k * scale_y
Chris@0 91 end
Chris@0 92 else
Chris@0 93 if h > 0
Chris@0 94 scale_y = $h/((@y2-@y1)/$k)
Chris@0 95 scale_x = scale_y
Chris@0 96 w = (@x2-@x1)/k * scale_x
Chris@0 97 else
Chris@0 98 w = (@x2-@x1)/k
Chris@0 99 h = (@y2-@y1)/k
Chris@0 100 end
Chris@0 101 end
Chris@0 102
Chris@0 103 if !scale_x.nil?
Chris@0 104 # Scale
Chris@0 105 tm = [scale_x,0,0,scale_y,0,@hPt*(1-scale_y)]
Chris@0 106 out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm',
Chris@0 107 tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]))
Chris@0 108 end
Chris@0 109
Chris@0 110 data.split(/\r\n|[\r\n]/).each do |line|
Chris@0 111 next if line == '' || line[0,1] == '%'
Chris@0 112 len = line.length
Chris@0 113 # next if (len > 2 && line[len-2,len] != ' ')
Chris@0 114 cmd = line[len-2,len].strip
Chris@0 115 case cmd
Chris@0 116 when 'm', 'l', 'v', 'y', 'c', 'k', 'K', 'g', 'G', 's', 'S', 'J', 'j', 'w', 'M', 'd':
Chris@0 117 out(line)
Chris@0 118
Chris@0 119 when 'L':
Chris@0 120 line[len-1,len]='l'
Chris@0 121 out(line)
Chris@0 122
Chris@0 123 when 'C':
Chris@0 124 line[len-1,len]='c'
Chris@0 125 out(line)
Chris@0 126
Chris@0 127 when 'f', 'F':
Chris@0 128 out('f*')
Chris@0 129
Chris@0 130 when 'b', 'B':
Chris@0 131 out(cmd + '*')
Chris@0 132 end
Chris@0 133 end
Chris@0 134
Chris@0 135 # restore previous graphic state
Chris@0 136 out('Q')
Chris@0 137 Link(x,y,w,h,link) if link
Chris@0 138 end
Chris@0 139 end