KMDocumentAIViewController.m 26 KB

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