Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Collect useful commits from other forks and package it in a pull request. #11

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
626 changes: 357 additions & 269 deletions CocoaTouchBarcodes.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

@interface JapanPostBarcode : NKDBarcode {
@private
NSString *japanpostContents;
NSString *_japanpostContents;
}

- (NSString *)japanpostContents;
@property (readonly, strong) NSString *japanpostContents;

@end
87 changes: 43 additions & 44 deletions JapanPostBarcode.m → ...TouchBarcodes/Barcodes/JapanPostBarcode.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

#import "JapanPostBarcode.h"

double japanpost_barTop(NSUInteger hexDigit,double size);
double japanpost_barBottom(NSUInteger hexDigit,double size);
NSUInteger japanpost_characterDescriptor(unichar character);
NSUInteger japanpost_barDescriptor(NSUInteger descriptor,NSUInteger bar,NSUInteger bars);

@implementation JapanPostBarcode

#define MILLIMETERPERPOINT 0.3527777777777777 // base72.0 //
Expand All @@ -27,13 +32,13 @@ @implementation JapanPostBarcode
// 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; // ストップコード //
NSUInteger openBracketLength = 2;
NSUInteger openBracket = 0x31; // スタートコード //
NSUInteger closeBracketLength = 2;
NSUInteger closeBracket = 0x13; // ストップコード //
NSString *barFormat = @"101010101010101010101010";
unsigned int numberLength = 3;
unsigned int numberCodesOfJapan[] = {
NSUInteger numberLength = 3;
NSUInteger numberCodesOfJapan[] = {
0x030, // - //
0x003, // CC7 . //
0x333, // CC8 / //
Expand All @@ -55,32 +60,32 @@ @implementation JapanPostBarcode
0x021 // CC6 ? //
};

double japanpost_barTop(unsigned int hexDigit,double size) { // mm単位で返す。 //
double japanpost_barTop(NSUInteger 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単位で返す。 //
double japanpost_barBottom(NSUInteger 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) {
NSUInteger japanpost_characterDescriptor(unichar character) {
return numberCodesOfJapan[character - '-'];
}

unsigned int japanpost_barDescriptor(unsigned int descriptor,unsigned int bar,unsigned int bars)
NSUInteger japanpost_barDescriptor(NSUInteger descriptor,NSUInteger bar,NSUInteger bars)
{
unsigned int shift = (bars - 1 - (bar / 2)) * 4; // 4ビットずつで1つのバーを表す。bar / 2 番目 //
unsigned int mask = 0xF << shift; // 0xfは4ビット分 //
NSUInteger shift = (bars - 1 - (bar / 2)) * 4; // 4ビットずつで1つのバーを表す。bar / 2 番目 //
NSUInteger mask = 0xF << shift; // 0xfは4ビット分 //

return (descriptor & mask) >> shift;
}

- (unsigned int)_barDescriptor:(unsigned int)index
- (NSUInteger)_barDescriptor:(NSUInteger)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];
NSUInteger bar;
NSUInteger hexDigit;
NSUInteger descriptor;
NSUInteger contentLength = [_japanpostContents length];

if ([[self initiator] length] > index) {
bar = index % [[self initiator] length];
Expand All @@ -94,11 +99,11 @@ - (unsigned int)_barDescriptor:(unsigned int)index
descriptor = japanpost_barDescriptor(hexDigit,bar,closeBracketLength);
}
else {
unsigned int digit = (index - [[self initiator] length]) / (2 * numberLength); // 何文字目か //
NSUInteger 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] );
hexDigit = japanpost_characterDescriptor( [_japanpostContents characterAtIndex:digit] );
descriptor = japanpost_barDescriptor( hexDigit, bar,numberLength);
}
else { // last digit is checksum
Expand All @@ -110,10 +115,6 @@ - (unsigned int)_barDescriptor:(unsigned int)index
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
Expand Down Expand Up @@ -161,12 +162,12 @@ - (void)setBarWidth:(float)inBarWidth

- (void)generateChecksum
{
unsigned int i,cd;
NSUInteger i,cd;
unichar uChar,character;
unsigned int checkValue = 0;
NSUInteger checkValue = 0;

for (i = 0; i < [japanpostContents length]; i++) {
uChar = [japanpostContents characterAtIndex:i];
for (i = 0; i < _japanpostContents.length; i++) {
uChar = [_japanpostContents characterAtIndex:i];
if ('-' == uChar)
checkValue += 10; // 10 //
else {
Expand Down Expand Up @@ -201,30 +202,30 @@ - (void)setContent:(NSString *)inContent
// 大文字化する。20文字に揃える。使用できない文字を排除する。郵便番号の整合性はチェックしない。 //
{
unichar uChar;
unsigned int i;
NSUInteger i;
NSString *uppercaseString = [inContent uppercaseString];
NSMutableString *tempStr = [NSMutableString string];
NSMutableString *tempContents = [NSMutableString string];
NSString *tenStr = @"0123456789";
unsigned int maxLength = 20; // 郵便番号7文字+住居表示番号13文字 //
NSUInteger 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)]];
[tempStr appendString:[tenStr substringWithRange:NSMakeRange((NSUInteger)(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)]];
[tempStr appendString:[tenStr substringWithRange:NSMakeRange((NSUInteger)(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)]];
[tempStr appendString:[tenStr substringWithRange:NSMakeRange((NSUInteger)(uChar - 'U'),1)]];
[tempContents appendString:[NSString stringWithCharacters:&uChar length:1]];
}
}
Expand All @@ -242,8 +243,8 @@ - (void)setContent:(NSString *)inContent
}
else {
if (maxLength < [tempStr length]) {
unsigned int validCount;
unsigned int tempL = [tempStr length];
NSUInteger validCount;
NSUInteger tempL = [tempStr length];
NSString *tempCStr = [NSString stringWithString:tempContents];

[tempStr deleteCharactersInRange:NSMakeRange(maxLength,tempL - maxLength)];
Expand All @@ -257,16 +258,14 @@ - (void)setContent:(NSString *)inContent
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];
_japanpostContents = [[NSString alloc] initWithString:tempStr];
content = [[NSString alloc] initWithString:tempContents];
[self generateChecksum];
}

- (BOOL)isContentValid
{
unsigned int i;
NSUInteger i;
unichar uChar;
BOOL result = YES;

Expand All @@ -283,11 +282,11 @@ - (BOOL)isContentValid
- (NSString *)barcode
// japanpostContentsを参照するところがNKDBarcodeと異なる。 //
{
unsigned int i;
NSUInteger i;
NSMutableString *theReturn = [NSMutableString string];

for (i = 0; i < [japanpostContents length]; i++)
[theReturn appendString:[self _encodeChar:[japanpostContents characterAtIndex:i]]];
for (i = 0; i < _japanpostContents.length; i++)
[theReturn appendString:[self _encodeChar:[_japanpostContents characterAtIndex:i]]];
if (checkDigit != -1)
[theReturn appendString:[self _encodeChar:checkDigit]];
return theReturn;
Expand All @@ -305,10 +304,10 @@ - (NSString *)terminator {
}

- (float)barTop:(int)index {
return (float)(japanpost_barTop([self _barDescriptor:(unsigned int)index],(double)height) / (double)MILLIMETERPERPOINT);
return (float)(japanpost_barTop([self _barDescriptor:(NSUInteger)index],(double)height) / (double)MILLIMETERPERPOINT);
}
- (float)barBottom:(int)index {
return (float)(japanpost_barBottom([self _barDescriptor:(unsigned int)index],(double)height) / (double)MILLIMETERPERPOINT);
return (float)(japanpost_barBottom([self _barDescriptor:(NSUInteger)index],(double)height) / (double)MILLIMETERPERPOINT);
}

- (NSString *)_encodeChar:(char)inChar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// NKDAbstractUPCEANBarcode.h
// -----------------------------------------------------------------------------------
// Created by Jeff LaMarche on Sat May 11 2002.
// �2002 Naked Software. All rights reserved.
// �2002 Naked Software. All rights reserved.
// -----------------------------------------------------------------------------------
// THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
// -----------------------------------------------------------------------------------
Expand Down Expand Up @@ -117,7 +117,7 @@ typedef BOOL Handedness;
@abstract Overridden to specify that guard bars, terminator and initiator should extend down into the caption area
@param index The index of the bar that you want to find the bottom for (assuming origin at lower left) as an index of
completeBarcode
@result Bottom of the bar specified in inches * kScreenResolution
@result Bottom of the bar specified in inches * screenResolution()
*/
-(float)barBottom:(int)index;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ -(float)barBottom:(int)index
{

if ( (index < 7) || (index > ([[self completeBarcode] length] - 7)) || ( (index >= 45) && (index <= 49)))
return 0.05*kScreenResolution;
return 0.05*screenResolution();
else
return [self captionHeight] * kScreenResolution;
return [self captionHeight] * screenResolution();

}
// -----------------------------------------------------------------------------------
-(void)generateChecksum
// -----------------------------------------------------------------------------------
{
int oddSum = 0;
int evenSum = 0;
int i, checkInt;
int even = 1;
//char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
NSInteger oddSum = 0;
NSInteger evenSum = 0;
NSInteger i;
NSInteger checkInt;
NSInteger even = 1;
char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
if (strlen(code) == 11)
{
Expand Down
6 changes: 2 additions & 4 deletions NKDBarcode.h → CocoaTouchBarcodes/Barcodes/NKDBarcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@
#import <Foundation/Foundation.h>

/*!
@defined kScreenResolution
@discussion This value is used to determine the drawing location of various bars. Though many
devices have different resolution, drawing in Cocoa seems to based on 72 pixels per inch
@discussion This value is used to determine the drawing location of various bars.
*/
#define kScreenResolution 163.00
float screenResolution(void);

/*!
@class NKDBarcode
Expand Down
50 changes: 35 additions & 15 deletions NKDBarcode.m → CocoaTouchBarcodes/Barcodes/NKDBarcode.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// -----------------------------------------------------------------------------------
#import "NKDBarcode.h"
#import <UIKit/UIKit.h>
#import <sys/utsname.h>


@implementation NKDBarcode
// -----------------------------------------------------------------------------------
Expand All @@ -26,8 +28,8 @@ -(id)initWithContent: (NSString *)inContent
{
return [self initWithContent:inContent
printsCaption:inPrints
andBarWidth:.013*kScreenResolution
andHeight:.5*kScreenResolution
andBarWidth:.013*screenResolution()
andHeight:.5*screenResolution()
andFontSize:6.0
andCheckDigit:(char)-1];
}
Expand All @@ -44,7 +46,6 @@ -(id)initWithContent: (NSString *)inContent
{
if (!inContent)
{
[self release];
return nil;
}
[self setContent:inContent];
Expand All @@ -55,7 +56,7 @@ -(id)initWithContent: (NSString *)inContent
//if (![self printsCaption])
[self setHeight:inHeight];
//else
[self setHeight:inHeight + (captionHeight*kScreenResolution)];
[self setHeight:inHeight + (captionHeight*screenResolution())];

// Calculate width based on number of bars needed to encode this content
[self calculateWidth];
Expand All @@ -70,11 +71,37 @@ -(id)initWithContent: (NSString *)inContent
return self;
}
// -----------------------------------------------------------------------------------
float screenResolution(void)
// -----------------------------------------------------------------------------------
{
struct utsname systemInfo;
uname(&systemInfo);
char *name = systemInfo.machine;

float ppi;
if ((strstr(name, "iPod") != NULL) && (strstr(name, "iPod4") == NULL)) {
// older ipod touches
ppi = 163;
} else if ((strstr(name, "iPhone") != NULL) && (strstr(name, "iPhone3") == NULL)) {
// older non-retina iphones
ppi = 163;
} else if ((strstr(name, "iPad") != NULL) && (strstr(name, "iPad3") == NULL)) {
// ipad 1, ipad 2
ppi = 132;
} else if (strstr(name, "iPad3") != NULL) {
// ipad 3
ppi = 264;
} else {
// iphone 4/4s, ipod touch 4g or simulator
ppi = 326;
}
return ppi;
}
// -----------------------------------------------------------------------------------
-(void)setContent:(NSString *)inContent
// -----------------------------------------------------------------------------------
{
[content autorelease];
content = [inContent retain];
content = inContent;
}
// -----------------------------------------------------------------------------------
-(void)setHeight:(float)inHeight
Expand Down Expand Up @@ -288,7 +315,7 @@ -(float)barBottom:(int)index
// -----------------------------------------------------------------------------------
{
if ([self printsCaption])
return captionHeight * kScreenResolution;
return captionHeight * screenResolution();
else
return 0.0;
}
Expand All @@ -314,7 +341,7 @@ -(float)lastBar
-(NSString *)description
// -----------------------------------------------------------------------------------
{
return [NSString stringWithFormat:@"\n\tBarcode Class: %@\n\tContent: %@\n\tCheck Digit:%c\n\tWidth:%f\n\t%Height:%f\n\tBar Width:%f\n\tFont Size:%f\n\tCaption Height:%f",
return [NSString stringWithFormat:@"\n\tBarcode Class: %@\n\tContent: %@\n\tCheck Digit:%c\n\tWidth:%f\n\tHeight:%f\n\tBar Width:%f\n\tFont Size:%f\n\tCaption Height:%f",
[self class],
[self content],
[self checkDigit],
Expand Down Expand Up @@ -385,11 +412,4 @@ - (void) encodeWithCoder: (NSCoder *)coder
[coder encodeValueOfObjCType:@encode(char) at:&checkDigit];

}
// -----------------------------------------------------------------------------------
-(void)dealloc
// -----------------------------------------------------------------------------------
{
[content release];
[super dealloc];
}
@end
Loading