5 // Created by Christian Brunschen on 22/05/2008.
7 * Copyright 2008 ZXing authors
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 #import "ParsedResult.h"
24 #import "TextParsedResult.h"
25 #import "TelParsedResult.h"
26 #import "EmailDoCoMoParsedResult.h"
27 #import "AddressBookDoCoMoParsedResult.h"
28 #import "URIParsedResult.h"
29 #import "URLTOParsedResult.h"
30 #import "BookmarkDoCoMoParsedResult.h"
31 #import "GeoParsedResult.h"
33 #import "UIKit/UIStringDrawing.h"
36 @implementation ParsedResult
38 static NSArray *parsedResultTypes = nil;
39 static NSMutableDictionary *iconsByClass = nil;
41 + (NSArray *)parsedResultTypes {
42 if (parsedResultTypes == nil) {
44 [[NSArray alloc] initWithObjects:
45 [AddressBookDoCoMoParsedResult class],
46 [EmailDoCoMoParsedResult class],
47 [BookmarkDoCoMoParsedResult class],
48 [URLTOParsedResult class],
49 [TelParsedResult class],
50 [GeoParsedResult class],
51 [URIParsedResult class],
52 [TextParsedResult class],
55 return parsedResultTypes;
58 + parsedResultForString:(NSString *)s {
60 NSLog(@"parsing result:\n<<<\n%@\n>>>\n", s);
62 for (Class c in [self parsedResultTypes]) {
63 ParsedResult *result = [c parsedResultForString:s];
71 + (NSString *)typeName {
72 return NSStringFromClass(self);
75 - (NSString *)stringForDisplay {
80 #define ICON_INSIDE 36
83 if (iconsByClass == nil) {
84 iconsByClass = [[NSMutableDictionary alloc] initWithCapacity:16];
86 UIImage *icon = [iconsByClass objectForKey:[self class]];
88 UIGraphicsBeginImageContext(CGSizeMake(ICON_SIZE, ICON_SIZE));
89 CGContextRef ctx = UIGraphicsGetCurrentContext();
91 [[UIColor lightGrayColor] set];
92 UIRectFill(CGRectMake(0, 0, ICON_SIZE, ICON_SIZE));
94 [[UIColor blackColor] set];
95 NSString *s = [[self class] typeName];
96 UIFont *font = [UIFont systemFontOfSize:16];
97 CGSize stringSize = [s sizeWithFont:font];
98 float xScale = fminf(1.0, ICON_INSIDE / stringSize.width);
99 float yScale = fminf(1.0, ICON_INSIDE / stringSize.height);
101 CGContextTranslateCTM(ctx, (ICON_SIZE / 2), (ICON_SIZE / 2));
102 CGContextRotateCTM(ctx, -M_PI / 6.0);
103 CGContextScaleCTM(ctx, xScale, yScale);
104 CGContextTranslateCTM(ctx,
105 -(stringSize.width)/2.0,
106 -(stringSize.height)/2.0);
108 [s drawAtPoint:CGPointMake(0, 0) withFont:font];
110 icon = [UIGraphicsGetImageFromCurrentImageContext() retain];
111 [iconsByClass setObject:icon forKey:[self class]];
112 UIGraphicsEndImageContext();
118 return [[self class] icon];
121 - (NSArray *)actions {
123 actions = [[NSMutableArray alloc] init];
124 [self populateActions];
129 - (void) populateActions {