annotate .svn/pristine/d6/d686576cc5313c6c5ff9ec468ab9b2b763d728a1.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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