pdf_writing_tools 0.0.19 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pdf_writing_tools.rb +66 -40
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c9eda846aa608aa060dfcdf248785641d8157fd11a6854ca7bb28190ecff249
4
- data.tar.gz: '09d6d26696ae73858b17c6cd41a5b228e021f661d9d8d6c8182be07b0dbe3ea9'
3
+ metadata.gz: 5689acbfbb2ea127782f06799e8dfeac843c86d7116f63f60a21caec2f4bcd8c
4
+ data.tar.gz: 986bdeb9b09545b0873478f2f024c15a8bc019e4a3dd231d45d37b4379f9f8b2
5
5
  SHA512:
6
- metadata.gz: 3b3ee0196bc304ad36c7b4c08af7083b6a9b92a786b3f1a5dd500dae85278362c875822fdddb13950d232d3ba2af35794bb4491b9cdf83deb732736f523912ba
7
- data.tar.gz: 3f315a5b62258ebdb244d4b7da40135047932a5626ec2401ddcf5bb4b24e5ad74b5104fdba7909498695e2681743cd65f27792526ebbd9b3c636ff75987a9379
6
+ metadata.gz: bdea9aea430d42471a7b08509317bc3585a25c3782898f79727c386037e19bcd84357d628686e471b90626d414646c6e5d3da32ac2726a3156579fd8feea3e9d
7
+ data.tar.gz: 54b31a363356052fd7f902076e7a1fec6f7141e17fdbdd2da2ae7027ed9b0266ceb6c4b62ff0e0aabd17b30da75abccfe17642e48818a5478c1c5e2662467f7d
@@ -59,7 +59,7 @@ module PdfWritingTools
59
59
  # quello basso della pagina).
60
60
  # Bisogna pertanto prestare attenzione quando si dovessero mischiare primitive prawn
61
61
  # con queste funzioni.
62
- def self.draw_cell_fixed_height(pdf, x, y, w, h, t, opts={}, auto_y=false, no_background=false)
62
+ def self.draw_cell_fixed_height(pdf, draw_simulation, x, y, w, h, t, opts={}, auto_y=false, no_background=false)
63
63
  t = (t.class == String ? [{text: t}] : t)
64
64
  font_size = opts[:font_size] || 10
65
65
  style = opts[:style] || :normal
@@ -86,13 +86,15 @@ module PdfWritingTools
86
86
  pdf.fill_color(background_color)
87
87
 
88
88
  # Disegna lo sfondo della cella
89
- if no_background
89
+ if no_background or draw_simulation
90
90
  else
91
91
  pdf.fill_rectangle([x, y_pos1], w, h)
92
92
  end
93
93
 
94
- pdf.stroke_color border_color
95
- pdf.stroke_rectangle([x, y_pos1], w, h)
94
+ if not draw_simulation
95
+ pdf.stroke_color border_color
96
+ pdf.stroke_rectangle([x, y_pos1], w, h)
97
+ end
96
98
 
97
99
  # Colore del testo nella cella
98
100
  pdf.fill_color(font_color)
@@ -100,7 +102,23 @@ module PdfWritingTools
100
102
  at = [x + left_padding, y_pos2]
101
103
  width = w - left_padding - right_padding
102
104
  height = h - top_padding - bottom_padding + 1.cm
103
- result = pdf.formatted_text_box(t, width: width, height: height, at: at, size: font_size, style: style, align: align, valign: valign)
105
+ #result = pdf.formatted_text_box(t, width: width, height: height, at: at, size: font_size, style: style, align: align, valign: valign)
106
+
107
+ b = Prawn::Text::Formatted::Box.new(
108
+ t,
109
+ {
110
+ width: width,
111
+ height: height,
112
+ at: at,
113
+ size: font_size,
114
+ style: style,
115
+ align: align,
116
+ valign: valign,
117
+ document: pdf
118
+ }
119
+ )
120
+
121
+ result = b.render(:dry_run => draw_simulation)
104
122
  end
105
123
 
106
124
  result
@@ -183,9 +201,7 @@ module PdfWritingTools
183
201
  end
184
202
  # ... e infine, sopra lo sfondo disegno il testo
185
203
  pdf.fill_color(font_color) # colore del testo
186
- text_overflow = b.render()
187
-
188
- # text_overflow è l'eventuale testo avanzato
204
+ text_overflow = b.render() # text_overflow è l'eventuale testo avanzato
189
205
  end
190
206
 
191
207
  # La cella potrebbe non riuscire ad espandersi a sufficienza (troppo vicina al
@@ -237,7 +253,7 @@ module PdfWritingTools
237
253
  cell_opts[:pdf_width] = 21.cm
238
254
  cell_opts[:pdf_height] = 297.mm
239
255
 
240
- draw_cell_fixed_height( pdf, x+offset, y, width, height, texts[i], cell_opts, auto_y)
256
+ draw_cell_fixed_height( pdf, false, x+offset, y, width, height, texts[i], cell_opts, auto_y)
241
257
 
242
258
  offset += width
243
259
  end
@@ -263,7 +279,13 @@ module PdfWritingTools
263
279
 
264
280
  new_y = pdf_margin_top
265
281
 
266
- overflowed = false
282
+
283
+ # Se la y della nuova riga (angolo superiore sinistro), esce dal margine di pagina,
284
+ # non posso disegnare la riga in questa pagina, ma devo passare alla successiva
285
+ if (y) > (pdf_height - pdf_margin_bottom)
286
+ pdf.start_new_page
287
+ y = pdf_margin_top
288
+ end
267
289
 
