Skip to content

Instantly share code, notes, and snippets.

@odrobnik odrobnik/BCKEAN13Code.m
Last active Dec 20, 2015

Embed
What would you like to do?
First progress on making a modern EAN13 Barcode generator ...
//
// BCKEAN13Code.m
// BarCodeKit
//
// Created by Oliver Drobnik on 8/9/13.
// Copyright (c) 2013 Oliver Drobnik. All rights reserved.
//
#import "BCKEAN13Code.h"
typedef NS_ENUM(NSUInteger, CodeVariant)
{
CodeVariant_L = 0,
CodeVariant_G = 1,
CodeVariant_R = 2
};
// source: https://en.wikipedia.org/wiki/European_Article_Number
// the encoding variants for each digits, L/G/R
static char *digit_encodings[10][3] = {{"0001101", "0100111", "1110010"}, // 0
{"0011001", "0110011", "1100110"}, // 1
{"0010011", "0011011", "1101100"}, // 2
{"0111101", "0100001", "1000010"}, // 3
{"0100011", "0011101", "1011100"}, // 4
{"0110001", "0111001", "1001110"}, // 5
{"0101111", "0000101", "1010000"}, // 6
{"0111011", "0010001", "1000100"}, // 7
{"0110111", "0001001", "1001000"}, // 8
{"0001011", "0010111", "1110100"} // 9
};
// the variant pattern to use based on the first digit
static char *variant_patterns[10] = {"LLLLLLRRRRRR", // 0
"LLGLGGRRRRRR", // 1
"LLGGLGRRRRRR", // 2
"LLGGGLRRRRRR", // 3
"LGLLGGRRRRRR", // 4
"LGGLLGRRRRRR", // 5
"LGGGLLRRRRRR", // 6
"LGLGLGRRRRRR", // 7
"LGLGGLRRRRRR", // 8
"LGGLGLRRRRRR" // 9
};
// the marker to use at the left and right ends
static char *outside_marker = "101";
// the marker to use in the center
static char *middle_marker = "01010";
@implementation BCKEAN13Code
- (NSUInteger)_digitAtIndex:(NSUInteger)index
{
NSString *digitStr = [self.content substringWithRange:NSMakeRange(index, 1)];
return [digitStr integerValue];
}
- (NSUInteger)_codeVariantIndexForDigitAtIndex:(NSUInteger)index withVariantPattern:(char *)variantPattern
{
NSAssert(index>0 && index<13, @"Index must be from 1 to 12");
char variantForDigit = variantPattern[index-1];
NSUInteger variantIndex = CodeVariant_L;
if (variantForDigit == 'G')
{
variantIndex = CodeVariant_G;
}
else if (variantForDigit == 'R')
{
variantIndex = CodeVariant_R;
}
return variantIndex;
}
- (NSString *)description
{
NSMutableString *tmpString = [NSMutableString string];
NSUInteger firstDigit = [self _digitAtIndex:0];
char *variant_pattern = variant_patterns[firstDigit];
// add left end marker
[tmpString appendString:[NSString stringWithUTF8String:outside_marker]];
for (NSUInteger i=1; i<[self.content length]; i++)
{
NSUInteger digit = [self _digitAtIndex:i];
NSUInteger variantIndex = [self _codeVariantIndexForDigitAtIndex:i withVariantPattern:variant_pattern];
NSString *encoding = [NSString stringWithUTF8String:digit_encodings[digit][variantIndex]];
[tmpString appendString:encoding];
if (i==6)
{
// add middle marker
[tmpString appendString:[NSString stringWithUTF8String:middle_marker]];
}
}
// add right end marker
[tmpString appendString:[NSString stringWithUTF8String:outside_marker]];
return tmpString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.