KMDocumentAIViewController.m 26 KB

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