KMDocumentAIViewController.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. //
  2. // KMDocumentAIViewController.m
  3. // PDF Master
  4. //
  5. // Created by 丁林圭 on 2023/1/30.
  6. //
  7. #import "KMDocumentAIViewController.h"
  8. #import "KMOCRBox.h"
  9. #import "KMDocumentAIManager.h"
  10. #import "KMFileAttribute.h"
  11. #import "KMOCRComboBox.h"
  12. #import <PDF_Master-Swift.h>
  13. #import <Masonry/Masonry.h>
  14. #define kIndicatorWidth 32.0
  15. #define kIndicatorHeight 32.0
  16. #pragma mark - KMBookletMaskView
  17. @interface KMBookletMaskView : NSView
  18. @property (nonatomic, retain) NSProgressIndicator *progressIndicator;
  19. @end
  20. @implementation KMBookletMaskView
  21. - (void)drawRect:(NSRect)dirtyRect {
  22. [super drawRect:dirtyRect];
  23. self.wantsLayer = YES;
  24. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  25. }
  26. - (instancetype)initWithFrame:(NSRect)frameRect {
  27. self = [super initWithFrame:frameRect];
  28. if (self) {
  29. NSProgressIndicator *indicator = [[NSProgressIndicator alloc] initWithFrame:CGRectMake((frameRect.size.width - kIndicatorWidth)/2, (frameRect.size.height - kIndicatorHeight)/2, kIndicatorWidth, kIndicatorHeight)];
  30. indicator.style = NSProgressIndicatorStyleSpinning;
  31. self.progressIndicator = indicator;
  32. [indicator startAnimation:nil];
  33. [self addSubview:indicator];
  34. }
  35. return self;
  36. }
  37. - (void)mouseDown:(NSEvent *)event {
  38. }
  39. - (void)mouseUp:(NSEvent *)event {
  40. }
  41. @end
  42. #pragma mark - KMDocumentAIViewController
  43. #define kDocumentAIFileSavePath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"documentAI.pdf"]
  44. @interface KMDocumentAIViewController ()<CPDFViewDelegate,KMDocumentAIManagerDelegate,NSComboBoxDelegate,CPDFViewDelegate>
  45. @property(nonatomic,assign) IBOutlet KMDocumentAIPDFView *pdfPreView;
  46. //@property(nonatomic,assign) COCRModelType modelType;
  47. @property(nonatomic,retain) CPDFDocument *orgDocument;
  48. @property(nonatomic,retain) CPDFDocument *preDocument;
  49. @property (nonatomic,retain) KMBookletMaskView *posterMaskView;
  50. @property (nonatomic,retain) NSMutableArray *datas;
  51. @property (nonatomic,retain) NSMutableArray *resultDics;
  52. @property (nonatomic,assign) CGRect recognitionRect;
  53. @property (nonatomic,assign) BOOL isContinusOCR;
  54. @property (weak) IBOutlet NSView *rightView;
  55. @property (weak) IBOutlet NSTextField *rightTitleLabel;
  56. @property (weak) IBOutlet KMOCRBox *pageRecognitionBox;
  57. @property (weak) IBOutlet NSTextField *pageRecognitionTitle;
  58. @property (weak) IBOutlet NSImageView *pageRecognitionImageView;
  59. @property (weak) IBOutlet KMOCRBox *areaRecognitionBox;
  60. @property (weak) IBOutlet NSTextField *areaRecognitionTitle;
  61. @property (weak) IBOutlet NSImageView *areaRecognitionImageView;
  62. @property (weak) IBOutlet NSTextField *textRecognitionSetLabel;
  63. @property (weak) IBOutlet KMOCRComboBox *languagePopButton;
  64. @property (weak) IBOutlet NSTextField *pageRecognitionSetLabel;
  65. @property (weak) IBOutlet KMOCRComboBox *pageRangePopButton;
  66. @property (weak) IBOutlet NSButton *OCRButton;
  67. @property (weak) IBOutlet NSView *languageSetView;
  68. @property (weak) IBOutlet NSView *rangeSetView;
  69. @property (nonatomic,copy) NSString *pagesString;
  70. @property (weak) IBOutlet NSLayoutConstraint *tipConstraint;
  71. @property (weak) IBOutlet NSTextField *topTitleLabel;
  72. @property (weak) IBOutlet NSView *topView;
  73. @property (weak) IBOutlet NSView *scanView;
  74. @property (weak) IBOutlet NSTextField *scanTipLabel;
  75. @property (weak) IBOutlet NSProgressIndicator *progressIndicator;
  76. @property (weak) IBOutlet NSView *ocrTipView;
  77. @property (weak) IBOutlet NSTextField *ocrTipLabel;
  78. @end
  79. @implementation KMDocumentAIViewController
  80. static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
  81. CGFloat fontsize = 1.0;
  82. NSFont *font = [NSFont systemFontOfSize:fontsize];
  83. CGSize strSize = [strChar sizeWithAttributes:@{NSFontAttributeName:font}];
  84. while ((fontsize<127) && (strSize.width>size.width && strSize.height>size.height)) {
  85. fontsize += 1.0;
  86. font = [NSFont systemFontOfSize:fontsize];
  87. strSize = [strChar sizeWithAttributes:@{NSFontAttributeName:font}];
  88. }
  89. return [NSFont systemFontOfSize:fontsize-1];
  90. }
  91. - (void)dealloc {
  92. [KMDocumentAIManager defaultManager].delegate = nil;
  93. }
  94. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  95. if(self = [super initWithNibName:@"KMDocumentAIViewController" bundle:nil]) {
  96. self.orgDocument = pdfView.document;
  97. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  98. [pdfView.document writeToURL:[NSURL fileURLWithPath:kDocumentAIFileSavePath]];
  99. self.preDocument = [[CPDFDocument alloc]initWithURL:[NSURL fileURLWithPath:kDocumentAIFileSavePath]];
  100. [self.preDocument unlockWithPassword:pdfView.document.password];
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. self.pdfPreView.delegate = self;
  103. self.pdfPreView.displayDirection = CPDFDisplayDirectionHorizontal;
  104. self.pdfPreView.scaleFactor = pdfView.scaleFactor;
  105. self.pdfPreView.displayTwoUp = NO;
  106. self.pdfPreView.autoScales = YES;
  107. self.pdfPreView.document = self.preDocument;
  108. NSInteger pageIndex = pdfView.currentPageIndex;
  109. [self.pdfPreView goToPageIndex:pageIndex animated:NO];
  110. });
  111. });
  112. }
  113. return self;
  114. }
  115. - (void)loadView {
  116. [super loadView];
  117. [self initUI];
  118. }
  119. - (void)viewDidLoad {
  120. [super viewDidLoad];
  121. self.datas = [NSMutableArray array];
  122. self.resultDics = [NSMutableArray array];
  123. __block typeof(self) blockSelf = self;
  124. [KMDocumentAIManager defaultManager].delegate = self;
  125. self.pdfPreView.callback = ^(NSRect recognitionRect, CPDFPage *page) {
  126. if (!CGRectEqualToRect(recognitionRect, CGRectZero) && blockSelf.pdfPreView.isRecognitionErea) {
  127. [blockSelf.datas removeAllObjects];
  128. [blockSelf.datas addObject:page];
  129. blockSelf.recognitionRect = recognitionRect;
  130. CPDFPage *copyPage = [page copy];
  131. [copyPage setBounds:recognitionRect forBox:CPDFDisplayCropBox];
  132. NSImage * image = [[NSImage alloc] initWithSize:recognitionRect.size];
  133. [image lockFocus];
  134. [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
  135. CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  136. [copyPage drawWithBox:CPDFDisplayCropBox toContext:context];
  137. [image unlockFocus];
  138. // [[KMDocumentAIManager defaultManager] recognitionImages:@[image] withModelType:blockSelf.modelType];
  139. }
  140. };
  141. }
  142. - (void)initUI {
  143. self.scanView.hidden = YES;
  144. self.scanView.frame = CGRectMake(CGRectGetWidth(self.view.bounds),20, 226, 53);
  145. self.scanView.wantsLayer = YES;
  146. self.scanView.layer.backgroundColor = [NSColor colorWithSRGBRed:54/255.0 green:56/255.0 blue:59/255.0 alpha:1].CGColor;
  147. self.scanView.layer.cornerRadius = 4;
  148. self.scanTipLabel.textColor = [NSColor whiteColor];
  149. [self.view addSubview:self.scanView];
  150. [self.scanView mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.bottom.equalTo(self.view.mas_bottom).offset(-20);
  152. make.right.equalTo(self.view.mas_right).offset(-20);
  153. make.width.offset(226);
  154. make.height.offset(53);
  155. }];
  156. self.rightView.hidden = YES;
  157. self.rightTitleLabel.stringValue = NSLocalizedString(@"OCR Text Recognition", nil);
  158. self.pageRecognitionTitle.stringValue = NSLocalizedString(@"Page Recognition", nil);
  159. self.areaRecognitionTitle.stringValue = NSLocalizedString(@"Area Recognition", nil);
  160. self.textRecognitionSetLabel.stringValue = NSLocalizedString(@"Text Recognition Settings", nil);
  161. self.pageRecognitionSetLabel.stringValue = NSLocalizedString(@"Page Range", nil);
  162. self.topTitleLabel.stringValue = NSLocalizedString(@"This file is an image file, do you need to perform OCR?", nil);
  163. self.languagePopButton.type = KMOCRComboBoxType_None;
  164. self.pageRangePopButton.type = KMOCRComboBoxType_None;
  165. self.languagePopButton.cell.focusRingType = NSFocusRingTypeNone;
  166. self.pageRangePopButton.cell.focusRingType = NSFocusRingTypeNone;
  167. self.OCRButton.wantsLayer =
  168. self.topView.wantsLayer =
  169. self.rightView.wantsLayer = YES;
  170. self.rightView.layer.backgroundColor = [NSColor colorWithSRGBRed:247.0/255.0 green:248.0/255.0 blue:250.0/255.0 alpha:1].CGColor;
  171. self.topView.layer.backgroundColor = [NSColor colorWithSRGBRed:189.0/255.0 green:223.0/255.0 blue:253.0/255.0 alpha:1].CGColor;
  172. self.pageRecognitionBox.fillColor =
  173. self.areaRecognitionBox.fillColor = [NSColor whiteColor];
  174. self.pageRecognitionBox.borderColor =
  175. self.areaRecognitionBox.borderColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  176. self.languageSetView.hidden =
  177. self.rangeSetView.hidden = YES;
  178. self.OCRButton.layer.backgroundColor = [NSColor blueColor].CGColor;
  179. self.OCRButton.layer.cornerRadius = 4;
  180. [self.OCRButton setTitle:NSLocalizedString(@"OCR", nil)];
  181. [self.OCRButton setTitleColorWithColor:[NSColor whiteColor] font:nil];
  182. self.OCRButton.enabled = NO;
  183. self.ocrTipView.hidden = YES;
  184. self.ocrTipView.wantsLayer = YES;
  185. self.ocrTipView.layer.backgroundColor = [NSColor colorWithSRGBRed:255.0/255.0 green:241.0/255.0 blue:193.0/255.0 alpha:1].CGColor;
  186. self.ocrTipLabel.stringValue = NSLocalizedString(@"A page will only record the last OCR result and will not display multiple results.", nil);
  187. [self.languagePopButton removeAllItems];
  188. NSArray *languages = @[@"Chinese Simplified",@"Chinese Traditional",@"English",@"Japanese",@"Korean"];
  189. for (NSString *language in languages) {
  190. [self.languagePopButton addItemWithObjectValue:language];
  191. }
  192. [self.languagePopButton selectItemAtIndex:0];
  193. [self.languagePopButton setEditable:NO];
  194. self.pageRangePopButton.delegate = nil;
  195. [self.pageRangePopButton removeAllItems];
  196. [self.pageRangePopButton addItemsWithObjectValues:@[NSLocalizedString(@"All Pages", nil),
  197. NSLocalizedString(@"Odd Pages Only", nil),
  198. NSLocalizedString(@"Even Pages Only",nil),
  199. NSLocalizedString(@"e.g. 1,3-5,10",nil)]];
  200. [self.pageRangePopButton.cell setPlaceholderString:NSLocalizedString(@"e.g. 1,3-5,10", nil)];
  201. [self.pageRangePopButton selectItemAtIndex:0];
  202. [self.pageRangePopButton setEditable:NO];
  203. self.pageRangePopButton.delegate = self;
  204. self.pageRecognitionBox.mouseDownCallback = ^(BOOL downEntered, KMOCRBox *mouseBox) {
  205. if (downEntered) {
  206. if (self.languageSetView.hidden || self.rangeSetView.hidden) {
  207. self.pageRecognitionBox.borderColor = [NSColor colorWithSRGBRed:23.0/255.0 green:12.0/255.0 blue:244.0/255.0 alpha:1];
  208. self.areaRecognitionBox.borderColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  209. self.pageRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRPageSelect"];
  210. self.areaRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRArea"];
  211. self.languageSetView.hidden =
  212. self.rangeSetView.hidden = NO;
  213. self.OCRButton.enabled = YES;
  214. self.ocrTipView.hidden = NO;
  215. } else {
  216. self.pageRecognitionBox.borderColor =
  217. self.areaRecognitionBox.borderColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  218. self.areaRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRArea"];
  219. self.pageRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRPage"];
  220. self.languageSetView.hidden =
  221. self.rangeSetView.hidden = YES;
  222. self.OCRButton.enabled = NO;
  223. self.ocrTipView.hidden = YES;
  224. }
  225. self.pdfPreView.isRecognitionErea = NO;
  226. }
  227. };
  228. self.areaRecognitionBox.mouseDownCallback = ^(BOOL downEntered, KMOCRBox *mouseBox) {
  229. if (downEntered) {
  230. if(!self.languageSetView.hidden && self.rangeSetView.hidden) {
  231. self.pageRecognitionBox.borderColor =
  232. self.areaRecognitionBox.borderColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  233. self.areaRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRArea"];
  234. self.pageRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRPage"];
  235. self.languageSetView.hidden =
  236. self.rangeSetView.hidden = YES;
  237. self.OCRButton.enabled = NO;
  238. self.ocrTipView.hidden = YES;
  239. self.pdfPreView.isRecognitionErea = NO;
  240. } else {
  241. self.areaRecognitionBox.borderColor = [NSColor colorWithSRGBRed:23.0/255.0 green:12.0/255.0 blue:244.0/255.0 alpha:1];
  242. self.pageRecognitionBox.borderColor = [NSColor colorWithSRGBRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  243. self.pageRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRPage"];
  244. self.areaRecognitionImageView.image = [NSImage imageNamed:@"KMImageNameOCRAreaSelect"];
  245. self.languageSetView.hidden = NO;
  246. // [self recognitionPartModelWithModelType:self.languagePopButton.indexOfSelectedItem+1];
  247. self.rangeSetView.hidden = YES;
  248. self.OCRButton.enabled = NO;
  249. self.ocrTipView.hidden = NO;
  250. }
  251. }
  252. };
  253. self.pdfPreView.delegate = self;
  254. [self closeTopViewAction:nil];
  255. }
  256. - (void)updateToolState:(BOOL)isOCR {
  257. self.rightView.hidden = !isOCR;
  258. self.isContinusOCR = !self.rightView.hidden;
  259. }
  260. - (IBAction)closeScanViewAction:(id)sender {
  261. }
  262. - (IBAction)closeTopViewAction:(id)sender {
  263. self.topView.hidden = YES;
  264. self.tipConstraint.constant = 0;
  265. }
  266. - (IBAction)seleclanguageAction:(id)sender {
  267. if(!self.languageSetView.hidden && self.rangeSetView.hidden) {
  268. // [self recognitionPartModelWithModelType:self.languagePopButton.indexOfSelectedItem+1];
  269. }
  270. }
  271. - (IBAction)selectPageRangeAction:(KMOCRComboBox *)sender {
  272. if (sender.indexOfSelectedItem == 3) {
  273. self.pageRangePopButton.stringValue = @"";
  274. [self.pageRangePopButton setEditable:YES];
  275. } else {
  276. [self.pageRangePopButton setEditable:NO];
  277. }
  278. }
  279. - (IBAction)OCRDone:(id)sender {
  280. //避免范围提示弹框弹2次
  281. NSMutableArray * pages = [NSMutableArray array];
  282. if(0 == [_pageRangePopButton indexOfSelectedItem]){
  283. for (NSUInteger i = 0; i<self.pdfPreView.document.pageCount; i++) {
  284. [pages addObject:@(i+1)];
  285. }
  286. } else if (1 == [_pageRangePopButton indexOfSelectedItem]) {
  287. for (NSUInteger i = 0; i < self.pdfPreView.document.pageCount; i ++) {
  288. if (i % 2 == 0) {
  289. [pages addObject:@(i+1)];
  290. }
  291. }
  292. } else if (2 == [_pageRangePopButton indexOfSelectedItem]) {
  293. for (NSUInteger i = 0; i < self.pdfPreView.document.pageCount; i ++) {
  294. if (i % 2 != 0) {
  295. [pages addObject:@(i+1)];
  296. }
  297. }
  298. } else {
  299. KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  300. fileAttribute.filePath = [self.pdfPreView.document.documentURL path];
  301. fileAttribute.bAllPage = NO;
  302. fileAttribute.pagesString = _pageRangePopButton.stringValue;
  303. if (!fileAttribute.selectPages) {
  304. NSAlert *alert = [[NSAlert alloc] init];
  305. [alert setAlertStyle:NSAlertStyleCritical];
  306. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[fileAttribute.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  307. [alert runModal];
  308. return;
  309. }
  310. for (NSNumber * num in fileAttribute.selectPages) {
  311. [pages addObject:@(num.integerValue)];
  312. }
  313. }
  314. if (pages.count >0) {
  315. self.pagesString = [pages componentsJoinedByString:@","];
  316. } else {
  317. self.pagesString = nil;
  318. }
  319. if (self.pagesString) {
  320. // [self recognitionPageString:self.pagesString withModelType:self.languagePopButton.indexOfSelectedItem+1];
  321. }
  322. }
  323. - (void)enteredIncreaseAllPage {
  324. NSMutableArray *images = [NSMutableArray array];
  325. for(NSInteger i = 0;i<self.orgDocument.pageCount;i++) {
  326. CPDFPage *page = [self.orgDocument pageAtIndex:i];
  327. NSImage *image = [page thumbnailOfSize:page.bounds.size];
  328. [images addObject:image];
  329. [self.datas addObject:page];
  330. }
  331. self.isContinusOCR = NO;
  332. [[KMDocumentAIManager defaultManager] increaseImages:images];
  333. self.pdfPreView.isRecognitionErea = NO;
  334. }
  335. - (void)resetIncreaseAllPage {
  336. self.pdfPreView.document = self.orgDocument;
  337. [self.pdfPreView layoutDocumentView];
  338. self.pdfPreView.isRecognitionErea = NO;
  339. }
  340. //- (void)recognitionPageString:(NSString *)pageString withModelType:(COCRModelType)modelType {
  341. // self.pdfPreView.isRecognitionErea = NO;
  342. //
  343. // KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  344. // fileAttribute.pdfDocument = self.orgDocument;
  345. // fileAttribute.bAllPage = NO;
  346. // fileAttribute.filePath = self.orgDocument.documentURL.path;
  347. // fileAttribute.pagesString = pageString;
  348. // fileAttribute.password = self.orgDocument.password;
  349. // NSMutableArray *images = [NSMutableArray array];
  350. // self.datas = [NSMutableArray array];
  351. // self.resultDics = [NSMutableArray array];
  352. // for (NSNumber * num in fileAttribute.selectPages) {
  353. // CPDFPage *page = [self.preDocument pageAtIndex:num.integerValue-1];
  354. // NSImage *image = [page thumbnailOfSize:page.bounds.size];
  355. // if(image) {
  356. // [images addObject:image];
  357. // [self.datas addObject:page];
  358. // }else {
  359. // NSLog(@"111");
  360. // }
  361. // }
  362. //
  363. // [KMDocumentAIManager defaultManager].delegate = self;
  364. // [[KMDocumentAIManager defaultManager] recognitionImages:images withModelType:modelType];
  365. // self.modelType = modelType;
  366. //}
  367. //
  368. //- (void)recognitionPartModelWithModelType:(COCRModelType)modelType {
  369. // self.pdfPreView.isRecognitionErea = YES;
  370. // self.modelType = modelType;
  371. //}
  372. - (void)creatRecognitionDocument {
  373. for (NSDictionary *dic in self.resultDics) {
  374. NSNumber *num = [dic objectForKey:@"page"];
  375. if(num.integerValue < self.orgDocument.pageCount) {
  376. CPDFPage *page = [self.orgDocument pageAtIndex:num.integerValue];
  377. NSArray *results = [dic objectForKey:@"results"];
  378. // for (COCRResult *result in results) {
  379. // CGRect rect = result.textBounds;
  380. //
  381. // NSString *strChar = result.text;
  382. // CGFloat fontsize = 1.0;
  383. //
  384. // NSFont *font = [NSFont systemFontOfSize:fontsize];
  385. // CGSize strSize = [strChar sizeWithAttributes:@{NSFontAttributeName:font}];
  386. // while ((fontsize<127) && !(strSize.width>=result.textBounds.size.width || strSize.height>=result.textBounds.size.height)) {
  387. // fontsize += 1.0;
  388. // font = [NSFont systemFontOfSize:fontsize];
  389. // strSize = [strChar sizeWithAttributes:@{NSFontAttributeName:font}];
  390. // }
  391. // NSFont *drawFont = [NSFont systemFontOfSize:fontsize-1];
  392. //
  393. // NSDictionary *dic = @{NSFontAttributeName:drawFont};
  394. //
  395. // [page addStringBounds:result.textBounds withString:result.text withAttributes:dic];
  396. // }
  397. }
  398. }
  399. }
  400. - (void)controlTextDidEndEditing:(NSNotification *)obj {
  401. if ([obj.object isEqual:self.pageRangePopButton]) {
  402. if (self.pageRangePopButton.indexOfSelectedItem == -1) {
  403. if (![self checkPageRangeValidate:self.pageRangePopButton.stringValue]) {
  404. NSAlert *alert = [[NSAlert alloc] init];
  405. [alert setAlertStyle:NSAlertStyleCritical];
  406. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[self.pdfPreView.document.documentURL.lastPathComponent lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  407. [alert runModal];
  408. [self.view.window makeFirstResponder:self.pageRangePopButton];
  409. return;
  410. } else {
  411. self.pagesString = self.pageRangePopButton.stringValue;
  412. }
  413. }
  414. }
  415. }
  416. - (BOOL)checkPageRangeValidate:(NSString *)pageRangeString {
  417. KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  418. fileAttribute.filePath = [self.pdfPreView.document.documentURL path];
  419. fileAttribute.bAllPage = NO;
  420. fileAttribute.pagesString = self.pageRangePopButton.stringValue;
  421. if (!fileAttribute.selectPages) {
  422. return NO;
  423. }
  424. return YES;
  425. }
  426. #pragma mark - CPDFViewDelegate
  427. - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
  428. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  429. BOOL isImageDocument = [self.pdfPreView.document isImageDocument];
  430. if(isImageDocument) {
  431. dispatch_async(dispatch_get_main_queue(), ^{
  432. self.topView.hidden = NO;
  433. self.tipConstraint.constant = 32;
  434. });
  435. }
  436. });
  437. }
  438. #pragma mark - KMDocumentAIManagerDelegate
  439. - (void)documentAIManagerDidStart:(KMDocumentAIManager *)manager {
  440. if(self.isContinusOCR) {
  441. self.rightView.hidden = YES;
  442. }
  443. self.scanView.hidden = NO;
  444. [self.pdfPreView clearOCRResult];
  445. [self showWaitting];
  446. }
  447. - (void)documentAIDidFinish:(KMDocumentAIManager *)manager {
  448. if(self.isContinusOCR) {
  449. self.rightView.hidden = NO;
  450. }
  451. self.scanView.hidden = YES;
  452. [self hideWaitting];
  453. }
  454. - (void)documentAIManager:(KMDocumentAIManager *)manager didStartImageAtIndex:(NSInteger)index {
  455. }
  456. //- (void)documentAIManager:(KMDocumentAIManager *)manager didFinishOCRImageAtIndex:(NSInteger)index results:(NSArray<COCRResult *> *)results {
  457. // CPDFPage *page = self.datas[index];
  458. // NSInteger pageIndex = [self.preDocument indexForPage:page];
  459. //
  460. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  461. // [dic setValue:@(pageIndex) forKey:@"page"];
  462. // NSMutableDictionary *recognitionDic = [NSMutableDictionary dictionary];
  463. //
  464. // CGRect recognitionRect = page.bounds;
  465. // if(self.pdfPreView.isRecognitionErea) {
  466. // NSMutableArray *tResults = [NSMutableArray array];
  467. // for (COCRResult * result in results) {
  468. // CGRect rect = result.textBounds;
  469. // rect.origin.x += self.recognitionRect.origin.x;
  470. // rect.origin.y += self.recognitionRect.origin.y;
  471. // result.textBounds = rect;
  472. // [tResults addObject:result];
  473. // }
  474. // recognitionRect = self.recognitionRect;
  475. // results = tResults;
  476. // }
  477. //
  478. // NSValue *value = [NSValue valueWithRect:recognitionRect];
  479. // [dic setValue:value forKey:@"recognitionRect"];
  480. // [dic setValue:results forKey:@"results"];
  481. //
  482. // [self.resultDics addObject:dic];
  483. // self.pdfPreView.resultDics = self.resultDics;
  484. // if(pageIndex == self.pdfPreView.currentPageIndex) {
  485. // [self.pdfPreView updateResult:results recognitionRect:recognitionRect];
  486. // }
  487. // float allPages = [[NSString stringWithFormat:@"%lu",(unsigned long)self.datas.count] floatValue];
  488. // self.progressIndicator.doubleValue = (index / allPages) * 100 ;
  489. // [self hideWaitting];
  490. //}
  491. - (void)imageEngineManager:(KMDocumentAIManager *)manager didFinishCIImageAtIndex:(NSInteger)index outImage:(NSString *)filePath {
  492. NSImage *image = [[NSImage alloc]initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
  493. BOOL success = [self.preDocument insertPage:image.size withImage:filePath atIndex:index];
  494. CPDFPage *page = [self.preDocument pageAtIndex:index];
  495. [self.datas replaceObjectAtIndex:index withObject:page];
  496. if(success) {
  497. [self.preDocument removePageAtIndex:index+1];
  498. }
  499. self.pdfPreView.document = self.preDocument;
  500. [self.pdfPreView layoutDocumentView];
  501. [self hideWaitting];
  502. }
  503. - (void)documentAIManager:(KMDocumentAIManager *)manager didFailureImageAtIndex:(NSInteger)index error:(NSError *)error {
  504. [self hideWaitting];
  505. }
  506. #pragma mark - CPDFViewDelegate
  507. - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
  508. if ([self.documentAIViewDelegate respondsToSelector:@selector(documentAIViewController:currentPageDidChanged:)]) {
  509. [self.documentAIViewDelegate documentAIViewController:self currentPageDidChanged:pdfView];
  510. }
  511. for (NSDictionary *dic in self.resultDics) {
  512. NSNumber *num = [dic objectForKey:@"page"];
  513. if(num.integerValue == self.pdfPreView.currentPageIndex) {
  514. NSValue *value = [dic objectForKey:@"recognitionRect"];
  515. // [self.pdfPreView updateResult:[dic objectForKey:@"results"] recognitionRect:value.rectValue];
  516. }
  517. }
  518. }
  519. - (void)PDFViewScaleDidChanged:(CPDFView *)pdfView {
  520. if ([self.documentAIViewDelegate respondsToSelector:@selector(documentAIViewController:scaleDidChanged:)]) {
  521. [self.documentAIViewDelegate documentAIViewController:self scaleDidChanged:pdfView];
  522. }
  523. }
  524. #pragma mark - show / hide Waiting
  525. - (void)showWaitting {
  526. if (!_posterMaskView) {
  527. _posterMaskView = [[KMBookletMaskView alloc] initWithFrame:NSMakeRect(0, 0, self.view.window.frame.size.width, self.view.window.frame.size.height)];
  528. }
  529. [self.view.window.contentView addSubview:self.posterMaskView];
  530. }
  531. - (void)hideWaitting{
  532. [self.posterMaskView removeFromSuperview];
  533. }
  534. @end