osx - Image Generation from TrueType font in Swift/Cocoa -


i'm trying generate nsimage ttf font using swift in cocoa (not uikit) , i'm struggling context creation @ moment.

my base code came project: https://github.com/reeonce/ionicons.swift it's designed uikit, tried translating cocoa.

here's original code:

extension uiimage {     public class func imagewithionicon(icon: ionicons, height: cgfloat, color: uicolor) -> uiimage {         let font = uifont(name: "ionicons", size: height)!         let iconsize = (icon.rawvalue nsstring).sizewithattributes([nsfontattributename: font])         uigraphicsbeginimagecontextwithoptions(iconsize, false, 0.0)         (icon.rawvalue nsstring).drawatpoint(cgpointzero, withattributes: [nsfontattributename: font, nsforegroundcolorattributename: color])         let image = uigraphicsgetimagefromcurrentimagecontext()         uigraphicsendimagecontext()          return image     } } 

and here have @ moment:

extension nsimage {     public class func imagewithionicon(icon: ionicons, height: cgfloat, color: nscolor) -> nsimage? {         if let font = nsfont(name: "ionicons", size: height) {             let iconsize = (icon.rawvalue nsstring).sizewithattributes([nsfontattributename: font])             let context = cgbitmapcontextcreate(nil, int(iconsize.width), int(iconsize.height), 8, int(iconsize.width)*8, cgcolorspacecreatedevicergb(), nil)             (icon.rawvalue nsstring).drawatpoint(cgpointzero, withattributes: [nsfontattributename: font, nsforegroundcolorattributename: color])             let image = nsimage(cgimage: cgbitmapcontextcreateimage(context), size: iconsize)             return image         }         return nil     } } 

this gives me runtime error because have no idea how initialize context:

<error>: cgbitmapcontextcreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kcgimagealphanone; 168 bytes/row. 

any hint beginner? thanks

here's example of drawing text in specified rect

    let context = nsgraphicscontext.currentcontext()!.cgcontext      //// text drawing     let textrect = nsmakerect(55, 33, 88, 56)     let texttextcontent = nsstring(string: "hello, world!")     let textstyle = nsparagraphstyle.defaultparagraphstyle().mutablecopy() as! nsmutableparagraphstyle     textstyle.alignment = nstextalignment.lefttextalignment      let textfontattributes = [nsfontattributename: nsfont(name: "helveticaneue-bold", size: 17)!, nsforegroundcolorattributename: nscolor.blackcolor(), nsparagraphstyleattributename: textstyle]      let texttextheight: cgfloat = texttextcontent.boundingrectwithsize(nsmakesize(textrect.width, cgfloat.infinity), options: nsstringdrawingoptions.useslinefragmentorigin, attributes: textfontattributes).size.height     let texttextrect: nsrect = nsmakerect(textrect.minx, textrect.miny + (textrect.height - texttextheight) / 2, textrect.width, texttextheight)     nsgraphicscontext.savegraphicsstate()     nsrectclip(textrect)     texttextcontent.drawinrect(nsoffsetrect(texttextrect, 0, 5), withattributes: textfontattributes)     nsgraphicscontext.restoregraphicsstate() 

check out docs on nsgraphicscontext more on https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/nsgraphicscontext_class/


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -