Class: PetitFelix::Worker::BasicPDFWriter

Inherits:
DefaultPDFWriter show all
Defined in:
lib/worker/basic_pdf_writer_classic.rb

Instance Method Summary collapse

Methods inherited from DefaultPDFWriter

#add_font, #alternates_pages, #clear_copied_page, #copy_page, #copy_page_data, #paste_page, #reorder_pages, #reorder_pages_for_2_page, #right_to_left, #set_alternate_pages, #set_options, #set_right_to_left

Instance Method Details

#back_pageObject

Draws back page



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/worker/basic_pdf_writer_classic.rb', line 135

def back_page

	if @options.key?("back_cover")
		if @options["back_cover"]
			if @options.key?("back_extra_page") && @options["back_extra_page"]
				start_new_page()
			end
		
			start_new_page()

			font "back_default"
		
			# draw end text, can be markdown
			font_size(@options["back_text_size"].to_i)
			
			margin = @options["back_text_margin"].to_i
			
			half_margin = (margin * 0.5).floor
			
			bounding_box([half_margin, cursor - 20 - half_margin], 
			width: bounds.width - margin,
			height: 300 - margin) do
				markdown(@options["back_text"], options: @options)
				#transparent(0.5) { stroke_bounds }
			end
		
			# draw art
			
			box_width = 128
			
			bounding_box([bounds.width * 0.5 - 64, cursor - 180], 
			width: bounds.width - 100 - box_width,
			height: box_width) do
			
				if @options.key?("back_cover_image")
					var = PetitFelix::Metadata.new.get_image_location(@options["image_dir"], @options["back_cover_image"])
					
					if File.file?(var)
						image(var,
						width: box_width,
						height: box_width,
						align: :center)
					end
				end
				#transparent(0.5) { stroke_bounds }
			end
			
			# draw name
			
			font "back_author"
			
			font_size(@options["back_author_size"].to_i)
			bounding_box([50, cursor - 20], 
			width: bounds.width - 100,
			height: 40) do
				if @options.key?("back_author")
					text_box(@options["back_author"],
					align: :center)
				end
				#transparent(0.5) { stroke_bounds }
			end
			
			font "back_publish"
			
			font_size(@options["back_publisher_size"].to_i)
			bounding_box([25, cursor - 60], 
			width: bounds.width - 50,
			height: 32) do
			
				text_box(@options["back_publisher"],
				align: :center)
				#transparent(0.5) { stroke_bounds }
			end
	
		end
	end
end

#initialize_fontObject

Initializes fonts



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/worker/basic_pdf_writer_classic.rb', line 14

def initialize_font

	# Fonts that must always be defined
	fonts = [
		"quote",
		"header1",
		"header2",
		"header3",
		"header4",
		"header5",
		"header6",
		"front_default", # Default title text
		"back_default", # Default title text
		"paginator",
	]
	
	fonts_special_default = {
		"front_title" => "front_default",	# Title Text
		"front_author" => "front_default",	# Author Text
		"front_date" => "front_default", # Date Text
		"front_publish" => "front_default", # Publication Text
		"back_author" => "back_default", # Publication Text
		"back_publish" => "back_default", # Publication Text
	}

	add_font "default"
	
	# Adds the fonts in the options
	fonts.each do |key|
		add_font key
		@options[key +"_font"] = key
	end
	
	# Adds special fonts with custom default fonts
	fonts_special_default.keys.each do |key|
		add_font key, default_font: fonts_special_default[key]
		@options[key +"_font"] = key
	end
	
end

#main_block(content) ⇒ Object

Draws the main block of content



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/worker/basic_pdf_writer_classic.rb', line 302

def main_block(content)
	font "default"

	margin = 25
	columns = 1
	
	if @options.key?("margin")
		margin = @options["margin"].to_i
	end
	
	if @options.key?("columns")
		columns = @options["columns"].to_i
	end

	half_margin = (margin * 0.5).floor()

	# content generation
	font_size(@options["default_font_size"].to_i)
	
	base_margin = [{
			:left => 10,
			:right => 10,
			:top => 10,
			:bottom => 30
		}]

	begin
		obj = PetitFelix::Metadata.new.parse_property @options["markdown_margin_array"], "{\"margin\" : ${x}}"
	
		base_margin = obj[:margin]
	
	rescue
		if @options.key?("markdown_margin_array")
			print "\n"
			print "Note: unable to parse argument #{@options["markdown_margin_array"]}"
		end
	end
	
	if columns == 1

		bounding_box([0, cursor],
			width: bounds.width,
			height: bounds.height,
			base_margins: base_margin) do
			markdown(content, options: @options)
		end
		
	else
	
		column_box([0, cursor],
			columns: columns,
			width: bounds.width,
			height: [bounds.height, bounds.height].min,
			base_margins: base_margin) do
			
			markdown(content, options: @options)
			
		end
	end
	
end

#outputObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/worker/basic_pdf_writer_classic.rb', line 55

def output
	fileedit = PetitFelix::FileManager.new

	file_output = fileedit.file_array_from_string(@options["output_dir"]) + [@options["output_file"]]

	file = File.join(file_output)

	FileUtils.mkdir_p File.dirname(file)
	
	render_file(file)
end

#page_numberingObject

Draws page numbering



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/worker/basic_pdf_writer_classic.rb', line 69

def page_numbering

	if @options.key?("paginator")
		if @options["paginator"]
		
			font "paginator"
		
			font_size(@options["paginator_size"].to_i)
			
			string = "#{@options["title"]}: <page>"
			
			page_start_count = @options["paginator_start_count"].to_i
			page_start = @options["paginator_start"].to_i
			
			align_odd = :left
			align_even = :right
			
			if @options["front_extra_page"]
				page_start += 1
				align_odd = :right
				align_even = :left
			end
			
			if @options["paginator_alternate"]
			
				odd_options = {
					at: [0, 0],
					width: bounds.width,
					align: align_odd,
					start_count_at: page_start_count,
				}
				
				odd_options[:page_filter] = ->(pg) { pg > page_start && pg % 2 == 1 }
				
				even_options = {
					at: [bounds.left, 0],
					width: bounds.width,
					align: align_even,
					start_count_at: page_start_count + 1,
				}
				
				even_options[:page_filter] = ->(pg) { pg > page_start && pg % 2 == 0 }
				
				number_pages string, odd_options
				number_pages string, even_options
				
			else
			
				options = {
					at: [0, -10],
					width: bounds.width,
					align: :right,
					start_count_at: page_start_count,
					color: '000000'
				}
				options[:page_filter] = ->(pg) { pg > page_start }
				
				number_pages string, options
				
			end
			
		end
	end
end

#title_pageObject

Draws title page



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/worker/basic_pdf_writer_classic.rb', line 215

def title_page
	
	if @options.key?("front_cover")
		if @options["front_cover"]
	
				font "front_title"
	
			# draw title text
			font_size(@options["front_title_size"].to_i)
			bounding_box([25, cursor ], 
			width: bounds.width - 50,
			height: 250) do
				text_box(@options["title"],
				align: :center,
				style: :bold)
				#transparent(0.5) { stroke_bounds }
			end
			
			# draw art
			
			box_width = bounds.width - 150
			
			bounding_box([50, cursor + 10], 
			width: box_width,
			height: box_width) do
			
				if @options.key?("front_cover_image")
					var = PetitFelix::Metadata.new.get_image_location(@options["image_dir"], @options["front_cover_image"])
					
					if File.file?(var)
						image(var,
						width: box_width,
						height: box_width)
					end
				end
				#transparent(0.5) { stroke_bounds }
			end
			
			font "front_author"
			
			# draw author
			
			font_size(@options["front_author_size"].to_i)
			bounding_box([50, cursor - 10], 
			width: bounds.width - 100,
			height: 30) do
				if @options.key?("author")
					text_box(@options["author"],
					align: :center)
				end
				#transparent(0.5) { stroke_bounds }
			end
			
			font "front_date"
			
			font_size(@options["front_date_size"].to_i)
			bounding_box([50, cursor], 
			width: bounds.width - 100,
			height: 30) do
				text_box("Original Publication: #{@options["date"]}",#.strftime('%e %B, %Y'),
				align: :center)
				#transparent(0.5) { stroke_bounds }
			end
			
			font "front_publish"
			
			font_size(@options["front_publisher_size"].to_i)
			bounding_box([25, cursor - 20], 
			width: bounds.width - 50,
			height: 32) do
			
				text_box(@options["front_publisher"],
				align: :center)
				#transparent(0.5) { stroke_bounds }
			end
			
			start_new_page()
			if @options.key?("front_extra_page") && @options["front_extra_page"]
				start_new_page()
			end
			
		end
	end
	
end