Revision 1297:0a574315af3e .svn/pristine/09

View differences:

.svn/pristine/09/0930e7dcd668d1bc526e085d51f359ac0b2bfdf3.svn-base
1
api.user do
2
  api.id         @user.id
3
  api.login      @user.login if User.current.admin?
4
  api.firstname  @user.firstname
5
  api.lastname   @user.lastname
6
  api.mail       @user.mail if User.current.admin? || !@user.pref.hide_mail
7
  api.created_on @user.created_on
8
  api.last_login_on @user.last_login_on
9

  
10
  render_api_custom_values @user.visible_custom_field_values, api
11

  
12
  api.array :groups do |groups|
13
    @user.groups.each do |group|
14
      api.group :id => group.id, :name => group.name
15
    end
16
  end if User.current.admin? && include_in_api_response?('groups')
17

  
18
  api.array :memberships do
19
    @memberships.each do |membership|
20
      api.membership do
21
        api.id membership.id
22
        api.project :id => membership.project.id, :name => membership.project.name
23
        api.array :roles do
24
          membership.member_roles.each do |member_role|
25
            if member_role.role
26
              attrs = {:id => member_role.role.id, :name => member_role.role.name}
27
              attrs.merge!(:inherited => true) if member_role.inherited_from.present?
28
              api.role attrs
29
            end 
30
          end
31
        end
32
      end if membership.project
33
    end
34
  end if include_in_api_response?('memberships') && @memberships
35
end
.svn/pristine/09/09319bb04693b301a469674c2043f8c089fbdcbb.svn-base
1
#============================================================+
2
# File name   : tcpdf.rb
3
# Begin       : 2002-08-03
4
# Last Update : 2007-03-20
5
# Author      : Nicola Asuni
6
# Version     : 1.53.0.TC031
7
# License     : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
8
#
9
# Description : This is a Ruby class for generating PDF files 
10
#               on-the-fly without requiring external 
11
#               extensions.
12
#
13
# IMPORTANT:
14
# This class is an extension and improvement of the Public Domain 
15
# FPDF class by Olivier Plathey (http://www.fpdf.org).
16
#
17
# Main changes by Nicola Asuni:
18
#    Ruby porting;
19
#    UTF-8 Unicode support;
20
#    code refactoring;
21
#    source code clean up;
22
#    code style and formatting;
23
#    source code documentation using phpDocumentor (www.phpdoc.org);
24
#    All ISO page formats were included;
25
#    image scale factor;
26
#    includes methods to parse and printsome XHTML code, supporting the following elements: h1, h2, h3, h4, h5, h6, b, u, i, a, img, p, br, strong, em, font, blockquote, li, ul, ol, hr, td, th, tr, table, sup, sub, small;
27
#    includes a method to print various barcode formats using an improved version of "Generic Barcode Render Class" by Karim Mribti (http://www.mribti.com/barcode/) (require GD library: http://www.boutell.com/gd/);
28
#    defines standard Header() and Footer() methods.
29
#
30
#   Ported to Ruby by Ed Moss 2007-08-06
31
#
32
#============================================================+
33

  
34
require 'tempfile'
35
require 'core/rmagick'
36

  
37
#
38
# TCPDF Class.
39
# @package com.tecnick.tcpdf
40
#
41
 
42
@@version = "1.53.0.TC031"
43
@@fpdf_charwidths = {}
44

  
45
PDF_PRODUCER = 'TCPDF via RFPDF 1.53.0.TC031 (http://tcpdf.sourceforge.net)'
46

  
47
module TCPDFFontDescriptor
48
  @@descriptors = { 'freesans' => {} }
49
  @@font_name = 'freesans'
50

  
51
  def self.font(font_name)
52
    @@descriptors[font_name.gsub(".rb", "")]
53
  end
54

  
55
  def self.define(font_name = 'freesans')
56
    @@descriptors[font_name] ||= {}
57
    yield @@descriptors[font_name]
58
  end
59
end
60

  
61
# This is a Ruby class for generating PDF files on-the-fly without requiring external extensions.<br>
62
# This class is an extension and improvement of the FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
63
# This version contains some changes: [porting to Ruby, support for UTF-8 Unicode, code style and formatting, php documentation (www.phpdoc.org), ISO page formats, minor improvements, image scale factor]<br>
64
# TCPDF project (http://tcpdf.sourceforge.net) is based on the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
65
# To add your own TTF fonts please read /fonts/README.TXT
66
# @name TCPDF
67
# @package com.tecnick.tcpdf
68
# @@version 1.53.0.TC031
69
# @author Nicola Asuni
70
# @link http://tcpdf.sourceforge.net
71
# @license http://www.gnu.org/copyleft/lesser.html LGPL
72
#
73
class TCPDF
74
  include RFPDF
75
  include Core::RFPDF
76
  include RFPDF::Math
77
  
78
  def logger
79
    Rails.logger
80
  end
81

  
82
  cattr_accessor :k_cell_height_ratio
83
  @@k_cell_height_ratio = 1.25
84

  
85
  cattr_accessor :k_blank_image
86
  @@k_blank_image = ""
87
  
88
  cattr_accessor :k_small_ratio  
89
  @@k_small_ratio = 2/3.0
90
  
91
  cattr_accessor :k_path_cache
92
  @@k_path_cache = Rails.root.join('tmp')
93
  
94
  cattr_accessor :k_path_url_cache
95
  @@k_path_url_cache = Rails.root.join('tmp')
96
  
97
	attr_accessor :barcode
98
	
99
	attr_accessor :buffer
100
	
101
	attr_accessor :diffs
102
	
103
	attr_accessor :color_flag
104
	
105
	attr_accessor :default_table_columns
106

  
107
	attr_accessor :max_table_columns
108
	
109
	attr_accessor :default_font
110

  
111
	attr_accessor :draw_color
112
	
113
	attr_accessor :encoding
114
	
115
	attr_accessor :fill_color
116
	
117
	attr_accessor :fonts
118
	
119
	attr_accessor :font_family
120
	
121
	attr_accessor :font_files
122
	
123
	cattr_accessor :font_path
124
	
125
	attr_accessor :font_style
126
	
127
	attr_accessor :font_size_pt
128
	
129
	attr_accessor :header_width
130
	
131
	attr_accessor :header_logo
132
	
133
	attr_accessor :header_logo_width
134
	
135
	attr_accessor :header_title
136
	
137
	attr_accessor :header_string
138
	
139
	attr_accessor :images
140
	
141
	attr_accessor :img_scale
142
	
143
	attr_accessor :in_footer
144
	
145
	attr_accessor :is_unicode
146

  
147
	attr_accessor :lasth
148
	
149
	attr_accessor :links
150
	
151
	attr_accessor :list_ordered
152
	
153
	attr_accessor :list_count
154
	
155
	attr_accessor :li_spacer
156
	
157
	attr_accessor :n
158
	
159
	attr_accessor :offsets
160
	
161
	attr_accessor :orientation_changes
162
	
163
	attr_accessor :page
164
	
165
	attr_accessor :page_links
166
	
167
	attr_accessor :pages
168
	
169
	attr_accessor :pdf_version
170
	
171
	attr_accessor :prevfill_color
172
	
173
	attr_accessor :prevtext_color
174
	
175
	attr_accessor :print_header
