2007-02-13 03:41:55 +00:00
|
|
|
/* Copyright (c) 2007 Christopher J. W. Lloyd
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
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. */
|
2007-02-13 03:41:55 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
#import <Foundation/NSString.h>
|
2010-03-31 14:36:14 +00:00
|
|
|
#import <Onyx2D/O2PDFArray.h>
|
2020-05-11 15:52:05 +00:00
|
|
|
#import <Onyx2D/O2PDFContext.h>
|
2010-03-31 14:36:14 +00:00
|
|
|
#import <Onyx2D/O2PDFObject_Boolean.h>
|
|
|
|
#import <Onyx2D/O2PDFObject_Integer.h>
|
2020-05-11 15:52:05 +00:00
|
|
|
#import <Onyx2D/O2PDFObject_Real.h>
|
2007-02-13 03:41:55 +00:00
|
|
|
#import <stddef.h>
|
|
|
|
|
2009-10-06 16:02:46 +00:00
|
|
|
@implementation O2PDFArray
|
2007-02-13 03:41:55 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
+ (O2PDFArray *) pdfArray {
|
|
|
|
return [[[self alloc] init] autorelease];
|
2007-10-15 18:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
+ (O2PDFArray *) pdfArrayWithRect: (O2Rect) rect {
|
|
|
|
O2PDFArray *result = [self pdfArray];
|
|
|
|
|
|
|
|
[result addNumber: rect.origin.x];
|
|
|
|
[result addNumber: rect.origin.y];
|
|
|
|
[result addNumber: rect.size.width];
|
|
|
|
[result addNumber: rect.size.height];
|
|
|
|
|
|
|
|
return result;
|
2007-10-15 18:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
+ (O2PDFArray *) pdfArrayWithNumbers: (O2PDFReal *) values
|
2020-05-12 00:04:26 +00:00
|
|
|
count: (NSUInteger) count
|
|
|
|
{
|
2020-05-11 15:52:05 +00:00
|
|
|
O2PDFArray *result = [self pdfArray];
|
|
|
|
NSInteger i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
[result addNumber: values[i]];
|
|
|
|
|
|
|
|
return result;
|
2007-10-22 19:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
+ (O2PDFArray *) pdfArrayWithIntegers: (O2PDFInteger *) values
|
2020-05-12 00:04:26 +00:00
|
|
|
count: (NSUInteger) count
|
|
|
|
{
|
2020-05-11 15:52:05 +00:00
|
|
|
O2PDFArray *result = [self pdfArray];
|
|
|
|
NSInteger i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
[result addInteger: values[i]];
|
|
|
|
|
|
|
|
return result;
|
2007-10-22 19:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- init {
|
|
|
|
_capacity = 1;
|
|
|
|
_count = 0;
|
|
|
|
_objects = NSZoneMalloc(NULL, sizeof(O2PDFObject *) * _capacity);
|
|
|
|
return self;
|
|
|
|
}
|
2007-10-22 19:00:18 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) dealloc {
|
|
|
|
NSZoneFree(NULL, _objects);
|
|
|
|
[super dealloc];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (O2PDFObjectType) objectType {
|
|
|
|
return kO2PDFObjectTypeArray;
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) checkForType: (O2PDFObjectType) type value: (void *) value {
|
|
|
|
if (type != kO2PDFObjectTypeArray)
|
|
|
|
return NO;
|
2007-02-13 03:41:55 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
*((O2PDFArray **) value) = self;
|
|
|
|
return YES;
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSUInteger) count {
|
|
|
|
return _count;
|
|
|
|
}
|
2007-02-13 03:41:55 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) addObject: (O2PDFObject *) object {
|
|
|
|
[object retain];
|
2007-02-13 03:41:55 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
_count++;
|
|
|
|
if (_count > _capacity) {
|
|
|
|
_capacity = _count * 2;
|
|
|
|
_objects = NSZoneRealloc([self zone], _objects, sizeof(id) * _capacity);
|
|
|
|
}
|
|
|
|
_objects[_count - 1] = object;
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) addNumber: (O2PDFReal) value {
|
|
|
|
[self addObject: [O2PDFObject_Real pdfObjectWithReal: value]];
|
2007-10-15 18:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) addInteger: (O2PDFInteger) value {
|
|
|
|
[self addObject: [O2PDFObject_Integer pdfObjectWithInteger: value]];
|
2007-10-22 19:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) addBoolean: (O2PDFBoolean) value {
|
|
|
|
[self addObject: [O2PDFObject_Boolean pdfObjectWithBoolean: value]];
|
2007-10-22 19:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (O2PDFObject *) objectAtIndex: (NSUInteger) index {
|
|
|
|
if (index < _count)
|
|
|
|
return _objects[index];
|
|
|
|
else
|
|
|
|
return nil;
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getObjectAtIndex: (NSUInteger) index value: (O2PDFObject **) objectp {
|
|
|
|
*objectp = [[self objectAtIndex: index] realObject];
|
|
|
|
|
|
|
|
return YES;
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getNullAtIndex: (NSUInteger) index {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return ([object objectType] == kO2PDFObjectTypeNull) ? YES : NO;
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getBooleanAtIndex: (NSUInteger) index value: (O2PDFBoolean *) valuep {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeBoolean value: valuep];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getIntegerAtIndex: (NSUInteger) index value: (O2PDFInteger *) valuep {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeInteger value: valuep];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getNumberAtIndex: (NSUInteger) index value: (O2PDFReal *) valuep {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeReal value: valuep];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getNameAtIndex: (NSUInteger) index value: (const char **) namep {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeName value: namep];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getStringAtIndex: (NSUInteger) index value: (O2PDFString **) stringp {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeString value: stringp];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getArrayAtIndex: (NSUInteger) index value: (O2PDFArray **) arrayp {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeArray value: arrayp];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getDictionaryAtIndex: (NSUInteger) index
|
2020-05-12 00:04:26 +00:00
|
|
|
value: (O2PDFDictionary **) dictionaryp
|
|
|
|
{
|
2020-05-11 15:52:05 +00:00
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeDictionary value: dictionaryp];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getStreamAtIndex: (NSUInteger) index value: (O2PDFStream **) streamp {
|
|
|
|
O2PDFObject *object = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
return [object checkForType: kO2PDFObjectTypeStream value: streamp];
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (BOOL) getNumbers: (O2PDFReal **) numbersp count: (NSUInteger *) countp {
|
|
|
|
NSUInteger i, count = [self count];
|
|
|
|
O2PDFReal *numbers;
|
|
|
|
|
|
|
|
numbers = NSZoneMalloc(NULL, sizeof(O2PDFReal) * count);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (![self getNumberAtIndex: i value: numbers + i]) {
|
|
|
|
NSZoneFree(NULL, numbers);
|
|
|
|
*numbersp = NULL;
|
|
|
|
*countp = 0;
|
|
|
|
return NO;
|
|
|
|
}
|
2007-02-13 03:41:55 +00:00
|
|
|
}
|
2020-05-11 15:52:05 +00:00
|
|
|
|
|
|
|
*numbersp = numbers;
|
|
|
|
*countp = count;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) getIntegers: (O2PDFInteger **) intsp count: (NSUInteger *) countp {
|
|
|
|
NSUInteger i, count = [self count];
|
|
|
|
O2PDFInteger *result;
|
|
|
|
|
|
|
|
result = NSZoneMalloc(NULL, sizeof(O2PDFInteger) * count);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (![self getIntegerAtIndex: i value: result + i]) {
|
|
|
|
NSZoneFree(NULL, result);
|
|
|
|
*intsp = NULL;
|
|
|
|
*countp = 0;
|
|
|
|
return NO;
|
|
|
|
}
|
2010-10-22 15:57:22 +00:00
|
|
|
}
|
2020-05-11 15:52:05 +00:00
|
|
|
|
|
|
|
*intsp = result;
|
|
|
|
*countp = count;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) description {
|
|
|
|
NSMutableString *result = [NSMutableString string];
|
|
|
|
NSInteger i;
|
|
|
|
|
|
|
|
[result appendString: @"[ \n"];
|
|
|
|
for (i = 0; i < _count; i++)
|
|
|
|
[result appendFormat: @"%@ ", _objects[i]];
|
|
|
|
[result appendString: @" ]\n"];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) encodeWithPDFContext: (O2PDFContext *) encoder {
|
|
|
|
NSInteger i;
|
|
|
|
|
|
|
|
[encoder appendString: @"[ "];
|
|
|
|
for (i = 0; i < _count; i++)
|
|
|
|
[encoder encodePDFObject: _objects[i]];
|
|
|
|
[encoder appendString: @"]\n"];
|
2007-10-15 18:08:57 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 03:41:55 +00:00
|
|
|
@end
|