KMDocumentAIViewController.m 26 KB

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