forked from antonscheffer/as_barcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas_barcode.pkb
2497 lines (2493 loc) · 89.5 KB
/
as_barcode.pkb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
133
134
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
212
213
214
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
300
301
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
create or replace package body as_barcode
is
/**********************************************
**
** Author: Anton Scheffer
**
** Changelog:
** Date: 2016-12-14
** beta release
** Date: 2017-02-11
** version 0.20
** Date: 2017-08-15
** fixed bug in used png format
******************************************************************************
******************************************************************************
Copyright (C) 2016 by Anton Scheffer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
******************************************************************************
******************************************** */
type tp_ean_bars is table of raw(7) index by pls_integer;
type tp_bits is table of pls_integer index by pls_integer;
--
procedure save_raw
( p_file raw
, p_dir varchar2 := 'HOME'
, p_filename varchar2 := 'test.png'
)
is
t_fh utl_file.file_type;
t_len pls_integer := 32767;
begin
t_fh := utl_file.fopen( p_dir, p_filename, 'wb' );
utl_file.put_raw( t_fh, p_file );
utl_file.fclose( t_fh );
end;
--
function generate_png( p_dat blob, p_width pls_integer, p_height pls_integer )
return raw
is
t_ihdr raw(25);
t_idat raw(32767);
--
function crc32( p_src raw )
return raw
is
begin
return utl_raw.reverse( utl_raw.substr( utl_raw.substr( utl_compress.lz_compress( p_src ), -8 ), 1, 4 ) );
end;
--
function method0_compress( p_val blob )
return raw
is
t_tmp raw(32767);
function adler32( p_val blob )
return varchar2
is
s1 pls_integer := 1;
s2 pls_integer := 0;
t_val varchar2(32766);
t_pos number := 1;
t_len number := dbms_lob.getlength( p_val );
begin
loop
exit when t_pos > t_len;
t_val := rawtohex( dbms_lob.substr( p_val, 16383, t_pos ) );
for i in 1 .. length( t_val ) / 2
loop
begin
s1 := s1 + to_number( substr( t_val, i * 2 - 1, 2 ), 'XX' );
exception
when others then
s1 := mod( s1, 65521 ) + to_number( substr( t_val, i * 2 - 1, 2 ), 'XX' );
end;
begin
s2 := s2 + s1;
exception
when others then
s2 := mod( s2, 65521 ) + s1;
end;
end loop;
t_pos := t_pos + 16383;
end loop;
s1 := mod( s1, 65521 );
s2 := mod( s2, 65521 );
return to_char( s2, 'fm0XXX' ) || to_char( s1, 'fm0XXX' );
end;
begin
t_tmp := utl_compress.lz_compress( p_val );
return utl_raw.concat( '789C'
, utl_raw.substr( t_tmp, 11, utl_raw.length( t_tmp ) - 18 )
, adler32( p_val )
);
end;
--
begin
t_ihdr := utl_raw.concat( '49484452' -- IHDR
, to_char( p_width, 'fm0XXXXXXX' )
, to_char( p_height, 'fm0XXXXXXX' )
, '0800000000' -- Bit depth 8
-- Colour type 0
-- Compression method 0
-- Filter method 0
-- Interlace method 0
);
t_idat := utl_raw.concat( '49444154' -- IDAT
, method0_compress( p_dat )
);
return utl_raw.concat( '89504E470D0A1A0A' -- signature
, '0000000D'
, t_ihdr
, crc32( t_ihdr )
, to_char( utl_raw.length( t_idat ) - 4, 'fm0XXXXXXX' )
, t_idat
, crc32( t_idat )
, '0000000049454E44AE426082' -- IEND
);
end;
--
function bitxor( x number, y number )
return number
is
begin
return x + y - 2 * bitand( x, y );
end;
--
function reed_solomon
( p_data tp_bits
, p_primitive pls_integer := 285
, p_size pls_integer := 256
, p_degree pls_integer := 16
, p_b pls_integer := 1
)
return tp_bits
is
type tp_ecc is table of pls_integer index by pls_integer;
t_exp tp_ecc;
t_log tp_ecc;
t_g tp_ecc;
t_ecc tp_ecc;
t_x pls_integer;
t_rv tp_bits;
--
begin
t_x := 1;
for i in 0 .. p_size - 1
loop
t_exp( i ) := t_x;
t_x := t_x * 2;
if t_x >= p_size
then
t_x := bitand( p_size - 1, bitxor( p_primitive, t_x ) );
end if;
end loop;
for i in 0 .. p_size - 2
loop
t_log( t_exp( i ) ) := i;
end loop;
--
t_g(0) := 1;
for i in 1 .. p_degree
loop
t_x := i - 1 + p_b;
t_g(i) := t_exp( mod( t_log( t_g( i-1 ) ) + t_x, p_size - 1 ) );
for j in reverse 1 .. i - 1
loop
t_g(j) := bitxor( t_exp( mod( t_log(t_g(j-1)) + t_x, p_size - 1 ) )
, t_g(j)
);
end loop;
-- t_log( 1 ) is altijd 0
-- t_g(0) := bitxor( 0, t_exp( t_log( t_g( 0 ) ) + t_log( 1 ) ) );
end loop;
--
t_x := p_data.first;
for i in t_x .. p_data.last
loop
t_ecc( i - t_x ) := p_data( i );
end loop;
for i in t_ecc.count .. t_ecc.count + p_degree - 1
loop
t_ecc( i ) := 0;
end loop;
--
while t_ecc.count >= t_g.count
loop
t_x := t_ecc( t_ecc.first );
if t_x > 0 then
for i in 0 .. t_g.count - 1
loop
t_ecc( t_ecc.first + i ) := bitxor( t_ecc( t_ecc.first + i )
, t_exp( mod( t_log( t_g( i ) ) + t_log( t_x ), p_size - 1 ) )
);
end loop;
end if;
t_ecc.delete( t_ecc.first );
end loop;
--
t_x := t_ecc.first;
for i in t_ecc.first .. t_ecc.last
loop
t_rv( i - t_x ) := t_ecc( i );
end loop;
return t_rv;
end;
--
function reed_solomon
( p_data raw
, p_primitive pls_integer := 285
, p_size pls_integer := 256
, p_degree pls_integer := 16
, p_b pls_integer := 0
)
return raw
is
t_bits tp_bits;
t_rv raw(3000);
begin
for i in 1 .. utl_raw.length( p_data )
loop
t_bits( i - 1 ) := to_number( utl_raw.substr( p_data, i, 1 ), 'xx' );
end loop;
t_bits := reed_solomon( t_bits, p_primitive, p_size, p_degree, p_b );
for i in t_bits.first .. t_bits.last
loop
t_rv := utl_raw.concat( t_rv, to_char( t_bits( i ), 'fm0X' ) );
end loop;
return t_rv;
end;
--
-- 7x9 font, containing 0,1,2,3,4,5,6,7,8,9,<,>
function number_font
return raw
is
t_def varchar2(150) :=
'H4sIAAAAAAAACyVMIRIAQQgqBqPhwkWfQdhA8FEXfPyJy4yIIqIuEO8TQqPIDLcV' ||
'IAvym4M2szP9oEYoNevMCHcdbYk+pfaJR3JJww8KA+wEbAAAAA==';
t_tmp raw(200);
t_fnt raw(32767);
t_line pls_integer;
begin
t_tmp := utl_compress.lz_uncompress( utl_encode.base64_decode( utl_raw.cast_to_raw( t_def ) ) );
for c in 0 .. 11
loop
for i in 1 .. 9
loop
t_line := to_number( utl_raw.substr( t_tmp, c * 9 + i, 1 ), 'XX' );
for j in 1 .. 7
loop
t_fnt := utl_raw.concat( t_fnt
, case when bitand( t_line, power( 2, j ) ) = 0 then 'FF' else '00' end
);
end loop;
end loop;
end loop;
return t_fnt;
end;
--
-- a 8x14 font, containing codepage 1252, which happens to be a superset of ISO-8859-1
function cp1252_font
return raw
is
t_def varchar2(2000) :=
'H4sIAAAAAAAAC+1WMYslRRAehkEGWZZmogmWY3h08AKDh9EgzTKMzTIsi6yLl4k8' ||
'FgODC+SiRYbBe6EYDMMlJ7LgworuPzgwMrjfYeAvMBI8Dquqq7ur3zvNBWv3bt73' ||
'qqaqvqqurs2yVJQTeALQWksdwIl+NRmOyzyPVbWMaPv58y9KpZr3ri7Qsta63pxb' ||
'az9wLqWXUjUkqkQEDxLV+CD1VMeoECV9G7/yptm0l7zLOiuKEnNpug6R6fuz86vr' ||
'vjcUvV67gKesywuI3U0eneQ5W+bvvFut+inPcwrVdd0In53uBHyPfc+WYONCKvbS' ||
'm6CjT6dg0MoUAydGxIgSgXoUkV0gSN+j3vuEeFlZ7vP/v3//vf6Nu9u7u7sfv/va' ||
'c+j7Cf8jHTwpz5F1wKLjzG5sT2JvPL8b1E4J6sJ7H3lGIQBFaPnAKOJ3XJBYS6i3' ||
'w+V2ezlYsuy8UITdq4dl2aGwz+ur8zP2aXoW4zn0o8iF5NzkQecjGEjThP698Zkp' ||
'n3X0SZ/gHNc15YKyLA+vOBf8XmuXy25n7Tp4wU5jFxyHtmFpHT8403CaC+pfW7Kg' ||
'Tq1t9laZMnU4gyY/xTPDNSOKrn9MsePzmZMdW7rSQN9Id0xzo+LksWVuhE/iBxab' ||
'2D/I/Sh2EEPZ4Uuu7ibpNMjrhYSR8BnaZBLd6DoYcqG5yj7bhnOGOurg6DJjDses' ||
'6xO2vesTI+zfsoxBp2sdcukjdyx5nPcjcN5ghCOOJ87LBlpXYP82AK6Xbw+6FOVk' ||
'1f0JP6uTfzaBywHjVZ7SvI06rQdh6eoZ2qcEopuwTm7kLBt+usSb7pOH53h1hhHw' ||
'lUeyfKNM8zx/D//mg1sT3pvkuf4XKZPb2NHyUiXboqr2Ms1qY2qJ01hv3OP9i/Sl' ||
'vx4//jjJVJyQjNZHEXfHuCy3sJMO00aGvu/WxqE+tKTVFyfHbbl54do3zQ2M+4oz' ||
'ty1suNaNNrhcQ4dG3zF8spfWdpvBWl0XMFR6vyrTsxf3P9y/eIY+28JYk8n8Kz3M' ||
'g668LS8jL0Z4eXn/8ud79GKyRHAV71PELEfSlSqat0UdtlSWdNYJjdHZVzirE07b' ||
'0wpE1G1fcK2pjawy1Svhh+SAIjRy26wgm9XZN4+AI6PmYinKR9lvalv9HnTco8bd' ||
'L3B/mrjxIGuBYFYEgpMlkNapZS2Qm5XXOCu3VOawN3GYwnKcKJ5AEE8gnaBGhVXZ' ||
'0nsCwXsCwfkUyO3pP3hPAwexKZk737TMnRFzZ8TcGelE53qLK89dmOZD9/fSr1Rd' ||
'uTdLJRFE6BOfiWW6N3nl8AaAYzAgvv6UjkkpNx6yiAhZRIQsItI6tawFAnlSPYW7' ||
'4Akj3pt4U7iq+U3pquaRq5pHOkFNsinLBG0GiYYErdVabEPgILdvKTdl6KDIRbwn' ||
'kNbphqV5DoOWG2rgLxxB7E2IIFDooK/nWy2Tvxmog9EyN38DB5K36AAOAAA=';
t_tmp raw(3999);
t_fnt raw(32767);
t_line pls_integer;
begin
t_tmp := utl_compress.lz_uncompress( utl_encode.base64_decode( utl_raw.cast_to_raw( t_def ) ) );
for c in 0 .. 255
loop
for i in 1 .. 14
loop
t_line := to_number( utl_raw.substr( t_tmp, c * 14 + i, 1 ), 'XX' );
for j in reverse 0 .. 7
loop
t_fnt := utl_raw.concat( t_fnt
, case when bitand( t_line, power( 2, j ) ) = 0 then 'FF' else '00' end
);
end loop;
end loop;
end loop;
return t_fnt;
end;
--
procedure get_ean_bars( p_bars in out tp_ean_bars )
is
t_def raw(70) := utl_raw.concat( '000000FFFF00FF0000FFFF0000FF0000FF00'
, '00FFFF00FFFFFFFF00FF00FF000000FFFF00'
, 'FFFF000000FF00FF00FFFFFFFF00FFFFFF00'
, 'FFFF00FFFF00FFFFFF000000FF00FFFF'
);
begin
for i in 0 .. 9
loop
p_bars(i+20) := utl_raw.substr( t_def, i*7 + 1, 7 ); -- R-code
p_bars(i) := utl_raw.bit_complement( p_bars(i+20) ); -- L-code
p_bars(i+10) := utl_raw.reverse( p_bars(i+20) ); -- G-code
end loop;
end;
--
function ean2( p_val varchar2 )
return raw
is
t_dat raw(3999);
t_line raw(200);
t_height pls_integer := 40;
t_bar tp_ean_bars;
t_fnt raw(1000);
t_idx1 pls_integer;
t_idx2 pls_integer;
t_space pls_integer := 1;
begin
--
get_ean_bars( t_bar );
t_fnt := number_font;
--
t_idx1 := ascii( p_val ) - 48;
t_idx2 := ascii( substr( p_val, 2 ) ) - 48;
--
for j in 0 .. 8
loop
t_dat := utl_raw.concat( t_dat
, '00' -- filter type None
, utl_raw.copies( 'FF', 7 ) -- left quiet zone
, 'FFFFFFFFFF' -- start
, utl_raw.substr( t_fnt, ( t_idx1 * 9 + j ) * 7 + 1, 7 )
, 'FFFF' -- separator
, utl_raw.substr( t_fnt, ( t_idx2 * 9 + j ) * 7 + 1, 7 )
, utl_raw.copies( 'FF', 7 ) -- right quiet zone
);
end loop;
--
for i in 1 .. t_space
loop
t_dat := utl_raw.concat( t_dat
, '00' -- filter type None
, utl_raw.copies( 'FF', 7 ) -- left quiet zone
, 'FFFFFFFFFF' -- start
, utl_raw.copies( 'FF', 7 ) -- first char
, 'FFFF' -- separator
, utl_raw.copies( 'FF', 7 ) -- second char
, utl_raw.copies( 'FF', 7 ) -- right quiet zone
);
end loop;
--
case mod( to_number( p_val ), 4 )
when 0 then
null;
when 1 then
t_idx2 := t_idx2 + 10;
when 2 then
t_idx1 := t_idx1 + 10;
when 3 then
t_idx1 := t_idx1 + 10;
t_idx2 := t_idx2 + 10;
end case;
--
t_line := utl_raw.concat( '00' -- filter type None
, utl_raw.copies( 'FF', 7 ) -- left quiet zone
, 'FF00FF0000' -- start
, t_bar( t_idx1 )
, 'FF00' -- separator
, t_bar( t_idx2 )
, utl_raw.copies( 'FF', 7 ) -- right quiet zone
);
--
for i in 1 .. t_height
loop
t_dat := utl_raw.concat( t_dat, t_line );
end loop;
--
return generate_png( t_dat, utl_raw.length( t_line ) - 1, t_height + 9 + t_space );
end;
--
function ean5( p_val varchar2 )
return raw
is
t_dat raw(3999);
t_line raw(200);
t_height pls_integer := 40;
t_bar tp_ean_bars;
t_fnt raw(1000);
type tp_idx is table of pls_integer index by pls_integer;
t_idx tp_idx;
t_check pls_integer := 0;
t_space pls_integer := 1;
begin
--
get_ean_bars( t_bar );
t_fnt := number_font;
--
for i in 1 .. 5
loop
t_idx(i) := ascii( substr( p_val, i ) ) - 48;
t_check := t_check
+ to_number( substr( p_val, i, 1 ) )
* case when mod( i, 2 ) = 1 then 3 else 9 end;
end loop;
t_check := mod( t_check, 10 );
--
for j in 0 .. 8
loop
t_dat := utl_raw.concat( t_dat
, '00' -- filter type None
, utl_raw.copies( 'FF', 7 ) -- left quiet zone
, 'FFFFFFFFFF' -- start
);
for i in 1 .. 5
loop
t_dat := utl_raw.concat( t_dat
, utl_raw.substr( t_fnt, ( t_idx(i) * 9 + j ) * 7 + 1, 7 )
, case
when i < 5 then 'FFFF' -- separator
else utl_raw.copies( 'FF', 7 ) -- right quiet zone
end
);
end loop;
end loop;
--
for i in 1 .. t_space
loop
t_dat := utl_raw.concat( t_dat
, '00' -- filter type None
, utl_raw.copies( 'FF', 7 ) -- left quiet zone
, 'FFFFFFFFFF' -- start
, utl_raw.copies( 'FF', 7 * 5 ) -- 5 chars
, utl_raw.copies( 'FF', 2 * 4 ) -- 4 separators
, utl_raw.copies( 'FF', 7 ) -- right quiet zone
);
end loop;
--
t_line := utl_raw.concat( '00' -- filter type None
, utl_raw.copies( 'FF', 7 ) -- left quiet zone
, 'FF00FF0000' -- start
);
for i in 1 .. 5
loop
if substr( 'GGLLLGLGLLGLLGLGLLLGLGGLLLLGGLLLLGGLGLGLLGLLGLLGLG', t_check * 5 + i, 1 ) = 'G'
then
t_idx( i ) := t_idx( i ) + 10;
end if;
t_line := utl_raw.concat( t_line
, t_bar( t_idx(i) )
, case
when i < 5 then 'FF00' -- separator
else utl_raw.copies( 'FF', 7 ) -- right quiet zone
end
);
end loop;
--
for i in 1 .. t_height
loop
t_dat := utl_raw.concat( t_dat, t_line );
end loop;
--
return generate_png( t_dat, utl_raw.length( t_line ) - 1, t_height + 9 + t_space );
end;
--
function ean8( p_val varchar2 )
return raw
is
t_dat raw(32767);
t_line raw(200);
t_height pls_integer := 55;
t_bar tp_ean_bars;
t_fnt raw(1000);
--
function add_bars( p_val varchar2, p_bar_index pls_integer )
return raw
is
begin
return utl_raw.concat( t_bar( p_bar_index + ascii( substr( p_val, 1 ) ) - 48 )
, t_bar( p_bar_index + ascii( substr( p_val, 2 ) ) - 48 )
, t_bar( p_bar_index + ascii( substr( p_val, 3 ) ) - 48 )
, t_bar( p_bar_index + ascii( substr( p_val, 4 ) ) - 48 )
);
end;
begin
--
get_ean_bars( t_bar );
t_fnt := number_font;
--
t_line := '00'; -- filter type None
t_line := utl_raw.concat( t_line, utl_raw.copies( 'FF', 7 ) ); -- left quiet zone
--
t_line := utl_raw.concat( t_line, '00FF00' ); -- Normal Guard Bar Pattern
t_line := utl_raw.concat( t_line, add_bars( substr( p_val, 1, 4 ), 0 ) );
t_line := utl_raw.concat( t_line, 'FF00FF00FF' ); -- Centre Guard Bar Pattern
t_line := utl_raw.concat( t_line, add_bars( substr( p_val, 5, 4 ), 20 ) );
t_line := utl_raw.concat( t_line, '00FF00' ); -- Normal Guard Bar Pattern
t_line := utl_raw.concat( t_line, utl_raw.copies( 'FF', 7 ) ); -- right quiet zone
--
for i in 1 .. t_height
loop
t_dat := utl_raw.concat( t_dat, t_line );
end loop;
--
t_dat := utl_raw.concat( t_dat, '00' ); -- filter type None
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 7 ) ); -- left quiet zone
t_dat := utl_raw.concat( t_dat, '00FF00' ); -- Normal Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 7 * 4) );
t_dat := utl_raw.concat( t_dat, 'FF00FF00FF' ); -- Centre Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 7 * 4) );
t_dat := utl_raw.concat( t_dat, '00FF00' ); -- Normal Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 7 ) ); -- right quiet zone
for j in 0 .. 8
loop
t_dat := utl_raw.concat( t_dat, '00' ); -- filter type None
t_dat := utl_raw.concat( t_dat, utl_raw.substr( t_fnt, ( 90 + j ) * 7 + 1, 7 ) ); -- left quiet zone
t_dat := utl_raw.concat( t_dat, case when j < 4 then '00FF00' else 'FFFFFF' end ); -- Normal Guard Bar Pattern
for c in 1 .. 4
loop
t_dat := utl_raw.concat( t_dat, utl_raw.substr( t_fnt, ( ( ascii( substr( p_val, c ) ) - 48 ) * 9 + j ) * 7 + 1, 7 ) );
end loop;
t_dat := utl_raw.concat( t_dat, case when j < 4 then 'FF00FF00FF' else 'FFFFFFFFFF' end ); -- Centre Guard Bar Pattern
for c in 5 .. 8
loop
t_dat := utl_raw.concat( t_dat, utl_raw.substr( t_fnt, ( ( ascii( substr( p_val, c ) ) - 48 ) * 9 + j ) * 7 + 1, 7 ) );
end loop;
t_dat := utl_raw.concat( t_dat, case when j < 4 then '00FF00' else 'FFFFFF' end ); -- Normal Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.substr( t_fnt, ( 99 + j ) * 7 + 1, 7 ) ); -- right quiet zone
end loop;
--
return generate_png( t_dat, 81, t_height + 10 );
end;
--
function ean13( p_val varchar2 )
return raw
is
t_dat raw(32767);
t_line raw(200);
t_height pls_integer := 69;
type tp_bar is table of raw(7) index by pls_integer;
t_bar tp_ean_bars;
t_fnt raw(1000);
--
function add_bars( p_val varchar2, p_check varchar2 := null )
return raw
is
type tp_bar_index is table of pls_integer index by pls_integer;
t_bar_index tp_bar_index;
t_check_number pls_integer;
begin
if p_check is null
then
for i in 1 .. 6
loop
t_bar_index(i) := 20;
end loop;
else
t_check_number := ascii( p_check ) - 48;
t_bar_index(1) := 0;
t_bar_index(2) := case when t_check_number < 4 then 0 else 10 end;
t_bar_index(3) := case when t_check_number in ( 0, 4, 7, 8 ) then 0 else 10 end;
t_bar_index(4) := case when t_check_number in ( 0, 1, 4, 5, 9 ) then 0 else 10 end;
t_bar_index(5) := case when t_check_number in ( 0, 2, 5, 6, 7 ) then 0 else 10 end;
t_bar_index(6) := case when t_check_number in ( 0, 3, 6, 8, 9 ) then 0 else 10 end;
end if;
return utl_raw.concat( t_bar( t_bar_index(1) + ascii( substr( p_val, 1 ) ) - 48 )
, t_bar( t_bar_index(2) + ascii( substr( p_val, 2 ) ) - 48 )
, t_bar( t_bar_index(3) + ascii( substr( p_val, 3 ) ) - 48 )
, t_bar( t_bar_index(4) + ascii( substr( p_val, 4 ) ) - 48 )
, t_bar( t_bar_index(5) + ascii( substr( p_val, 5 ) ) - 48 )
, t_bar( t_bar_index(6) + ascii( substr( p_val, 6 ) ) - 48 )
);
end;
begin
--
get_ean_bars( t_bar );
t_fnt := number_font;
--
t_line := '00'; -- filter type None
t_line := utl_raw.concat( t_line, utl_raw.copies( 'FF', 11 ) ); -- left quiet zone
t_line := utl_raw.concat( t_line, '00FF00' ); -- Normal Guard Bar Pattern
t_line := utl_raw.concat( t_line, add_bars( substr( p_val, 2, 6 ), p_val ) );
t_line := utl_raw.concat( t_line, 'FF00FF00FF' ); -- Centre Guard Bar Pattern
t_line := utl_raw.concat( t_line, add_bars( substr( p_val, 8, 6 ) ) );
t_line := utl_raw.concat( t_line, '00FF00' ); -- Normal Guard Bar Pattern
t_line := utl_raw.concat( t_line, utl_raw.copies( 'FF', 7 ) ); -- right quiet zone
--
for i in 1 .. t_height
loop
t_dat := utl_raw.concat( t_dat, t_line );
end loop;
--
t_dat := utl_raw.concat( t_dat, '00' ); -- filter type None
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 11 ) ); -- left quiet zone
t_dat := utl_raw.concat( t_dat, '00FF00' ); -- Normal Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 7 * 6) );
t_dat := utl_raw.concat( t_dat, 'FF00FF00FF' ); -- Centre Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 7 * 6) );
t_dat := utl_raw.concat( t_dat, '00FF00' ); -- Normal Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.copies( 'FF', 7 ) ); -- right quiet zone
for j in 0 .. 8
loop
t_dat := utl_raw.concat( t_dat, '00' ); -- filter type None
t_dat := utl_raw.concat( t_dat
, 'FFFF'
, utl_raw.substr( t_fnt, ( ( ascii( p_val ) - 48 ) * 9 + j ) * 7 + 1, 7 )
, 'FFFF'
); -- left quiet zone
t_dat := utl_raw.concat( t_dat, case when j < 4 then '00FF00' else 'FFFFFF' end ); -- Normal Guard Bar Pattern
for c in 2 .. 7
loop
t_dat := utl_raw.concat( t_dat, utl_raw.substr( t_fnt, ( ( ascii( substr( p_val, c ) ) - 48 ) * 9 + j ) * 7 + 1, 7 ) );
end loop;
t_dat := utl_raw.concat( t_dat, case when j < 4 then 'FF00FF00FF' else 'FFFFFFFFFF' end ); -- Centre Guard Bar Pattern
for c in 8 .. 13
loop
t_dat := utl_raw.concat( t_dat, utl_raw.substr( t_fnt, ( ( ascii( substr( p_val, c ) ) - 48 ) * 9 + j ) * 7 + 1, 7 ) );
end loop;
t_dat := utl_raw.concat( t_dat, case when j < 4 then '00FF00' else 'FFFFFF' end ); -- Normal Guard Bar Pattern
t_dat := utl_raw.concat( t_dat, utl_raw.substr( t_fnt, ( 99 + j ) * 7 + 1, 7 ) ); -- right quiet zone
end loop;
--
return generate_png( t_dat, 113, t_height + 10 );
end;
--
function code39( p_val varchar2 )
return raw
is
t_dat blob;
t_line raw(32767);
t_width pls_integer;
t_height pls_integer := 45;
c_ratio constant pls_integer := 3; -- 2 or 3
t_start_stop raw(18);
type tp_bars is table of raw(18) index by pls_integer;
t_bars tp_bars;
t_pre pls_integer;
t_post pls_integer;
t_fnt raw(32767);
--
function char_bars( p_char varchar2 )
return raw
is
begin
return utl_raw.concat( utl_raw.copies( '00', case when substr( p_char, 1, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( 'FF', case when substr( p_char, 2, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( '00', case when substr( p_char, 3, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( 'FF', case when substr( p_char, 4, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( '00', case when substr( p_char, 5, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( 'FF', case when substr( p_char, 6, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( '00', case when substr( p_char, 7, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( 'FF', case when substr( p_char, 8, 1 ) = 1 then c_ratio else 1 end )
, utl_raw.copies( '00', case when substr( p_char, 9, 1 ) = 1 then c_ratio else 1 end )
);
end;
begin
t_fnt := cp1252_font;
--
t_start_stop := char_bars( '010010100' );
t_bars( 48 ) := char_bars( '000110100' );
t_bars( 49 ) := char_bars( '100100001' );
t_bars( 50 ) := char_bars( '001100001' );
t_bars( 51 ) := char_bars( '101100000' );
t_bars( 52 ) := char_bars( '000110001' );
t_bars( 53 ) := char_bars( '100110000' );
t_bars( 54 ) := char_bars( '001110000' );
t_bars( 55 ) := char_bars( '000100101' );
t_bars( 56 ) := char_bars( '100100100' );
t_bars( 57 ) := char_bars( '001100100' );
--
t_bars( 65 ) := char_bars( '100001001' );
t_bars( 66 ) := char_bars( '001001001' );
t_bars( 67 ) := char_bars( '101001000' );
t_bars( 68 ) := char_bars( '000011001' );
t_bars( 69 ) := char_bars( '100011000' );
t_bars( 70 ) := char_bars( '001011000' );
t_bars( 71 ) := char_bars( '000001101' );
t_bars( 72 ) := char_bars( '100001100' );
t_bars( 73 ) := char_bars( '001001100' );
t_bars( 74 ) := char_bars( '000011100' );
t_bars( 75 ) := char_bars( '100000011' );
t_bars( 76 ) := char_bars( '001000011' );
t_bars( 77 ) := char_bars( '101000010' );
t_bars( 78 ) := char_bars( '000010011' );
t_bars( 79 ) := char_bars( '100010010' );
t_bars( 80 ) := char_bars( '001010010' );
t_bars( 81 ) := char_bars( '000000111' );
t_bars( 82 ) := char_bars( '100000110' );
t_bars( 83 ) := char_bars( '001000110' );
t_bars( 84 ) := char_bars( '000010110' );
t_bars( 85 ) := char_bars( '110000001' );
t_bars( 86 ) := char_bars( '011000001' );
t_bars( 87 ) := char_bars( '111000000' );
t_bars( 88 ) := char_bars( '010010001' );
t_bars( 89 ) := char_bars( '110010000' );
t_bars( 90 ) := char_bars( '011010000' );
--
t_bars( 32 ) := char_bars( '011000100' );
t_bars( 36 ) := char_bars( '010101000' );
t_bars( 37 ) := char_bars( '000101010' );
t_bars( 43 ) := char_bars( '010001010' );
t_bars( 45 ) := char_bars( '010000101' );
t_bars( 46 ) := char_bars( '110000100' );
t_bars( 47 ) := char_bars( '010100010' );
--
t_line := '00'; -- filter type None
t_line := utl_raw.concat( t_line, t_start_stop );
for c in 1 .. length( p_val )
loop
t_line := utl_raw.concat( t_line, 'FF', t_bars( ascii( substr( p_val, c ) ) ) );
end loop;
t_line := utl_raw.concat( t_line, 'FF', t_start_stop );
t_width := utl_raw.length( t_line ) - 1;
--
dbms_lob.createtemporary( t_dat, true, dbms_lob.call );
for i in 1 .. t_height
loop
dbms_lob.writeappend( t_dat, t_width + 1, t_line );
end loop;
--
t_post := t_width - 8 * length( p_val ) - 8 - 4 - 4 - 8;
t_pre := t_post / 2;
t_post := t_post - t_pre;
for j in 0 .. 13
loop
t_line := utl_raw.concat( '00' -- filter type None
, utl_raw.copies( 'FF', t_pre )
, utl_raw.substr( t_fnt, ( 42 * 14 + j ) * 8 + 1, 8 )
, utl_raw.copies( 'FF', 4 )
);
for c in 1 .. length( p_val )
loop
t_line := utl_raw.concat( t_line
, utl_raw.substr( t_fnt, ( ascii( substr( p_val, c ) ) * 14 + j ) * 8 + 1, 8 )
);
end loop;
t_line := utl_raw.concat( t_line
, utl_raw.copies( 'FF', 4 )
, utl_raw.substr( t_fnt, ( 42 * 14 + j ) * 8 + 1, 8 )
, utl_raw.copies( 'FF', t_post )
);
dbms_lob.writeappend( t_dat, t_width + 1, t_line );
end loop;
--
return generate_png( t_dat, t_width, t_height + 14 );
end;
--
function code128( p_val raw )
return raw
is
c_factor constant pls_integer := 2;
t_dat blob;
t_line raw(32767);
t_width pls_integer;
t_height pls_integer := 45;
t_start_stop raw(18);
type tp_bars is table of raw(11) index by pls_integer;
t_bars tp_bars;
t_idx pls_integer;
t_char pls_integer;
t_check_digit number;
t_pre pls_integer;
t_post pls_integer;
t_fnt raw(32767);
t_bar_def varchar2(300) :=
'H4sIAAAAAAAAC3VT0RLAIAji/3+a3ZoianvoKhQ05gAC9DWvZFzi+AUoGAsZiblt' ||
'aopHLaXDmacHwKvBj9VzSd3yt6hHhHSh3tOsSq+TWuTPDlkQHtgx9DZCJ12QoaiH' ||
'SgxKKieV4GWGFxgBEi2nNMnBxE/xlnjTlgtnpcyLM5B4pAYoLKgZS1Sw2BLiVi5O' ||
'MVYBE+2kAL5Yd4Ci5YNiaqrf+j305gU56QYNScrA284qognSZHkPhnhoEPrnGkPQ' ||
'QsPc/pFOlQegSmJnjgQAAA==';
t_tmp raw(32767);
begin
t_fnt := cp1252_font;
t_tmp := utl_compress.lz_uncompress( utl_encode.base64_decode( utl_raw.cast_to_raw( t_bar_def ) ) );
for i in 0 .. 105
loop
t_bars( i ) := utl_raw.substr( t_tmp, i * 11 + 1, 11 );
end loop;
--
t_line := utl_raw.copies( 'FF', 10 ); -- quiet zone
t_line := utl_raw.concat( t_line, t_bars( 104 ) ); -- Start Code B
t_check_digit := 104;
t_idx := 1;
for c in 1 .. utl_raw.length( p_val )
loop
t_char := to_number( utl_raw.substr( p_val, c, 1 ), 'xx' );
if t_char > 127
then
t_line := utl_raw.concat( t_line, t_bars( 100 ) ); -- FNC 4
t_check_digit := t_check_digit + 100 * t_idx;
t_char := t_char - 128;
t_idx := t_idx + 1;
end if;
if t_char between 32 and 127
then
t_line := utl_raw.concat( t_line, t_bars( t_char - 32 ) );
t_check_digit := t_check_digit + ( t_char - 32 ) * t_idx;
elsif t_char < 32
then
t_line := utl_raw.concat( t_line, t_bars( 98 ) ); -- Shift A
t_check_digit := t_check_digit + 98 * t_idx;
t_idx := t_idx + 1;
t_line := utl_raw.concat( t_line, t_bars( t_char ) );
t_check_digit := t_check_digit + ( t_char ) * t_idx;
end if;
t_idx := t_idx + 1;
end loop;
t_line := utl_raw.concat( t_line, t_bars( mod( t_check_digit, 103 ) ) );
t_line := utl_raw.concat( t_line, '0000FFFFFF000000FF00FF0000' ); -- Stop
t_line := utl_raw.concat( t_line, utl_raw.copies( 'FF', 10 ) ); -- quiet zone
if c_factor > 1
then
t_tmp := null;
for i in 1 .. utl_raw.length( t_line )
loop
t_tmp := utl_raw.concat( t_tmp, utl_raw.copies( utl_raw.substr( t_line, i, 1 ), c_factor ) );
end loop;
t_line := t_tmp;
end if;
t_width := utl_raw.length( t_line );
t_line := utl_raw.concat( '00', t_line ); -- filter type None
--
dbms_lob.createtemporary( t_dat, true, dbms_lob.call );
for i in 1 .. t_height
loop
dbms_lob.writeappend( t_dat, t_width + 1, t_line );
end loop;
--
t_post := t_width - 8 * utl_raw.length( p_val );
t_pre := t_post / 2;
t_post := t_post - t_pre;
for i in 0 .. 13
loop
t_line := utl_raw.concat( '00' -- filter type None
, utl_raw.copies( 'FF', t_pre )
);
for c in 1 .. utl_raw.length( p_val )
loop
t_line := utl_raw.concat( t_line
, utl_raw.substr( t_fnt, ( to_number( utl_raw.substr( p_val, c, 1 ), 'XX' ) * 14 + i ) * 8 + 1, 8 )
);
end loop;
t_line := utl_raw.concat( t_line, utl_raw.copies( 'FF', t_post ) );
dbms_lob.writeappend( t_dat, t_width + 1, t_line );
end loop;
--
return generate_png( t_dat, t_width, t_height + 14 );
end;
--
function itf( p_val varchar2, p_parm varchar2 )
return raw
is
c_quiet constant pls_integer := 10;
c_ratio constant pls_integer := 3; -- 2 or 3
t_val varchar2(2000);
t_dat raw(32767);
t_line raw(3999);
t_height pls_integer := 40;
t_fnt raw(1000);
t_idx1 pls_integer;
t_idx2 pls_integer;
t_w pls_integer;
t_ll pls_integer;
t_pre pls_integer;
t_post pls_integer;
t_space pls_integer := 1;
t_check pls_integer;
t_bearer raw(100);
t_def varchar2(50) := 'nnWWnWnnnWnWnnWWWnnnnnWnWWnWnnnWWnnnnnWWWnnWnnWnWn';
begin
--
t_fnt := number_font;
--
t_val := trim( p_val );
if instr( upper( p_parm ), 'C' ) > 0
then
if mod( length( t_val ), 2 ) = 0
then
t_val := '0' || t_val;
end if;
t_check := 0;
for i in 1 .. ceil( length( t_val ) / 2 )
loop
t_check := t_check + substr( t_val, i * 2 - 1, 1 );
end loop;
t_check := t_check * 3;
for i in 1 .. trunc( length( t_val ) / 2 )
loop
t_check := t_check + substr( t_val, i * 2, 1 );
end loop;
t_check := mod( t_check, 10 );
if t_check > 0
then
t_check := 10 - t_check;
end if;
t_val := t_val || t_check;
else
if mod( length( t_val ), 2 ) = 1
then
t_val := '0' || t_val;
end if;
end if;
--
t_ll := ( c_ratio * 2 + 3 ) * length( t_val ) + c_quiet * 2 + 6 + c_ratio;
if instr( upper( p_parm ), 'B' ) > 0
then
t_ll := t_ll + c_ratio * 4;
t_line := utl_raw.copies( '00', t_ll );
t_line := utl_raw.concat( '00', t_line ); -- filter type None
for i in 1 .. c_ratio * 2
loop
t_dat := utl_raw.concat( t_dat, t_line );
end loop;
if instr( p_parm, 'B' ) > 0
then
t_bearer := utl_raw.copies( '00', c_ratio * 2 );
else
t_bearer := utl_raw.copies( 'FF', c_ratio * 2 );
end if;
end if;
--
t_line := utl_raw.concat( t_bearer
, utl_raw.copies( 'FF', c_quiet ) -- quiet zone
, '00FF00FF' -- Start Code
);
for c in 1 .. length( t_val ) / 2
loop
t_idx1 := substr( t_val, c * 2 - 1, 1 );
t_idx2 := substr( t_val, c * 2, 1 );
for i in 1 .. 5
loop
t_w := case substr( t_def, t_idx1 * 5 + i, 1 )
when 'n' then 1 else c_ratio
end;
t_line := utl_raw.concat( t_line, utl_raw.copies( '00', t_w ) );
t_w := case substr( t_def, t_idx2 * 5 + i, 1 )
when 'n' then 1 else c_ratio
end;
t_line := utl_raw.concat( t_line, utl_raw.copies( 'FF', t_w ) );
end loop;
end loop;
t_line := utl_raw.concat( t_line
, utl_raw.copies( '00', c_ratio ), 'FF00' -- Stop Code
, utl_raw.copies( 'FF', c_quiet ) -- quiet zone
, t_bearer
);
t_line := utl_raw.concat( '00', t_line ); -- filter type None
--
for i in 1 .. t_height
loop
t_dat := utl_raw.concat( t_dat, t_line );
end loop;
--
if instr( upper( p_parm ), 'B' ) > 0
then
t_line := utl_raw.copies( '00', t_ll );
t_line := utl_raw.concat( '00', t_line ); -- filter type None
for i in 1 .. c_ratio * 2
loop
t_dat := utl_raw.concat( t_dat, t_line );
end loop;
end if;
--
if instr( upper( p_parm ), 'H' ) > 0
then
for i in 1 .. t_space
loop