<commit_before>//
//  MDSymbol
//
//  Created by tichise on 2015年5月7日 15/05/07.
//  Copyright (c) 2015年 tichise. All rights reserved.
//

import UIKit

class MDSymbol: NSObject {
    
    var text:NSString = ""
    var mutableTextFontAttributes = [NSObject : AnyObject]()
    
    init(text:NSString, size:CGFloat) {
        self.text = text
        
        self.mutableTextFontAttributes = [NSObject : AnyObject]()
        self.mutableTextFontAttributes[NSParagraphStyleAttributeName] = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
        self.mutableTextFontAttributes[NSFontAttributeName] = UIFont(name: "googleicon" as String, size: size)!
    }
    
    func addAttribute(attributeName:NSObject, value:AnyObject) {
        self.mutableTextFontAttributes[attributeName as NSObject] = value
    }
    
    func imageWithSize(size:CGSize)->UIImage {
        let imageRect = CGRectMake(0,0,size.width, size.height)
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        
        let textRect  = CGRectMake(0, 0, size.width, size.height)
        self.text.drawInRect(textRect, withAttributes: self.mutableTextFontAttributes)
        
        let image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext()
        
        return image
    }
}<commit_msg>fix font load<commit_after>//
//  MDSymbol
//
//  Created by tichise on 2015年5月7日 15/05/07.
//  Copyright (c) 2015年 tichise. All rights reserved.
//

import UIKit

class MDSymbol: NSObject {
    
    var text:NSString = ""
    var mutableTextFontAttributes = [NSObject : AnyObject]()
    
    init(text:NSString, size:CGFloat) {
        self.text = text
        
        self.mutableTextFontAttributes = [NSObject : AnyObject]()
        self.mutableTextFontAttributes[NSParagraphStyleAttributeName] = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
        
        if (UIFont.fontNamesForFamilyName("googleicon").count == 0) {
            FontLoader.loadFont("googleicon")
        }
        
        self.mutableTextFontAttributes[NSFontAttributeName] = UIFont(name: "googleicon" as String, size: size)!
    }
    
    func addAttribute(attributeName:NSObject, value:AnyObject) {
        self.mutableTextFontAttributes[attributeName as NSObject] = value
    }
    
    func imageWithSize(size:CGSize)->UIImage {
        let imageRect = CGRectMake(0,0,size.width, size.height)
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        
        let textRect  = CGRectMake(0, 0, size.width, size.height)
        self.text.drawInRect(textRect, withAttributes: self.mutableTextFontAttributes)
        
        let image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext()
        
        return image
    }
}

private class FontLoader {
    class func loadFont(name: String) {
        let bundle = NSBundle.mainBundle()
        let fontURL = bundle.URLForResource(name, withExtension: "ttf")
        
        let data = NSData(contentsOfURL: fontURL!)!
        
        let provider = CGDataProviderCreateWithCFData(data)
        let font = CGFontCreateWithDataProvider(provider)!
        
        var error: Unmanaged<CFError>?
        if !CTFontManagerRegisterGraphicsFont(font, &error) {
            let errorDescription: CFStringRef = CFErrorCopyDescription(error!.takeUnretainedValue())
            let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
            NSException(name: NSInternalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
        }
    }
}
{'fraction_non_alphanumeric': 0.09093362115488397, 'fraction_numerical': 0.011602806260118727, 'mean_word_length': 2.926906779661017, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4402189', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1080, 'n_words': 207}