barby 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eebdb2eff35d0984c66ce11079fd6a524c95ae08
4
+ data.tar.gz: 03d708dff70b1b71f0117e9c23e6bbd1c00f5915
5
+ SHA512:
6
+ metadata.gz: 5bfe166db311575c6da4f69d95135443f47529b5eef5f23bdb947fd7076e60f77495e54f7559a3fce24bd7c062db2eb344b6b2f354915974ecc238f23763a019
7
+ data.tar.gz: e183f8086780beb20e4c0220024262a93d1f3bccb6e522cf7633bfb66831702051d61e17a727e3314b85f73c3ffbe365aeab5e46d9dcb21c0cab2275874ce892
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ * 0.6
2
+
3
+ * Add automatic character set to Code128
4
+ * Fix test for HtmlOutputter
5
+
1
6
  * 0.5.1
2
7
 
3
8
  * Fix some encoding issues (ruby 2.0 & 1.9)
@@ -20,6 +20,21 @@ module Barby
20
20
  # Code128A.new("ABC123\306def\3074567")
21
21
  class Code128 < Barcode1D
22
22
 
23
+ FNC1 = "\xc1"
24
+ FNC2 = "\xc2"
25
+ FNC3 = "\xc3"
26
+ FNC4 = "\xc4"
27
+ CODEA = "\xc5"
28
+ CODEB = "\xc6"
29
+ CODEC = "\xc7"
30
+ SHIFT = "\xc8"
31
+ STARTA = "\xc9"
32
+ STARTB = "\xca"
33
+ STARTC = "\xcb"
34
+
35
+ STOP = '11000111010'
36
+ TERMINATE = '11'
37
+
23
38
  ENCODINGS = {
24
39
  0 => "11011001100", 1 => "11001101100", 2 => "11001100110",
25
40
  3 => "10010011000", 4 => "10010001100", 5 => "10001001100",
@@ -93,10 +108,10 @@ module Barby
93
108
  87 => "\027", 88 => "\030", 89 => "\031",
94
109
  90 => "\032", 91 => "\e", 92 => "\034",
95
110
  93 => "\035", 94 => "\036", 95 => "\037",
96
- 96 => "\303", 97 => "\302", 98 => "SHIFT",
97
- 99 => "\307", 100 => "\306", 101 => "\304",
98
- 102 => "\301", 103 => "STARTA", 104 => "STARTB",
99
- 105 => "STARTC"
111
+ 96 => FNC3, 97 => FNC2, 98 => SHIFT,
112
+ 99 => CODEC, 100 => CODEB, 101 => FNC4,
113
+ 102 => FNC1, 103 => STARTA, 104 => STARTB,
114
+ 105 => STARTC
100
115
  }.invert,
101
116
 
102
117
  'B' => {
@@ -116,9 +131,9 @@ module Barby
116
131
  78 => "n", 79 => "o", 80 => "p", 81 => "q", 82 => "r", 83 => "s",
117
132
  84 => "t", 85 => "u", 86 => "v", 87 => "w", 88 => "x", 89 => "y",
118
133
  90 => "z", 91 => "{", 92 => "|", 93 => "}", 94 => "~", 95 => "\177",
119
- 96 => "\303", 97 => "\302", 98 => "SHIFT", 99 => "\307", 100 => "\304",
120
- 101 => "\305", 102 => "\301", 103 => "STARTA", 104 => "STARTB",
121
- 105 => "STARTC",
134
+ 96 => FNC3, 97 => FNC2, 98 => SHIFT, 99 => CODEC, 100 => FNC4,
135
+ 101 => CODEA, 102 => FNC1, 103 => STARTA, 104 => STARTB,
136
+ 105 => STARTC,
122
137
  }.invert,
123
138
 
124
139
  'C' => {
@@ -138,28 +153,23 @@ module Barby
138
153
  78 => "78", 79 => "79", 80 => "80", 81 => "81", 82 => "82", 83 => "83",
139
154
  84 => "84", 85 => "85", 86 => "86", 87 => "87", 88 => "88", 89 => "89",
140
155
  90 => "90", 91 => "91", 92 => "92", 93 => "93", 94 => "94", 95 => "95",
141
- 96 => "96", 97 => "97", 98 => "98", 99 => "99", 100 => "\306", 101 => "\305",
142
- 102 => "\301", 103 => "STARTA", 104 => "STARTB", 105 => "STARTC"
156
+ 96 => "96", 97 => "97", 98 => "98", 99 => "99", 100 => CODEB, 101 => CODEA,
157
+ 102 => FNC1, 103 => STARTA, 104 => STARTB, 105 => STARTC
143
158
  }.invert
144
159
  }
145
160
 
146
- FNC1 = "\xc1"
147
- FNC2 = "\xc2"
148
- FNC3 = "\xc3"
149
- FNC4 = "\xc4"
150
- CODEA = "\xc5"
151
- CODEB = "\xc6"
152
- CODEC = "\xc7"
153
-
154
- STOP = '11000111010'
155
- TERMINATE = '11'
161
+ CONTROL_CHARACTERS = VALUES['A'].invert.values_at(*(64..95).to_a)
156
162
 
157
163
  attr_reader :type
158
164
 
159
-
160
- def initialize(data, type)
161
- self.type = type
162
- self.data = "#{data}"
165
+
166
+ def initialize(data, type=nil)
167
+ if type
168
+ self.type = type
169
+ self.data = "#{data}"
170
+ else
171
+ self.type, self.data = self.class.determine_best_type_for_data("#{data}")
172
+ end
163
173
  raise ArgumentError, 'Data not valid' unless valid?
164
174
  end
165
175
 
@@ -350,14 +360,7 @@ module Barby
350
360
  end
351
361
 
352
362
  def class_for(character)
353
- case character
354
- when 'A' then Code128A
355
- when 'B' then Code128B
356
- when 'C' then Code128C
357
- when CODEA then Code128A
358
- when CODEB then Code128B
359
- when CODEC then Code128C
360
- end
363
+ self.class.class_for(character)
361
364
  end
362
365
 
363
366
  #Is the data in this barcode valid? Does a lookup of every character
@@ -371,8 +374,16 @@ module Barby
371
374
  VALUES[type]
372
375
  end
373
376
 
377
+ def start_character
378
+ case type
379
+ when 'A' then STARTA
380
+ when 'B' then STARTB
381
+ when 'C' then STARTC
382
+ end
383
+ end
384
+
374
385
  def start_num
375
- values["START#{type}"]
386
+ values[start_character]
376
387
  end
377
388
 
378
389
  def start_encoding
@@ -380,7 +391,153 @@ module Barby
380
391
  end
381
392
 
382
393
 
383
- end
394
+
395
+ CTRL_RE = /#{CONTROL_CHARACTERS.join('|')}/
396
+ LOWR_RE = /[a-z]/
397
+ DGTS_RE = /\d{4,}/
398
+
399
+ class << self
400
+
401
+
402
+ def class_for(character)
403
+ case character
404
+ when 'A' then Code128A
405
+ when 'B' then Code128B
406
+ when 'C' then Code128C
407
+ when CODEA then Code128A
408
+ when CODEB then Code128B
409
+ when CODEC then Code128C
410
+ end
411
+ end
412
+
413
+
414
+ #Insert code shift and switch characters where appropriate to get the
415
+ #shortest encoding possible
416
+ def apply_shortest_encoding_for_data(data)
417
+ extract_codec(data).map do |block|
418
+ if possible_codec_segment?(block)
419
+ "#{CODEC}#{block}"
420
+ else
421
+ if control_before_lowercase?(block)
422
+ handle_code_a(block)
423
+ else
424
+ handle_code_b(block)
425
+ end
426
+ end
427
+ end.join
428
+ end
429
+
430
+ def determine_best_type_for_data(data)
431
+ data = apply_shortest_encoding_for_data(data)
432
+ type = case data.slice!(0)
433
+ when CODEA then 'A'
434
+ when CODEB then 'B'
435
+ when CODEC then 'C'
436
+ end
437
+ [type, data]
438
+ end
439
+
440
+
441
+ private
442
+
443
+ #Extract all CODEC segments from the data. 4 or more evenly numbered contiguous digits.
444
+ #
445
+ # # C A or B C A or B
446
+ # extract_codec("12345abc678910DEF11") => ["1234", "5abc", "678910", "DEF11"]
447
+ def extract_codec(data)
448
+ segments = data.split(/(\d{4,})/).reject(&:empty?)
449
+ segments.each_with_index do |s,i|
450
+ if possible_codec_segment?(s) && s.size.odd?
451
+ if i == 0
452
+ if segments[1]
453
+ segments[1].insert(0, s.slice!(-1))
454
+ else
455
+ segments[1] = s.slice!(-1)
456
+ end
457
+ else
458
+ segments[i-1].insert(-1, s.slice!(0)) if segments[i-1]
459
+ end
460
+ end
461
+ end
462
+ segments
463
+ end
464
+
465
+ def possible_codec_segment?(data)
466
+ data =~ /\A\d{4,}\Z/
467
+ end
468
+
469
+ def control_character?(char)
470
+ char =~ CTRL_RE
471
+ end
472
+
473
+ def lowercase_character?(char)
474
+ char =~ LOWR_RE
475
+ end
476
+
477
+
478
+ #Handle a Code A segment which may contain Code B parts, but may not
479
+ #contain any Code C parts.
480
+ def handle_code_a(data)
481
+ indata = data.dup
482
+ outdata = CODEA.dup #We know it'll be A
483
+ while char = indata.slice!(0)
484
+ if lowercase_character?(char) #Found a lower case character (Code B)
485
+ if control_before_lowercase?(indata)
486
+ outdata << SHIFT << char #Control character appears before a new lowercase, use shift
487
+ else
488
+ outdata << handle_code_b(char+indata) #Switch to Code B
489
+ break
490
+ end
491
+ else
492
+ outdata << char
493
+ end
494
+ end#while
495
+
496
+ outdata
497
+ end
498
+
499
+
500
+ #Handle a Code B segment which may contain Code A parts, but may not
501
+ #contain any Code C parts.
502
+ def handle_code_b(data)
503
+ indata = data.dup
504
+ outdata = CODEB.dup #We know this is going to start with Code B
505
+ while char = indata.slice!(0)
506
+ if control_character?(char) #Found a control character (Code A)
507
+ if control_before_lowercase?(indata) #There is another control character before any lowercase, so
508
+ outdata << handle_code_a(char+indata) #switch over to Code A.
509
+ break
510
+ else
511
+ outdata << SHIFT << char #Can use a shift to only encode this char as Code A
512
+ end
513
+ else
514
+ outdata << char
515
+ end
516
+ end#while
517
+
518
+ outdata
519
+ end
520
+
521
+
522
+ #Test str to see if a control character (Code A) appears
523
+ #before a lower case character (Code B).
524
+ #
525
+ #Returns true only if it contains a control character and a lower case
526
+ #character doesn't appear before it.
527
+ def control_before_lowercase?(str)
528
+ ctrl = str =~ CTRL_RE
529
+ char = str =~ LOWR_RE
530
+
531
+ ctrl && (!char || ctrl < char)
532
+ end
533
+
534
+
535
+
536
+ end#class << self
537
+
538
+
539
+
540
+ end#class Code128
384
541
 