176
	
177
	attr_accessor :print_footer
178
	
179
	attr_accessor :state
180
	
181
	attr_accessor :tableborder
182
	
183
	attr_accessor :tdbegin
184
	
185
	attr_accessor :tdwidth
186
	
187
	attr_accessor :tdheight
188
	
189
	attr_accessor :tdalign
190
	
191
	attr_accessor :tdfill
192
	
193
	attr_accessor :tempfontsize
194
	
195
	attr_accessor :text_color
196
	
197
	attr_accessor :underline
198
	
199
	attr_accessor :ws
200
	
201
	#
202
	# This is the class constructor. 
203
	# It allows to set up the page format, the orientation and 
204
	# the measure unit used in all the methods (except for the font sizes).
205
	# @since 1.0
206
	# @param string :orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
207
	# @param string :unit User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
208
	# @param mixed :format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul>
209
	# @param boolean :unicode TRUE means that the input text is unicode (default = true)
210
	# @param String :encoding charset encoding; default is UTF-8
211
	#
212
	def initialize(orientation = 'P',  unit = 'mm', format = 'A4', unicode = true, encoding = "UTF-8")
213
		
214
		# Set internal character encoding to ASCII#
215
		#FIXME 2007-05-25 (EJM) Level=0 - 
216
		# if (respond_to?("mb_internal_encoding") and mb_internal_encoding())
217
		# 	@internal_encoding = mb_internal_encoding();
218
		# 	mb_internal_encoding("ASCII");
219
		# }
220
			
221
		#Some checks
222
		dochecks();
223
		
224
		#Initialization of properties
225
  	@barcode ||= false
226
		@buffer ||= ''
227
		@diffs ||= []
228
		@color_flag ||= false
229
  	@default_table_columns ||= 4
230
  	@table_columns ||= 0
231
  	@max_table_columns ||= []
232
  	@tr_id ||= 0
233
  	@max_td_page ||= []
234
  	@max_td_y ||= []
235
  	@t_columns ||= 0
236
  	@default_font ||= "FreeSans" if unicode
237
  	@default_font ||= "Helvetica"
238
		@draw_color ||= '0 G'
239
  	@encoding ||= "UTF-8"
240
		@fill_color ||= '0 g'
241
		@fonts ||= {}
242
		@font_family ||= ''
243
		@font_files ||= {}
244
		@font_style ||= ''
245
		@font_size ||= 12
246
		@font_size_pt ||= 12
247
  	@header_width ||= 0
248
  	@header_logo ||= ""
249
  	@header_logo_width ||= 30
250
  	@header_title ||= ""
251
  	@header_string ||= ""
252
		@images ||= {}
253
  	@img_scale ||= 1
254
		@in_footer ||= false
255
		@is_unicode = unicode
256
		@lasth ||= 0
257
		@links ||= []
258
  	@list_ordered ||= []
259
  	@list_count ||= []
260
  	@li_spacer ||= ""
261
  	@li_count ||= 0
262
  	@spacer ||= ""
263
  	@quote_count ||= 0
264
  	@prevquote_count ||= 0
265
  	@quote_top ||= []
266
  	@quote_page ||= []
267
		@n ||= 2
268
		@offsets ||= []
269
		@orientation_changes ||= []
270
		@page ||= 0
271
		@page_links ||= {}
272
		@pages ||= []
273
  	@pdf_version ||= "1.3"
274
  	@prevfill_color ||= [255,255,255]
275
  	@prevtext_color ||= [0,0,0]
276
  	@print_header ||= false
277
  	@print_footer ||= false
278
		@state ||= 0
279
  	@tableborder ||= 0
280
  	@tdbegin ||= false
281
	@tdtext ||= ''
282
  	@tdwidth ||= 0
283
  	@tdheight ||= 0
284
  	@tdalign ||= "L"
285
  	@tdfill ||= 0
286
  	@tempfontsize ||= 10
287
		@text_color ||= '0 g'
288
		@underline ||= false
289
		@deleted ||= false
290
		@ws ||= 0
291
		
292
		#Standard Unicode fonts
293
		@core_fonts = {
294
		'courier'=>'Courier',
295
		'courierB'=>'Courier-Bold',
296
		'courierI'=>'Courier-Oblique',
297
		'courierBI'=>'Courier-BoldOblique',
298
		'helvetica'=>'Helvetica',
299
		'helveticaB'=>'Helvetica-Bold',
300
		'helveticaI'=>'Helvetica-Oblique',
301
		'helveticaBI'=>'Helvetica-BoldOblique',
302
		'times'=>'Times-Roman',
303
		'timesB'=>'Times-Bold',
304
		'timesI'=>'Times-Italic',
305
		'timesBI'=>'Times-BoldItalic',
306
		'symbol'=>'Symbol',
307
		'zapfdingbats'=>'ZapfDingbats'}
308

  
309
		#Scale factor
310
		case unit.downcase
311
			when 'pt' ; @k=1
312
			when 'mm' ; @k=72/25.4
313
			when 'cm' ; @k=72/2.54
314
			when 'in' ; @k=72
315
			else Error("Incorrect unit: #{unit}")
316
		end
317

  
318
		#Page format
319
		if format.is_a?(String)
320
			# Page formats (45 standard ISO paper formats and 4 american common formats).
321
			# Paper cordinates are calculated in this way: (inches# 72) where (1 inch = 2.54 cm)
322
			case (format.upcase)
323
				when  '4A0' ; format = [4767.87,6740.79]
324
				when  '2A0' ; format = [3370.39,4767.87]
325
				when  'A0' ; format = [2383.94,3370.39]
326
				when  'A1' ; format = [1683.78,2383.94]
327
				when  'A2' ; format = [1190.55,1683.78]
328
				when  'A3' ; format = [841.89,1190.55]
329
				when  'A4' ; format = [595.28,841.89] # ; default
330
				when  'A5' ; format = [419.53,595.28]
331
				when  'A6' ; format = [297.64,419.53]
332
				when  'A7' ; format = [209.76,297.64]
333
				when  'A8' ; format = [147.40,209.76]
334
				when  'A9' ; format = [104.88,147.40]
335
				when  'A10' ; format = [73.70,104.88]
336
				when  'B0' ; format = [2834.65,4008.19]
337
				when  'B1' ; format = [2004.09,2834.65]
338
				when  'B2' ; format = [1417.32,2004.09]
339
				when  'B3' ; format = [1000.63,1417.32]
340
				when  'B4' ; format = [708.66,1000.63]
341
				when  'B5' ; format = [498.90,708.66]
342
				when  'B6' ; format = [354.33,498.90]
343
				when  'B7' ; format = [249.45,354.33]
344
				when  'B8' ; format = [175.75,249.45]
345
				when  'B9' ; format = [124.72,175.75]
346
				when  'B10' ; format = [87.87,124.72]
347
				when  'C0' ; format = [2599.37,3676.54]
348
				when  'C1' ; format = [1836.85,2599.37]
349
				when  'C2' ; format = [1298.27,1836.85]
350
				when  'C3' ; format = [918.43,1298.27]
351
				when  'C4' ; format = [649.13,918.43]
352
				when  'C5' ; format = [459.21,649.13]
353
				when  'C6' ; format = [323.15,459.21]
