/
Scanner.m
142 lines (114 loc) · 4.66 KB
/
Scanner.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#import "Scanner.h"
#import "pdfScannerCallbacks.mm"
@implementation Scanner
+ (Scanner *)scannerWithPage:(CGPDFPageRef)page {
return [[[Scanner alloc] initWithPage:page] autorelease];
}
- (id)initWithPage:(CGPDFPageRef)page {
if (self = [super init]) {
pdfPage = page;
self.fontCollection = [self fontCollectionWithPage:pdfPage];
self.selections = [NSMutableArray array];
}
return self;
}
- (NSArray *)select:(NSString *)keyword {
self.content = [NSMutableString string];
self.stringDetector = [StringDetector detectorWithKeyword:keyword delegate:self];
[self.selections removeAllObjects];
self.renderingStateStack = [RenderingStateStack stack];
CGPDFOperatorTableRef operatorTable = [self newOperatorTable];
CGPDFContentStreamRef contentStream = CGPDFContentStreamCreateWithPage(pdfPage);
CGPDFScannerRef scanner = CGPDFScannerCreate(contentStream, operatorTable, self);
CGPDFScannerScan(scanner);
CGPDFScannerRelease(scanner);
CGPDFContentStreamRelease(contentStream);
CGPDFOperatorTableRelease(operatorTable);
self.stringDetector.delegate = nil;
self.stringDetector = nil;
return self.selections;
}
- (CGPDFOperatorTableRef)newOperatorTable {
CGPDFOperatorTableRef operatorTable = CGPDFOperatorTableCreate();
// Text-showing operators
CGPDFOperatorTableSetCallback(operatorTable, "Tj", printString);
CGPDFOperatorTableSetCallback(operatorTable, "\'", printStringNewLine);
CGPDFOperatorTableSetCallback(operatorTable, "\"", printStringNewLineSetSpacing);
CGPDFOperatorTableSetCallback(operatorTable, "TJ", printStringsAndSpaces);
// Text-positioning operators
CGPDFOperatorTableSetCallback(operatorTable, "Tm", setTextMatrix);
CGPDFOperatorTableSetCallback(operatorTable, "Td", newLineWithLeading);
CGPDFOperatorTableSetCallback(operatorTable, "TD", newLineSetLeading);
CGPDFOperatorTableSetCallback(operatorTable, "T*", newLine);
// Text state operators
CGPDFOperatorTableSetCallback(operatorTable, "Tw", setWordSpacing);
CGPDFOperatorTableSetCallback(operatorTable, "Tc", setCharacterSpacing);
CGPDFOperatorTableSetCallback(operatorTable, "TL", setTextLeading);
CGPDFOperatorTableSetCallback(operatorTable, "Tz", setHorizontalScale);
CGPDFOperatorTableSetCallback(operatorTable, "Ts", setTextRise);
CGPDFOperatorTableSetCallback(operatorTable, "Tf", setFont);
// Graphics state operators
CGPDFOperatorTableSetCallback(operatorTable, "cm", applyTransformation);
CGPDFOperatorTableSetCallback(operatorTable, "q", pushRenderingState);
CGPDFOperatorTableSetCallback(operatorTable, "Q", popRenderingState);
CGPDFOperatorTableSetCallback(operatorTable, "BT", newParagraph);
return operatorTable;
}
/* Create a font dictionary given a PDF page */
- (FontCollection *)fontCollectionWithPage:(CGPDFPageRef)page {
CGPDFDictionaryRef dict = CGPDFPageGetDictionary(page);
if (!dict) {
NSLog(@"Scanner: fontCollectionWithPage: page dictionary missing");
return nil;
}
CGPDFDictionaryRef resources;
if (!CGPDFDictionaryGetDictionary(dict, "Resources", &resources)) {
NSLog(@"Scanner: fontCollectionWithPage: page dictionary missing Resources dictionary");
return nil;
}
CGPDFDictionaryRef fonts;
if (!CGPDFDictionaryGetDictionary(resources, "Font", &fonts)) {
return nil;
}
FontCollection *collection = [[FontCollection alloc] initWithFontDictionary:fonts];
return [collection autorelease];
}
- (void)detector:(StringDetector *)detector didScanCharacter:(unichar)character {
Font *font = self.renderingState.font;
unichar cid = character;
if (font.toUnicode) {
cid = [font.toUnicode cidCharacter:character];
}
CGFloat width = [font widthOfCharacter:cid withFontSize:self.renderingState.fontSize];
width /= 1000;
width += self.renderingState.characterSpacing;
if (character == 32) {
width += self.renderingState.wordSpacing;
}
[self.renderingState translateTextPosition:CGSizeMake(width, 0)];
}
- (void)detectorDidStartMatching:(StringDetector *)detector {
possibleSelection = [[Selection selectionWithState:self.renderingState] retain];
}
- (void)detectorFoundString:(StringDetector *)detector {
if (possibleSelection) {
possibleSelection.finalState = self.renderingState;
[self.selections addObject:possibleSelection];
[possibleSelection release];
possibleSelection = nil;
}
}
- (RenderingState *)renderingState {
return [self.renderingStateStack topRenderingState];
}
- (void)dealloc {
[possibleSelection release];
[fontCollection release];
[selections release];
[renderingStateStack release];
[stringDetector release];
[content release];
[super dealloc];
}
@synthesize stringDetector, fontCollection, renderingStateStack, content, selections, renderingState;
@end