385
542
 
386
543
  class Code128A < Code128
@@ -1,8 +1,8 @@
1
1
  module Barby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 5
5
- TINY = 1
4
+ MINOR = 6
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
5
- prerelease:
4
+ version: 0.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tore Darell
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-12 00:00:00.000000000 Z
11
+ date: 2014-05-28 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Barby creates barcodes.
15
14
  email: toredarell@gmail.com
@@ -19,8 +18,11 @@ extra_rdoc_files:
19
18
  - README
20
19
  files:
21
20
  - CHANGELOG
22
- - README
23
21
  - LICENSE
22
+ - README
23
+ - bin/barby
24
+ - lib/barby.rb
25
+ - lib/barby/barcode.rb
24
26
  - lib/barby/barcode/bookland.rb
25
27
  - lib/barby/barcode/code_128.rb
26
28
  - lib/barby/barcode/code_25.rb
@@ -35,7 +37,7 @@ files:
35
37
  - lib/barby/barcode/pdf_417.rb
36
38
  - lib/barby/barcode/qr_code.rb
37
39
  - lib/barby/barcode/upc_supplemental.rb
38
- - lib/barby/barcode.rb
40
+ - lib/barby/outputter.rb
39
41
  - lib/barby/outputter/ascii_outputter.rb
40
42
  - lib/barby/outputter/cairo_outputter.rb
41
43
  - lib/barby/outputter/html_outputter.rb
@@ -45,50 +47,39 @@ files:
45
47
  - lib/barby/outputter/prawn_outputter.rb
46
48
  - lib/barby/outputter/rmagick_outputter.rb
47
49
  - lib/barby/outputter/svg_outputter.rb
48
- - lib/barby/outputter.rb
49
50
  - lib/barby/vendor.rb
50
51
  - lib/barby/version.rb
51
- - lib/barby.rb
52
52
  - vendor/Pdf417lib-java-0.91/lib/Pdf417lib.jar
53
53
  - vendor/Pdf417lib-java-0.91/lib/Pdf417lib.java
54
- - bin/barby
55
54
  homepage: http://toretore.github.com/barby
56
55
  licenses: []
57
- post_install_message: ! '
56
+ metadata: {}
57
+ post_install_message: |2+
58
58
 
59
59
  *** NEW REQUIRE POLICY ***"
60
-
61
- Barby no longer require all barcode symbologies by default. You''ll have
62
-
60
+ Barby no longer require all barcode symbologies by default. You'll have
63
61
  to require the ones you need. For example, if you need EAN-13,
64
-
65
- require ''barby/barcode/ean_13''; For a full list of symbologies and their
66
-
62
+ require 'barby/barcode/ean_13'; For a full list of symbologies and their
67
63
  filenames, see README.
68
-
69
64
  ***
70
65
 
71
-
72
- '
73
66
  rdoc_options: []
74
67
  require_paths:
75
68
  - lib
76
69
  required_ruby_version: !ruby/object:Gem::Requirement
77
- none: false
78
70
  requirements:
79
- - - ! '>='
71
+ - - ">="
80
72
  - !ruby/object:Gem::Version
81
73
  version: '0'
82
74
  required_rubygems_version: !ruby/object:Gem::Requirement
83
- none: false
84
75
  requirements:
85
- - - ! '>='
76
+ - - ">="
86
77
  - !ruby/object:Gem::Version
87
78
  version: '0'
88
79
  requirements: []
89
80
  rubyforge_project: barby
90
- rubygems_version: 1.8.23
81
+ rubygems_version: 2.2.2
91
82
  signing_key:
92
- specification_version: 3
83
+ specification_version: 4
93
84
  summary: The Ruby barcode generator
94
85
  test_files: []