354
				when  'C7' ; format = [229.61,323.15]
355
				when  'C8' ; format = [161.57,229.61]
356
				when  'C9' ; format = [113.39,161.57]
357
				when  'C10' ; format = [79.37,113.39]
358
				when  'RA0' ; format = [2437.80,3458.27]
359
				when  'RA1' ; format = [1729.13,2437.80]
360
				when  'RA2' ; format = [1218.90,1729.13]
361
				when  'RA3' ; format = [864.57,1218.90]
362
				when  'RA4' ; format = [609.45,864.57]
363
				when  'SRA0' ; format = [2551.18,3628.35]
364
				when  'SRA1' ; format = [1814.17,2551.18]
365
				when  'SRA2' ; format = [1275.59,1814.17]
366
				when  'SRA3' ; format = [907.09,1275.59]
367
				when  'SRA4' ; format = [637.80,907.09]
368
				when  'LETTER' ; format = [612.00,792.00]
369
				when  'LEGAL' ; format = [612.00,1008.00]
370
				when  'EXECUTIVE' ; format = [521.86,756.00]
371
				when  'FOLIO' ; format = [612.00,936.00]
372
				#else then Error("Unknown page format: #{format}"
373
			end
374
			@fw_pt = format[0]
375
			@fh_pt = format[1]
376
		else
377
			@fw_pt = format[0]*@k
378
			@fh_pt = format[1]*@k
379
		end
380

  
381
		@fw = @fw_pt/@k
382
		@fh = @fh_pt/@k
383

  
384
		#Page orientation
385
		orientation = orientation.downcase
386
		if orientation == 'p' or orientation == 'portrait'
387
			@def_orientation = 'P'
388
			@w_pt = @fw_pt
389
			@h_pt = @fh_pt
390
		elsif orientation == 'l' or orientation == 'landscape'
391
			@def_orientation = 'L'
392
			@w_pt = @fh_pt
393
			@h_pt = @fw_pt
394
		else
395
			Error("Incorrect orientation: #{orientation}")
396
		end
397

  
398
    @fw = @w_pt/@k
399
    @fh = @h_pt/@k
400
    
401
		@cur_orientation = @def_orientation
402
		@w = @w_pt/@k
403
		@h = @h_pt/@k
404
		#Page margins (1 cm)
405
		margin = 28.35/@k
406
		SetMargins(margin, margin)
407
		#Interior cell margin (1 mm)
408
		@c_margin = margin / 10
409
		#Line width (0.2 mm)
410
		@line_width = 0.567 / @k
411
		#Automatic page break
412
		SetAutoPageBreak(true, 2 * margin)
413
		#Full width display mode
414
		SetDisplayMode('fullwidth')
415
		#Compression
416
		SetCompression(true)
417
		#Set default PDF version number
418
		@pdf_version = "1.3"
419
		
420
		@encoding = encoding
421
		@b = 0
422
		@i = 0
423
		@u = 0
424
		@href = ''
425
		@fontlist = ["arial", "times", "courier", "helvetica", "symbol"]
426
		@issetfont = false
427
		@issetcolor = false
428
	
429
		SetFillColor(200, 200, 200, true)
430
		SetTextColor(0, 0, 0, true)
431
	end
432
	
433
	#
434
	# Set the image scale.
435
	# @param float :scale image scale.
436
	# @author Nicola Asuni
437
	# @since 1.5.2
438
	#
439
	def SetImageScale(scale)
440
		@img_scale = scale;
441
	end
442
  alias_method :set_image_scale, :SetImageScale
443
	
444
	#
445
	# Returns the image scale.
446
	# @return float image scale.
447
	# @author Nicola Asuni
448
	# @since 1.5.2
449
	#
450
	def GetImageScale()
451
		return @img_scale;
452
	end
453
  alias_method :get_image_scale, :GetImageScale
454
  
455
	#
456
	# Returns the page width in units.
457
	# @return int page width.
458
	# @author Nicola Asuni
459
	# @since 1.5.2
460
	#
461
	def GetPageWidth()
462
		return @w;
463
	end
464
  alias_method :get_page_width, :GetPageWidth
465
  
466
	#
467
	# Returns the page height in units.
468
	# @return int page height.
469
	# @author Nicola Asuni
470
	# @since 1.5.2
471
	#
472
	def GetPageHeight()
473
		return @h;
474
	end
475
  alias_method :get_page_height, :GetPageHeight
476
  
477
	#
478
	# Returns the page break margin.
479
	# @return int page break margin.
480
	# @author Nicola Asuni
481
	# @since 1.5.2
482
	#
483
	def GetBreakMargin()
484
		return @b_margin;
485
	end
486
  alias_method :get_break_margin, :GetBreakMargin
487

  
488
	#
489
	# Returns the scale factor (number of points in user unit).
490
	# @return int scale factor.
491
	# @author Nicola Asuni
492
	# @since 1.5.2
493
	#
494
	def GetScaleFactor()
495
		return @k;
496
	end
497
  alias_method :get_scale_factor, :GetScaleFactor
498

  
499
	#
500
	# Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.
501
	# @param float :left Left margin.
502
	# @param float :top Top margin.
503
	# @param float :right Right margin. Default value is the left one.
504
	# @since 1.0
505
	# @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
506
	#
507
	def SetMargins(left, top, right=-1)
508
		#Set left, top and right margins
509
		@l_margin = left
510
		@t_margin = top
511
		if (right == -1)
512
			right = left
513
		end
514
		@r_margin = right
515
	end
516
  alias_method :set_margins, :SetMargins
517

  
518
	#
519
	# Defines the left margin. The method can be called before creating the first page. If the current abscissa gets out of page, it is brought back to the margin.
520
	# @param float :margin The margin.
521
	# @since 1.4
522
	# @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
523
	#
524
	def SetLeftMargin(margin)
525
		#Set left margin
526
		@l_margin = margin
527
		if ((@page>0) and (@x < margin))
528
			@x = margin
529
		end
530
	end
531
  alias_method :set_left_margin, :SetLeftMargin
532

  
533
	#
534
	# Defines the top margin. The method can be called before creating the first page.
535
	# @param float :margin The margin.
536
	# @since 1.5
537
	# @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
538
	#
539
	def SetTopMargin(margin)
540
		#Set top margin
541
		@t_margin = margin
542
	end
543
  alias_method :set_top_margin, :SetTopMargin
544

  
545
	#
546
	# Defines the right margin. The method can be called before creating the first page.
547
	# @param float :margin The margin.
548
	# @since 1.5
549
	# @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
550
	#
551
	def SetRightMargin(margin)
552
		#Set right margin
553
		@r_margin = margin
554
	end
555
  alias_method :set_right_margin, :SetRightMargin
556

  
557
	#
558
	# Enables or disables the automatic page breaking mode. When enabling, the second parameter is the distance from the bottom of the page that defines the triggering limit. By default, the mode is on and the margin is 2 cm.
559
	# @param boolean :auto Boolean indicating if mode should be on or off.
560
	# @param float :margin Distance from the bottom of the page.
561
	# @since 1.0
562
	# @see Cell(), MultiCell(), AcceptPageBreak()
563
	#
564
	def SetAutoPageBreak(auto, margin=0)
565
		#Set auto page break mode and triggering margin
566
		@auto_page_break = auto
567
		@b_margin = margin
568
		@page_break_trigger = @h - margin
569
	end
570
  alias_method :set_auto_page_break, :SetAutoPageBreak
571

  
572
	#
573
	# Defines the way the document is to be displayed by the viewer. The zoom level can be set: pages can be displayed entirely on screen, occupy the full width of the window, use real size, be scaled by a specific zooming factor or use viewer default (configured in the Preferences menu of Acrobat). The page layout can be specified too: single at once, continuous display, two columns or viewer default. By default, documents use the full width mode with continuous display.
574
	# @param mixed :zoom The zoom to use. It can be one of the following string values or a number indicating the zooming factor to use. <ul><li>fullpage: displays the entire page on screen </li><li>fullwidth: uses maximum width of window</li><li>real: uses real size (equivalent to 100% zoom)</li><li>default: uses viewer default mode</li></ul>
575
	# @param string :layout The page layout. Possible values are:<ul><li>single: displays one page at once</li><li>continuous: displays pages continuously (default)</li><li>two: displays two pages on two columns</li><li>default: uses viewer default mode</li></ul>
576
	# @since 1.2
577
	#
578
	def SetDisplayMode(zoom, layout = 'continuous')
579
		#Set display mode in viewer
580
		if (zoom == 'fullpage' or zoom == 'fullwidth' or zoom == 'real' or zoom == 'default' or !zoom.is_a?(String))
581
			@zoom_mode = zoom
582
		else
583
			Error("Incorrect zoom display mode: #{zoom}")
584
		end
585
		if (layout == 'single' or layout == 'continuous' or layout == 'two' or layout == 'default')
586
			@layout_mode = layout
587
		else
588
			Error("Incorrect layout display mode: #{layout}")
589
		end
590
	end
591
  alias_method :set_display_mode, :SetDisplayMode
592

  
593
	#
594
	# Activates or deactivates page compression. When activated, the internal representation of each page is compressed, which leads to a compression ratio of about 2 for the resulting document. Compression is on by default.
595
	# Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
596
	# @param boolean :compress Boolean indicating if compression must be enabled.
597
	# @since 1.4
598
	#
599
	def SetCompression(compress)
600
		#Set page compression
601
		if (respond_to?('gzcompress'))
602
			@compress = compress
603
		else
604
			@compress = false
605
		end
606
	end
607
  alias_method :set_compression, :SetCompression
608

  
609
	#
610
	# Defines the title of the document.
611
	# @param string :title The title.
612
	# @since 1.2
613
	# @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
614
	#
615
	def SetTitle(title)
616
		#Title of document
617
		@title = title
618
	end
619
  alias_method :set_title, :SetTitle
620

  
621
	#
622
	# Defines the subject of the document.
623
	# @param string :subject The subject.
624
	# @since 1.2
625
	# @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
626
	#
627
	def SetSubject(subject)
628
		#Subject of document
629
		@subject = subject
630
	end
631
  alias_method :set_subject, :SetSubject
632

  
633
	#
634
	# Defines the author of the document.
635
	# @param string :author The name of the author.
636
	# @since 1.2
637
	# @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
638
	#
639
	def SetAuthor(author)
640
		#Author of document
641
		@author = author
642
	end
643
  alias_method :set_author, :SetAuthor
644

  
645
	#
646
	# Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
647
	# @param string :keywords The list of keywords.
648
	# @since 1.2
649
	# @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
650
	#
651
	def SetKeywords(keywords)
652
		#Keywords of document
653
		@keywords = keywords
654
	end
655
  alias_method :set_keywords, :SetKeywords
656

  
657
	#
658
	# Defines the creator of the document. This is typically the name of the application that generates the PDF.
659
	# @param string :creator The name of the creator.
660
	# @since 1.2
661
	# @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
662
	#
663
	def SetCreator(creator)
664
		#Creator of document
665
		@creator = creator
666
	end
667
  alias_method :set_creator, :SetCreator
668

  
669
	#
670
	# Defines an alias for the total number of pages. It will be substituted as the document is closed.<br />
671
	# <b>Example:</b><br />
672
	# <pre>
673
	# class PDF extends TCPDF {
674
	# 	def Footer()
675
	# 		#Go to 1.5 cm from bottom
676
	# 		SetY(-15);
677
	# 		#Select Arial italic 8
678
	# 		SetFont('Arial','I',8);
679
	# 		#Print current and total page numbers
680
	# 		Cell(0,10,'Page '.PageNo().'/{nb}',0,0,'C');
681
	# 	end
682
	# }
683
	# :pdf=new PDF();
684
	# :pdf->alias_nb_pages();
685
	# </pre>
686
	# @param string :alias The alias. Default valuenb}.
687
	# @since 1.4
688
	# @see PageNo(), Footer()
689
	#
690
	def AliasNbPages(alias_nb ='{nb}')
691
		#Define an alias for total number of pages
692
		@alias_nb_pages = escapetext(alias_nb)
693
	end
694
	alias_method :alias_nb_pages, :AliasNbPages
695

  
696
	#
697
	# This method is automatically called in case of fatal error; it simply outputs the message and halts the execution. An inherited class may override it to customize the error handling but should always halt the script, or the resulting document would probably be invalid.
698
	# 2004-06-11 :: Nicola Asuni : changed bold tag with strong
699
	# @param string :msg The error message
700
	# @since 1.0
701
	#
702
	def Error(msg)
703
		#Fatal error
704
		raise ("TCPDF error: #{msg}")
705
	end
706
  alias_method :error, :Error
707

  
708
	#
709
	# This method begins the generation of the PDF document. It is not necessary to call it explicitly because AddPage() does it automatically.
710
	# Note: no page is created by this method
711
	# @since 1.0
712
	# @see AddPage(), Close()
713
	#
714
	def Open()
715
		#Begin document
716
		@state = 1
717
	end
718
  # alias_method :open, :Open
719

  
720
	#
721
	# Terminates the PDF document. It is not necessary to call this method explicitly because Output() does it automatically. If the document contains no page, AddPage() is called to prevent from getting an invalid document.
722
	# @since 1.0
723
	# @see Open(), Output()
724
	#
725
	def Close()
726
		#Terminate document
727
		if (@state==3)
728
			return;
729
		end
730
		if (@page==0)
731
			AddPage();
732
		end
733
		#Page footer
734
		@in_footer=true;
735
		Footer();
736
		@in_footer=false;
737
		#Close page
738
		endpage();
739
		#Close document
740
		enddoc();
741
	end
742
  # alias_method :close, :Close
743

  
744
	#
745
	# Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer. Then the page is added, the current position set to the top-left corner according to the left and top margins, and Header() is called to display the header.
746
	# The font which was set before calling is automatically restored. There is no need to call SetFont() again if you want to continue with the same font. The same is true for colors and line width.
747
	# The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
748
	# @param string :orientation Page orientation. Possible values are (case insensitive):<ul><li>P or Portrait</li><li>L or Landscape</li></ul> The default value is the one passed to the constructor.
749
	# @since 1.0
750
	# @see TCPDF(), Header(), Footer(), SetMargins()
751
	#
752
	def AddPage(orientation='')
753
		#Start a new page
754
		if (@state==0)
755
			Open();
756
		end
757
		family=@font_family;
