KMTools_OC.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // KMTools_OC.m
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/3/27.
  6. //
  7. #import "KMTools_OC.h"
  8. #import <AppKit/AppKit.h>
  9. @implementation KMTools_OC
  10. + (BOOL)isDefaultRoleHandlerForContentType:(NSString*)fileExtension {
  11. NSString *app = (NSString*)CFBridgingRelease(LSCopyDefaultRoleHandlerForContentType((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
  12. kLSRolesAll));
  13. if ([app isEqualToString:[[NSBundle mainBundle] bundleIdentifier]]) {
  14. return YES;
  15. }
  16. return NO;
  17. }
  18. + (BOOL)setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
  19. OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
  20. kLSRolesAll,
  21. (CFStringRef) CFBridgingRetain([[NSBundle mainBundle] bundleIdentifier]));
  22. if (returnStatus != 0) {
  23. return NO;
  24. }
  25. return YES;
  26. }
  27. + (BOOL) setPreviewAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
  28. OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
  29. kLSRolesAll,
  30. (CFStringRef)@"com.apple.Preview");
  31. if (returnStatus != 0) {
  32. return NO;
  33. }
  34. return YES;
  35. }
  36. #pragma mark --SetDefaultRoleHandlerForContentType
  37. + (NSString *)UTIforFileExtension:(NSString *) extension {
  38. NSString * UTIString = (NSString *)CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
  39. (CFStringRef)CFBridgingRetain(extension),
  40. NULL));
  41. return UTIString;
  42. }
  43. + (KMAppTarget)getAppTarget {
  44. // VERSION_DMG=1
  45. // VERSION_FREE=1
  46. // VERSION_PRO=1
  47. #if VERSION_FREE
  48. return KMAppTargetFree;
  49. #endif
  50. #if VERSION_PRO
  51. return KMAppTargetPro;
  52. #endif
  53. #if VERSION_DMG
  54. return KMAppTargetDMG;
  55. #endif
  56. return KMAppTargetFree;
  57. }
  58. @end
  59. @implementation KMTools_OC (Testing)
  60. + (NSData *)getPDFData {
  61. NSMutableData *pdfData = [[NSMutableData alloc] init];
  62. CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfData);
  63. CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL,
  64. 0,
  65. &kCFTypeDictionaryKeyCallBacks,
  66. &kCFTypeDictionaryValueCallBacks);
  67. CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("PDF Master"));
  68. CGRect mediaBox = CGRectMake(0, 0, 595, 842);
  69. CGContextRef pdfContext = CGPDFContextCreate(consumer, &mediaBox, myDictionary);
  70. CGContextSetRGBFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0);
  71. CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
  72. CFRelease(myDictionary);
  73. CGContextBeginPage(pdfContext, &mediaBox);
  74. CGContextEndPage(pdfContext);
  75. CGPDFContextClose(pdfContext);
  76. CGContextRelease (pdfContext);
  77. CGDataConsumerRelease(consumer);
  78. return pdfData;
  79. }
  80. @end
  81. @implementation KMPageSizeTool
  82. + (NSString *)conversionWithUnit:(NSString *)unit value:(CGFloat)value
  83. {
  84. CGFloat f = value;
  85. if ([unit isEqualToString:@"cm"]) {
  86. f = value/10.0;
  87. } else if([unit isEqualToString:@"in"]){
  88. f = value/25.4;
  89. } else {
  90. f = value;
  91. }
  92. if (fmodf(f, 1)==0) {//如果有一位小数点
  93. return [NSString stringWithFormat:@"%.0f",f];
  94. } else if (fmodf(value*10, 1)==0) {//如果有两位小数点
  95. return [NSString stringWithFormat:@"%.1f",f];
  96. } else {
  97. return [NSString stringWithFormat:@"%.2f",f];
  98. }
  99. }
  100. @end