123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- //
- // KMTools_OC.m
- // PDF Master
- //
- // Created by tangchao on 2023/3/27.
- //
- #import "KMTools_OC.h"
- #import <AppKit/AppKit.h>
- @implementation KMTools_OC
- + (BOOL)isDefaultRoleHandlerForContentType:(NSString*)fileExtension {
- NSString *app = (NSString*)CFBridgingRelease(LSCopyDefaultRoleHandlerForContentType((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
- kLSRolesAll));
- if ([app isEqualToString:[[NSBundle mainBundle] bundleIdentifier]]) {
- return YES;
- }
- return NO;
- }
- + (BOOL)setMyselfAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
- OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
- kLSRolesAll,
- (CFStringRef) CFBridgingRetain([[NSBundle mainBundle] bundleIdentifier]));
- if (returnStatus != 0) {
- return NO;
- }
- return YES;
- }
- + (BOOL) setPreviewAsDefaultApplicationForFileExtension:(NSString *) fileExtension {
- OSStatus returnStatus = LSSetDefaultRoleHandlerForContentType ((CFStringRef)CFBridgingRetain([self UTIforFileExtension:fileExtension]),
- kLSRolesAll,
- (CFStringRef)@"com.apple.Preview");
- if (returnStatus != 0) {
- return NO;
- }
- return YES;
- }
- #pragma mark --SetDefaultRoleHandlerForContentType
- + (NSString *)UTIforFileExtension:(NSString *) extension {
- NSString * UTIString = (NSString *)CFBridgingRelease(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
- (CFStringRef)CFBridgingRetain(extension),
- NULL));
-
- return UTIString;
- }
- + (KMAppTarget)getAppTarget {
- // VERSION_DMG=1
- // VERSION_FREE=1
- // VERSION_PRO=1
- #if VERSION_FREE
- return KMAppTargetFree;
- #endif
- #if VERSION_PRO
- return KMAppTargetPro;
- #endif
- #if VERSION_DMG
- return KMAppTargetDMG;
- #endif
- return KMAppTargetFree;
- }
- @end
- @implementation KMTools_OC (Testing)
- + (NSData *)getPDFData {
- NSMutableData *pdfData = [[NSMutableData alloc] init];
- CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfData);
- CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL,
- 0,
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
- CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("PDF Master"));
- CGRect mediaBox = CGRectMake(0, 0, 595, 842);
- CGContextRef pdfContext = CGPDFContextCreate(consumer, &mediaBox, myDictionary);
- CGContextSetRGBFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0);
- CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
- CFRelease(myDictionary);
- CGContextBeginPage(pdfContext, &mediaBox);
- CGContextEndPage(pdfContext);
- CGPDFContextClose(pdfContext);
- CGContextRelease (pdfContext);
- CGDataConsumerRelease(consumer);
-
- return pdfData;
- }
- @end
- @implementation KMPageSizeTool
- + (NSString *)conversionWithUnit:(NSString *)unit value:(CGFloat)value
- {
- CGFloat f = value;
- if ([unit isEqualToString:@"cm"]) {
- f = value/10.0;
- } else if([unit isEqualToString:@"in"]){
- f = value/25.4;
- } else {
- f = value;
- }
- if (fmodf(f, 1)==0) {//如果有一位小数点
- return [NSString stringWithFormat:@"%.0f",f];
- } else if (fmodf(value*10, 1)==0) {//如果有两位小数点
- return [NSString stringWithFormat:@"%.1f",f];
- } else {
- return [NSString stringWithFormat:@"%.2f",f];
- }
- }
- @end
|