758
		style=@font_style + (@underline ? 'U' : '') + (@deleted ? 'D' : '');
759
		size=@font_size_pt;
760
		lw=@line_width;
761
		dc=@draw_color;
762
		fc=@fill_color;
763
		tc=@text_color;
764
		cf=@color_flag;
765
		if (@page>0)
766
			#Page footer
767
			@in_footer=true;
768
			Footer();
769
			@in_footer=false;
770
			#Close page
771
			endpage();
772
		end
773
		#Start new page
774
		beginpage(orientation);
775
		#Set line cap style to square
776
		out('2 J');
777
		#Set line width
778
		@line_width = lw;
779
		out(sprintf('%.2f w', lw*@k));
780
		#Set font
781
		if (family)
782
			SetFont(family, style, size);
783
		end
784
		#Set colors
785
		@draw_color = dc;
786
		if (dc!='0 G')
787
			out(dc);
788
		end
789
		@fill_color = fc;
790
		if (fc!='0 g')
791
			out(fc);
792
		end
793
		@text_color = tc;
794
		@color_flag = cf;
795
		#Page header
796
		Header();
797
		#Restore line width
798
		if (@line_width != lw)
799
			@line_width = lw;
800
			out(sprintf('%.2f w', lw*@k));
801
		end
802
		#Restore font
803
		if (family)
804
			SetFont(family, style, size);
805
		end
806
		#Restore colors
807
		if (@draw_color != dc)
808
			@draw_color = dc;
809
			out(dc);
810
		end
811
		if (@fill_color != fc)
812
			@fill_color = fc;
813
			out(fc);
814
		end
815
		@text_color = tc;
816
		@color_flag = cf;
817
	end
818
	  alias_method :add_page, :AddPage
819
	
820
  #
821
  # Rotate object.
822
  # @param float :angle angle in degrees for counter-clockwise rotation
823
  # @param int :x abscissa of the rotation center. Default is current x position
824
  # @param int :y ordinate of the rotation center. Default is current y position
825
  #
826
  def Rotate(angle, x="", y="")
827

  
828
  	if (x == '')
829
  		x = @x;
830
  	end
831
  	
832
  	if (y == '')
833
  		y = @y;
834
  	end
835
  	
836
  	if (@rtl)
837
  		x = @w - x;
838
  		angle = -@angle;
839
  	end
840
  	
841
  	y = (@h - y) * @k;
842
  	x *= @k;
843

  
844
  	# calculate elements of transformation matrix
845
  	tm = []
846
  	tm[0] = ::Math::cos(deg2rad(angle));
847
  	tm[1] = ::Math::sin(deg2rad(angle));
848
  	tm[2] = -tm[1];
849
  	tm[3] = tm[0];
850
  	tm[4] = x + tm[1] * y - tm[0] * x;
851
  	tm[5] = y - tm[0] * y - tm[1] * x;
852

  
853
  	# generate the transformation matrix
854
  	Transform(tm);
855
  end
856
    alias_method :rotate, :Rotate
857
  
858
  #
859
	# Starts a 2D tranformation saving current graphic state.
860
	# This function must be called before scaling, mirroring, translation, rotation and skewing.
861
	# Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
862
	#
863
	def StartTransform
864
		out('q');
865
	end
866
	  alias_method :start_transform, :StartTransform
867
	
868
	#
869
	# Stops a 2D tranformation restoring previous graphic state.
870
	# This function must be called after scaling, mirroring, translation, rotation and skewing.
871
	# Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
872
	#
873
	def StopTransform
874
		out('Q');
875
	end
876
	  alias_method :stop_transform, :StopTransform
877
	
878
  #
879
	# Apply graphic transformations.
880
	# @since 2.1.000 (2008-01-07)
881
	# @see StartTransform(), StopTransform()
882
	#
883
	def Transform(tm)
884
		x = out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm', tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]));
885
	end
886
	  alias_method :transform, :Transform
887
		
888
	#
889
 	# Set header data.
890
	# @param string :ln header image logo
891
	# @param string :lw header image logo width in mm
892
	# @param string :ht string to print as title on document header
893
	# @param string :hs string to print on document header
894
	#
895
	def SetHeaderData(ln="", lw=0, ht="", hs="")
896
		@header_logo = ln || ""
897
		@header_logo_width = lw || 0
898
		@header_title = ht || ""
899
		@header_string = hs || ""
900
	end
901
	  alias_method :set_header_data, :SetHeaderData
902
	
903
	#
904
 	# Set header margin.
905
	# (minimum distance between header and top page margin)
906
	# @param int :hm distance in millimeters
907
	#
908
	def SetHeaderMargin(hm=10)
909
		@header_margin = hm;
910
	end
911
	  alias_method :set_header_margin, :SetHeaderMargin
912
	
913
	#
914
 	# Set footer margin.
915
	# (minimum distance between footer and bottom page margin)
916
	# @param int :fm distance in millimeters
917
	#
918
	def SetFooterMargin(fm=10)
919
		@footer_margin = fm;
920
	end
921
	  alias_method :set_footer_margin, :SetFooterMargin
922
	
923
	#
924
 	# Set a flag to print page header.
925
	# @param boolean :val set to true to print the page header (default), false otherwise. 
926
	#
927
	def SetPrintHeader(val=true)
928
		@print_header = val;
929
	end
930
	  alias_method :set_print_header, :SetPrintHeader
931
	
932
	#
933
 	# Set a flag to print page footer.
934
	# @param boolean :value set to true to print the page footer (default), false otherwise. 
935
	#
936
	def SetPrintFooter(val=true)
937
		@print_footer = val;
938
	end
939
	  alias_method :set_print_footer, :SetPrintFooter
940
	
941
	#
942
 	# This method is used to render the page header.
943
 	# It is automatically called by AddPage() and could be overwritten in your own inherited class.
944
	#
945
	def Header()
946
		if (@print_header)
947
			if (@original_l_margin.nil?)
948
				@original_l_margin = @l_margin;
949
			end
950
			if (@original_r_margin.nil?)
951
				@original_r_margin = @r_margin;
952
			end
953
			
954
			#set current position
955
			SetXY(@original_l_margin, @header_margin);
956
			
957
			if ((@header_logo) and (@header_logo != @@k_blank_image))
958
				Image(@header_logo, @original_l_margin, @header_margin, @header_logo_width);
959
			else
960
				@img_rb_y = GetY();
961
			end
962
			
963
			cell_height = ((@@k_cell_height_ratio * @header_font[2]) / @k).round(2)
964
			
965
			header_x = @original_l_margin + (@header_logo_width * 1.05); #set left margin for text data cell
966
			
967
			# header title
968
			SetFont(@header_font[0], 'B', @header_font[2] + 1);
969
			SetX(header_x);
970
			Cell(@header_width, cell_height, @header_title, 0, 1, 'L'); 
971
			
972
			# header string
973
			SetFont(@header_font[0], @header_font[1], @header_font[2]);
974
			SetX(header_x);
975
			MultiCell(@header_width, cell_height, @header_string, 0, 'L', 0);
976
			
977
			# print an ending header line
978
			if (@header_width)
979
				#set style for cell border
980
				SetLineWidth(0.3);
981
				SetDrawColor(0, 0, 0);
982
				SetY(1 + (@img_rb_y > GetY() ? @img_rb_y : GetY()));
