Issue 411
[zxing.git] / iphone / Classes / ParsedResult.m
1 //
2 //  ParsedResult.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 22/05/2008.
6 /*
7  * Copyright 2008 ZXing authors
8  *
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */
21
22 #import "ParsedResult.h"
23
24 #import "TextParsedResult.h"
25 #import "TelParsedResult.h"
26 #import "EmailParsedResult.h"
27 #import "BusinessCardParsedResult.h"
28 #import "URIParsedResult.h"
29 #import "GeoParsedResult.h"
30
31 #import "UIKit/UIStringDrawing.h"
32 #import <math.h>
33
34 @implementation ParsedResult
35
36 static NSMutableDictionary *iconsByClass = nil;
37
38 + (NSString *)typeName {
39   return NSStringFromClass(self);
40 }
41
42 - (NSString *)stringForDisplay {
43   return @"{none}";
44 }
45
46 #define ICON_SIZE 40
47 #define ICON_INSIDE 36
48
49 + (UIImage *)icon {
50   if (iconsByClass == nil) {
51     iconsByClass = [[NSMutableDictionary alloc] initWithCapacity:16];
52   }
53   UIImage *icon = [iconsByClass objectForKey:[self class]];
54   if (icon == nil) {
55     UIGraphicsBeginImageContext(CGSizeMake(ICON_SIZE, ICON_SIZE));
56     CGContextRef ctx = UIGraphicsGetCurrentContext();
57     
58     [[UIColor lightGrayColor] set];
59     UIRectFill(CGRectMake(0, 0, ICON_SIZE, ICON_SIZE));
60     
61     [[UIColor blackColor] set];
62     NSString *s = [[self class] typeName];
63     UIFont *font = [UIFont systemFontOfSize:16];
64     CGSize stringSize = [s sizeWithFont:font];
65     float xScale = fminf(1.0, ICON_INSIDE / stringSize.width);
66     float yScale = fminf(1.0, ICON_INSIDE / stringSize.height);
67     
68     CGContextTranslateCTM(ctx, (ICON_SIZE / 2), (ICON_SIZE / 2));
69     CGContextRotateCTM(ctx, -M_PI / 6.0);
70     CGContextScaleCTM(ctx, xScale, yScale);
71     CGContextTranslateCTM(ctx, 
72                           -(stringSize.width)/2.0, 
73                           -(stringSize.height)/2.0);
74     
75     [s drawAtPoint:CGPointMake(0, 0) withFont:font];
76     
77     icon = [UIGraphicsGetImageFromCurrentImageContext() retain];
78     [iconsByClass setObject:icon forKey:[self class]];
79     UIGraphicsEndImageContext();
80   }
81   return icon;
82 }
83
84 - (UIImage *)icon {
85   return [[self class] icon];
86 }
87
88 - (NSArray *)actions {
89   if (!actions) {
90     actions = [[NSMutableArray alloc] init];
91     [self populateActions];
92   }
93   return actions;
94 }
95
96 - (void) populateActions {
97 }
98
99 - (void) dealloc {
100   [actions release];
101   [super dealloc];
102 }
103
104 @end