268
290
  loop do
269
291
  max_cell_height = 0
@@ -273,7 +295,7 @@ module PdfWritingTools
273
295
  # In questo modo, posso sapere quale sarà la massima altezza raggiunta da una
274
296
  # cella, ossia l'altezza che deve avere la riga
275
297
  offset = 0
276
- widths.each_with_index do |width, i|
298
+ widths.each_with_index do |width, i|
277
299
  font_size = font_sizes[i] || font_sizes[0]
278
300
  style = styles[i] || styles[0]
279
301
  align = alignments[i] || alignments[0]
@@ -298,32 +320,37 @@ module PdfWritingTools
298
320
  cell_opts[:pdf_height] = 297.mm
299
321
  cell_opts[:pdf_weigth] = 21.cm
300
322
 
301
- r = draw_cell_auto_height(pdf, draw_simulation = true, x+offset, y, width, texts[i], cell_opts, auto_y)
302
-
303
- if r[:overflow] && r[:overflow] == ""
304
- overflowed = true
305
- end
323
+ r = draw_cell_auto_height(pdf, true, x+offset, y, width, texts[i], cell_opts, auto_y)
306
324
 
307
325
 
308
326
  if r[:height] > max_cell_height
309
327
  max_cell_height = r[:height]
310
328
  end
311
329
 
312
- offset += width
330
+ offset += width
313
331
  end
314
332
 
315
- # Non voglio che la riga ecceda il margine di pagina, quindi se così fosse
316
- # ricalcolo l'altezza di riga per non superare tale margine
317
- if y + max_cell_height > pdf_height - pdf_margin_bottom
318
- overflowed = true
319
- #max_cell_height = pdf_height - pdf_margin_bottom - y
320
- end
321
333
 
334
+ # Non voglio che la riga ecceda il margine di pagina...
335
+ if (y + max_cell_height) > (pdf_height - pdf_margin_bottom)
336
+ q = 1
322
337
 
323
- if overflowed
324
- pdf.start_new_page
325
- y = pdf_margin_top # Posiziono il cursore ad inizio della nuova pagina
326
- end
338
+ # Se fino al margine di pagina, non ho almeno q cm... **
339
+ if (pdf_height - pdf_margin_bottom - y) < q.cm
340
+ # ...cambio pagina...
341
+ pdf.start_new_page
342
+ # ... e mi riposiziono all'inizio della pagina nuova
343
+ y = pdf_margin_top
344
+ else
345
+ # ... altrimenti, reimposto max_cell_height: la riga verrà spezzata su due pagine.
346
+ max_cell_height = pdf_height - pdf_margin_bottom - y
347
+ end
348
+
349
+ # ** non voglio imporre il vincolo "una riga deve stare in una pagina", ma contemporaneamente, voglio evitare che
350
+ # si creino delle righe "sottili" sul bordo inferiore della pagina; così sottili che non vi si possa disegnare testo
351
+ # e quindi appaiano vuote. Questo controllo non è risolutivo: x, dovrebbe dipendere dalla dimensione del carattere
352
+ # usato per il testo della riga, ma in questa fase, x.cm = 1.cm, mi pare una stima sufficiente
353
+ end
327
354
 
328
355
  # A seguito della simulazione, ho ottenuto l'altezza di riga...
329
356
  # ... ora passo al disegno vero e proprie delle celle...
@@ -356,7 +383,7 @@ module PdfWritingTools
356
383
 
357
384
  # ...disegno le celle vere e proprie ad altezza fissa, con l'altezza
358
385
  # ricavata dal passo precedente.
359
- r = draw_cell_fixed_height(pdf, x+offset, y, width, max_cell_height, texts[i], cell_opts, auto_y)
386
+ r = draw_cell_fixed_height(pdf, draw_simulation, x+offset, y, width, max_cell_height, texts[i], cell_opts, auto_y)
360
387
 
361
388
  # Gli eventuali "testi residui" (es. raggiunto fine pagina) li raccolgo
362
389
  # nel seguente array
@@ -367,21 +394,20 @@ module PdfWritingTools
367
394
 
368
395
  texts = rtexts
369
396
 
370
- if false
371
- # Se nei testi residui, sono presenti solo stringhe vuote, posso uscire dal
372
- # loop, altrimenti, devo cambiare pagina e disegnare una nuova riga con i
373
- # testi rimanenti
374
- if !check_no_empty_string_presence(texts)
375
- new_y = y + max_cell_height
376
- break
377
- else
378
- pdf.start_new_page
379
- y = pdf_margin_top # Posiziono il cursore ad inizio della nuova pagina
380
- end
397
+ # Se nei testi residui, sono presenti solo stringhe vuote, posso uscire dal
398
+ # loop, altrimenti, devo cambiare pagina e disegnare una nuova riga con i
399
+ # testi rimanenti
400
+ if !check_no_empty_string_presence(texts)
401
+ new_y = y + max_cell_height
402
+ break
403
+ else
404
+ pdf.start_new_page
405
+ y = pdf_margin_top # Posiziono il cursore ad inizio della nuova pagina
381
406
  end
382
407
  end
408
+
383
409
 
384
- pdf.y = (pdf_height - new_y)
410
+ #pdf.y = (pdf_height - new_y)
385
411
 
386
412
  return new_y
387
413
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf_writing_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - JSalvo1978
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-08 00:00:00.000000000 Z
11
+ date: 2024-01-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Plugin for write pdf in a simplified manner, using prawn as backend
14
14
  email: