Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 297ae85625bc764f84bab8e78d52872666fe922a Chris committed Apr 21, 2010
Showing with 6,213 additions and 0 deletions.
  1. +401 −0 CocoaTouchBarcodes.xcodeproj/project.pbxproj
  2. +7 −0 CocoaTouchBarcodes_Prefix.pch
  3. +20 −0 JapanPostBarcode.h
  4. +323 −0 JapanPostBarcode.m
  5. +138 −0 NKDAbstractUPCEANBarcode.h
  6. +135 −0 NKDAbstractUPCEANBarcode.m
  7. +404 −0 NKDBarcode.h
  8. +395 −0 NKDBarcode.m
  9. +36 −0 NKDBarcodeFramework.h
  10. +71 −0 NKDBarcodeOffscreenView.h
  11. +235 −0 NKDBarcodeOffscreenView.m
  12. +36 −0 NKDCodabarBarcode.h
  13. +65 −0 NKDCodabarBarcode.m
  14. +178 −0 NKDCode128Barcode.h
  15. +491 −0 NKDCode128Barcode.m
  16. +71 −0 NKDCode39Barcode.h
  17. +209 −0 NKDCode39Barcode.m
  18. +95 −0 NKDEAN13Barcode.h
  19. +308 −0 NKDEAN13Barcode.m
  20. +87 −0 NKDEAN8Barcode.h
  21. +122 −0 NKDEAN8Barcode.m
  22. +50 −0 NKDExtendedCode39Barcode.h
  23. +139 −0 NKDExtendedCode39Barcode.m
  24. +38 −0 NKDIndustrialTwoOfFiveBarcode.h
  25. +39 −0 NKDIndustrialTwoOfFiveBarcode.m
  26. +99 −0 NKDInterleavedTwoOfFiveBarcode.h
  27. +135 −0 NKDInterleavedTwoOfFiveBarcode.m
  28. +117 −0 NKDModifiedPlesseyBarcode.h
  29. +105 −0 NKDModifiedPlesseyBarcode.m
  30. +38 −0 NKDModifiedPlesseyHexBarcode.h
  31. +40 −0 NKDModifiedPlesseyHexBarcode.m
  32. +27 −0 NKDPlanetBarcode.h
  33. +22 −0 NKDPlanetBarcode.m
  34. +117 −0 NKDPostnetBarcode.h
  35. +224 −0 NKDPostnetBarcode.m
  36. +146 −0 NKDRoyalMailBarcode.h
  37. +301 −0 NKDRoyalMailBarcode.m
  38. +76 −0 NKDUPCABarcode.h
  39. +161 −0 NKDUPCABarcode.m
  40. +98 −0 NKDUPCEBarcode.h
  41. +155 −0 NKDUPCEBarcode.m
  42. +41 −0 NSString-UPCEAN.h
  43. +48 −0 NSString-UPCEAN.m
  44. +34 −0 UIImage-NKDBarcode.h
  45. +24 −0 UIImage-NKDBarcode.m
  46. +41 −0 UIImage-Normalize.h
  47. +71 −0 UIImage-Normalize.m

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,7 @@
+//
+// Prefix header for all source files of the 'CocoaTouchStaticLibrary' target in the 'CocoaTouchStaticLibrary' project.
+//
+
+#ifdef __OBJC__
+ #import <Foundation/Foundation.h>
+#endif
@@ -0,0 +1,20 @@
+//
+// JapanPostBarcode.h
+// BarcodePod
+//
+// Created by 佐藤 昭 on Mon Jun 30 2003.
+// Copyright (c) 2003 SatoAkira. All rights reserved.
+//
+// 基本的に4ステイト3バー。数字と制御コードは3本のバーで1つのキャラクタ。大文字A~Zは制御コード+数字で1つのキャラクタ。スタートコード(STC)、ストップコード(SPC)は2本のバー。スタートコード(1)+郵便番号(7)+住居表示番号(13)+チェックデジット(1)+ストップコード(1)。郵便番号のハイフンは削除する。0で右側の不足分を埋める。住居表示番号は右側の不足分をCC4制御コードで埋める。英字は2つに数えるので13個目が制御コードのときは中途半端になる。バー幅は0.6mm。黒バーの間隔は1.2mmなので、(2 + (7+13+1) * 3 + 1) * 1.2 = 79.2mmが最初のバー中心から最後のバー中心までの長さになる。基準線の間隔は1.2mm。合計3.6mmになるので10ポイント相当になる。なお、この設定は10ポイントに設定したときの値である。8.5ポイントから11.5ポイントまで許容されているので、全数値を0.85から1.15の範囲で乗じても良い。 //
+
+#import <Foundation/Foundation.h>
+#import "NKDBarcode.h"
+
+@interface JapanPostBarcode : NKDBarcode {
+ @private
+ NSString *japanpostContents;
+}
+
+- (NSString *)japanpostContents;
+
+@end
@@ -0,0 +1,323 @@
+//
+// JapanPostBarcode.m
+// BarcodePod
+//
+// Created by 佐藤 昭 on Mon Jun 30 2003.
+// Copyright (c) 2003 SatoAkira. All rights reserved.
+//
+
+#import "JapanPostBarcode.h"
+
+@implementation JapanPostBarcode
+
+#define MILLIMETERPERPOINT 0.3527777777777777 // base72.0 //
+#define JP_DESCENDER_BOTTOM 0.0 // 最下段基準線(mm) //
+#define JP_TRACK_BOTTOM 1.2 // 中段下基準線(mm) //
+#define JP_TRACK_TOP 2.4 // 中段上基準線(mm) //
+#define JP_ASCENDER_TOP 3.6 // 最上段基準線(mm) //
+#define JP_ASCENDER_MASK (0x222)
+#define JP_DESCENDER_MASK (0x111)
+
+// -----------------------------------------------------------------------------------
+// Constants
+// -----------------------------------------------------------------------------------
+
+// Each hex constant represents a 4-bar code of the barcode corresponding to a single number or letter. The digits represent bars in this fashion: //
+// 0 - タイミングバー (JP_TRACK_BOTTOM to JP_TRACK_TOP) //
+// 1 - 下セミロングバー (JP_DESCENDER_BOTTOM to JP_TRACK_TOP) //
+// 2 - 上セミロングバー (JP_TRACK_BOTTOM to JP_ASCENDER_TOP) //
+// 3 - ロングバー (JP_DESCENDER_BOTTOM to JP_ASCENDER_TOP) //
+unsigned int openBracketLength = 2;
+unsigned int openBracket = 0x31; // スタートコード //
+unsigned int closeBracketLength = 2;
+unsigned int closeBracket = 0x13; // ストップコード //
+NSString *barFormat = @"101010101010101010101010";
+unsigned int numberLength = 3;
+unsigned int numberCodesOfJapan[] = {
+ 0x030, // - //
+ 0x003, // CC7 . //
+ 0x333, // CC8 / //
+ 0x300, // 0 //
+ 0x330, // 1 //
+ 0x312, // 2 //
+ 0x132, // 3 //
+ 0x321, // 4 //
+ 0x303, // 5 //
+ 0x123, // 6 //
+ 0x231, // 7 //
+ 0x213, // 8 //
+ 0x033, // 9 //
+ 0x120, // CC1 : //
+ 0x102, // CC2 ; //
+ 0x210, // CC3 < //
+ 0x012, // CC4 = //
+ 0x201, // CC5 > //
+ 0x021 // CC6 ? //
+};
+
+double japanpost_barTop(unsigned int hexDigit,double size) { // mm単位で返す。 //
+ return (hexDigit & JP_ASCENDER_MASK) ? JP_ASCENDER_TOP * size / 10.0 : JP_TRACK_TOP * size / 10.0;
+}
+double japanpost_barBottom(unsigned int hexDigit,double size) { // mm単位で返す。 //
+ return (hexDigit & JP_DESCENDER_MASK) ? JP_DESCENDER_BOTTOM * size / 10.0 : JP_TRACK_BOTTOM * size / 10.0;
+}
+unsigned int japanpost_characterDescriptor(unichar character) {
+ return numberCodesOfJapan[character - '-'];
+}
+
+unsigned int japanpost_barDescriptor(unsigned int descriptor,unsigned int bar,unsigned int bars)
+{
+ unsigned int shift = (bars - 1 - (bar / 2)) * 4; // 4ビットずつで1つのバーを表す。bar / 2 番目 //
+ unsigned int mask = 0xF << shift; // 0xfは4ビット分 //
+
+ return (descriptor & mask) >> shift;
+}
+
+- (unsigned int)_barDescriptor:(unsigned int)index
+// barcode is [self initiator][self barcode][self terminator] //
+// 2 * numberLength がほぼ定数に近い働きをしており、これを変数にすることが難しい。よって'A'~'Z'は2文字の扱いをした。 //
+{
+ unsigned int bar;
+ unsigned int hexDigit;
+ unsigned int descriptor;
+ unsigned int contentLength = [japanpostContents length];
+
+ if ([[self initiator] length] > index) {
+ bar = index % [[self initiator] length];
+ hexDigit = openBracket;
+ descriptor = japanpost_barDescriptor(hexDigit,bar,openBracketLength);
+ }
+ else {
+ if (index >= (contentLength + 1) * (2 * numberLength) + [[self initiator] length]) {
+ bar = (index - [[self initiator] length] - (contentLength + 1) * (2 * numberLength)) % [[self terminator] length];
+ hexDigit = closeBracket;
+ descriptor = japanpost_barDescriptor(hexDigit,bar,closeBracketLength);
+ }
+ else {
+ unsigned int digit = (index - [[self initiator] length]) / (2 * numberLength); // 何文字目か //
+
+ bar = (index - [[self initiator] length]) % (2 * numberLength); // 1文字の中の何番目のバーになるか //
+ if (digit != contentLength) {
+ hexDigit = japanpost_characterDescriptor( [[self japanpostContents] characterAtIndex:digit] );
+ descriptor = japanpost_barDescriptor( hexDigit, bar,numberLength);
+ }
+ else { // last digit is checksum
+ hexDigit = japanpost_characterDescriptor( [self checkDigit] );
+ descriptor = japanpost_barDescriptor( hexDigit, bar,numberLength);
+ }
+ }
+ }
+ return descriptor;
+}
+
+- (NSString *)japanpostContents {
+ return japanpostContents;
+}
+
+// override //
+
+- (id)initWithContent:(NSString *)inContent printsCaption:(BOOL)inPrints andBarWidth:(float)inBarWidth andHeight:(float)inHeight andFontSize:(float)inFontSize andCheckDigit:(char)inDigit
+// inPrints,inBarWidth,inFontSize,inDigitは意味を持たない。 //
+{
+ if ((nil != inContent) && (nil != (self = [super init]))) {
+ [self setContent:inContent];
+ if ((8.0 <= inHeight) && (11.5 >= inHeight))
+ [self setHeight:inHeight];
+ else
+ [self setHeight:10.0];
+ }
+ return self;
+}
+
+- (void)setHeight:(float)inHeight
+// inHeight:ポイント単位 //
+{
+ if (8.5 > inHeight)
+ height = 8.5;
+ else {
+ if (11.5 < inHeight)
+ height = 11.5;
+ else
+ height = inHeight;
+ }
+ barWidth = (float)((double)height / (double)6.0);
+ [self calculateWidth];
+}
+
+- (void)setBarWidth:(float)inBarWidth
+// inBarWidth:point単位 //
+{
+ if (1.3333333333333333 > inBarWidth) // 8.0 / 6.0 //
+ barWidth = 1.3333333333333333;
+ else {
+ if (1.9166666666667 < inBarWidth) // 11.5 / 6.0 //
+ barWidth = 1.9166666666667;
+ else
+ barWidth = inBarWidth;
+ }
+ height = (float)((double)6.0 * (double)barWidth);
+ [self calculateWidth];
+}
+
+- (void)generateChecksum
+{
+ unsigned int i,cd;
+ unichar uChar,character;
+ unsigned int checkValue = 0;
+
+ for (i = 0; i < [japanpostContents length]; i++) {
+ uChar = [japanpostContents characterAtIndex:i];
+ if ('-' == uChar)
+ checkValue += 10; // 10 //
+ else {
+ if ((',' == uChar) || ('/' == uChar))
+ checkValue += (uChar - '-') + 16; // 17 or 18 //
+ else {
+ if (('0' <= uChar) && ('9' >= uChar))
+ checkValue += uChar - '0'; // 0 ~ 9 //
+ else {
+ checkValue += uChar - ':' + 11; // 11 ~ 16 //
+ }
+ }
+ }
+ }
+ cd = 19 * (checkValue / 19 + 1) - checkValue;
+ if ((0 <= cd) && (10 > cd))
+ character = '0' + cd;
+ else {
+ if (10 == cd)
+ character = '-';
+ else {
+ if ((10 < cd) && (16 >= cd))
+ character = ':' + cd - 11;
+ else
+ character = '-' + cd - 16;
+ }
+ }
+ [self setCheckDigit:character];
+}
+
+- (void)setContent:(NSString *)inContent
+// 大文字化する。20文字に揃える。使用できない文字を排除する。郵便番号の整合性はチェックしない。 //
+{
+ unichar uChar;
+ unsigned int i;
+ NSString *uppercaseString = [inContent uppercaseString];
+ NSMutableString *tempStr = [NSMutableString string];
+ NSMutableString *tempContents = [NSMutableString string];
+ NSString *tenStr = @"0123456789";
+ unsigned int maxLength = 20; // 郵便番号7文字+住居表示番号13文字 //
+
+ for (i = 0; i < [uppercaseString length]; i++) {
+ uChar = [uppercaseString characterAtIndex:i];
+ if (('A' <= uChar) && ('Z' >= uChar)) {
+ if ('K' > uChar) {
+ [tempStr appendString:@":"]; // CC1 //
+ [tempStr appendString:[tenStr substringWithRange:NSMakeRange((unsigned int)(uChar - 'A'),1)]];
+ [tempContents appendString:[NSString stringWithCharacters:&uChar length:1]];
+ }
+ else {
+ if ('U' > uChar) {
+ [tempStr appendString:@";"]; // CC2 //
+ [tempStr appendString:[tenStr substringWithRange:NSMakeRange((unsigned int)(uChar - 'K'),1)]];
+ [tempContents appendString:[NSString stringWithCharacters:&uChar length:1]];
+ }
+ else { // ('Z' >= uChar) //
+ [tempStr appendString:@"<"]; // CC3 //
+ [tempStr appendString:[tenStr substringWithRange:NSMakeRange((unsigned int)(uChar - 'U'),1)]];
+ [tempContents appendString:[NSString stringWithCharacters:&uChar length:1]];
+ }
+ }
+ }
+ else {
+ if (('-' == uChar) || (('0' <= uChar) && ('9' >= uChar))) {
+ [tempStr appendString:[NSString stringWithCharacters:&uChar length:1]];
+ [tempContents appendString:[NSString stringWithCharacters:&uChar length:1]];
+ } // 使用できない文字を排除する。 //
+ }
+ }
+ if (maxLength > [tempStr length]) {
+ for (i = [tempStr length]; i < maxLength; i++)
+ [tempStr appendString:@"="]; // CC4 //
+ }
+ else {
+ if (maxLength < [tempStr length]) {
+ unsigned int validCount;
+ unsigned int tempL = [tempStr length];
+ NSString *tempCStr = [NSString stringWithString:tempContents];
+
+ [tempStr deleteCharactersInRange:NSMakeRange(maxLength,tempL - maxLength)];
+ tempL = [tempStr length];
+ validCount = 0;
+ for (i = 0; i < [tempStr length]; i++) {
+ uChar = [tempStr characterAtIndex:i];
+ if (('-' == uChar) || (('0' <= uChar) && ('9' >= uChar))) // 'A'~'Z'も制御文字+数字文字になっているのでカウントできる。 //
+ validCount++;
+ }
+ tempContents = [NSMutableString stringWithString:[tempCStr substringWithRange:NSMakeRange(0,validCount)]];
+ }
+ }
+ [japanpostContents release];
+ japanpostContents = [[NSString allocWithZone:[self zone]] initWithString:tempStr];
+ [content release];
+ content = [[NSString allocWithZone:[self zone]] initWithString:tempContents];
+ [self generateChecksum];
+}
+
+- (BOOL)isContentValid
+{
+ unsigned int i;
+ unichar uChar;
+ BOOL result = YES;
+
+ for (i = 0; i < 7; i++) {
+ uChar = [content characterAtIndex:i];
+ if (('0' > uChar) || ('9' < uChar)) {
+ result = NO;
+ break;
+ }
+ }
+ return result;
+}
+
+- (NSString *)barcode
+// japanpostContentsを参照するところがNKDBarcodeと異なる。 //
+{
+ unsigned int i;
+ NSMutableString *theReturn = [NSMutableString string];
+
+ for (i = 0; i < [japanpostContents length]; i++)
+ [theReturn appendString:[self _encodeChar:[japanpostContents characterAtIndex:i]]];
+ if (checkDigit != -1)
+ [theReturn appendString:[self _encodeChar:checkDigit]];
+ return theReturn;
+}
+
+- (BOOL)printsCaption {
+ return NO;
+}
+
+- (NSString *)initiator {
+ return [barFormat substringToIndex:2 * openBracketLength];
+}
+- (NSString *)terminator {
+ return [barFormat substringToIndex:2 * closeBracketLength - 1]; // 最後の"0"は不要 //
+}
+
+- (float)barTop:(int)index {
+ return (float)(japanpost_barTop([self _barDescriptor:(unsigned int)index],(double)height) / (double)MILLIMETERPERPOINT);
+}
+- (float)barBottom:(int)index {
+ return (float)(japanpost_barBottom([self _barDescriptor:(unsigned int)index],(double)height) / (double)MILLIMETERPERPOINT);
+}
+
+- (NSString *)_encodeChar:(char)inChar {
+ return [barFormat substringToIndex:2 * numberLength];
+}
+
+- (void)calculateWidth
+{
+ [self setWidth:(float)((double)[[self completeBarcode] length] * (double)[self barWidth])];
+}
+
+@end
Oops, something went wrong.

0 comments on commit 297ae85

Please sign in to comment.