KMOCTool.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // KMOCTool.m
  3. // PDF Master
  4. //
  5. // Created by liujiajie on 2023/11/15.
  6. //
  7. #import "KMOCTool.h"
  8. #import <Foundation/Foundation.h>
  9. #import <CoreGraphics/CoreGraphics.h>
  10. #import <Cocoa/Cocoa.h>
  11. #import <PDF_Master-Swift.h>
  12. @implementation KMOCTool
  13. + (void)createPDFFile:(NSString *)filePath imagePaths:(NSArray *)paths results:(NSArray *)resultsArray scale:(CGFloat)scale {
  14. if (paths.count < 1) {
  15. return;
  16. }
  17. CFStringRef path = (__bridge CFStringRef)filePath;
  18. CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  19. CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL,
  20. 0,
  21. &kCFTypeDictionaryKeyCallBacks,
  22. &kCFTypeDictionaryValueCallBacks);
  23. CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("Kdan Mobile PDF Reader"));
  24. CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &CGRectZero, myDictionary);
  25. CGContextSetRGBFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0);
  26. CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
  27. CFRelease(myDictionary);
  28. CFRelease(url);
  29. for (int i=0; i<paths.count; i++) {
  30. NSString *path = [paths objectAtIndex:i];
  31. NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
  32. CIImage *imageCIImage = [CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:path]];
  33. NSSize size = [imageCIImage extent].size;
  34. CGRect pageRect = CGRectMake(0, 0, size.width/scale, size.height/scale);
  35. CFMutableDictionaryRef pageDictionary = CFDictionaryCreateMutable(NULL,
  36. 0,
  37. &kCFTypeDictionaryKeyCallBacks,
  38. &kCFTypeDictionaryValueCallBacks);
  39. CFDataRef boxData = CFDataCreate(NULL,(const UInt8 *)&pageRect, sizeof (CGRect));
  40. CFDictionarySetValue(pageDictionary, kCGPDFContextMediaBox, boxData);
  41. CGPDFContextBeginPage (pdfContext, pageDictionary);
  42. NSData *imageData = [NSData dataWithContentsOfFile:path];
  43. CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
  44. CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
  45. CGContextSaveGState(pdfContext);
  46. CGContextDrawImage(pdfContext, pageRect, imageRef);
  47. CGContextRestoreGState(pdfContext);
  48. CGImageRelease(imageRef);
  49. [NSGraphicsContext saveGraphicsState];
  50. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithCGContext:pdfContext flipped:NO]];
  51. NSArray *results = nil;
  52. if (i < resultsArray.count) {
  53. results = resultsArray[i];
  54. CGFloat newScale = scale;
  55. if([KMGOCRManager defaultManager].OCRType == KMOCRType_Apple)
  56. newScale = 1;
  57. if (results.count == 1) {
  58. KMGOCRResult *result = results[0];
  59. NSRect bounds = NSMakeRect((result.textBounds.origin.x)/newScale,
  60. pageRect.size.height-(result.textBounds.origin.y+result.textBounds.size.height)/newScale,
  61. (result.textBounds.size.width)/newScale,
  62. (result.textBounds.size.height)/newScale);
  63. NSDictionary *dic = @{NSFontAttributeName:FontWithSize(result.text, CGSizeMake(result.textBounds.size.width/newScale, result.textBounds.size.height/newScale)),
  64. NSForegroundColorAttributeName:[NSColor clearColor]};
  65. [result.text drawInRect:bounds withAttributes:dic];
  66. } else {
  67. for (int i=1; i<results.count; i++) {
  68. KMGOCRResult *result = results[i];
  69. NSRect bounds = NSMakeRect((result.textBounds.origin.x)/newScale,
  70. pageRect.size.height-(result.textBounds.origin.y+result.textBounds.size.height)/newScale,
  71. (result.textBounds.size.width)/newScale,
  72. (result.textBounds.size.height)/newScale);
  73. NSDictionary *dic = @{NSFontAttributeName:FontWithSize(result.text, CGSizeMake(result.textBounds.size.width/newScale, result.textBounds.size.height/newScale)),
  74. NSForegroundColorAttributeName:[NSColor clearColor]};
  75. [result.text drawInRect:bounds withAttributes:dic];
  76. }
  77. }
  78. }
  79. [NSGraphicsContext restoreGraphicsState];
  80. CGPDFContextEndPage (pdfContext);
  81. CFRelease(pageDictionary);
  82. CFRelease(boxData);
  83. image = nil;
  84. }
  85. CGPDFContextClose(pdfContext);
  86. CGContextRelease (pdfContext);
  87. }
  88. static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
  89. CGFloat fontsize = 1.0;
  90. NSFont *font = [NSFont systemFontOfSize:fontsize];
  91. CGSize strSize = [strChar sizeWithAttributes:@{NSFontAttributeName:font}];
  92. while ((fontsize<127) && (strSize.width<size.width || strSize.height<size.height)) {
  93. fontsize += 1.0;
  94. font = [NSFont systemFontOfSize:fontsize];
  95. strSize = [strChar sizeWithAttributes:@{NSFontAttributeName:font}];
  96. }
  97. return [NSFont systemFontOfSize:fontsize-1];
  98. }
  99. @end