983
				SetX(@original_l_margin);
984
				Cell(0, 0, '', 'T', 0, 'C'); 
985
			end
986
			
987
			#restore position
988
			SetXY(@original_l_margin, @t_margin);
989
		end
990
	end
991
	  alias_method :header, :Header
992
	
993
	#
994
 	# This method is used to render the page footer. 
995
 	# It is automatically called by AddPage() and could be overwritten in your own inherited class.
996
	#
997
	def Footer()
998
		if (@print_footer)
999
			
1000
			if (@original_l_margin.nil?)
1001
				@original_l_margin = @l_margin;
1002
			end
1003
			if (@original_r_margin.nil?)
1004
				@original_r_margin = @r_margin;
1005
			end
1006
			
1007
			#set font
1008
			SetFont(@footer_font[0], @footer_font[1] , @footer_font[2]);
1009
			#set style for cell border
1010
			line_width = 0.3;
1011
			SetLineWidth(line_width);
1012
			SetDrawColor(0, 0, 0);
1013
			
1014
			footer_height = ((@@k_cell_height_ratio * @footer_font[2]) / @k).round; #footer height, was , 2)
1015
			#get footer y position
1016
			footer_y = @h - @footer_margin - footer_height;
1017
			#set current position
1018
			SetXY(@original_l_margin, footer_y); 
1019
			
1020
			#print document barcode
1021
			if (@barcode)
1022
				Ln();
1023
				barcode_width = ((@w - @original_l_margin - @original_r_margin)).round; #max width
1024
				writeBarcode(@original_l_margin, footer_y + line_width, barcode_width, footer_height - line_width, "C128B", false, false, 2, @barcode);
1025
			end
1026
			
1027
			SetXY(@original_l_margin, footer_y); 
1028
			
1029
			#Print page number
1030
			Cell(0, footer_height, @l['w_page'] + " " + PageNo().to_s + ' / {nb}', 'T', 0, 'R'); 
1031
		end
1032
	end
1033
	  alias_method :footer, :Footer
1034
	
1035
	#
1036
	# Returns the current page number.
1037
	# @return int page number
1038
	# @since 1.0
1039
	# @see alias_nb_pages()
1040
	#
1041
	def PageNo()
1042
		#Get current page number
1043
		return @page;
1044
	end
1045
  alias_method :page_no, :PageNo
1046

  
1047
	#
1048
	# Defines the color used for all drawing operations (lines, rectangles and cell borders). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
1049
	# @param int :r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
1050
	# @param int :g Green component (between 0 and 255)
1051
	# @param int :b Blue component (between 0 and 255)
1052
	# @since 1.3
1053
	# @see SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
1054
	#
1055
	def SetDrawColor(r, g=-1, b=-1)
1056
		#Set color for all stroking operations
1057
		if ((r==0 and g==0 and b==0) or g==-1)
1058
			@draw_color=sprintf('%.3f G', r/255.0);
1059
		else
1060
			@draw_color=sprintf('%.3f %.3f %.3f RG', r/255.0, g/255.0, b/255.0);
1061
		end
1062
		if (@page>0)
1063
			out(@draw_color);
1064
		end
1065
	end
1066
  alias_method :set_draw_color, :SetDrawColor
1067

  
1068
	#
1069
	# Defines the color used for all filling operations (filled rectangles and cell backgrounds). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
1070
	# @param int :r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
1071
	# @param int :g Green component (between 0 and 255)
1072
	# @param int :b Blue component (between 0 and 255)
1073
	# @param boolean :storeprev if true stores the RGB array on :prevfill_color variable.
1074
	# @since 1.3
1075
	# @see SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
1076
	#
1077
	def SetFillColor(r, g=-1, b=-1, storeprev=false)
1078
		#Set color for all filling operations
1079
		if ((r==0 and g==0 and b==0) or g==-1)
1080
			@fill_color=sprintf('%.3f g', r/255.0);
1081
		else
1082
			@fill_color=sprintf('%.3f %.3f %.3f rg', r/255.0, g/255.0, b/255.0);
1083
		end
1084
		@color_flag=(@fill_color!=@text_color);
1085
		if (@page>0)
1086
			out(@fill_color);
1087
		end
1088
		if (storeprev)
1089
			# store color as previous value
1090
			@prevfill_color = [r, g, b]
1091
		end
1092
	end
1093
  alias_method :set_fill_color, :SetFillColor
1094

  
1095
  # This hasn't been ported from tcpdf, it's a variation on SetTextColor for setting cmyk colors
1096
	def SetCmykFillColor(c, m, y, k, storeprev=false)
1097
		#Set color for all filling operations
1098
		@fill_color=sprintf('%.3f %.3f %.3f %.3f k', c, m, y, k);
1099
		@color_flag=(@fill_color!=@text_color);
1100
		if (storeprev)
1101
			# store color as previous value
1102
			@prevtext_color = [c, m, y, k]
1103
		end
1104
		if (@page>0)
1105
			out(@fill_color);
1106
		end
1107
	end
1108
  alias_method :set_cmyk_fill_color, :SetCmykFillColor
1109

  
1110
	#
1111
	# Defines the color used for text. It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
1112
	# @param int :r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
1113
	# @param int :g Green component (between 0 and 255)
1114
	# @param int :b Blue component (between 0 and 255)
1115
	# @param boolean :storeprev if true stores the RGB array on :prevtext_color variable.
1116
	# @since 1.3
1117
	# @see SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
1118
	#
1119
	def SetTextColor(r, g=-1, b=-1, storeprev=false)
1120
		#Set color for text
1121
		if ((r==0 and :g==0 and :b==0) or :g==-1)
1122
			@text_color=sprintf('%.3f g', r/255.0);
1123
		else
1124
			@text_color=sprintf('%.3f %.3f %.3f rg', r/255.0, g/255.0, b/255.0);
1125
		end
1126
		@color_flag=(@fill_color!=@text_color);
1127
		if (storeprev)
1128
			# store color as previous value
1129
			@prevtext_color = [r, g, b]
1130
		end
1131
	end
1132
  alias_method :set_text_color, :SetTextColor
1133

  
1134
  # This hasn't been ported from tcpdf, it's a variation on SetTextColor for setting cmyk colors
1135
	def SetCmykTextColor(c, m, y, k, storeprev=false)
1136
		#Set color for text
1137
		@text_color=sprintf('%.3f %.3f %.3f %.3f k', c, m, y, k);
1138
		@color_flag=(@fill_color!=@text_color);
1139
		if (storeprev)
1140
			# store color as previous value
1141
			@prevtext_color = [c, m, y, k]
1142
		end
1143
	end
1144
  alias_method :set_cmyk_text_color, :SetCmykTextColor
1145
  
1146
	#
1147
	# Returns the length of a string in user unit. A font must be selected.<br>
1148
	# Support UTF-8 Unicode [Nicola Asuni, 2005-01-02]
1149
	# @param string :s The string whose length is to be computed
1150
	# @return int
1151
	# @since 1.2
1152
	#
1153
	def GetStringWidth(s)
1154
		#Get width of a string in the current font
1155
		s = s.to_s;
1156
		cw = @current_font['cw']
1157
		w = 0;
1158
		if (@is_unicode)
1159
      unicode = UTF8StringToArray(s);
1160
      unicode.each do |char|
1161
				if (!cw[char].nil?)
1162
					w += cw[char];
1163
				# This should not happen. UTF8StringToArray should guarentee the array is ascii values.
1164
        # elsif (c!cw[char[0]].nil?)
1165
        #   w += cw[char[0]];
1166
        #         elsif (!cw[char.chr].nil?)
1167
        #           w += cw[char.chr];
1168
				elsif (!@current_font['desc']['MissingWidth'].nil?)
1169
					w += @current_font['desc']['MissingWidth']; # set default size
1170
				else
1171
					w += 500;
1172
				end
1173
			end
1174
		else
1175
		  s.each_byte do |c|
1176
				if cw[c.chr]
1177
					w += cw[c.chr];
1178
				elsif cw[?c.chr]
1179
					w += cw[?c.chr]
1180
				end
1181
			end
1182
		end
1183
		return (w * @font_size / 1000.0);
1184
	end
1185
  alias_method :get_string_width, :GetStringWidth
1186

  
1187
	#
1188
	# Defines the line width. By default, the value equals 0.2 mm. The method can be called before the first page is created and the value is retained from page to page.
1189
	# @param float :width The width.
1190
	# @since 1.0
1191
	# @see Line(), Rect(), Cell(), MultiCell()
1192
	#
1193
	def SetLineWidth(width)
1194
		#Set line width
1195
		@line_width = width;
1196
		if (@page>0)
1197
			out(sprintf('%.2f w', width*@k));
1198
		end
1199
	end
1200
  alias_method :set_line_width, :SetLineWidth
1201

  
1202
	#
1203
	# Draws a line between two points.
1204
	# @param float :x1 Abscissa of first point
1205
	# @param float :y1 Ordinate of first point
1206
	# @param float :x2 Abscissa of second point
1207
	# @param float :y2 Ordinate of second point
1208
	# @since 1.0
1209
	# @see SetLineWidth(), SetDrawColor()
1210
	#
1211
	def Line(x1, y1, x2, y2)
1212
		#Draw a line
1213
		out(sprintf('%.2f %.2f m %.2f %.2f l S', x1 * @k, (@h - y1) * @k, x2 * @k, (@h - y2) * @k));
1214
	end
1215
  alias_method :line, :Line
1216

  
1217
  def Circle(mid_x, mid_y, radius, style='')
1218
    mid_y = (@h-mid_y)*@k
1219
    out(sprintf("q\n")) # postscript content in pdf
1220
    # init line type etc. with /GSD gs G g (grey) RG rg (RGB) w=line witdh etc. 
1221
    out(sprintf("1 j\n")) # line join
1222
    # translate ("move") circle to mid_y, mid_y
1223
    out(sprintf("1 0 0 1 %f %f cm", mid_x, mid_y))
1224
    kappa = 0.5522847498307933984022516322796
1225
    # Quadrant 1 
1226
    x_s = 0.0 # 12 o'clock 
1227
    y_s = 0.0 + radius
1228
    x_e = 0.0 + radius # 3 o'clock 
1229
    y_e = 0.0
1230
    out(sprintf("%f %f m\n", x_s, y_s)) # move to 12 o'clock 
1231
    # cubic bezier control point 1, start height and kappa * radius to the right 
1232
    bx_e1 = x_s + (radius * kappa)
1233
    by_e1 = y_s
1234
    # cubic bezier control point 2, end and kappa * radius above 
1235
    bx_e2 = x_e
1236
    by_e2 = y_e + (radius * kappa)
1237
    # draw cubic bezier from current point to x_e/y_e with bx_e1/by_e1 and bx_e2/by_e2 as bezier control points
1238
    out(sprintf("%f %f %f %f %f %f c\n", bx_e1, by_e1, bx_e2, by_e2, x_e, y_e))
1239
    # Quadrant 2 
1240
    x_s = x_e 
1241
    y_s = y_e # 3 o'clock 
1242
    x_e = 0.0 
1243
    y_e = 0.0 - radius # 6 o'clock 
1244
    bx_e1 = x_s # cubic bezier point 1 
1245
    by_e1 = y_s - (radius * kappa)
1246
    bx_e2 = x_e + (radius * kappa) # cubic bezier point 2 
1247
    by_e2 = y_e
1248
    out(sprintf("%f %f %f %f %f %f c\n", bx_e1, by_e1, bx_e2, by_e2, x_e, y_e))
1249
    # Quadrant 3 
1250
    x_s = x_e 
1251
    y_s = y_e # 6 o'clock 
1252
    x_e = 0.0 - radius
1253
    y_e = 0.0 # 9 o'clock 
1254
    bx_e1 = x_s - (radius * kappa) # cubic bezier point 1 
1255
    by_e1 = y_s
1256
    bx_e2 = x_e # cubic bezier point 2 
1257
    by_e2 = y_e - (radius * kappa)
1258
    out(sprintf("%f %f %f %f %f %f c\n", bx_e1, by_e1, bx_e2, by_e2, x_e, y_e))
1259
    # Quadrant 4 
1260
    x_s = x_e 
1261
    y_s = y_e # 9 o'clock 
1262
    x_e = 0.0 
1263
    y_e = 0.0 + radius # 12 o'clock 
1264
    bx_e1 = x_s # cubic bezier point 1 
1265
    by_e1 = y_s + (radius * kappa)
1266
    bx_e2 = x_e - (radius * kappa) # cubic bezier point 2 
1267
    by_e2 = y_e
1268
    out(sprintf("%f %f %f %f %f %f c\n", bx_e1, by_e1, bx_e2, by_e2, x_e, y_e))
1269
    if style=='F'
1270
        op='f'
1271
    elsif style=='FD' or style=='DF'
1272
        op='b'
1273
    else
1274
        op='s'
1275
    end
1276
    out(sprintf("#{op}\n")) # stroke circle, do not fill and close path 
1277
    # for filling etc. b, b*, f, f*
1278
    out(sprintf("Q\n")) # finish postscript in PDF
1279
  end
1280
  alias_method :circle, :Circle
1281

  
1282
	#
1283
	# Outputs a rectangle. It can be drawn (border only), filled (with no border) or both.
1284
	# @param float :x Abscissa of upper-left corner
1285
	# @param float :y Ordinate of upper-left corner
1286
	# @param float :w Width
1287
	# @param float :h Height
1288
	# @param string :style Style of rendering. Possible values are:<ul><li>D or empty string: draw (default)</li><li>F: fill</li><li>DF or FD: draw and fill</li></ul>
1289
	# @since 1.0
1290
	# @see SetLineWidth(), SetDrawColor(), SetFillColor()
1291
	#
1292
	def Rect(x, y, w, h, style='')
1293
		#Draw a rectangle
1294
		if (style=='F')
1295
			op='f';
1296
		elsif (style=='FD' or style=='DF')
1297
			op='B';
1298
		else
1299
			op='S';
1300
		end
1301
		out(sprintf('%.2f %.2f %.2f %.2f re %s', x * @k, (@h - y) * @k, w * @k, -h * @k, op));
1302
	end
1303
  alias_method :rect, :Rect
1304

  
1305
	#
1306
	# Imports a TrueType or Type1 font and makes it available. It is necessary to generate a font definition file first with the makefont.rb utility. The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by FPDF_FONTPATH if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
1307
	# Support UTF-8 Unicode [Nicola Asuni, 2005-01-02].
1308
	# <b>Example</b>:<br />
1309
	# <pre>
1310
	# :pdf->AddFont('Comic','I');
1311
	# # is equivalent to:
1312
	# :pdf->AddFont('Comic','I','comici.rb');
1313
	# </pre>
1314
	# @param string :family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
1315
	# @param string :style Font style. Possible values are (case insensitive):<ul><li>empty string: regular (default)</li><li>B: bold</li><li>I: italic</li><li>BI or IB: bold italic</li></ul>
1316
	# @param string :file The font definition file. By default, the name is built from the family and style, in lower case with no space.
1317
	# @since 1.5
1318
	# @see SetFont()
1319
	#
1320
	def AddFont(family, style='', file='')
1321
		if (family.empty?)
1322
			return;
1323
		end
1324

  
1325
		#Add a TrueType or Type1 font
1326
		family = family.downcase
1327
		if ((!@is_unicode) and (family == 'arial'))
1328
			family = 'helvetica';
1329
		end
1330

  
1331
		style=style.upcase
1332
		style=style.gsub('U','');
1333
		style=style.gsub('D','');
1334
		if (style == 'IB')
1335
			style = 'BI';
1336
		end
1337

  
1338
		fontkey = family + style;
1339
		# check if the font has been already added
1340
		if !@fonts[fontkey].nil?
1341
			return;
1342
		end
1343

  
1344
		if (file=='')
1345
			file = family.gsub(' ', '') + style.downcase + '.rb';
1346
		end
1347
		font_file_name = getfontpath(file)
1348
		if (font_file_name.nil?)
1349
			# try to load the basic file without styles
1350
			file = family.gsub(' ', '') + '.rb';
1351
  		font_file_name = getfontpath(file)
1352
		end
1353
    if font_file_name.nil?
1354
			Error("Could not find font #{file}.")
1355
    end
1356
		require(getfontpath(file))
1357
		font_desc = TCPDFFontDescriptor.font(file)
1358

  
1359
		if (font_desc[:name].nil? and @@fpdf_charwidths.nil?)
1360
			Error('Could not include font definition file');
1361
		end
1362

  
1363
		i = @fonts.length+1;
1364
		if (@is_unicode)
1365
			@fonts[fontkey] = {'i' => i, 'type' => font_desc[:type], 'name' => font_desc[:name], 'desc' => font_desc[:desc], 'up' => font_desc[:up], 'ut' => font_desc[:ut], 'cw' => font_desc[:cw], 'enc' => font_desc[:enc], 'file' => font_desc[:file], 'ctg' => font_desc[:ctg], 'cMap' => font_desc[:cMap], 'registry' => font_desc[:registry]}
1366
			@@fpdf_charwidths[fontkey] = font_desc[:cw];
1367
		else
1368
			@fonts[fontkey]={'i' => i, 'type'=>'core', 'name'=>@core_fonts[fontkey], 'up'=>-100, 'ut'=>50, 'cw' => font_desc[:cw]}
1369
			@@fpdf_charwidths[fontkey] = font_desc[:cw];
1370
		end
1371

  
1372
		if (!font_desc[:diff].nil? and (!font_desc[:diff].empty?))
1373
			#Search existing encodings
1374
			d=0;
1375
			nb=@diffs.length;
1376
			1.upto(nb) do |i|
1377
				if (@diffs[i]== font_desc[:diff])
1378
					d = i;
1379
					break;
1380
				end
1381
			end
1382
			if (d==0)
1383
				d = nb+1;
1384
				@diffs[d] = font_desc[:diff];
1385
			end
1386
			@fonts[fontkey]['diff'] = d;
1387
		end
1388
		if (font_desc[:file] and font_desc[:file].length > 0)
1389
			if (font_desc[:type] == "TrueType") or (font_desc[:type] == "TrueTypeUnicode")
1390
				@font_files[font_desc[:file]] = {'length1' => font_desc[:originalsize]}
1391
			else
1392
				@font_files[font_desc[:file]] = {'length1' => font_desc[:size1], 'length2' => font_desc[:size2]}
1393
			end
1394
		end
1395
	end
1396
  alias_method :add_font, :AddFont
1397

  
1398
	#
1399
	# Sets the font used to print character strings. It is mandatory to call this method at least once before printing text or the resulting document would not be valid.
1400
	# The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
1401
	# The method can be called before the first page is created and the font is retained from page to page.
1402
	# If you just wish to change the current font size, it is simpler to call SetFontSize().
1403
	# Note: for the standard fonts, the font metric files must be accessible. There are three possibilities for this:<ul><li>They are in the current directory (the one where the running script lies)</li><li>They are in one of the directories defined by the include_path parameter</li><li>They are in the directory defined by the FPDF_FONTPATH constant</li></ul><br />
1404
	# Example for the last case (note the trailing slash):<br />
1405
	# <pre>
1406
	# define('FPDF_FONTPATH','/home/www/font/');
1407
	# require('tcpdf.rb');
1408
	#
1409
	# #Times regular 12
1410
	# :pdf->SetFont('Times');
1411
	# #Arial bold 14
1412
	# :pdf->SetFont('Arial','B',14);
1413
	# #Removes bold
1414
	# :pdf->SetFont('');
1415
	# #Times bold, italic and underlined 14
1416
	# :pdf->SetFont('Times','BIUD');
1417
	# </pre><br />
1418
	# If the file corresponding to the requested font is not found, the error "Could not include font metric file" is generated.
1419
	# @param string :family Family font. It can be either a name defined by AddFont() or one of the standard families (case insensitive):<ul><li>Courier (fixed-width)</li><li>Helvetica or Arial (synonymous; sans serif)</li><li>Times (serif)</li><li>Symbol (symbolic)</li><li>ZapfDingbats (symbolic)</li></ul>It is also possible to pass an empty string. In that case, the current family is retained.
1420
	# @param string :style Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li></ul>or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats
1421
	# @param float :size Font size in points. The default value is the current size. If no size has been specified since the beginning of the document, the value taken is 12
1422
	# @since 1.0
1423
	# @see AddFont(), SetFontSize(), Cell(), MultiCell(), Write()
1424
	#
1425
	def SetFont(family, style='', size=0)
1426
		# save previous values
1427
		@prevfont_family = @font_family;
1428
		@prevfont_style = @font_style;
1429

  
1430
		family=family.downcase;
1431
		if (family=='')
1432
			family=@font_family;
1433
		end
1434
		if ((!@is_unicode) and (family == 'arial'))
1435
			family = 'helvetica';
1436
		elsif ((family=="symbol") or (family=="zapfdingbats"))
1437
			style='';
1438
		end
1439
		
1440
		style=style.upcase;
1441

  
1442
		if (style.include?('U'))
1443
			@underline=true;
1444
			style= style.gsub('U','');
1445
		else
1446
			@underline=false;
1447
		end
1448
		if (style.include?('D'))
1449
			@deleted=true;
1450
			style= style.gsub('D','');
1451
		else
1452
			@deleted=false;
1453
		end
1454
		if (style=='IB')
1455
			style='BI';
1456
		end
1457
		if (size==0)
1458
			size=@font_size_pt;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff