--- /dev/null
+Slava Pestov
\ No newline at end of file
--- /dev/null
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test alien.destructors ;
+IN: alien.destructors.tests
--- /dev/null
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: functors destructors accessors kernel lexer words ;
+IN: alien.destructors
+
+FUNCTOR: define-destructor ( F -- )
+
+F IS ${F}
+F-destructor DEFINES ${F}-destructor
+&F DEFINES &${F}
+|F DEFINES |${F}
+
+WHERE
+
+TUPLE: F-destructor alien disposed ;
+
+M: F-destructor dispose* alien>> F execute ;
+
+: &F ( alien -- alien )
+ dup f F-destructor boa &dispose drop ; inline
+
+: |F ( alien -- alien )
+ dup f F-destructor boa |dispose drop ; inline
+
+;FUNCTOR
+
+: DESTRUCTOR: scan define-destructor ; parsing
\ No newline at end of file
--- /dev/null
+Functor for defining destructors which call a C function to dispose of resources
! Copyright (C) 2006, 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
-USING: alien.syntax destructors accessors kernel ;
+USING: alien.syntax alien.destructors accessors kernel ;
IN: core-foundation
TYPEDEF: void* CFTypeRef
FUNCTION: void CFRelease ( CFTypeRef cf ) ;
-TUPLE: CFRelease-destructor alien disposed ;
-
-M: CFRelease-destructor dispose* alien>> CFRelease ;
-
-: &CFRelease ( alien -- alien )
- dup f CFRelease-destructor boa &dispose drop ; inline
-
-: |CFRelease ( alien -- alien )
- dup f CFRelease-destructor boa |dispose drop ; inline
+DESTRUCTOR: CFRelease
\ No newline at end of file
--- /dev/null
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test core-graphics kernel ;
+IN: core-graphics.tests
+
+[ ] [ 100 200 [ drop ] with-bitmap-context ] unit-test
\ No newline at end of file
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: alien.syntax math ;
+USING: alien.syntax alien.c-types alien.destructors math
+locals fry sequences destructors kernel ;
IN: core-graphics
TYPEDEF: void* CGColorSpaceRef
FUNCTION: void CGColorSpaceRelease ( CGColorSpaceRef ref ) ;
+DESTRUCTOR: CGColorSpaceRelease
+
+FUNCTION: void CGContextRelease ( CGContextRef ref ) ;
+
+DESTRUCTOR: CGContextRelease
+
FUNCTION: void CGContextSetRGBStrokeColor (
CGContextRef c,
CGFloat red,
CGContextRef c,
CGFloat x,
CGFloat y
-) ;
\ No newline at end of file
+) ;
+
+:: <CGBitmapContext> ( data w h -- context )
+ [
+ data w h 8 w 4 *
+ CGColorSpaceCreateDeviceRGB &CGColorSpaceRelease
+ kCGImageAlphaPremultipliedLast CGBitmapContextCreate
+ ] with-destructors ;
+
+: with-bitmap-context ( w h quot -- data )
+ '[
+ [ * "uint" <c-array> ] 2keep
+ [ <CGBitmapContext> &CGContextRelease @ ] [ 2drop ] 3bi
+ ] with-destructors ; inline
\ No newline at end of file
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: tools.test core-text ;
+USING: tools.test core-text core-foundation ;
IN: core-text.tests
+
+[ ] [ "Helvetica" 12 <CTFont> CFRelease ] unit-test
\ No newline at end of file
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: alien.syntax core-foundation.attributed-strings ;
+USING: alien alien.c-types alien.syntax kernel destructors
+parser accessors fry words
+core-foundation core-foundation.strings
+core-foundation.attributed-strings ;
IN: core-text
TYPEDEF: void* CTLineRef
+TYPEDEF: void* CTFontRef
+
+FUNCTION: CTFontRef CTFontCreateWithName (
+ CFStringRef name,
+ CGFloat size,
+ CGAffineTransform* matrix
+) ;
+
+: <CTFont> ( name size -- font )
+ [
+ [ <CFString> &CFRelease ] dip f CTFontCreateWithName
+ ] with-destructors ;
+
+<<
+
+: C-GLOBAL:
+ CREATE-WORD
+ dup name>> '[ _ f dlsym *void* ]
+ (( -- value )) define-declared ; parsing
+
+>>
+
+C-GLOBAL: kCTFontAttributeName
+C-GLOBAL: kCTKernAttributeName
+C-GLOBAL: kCTLigatureAttributeName
+C-GLOBAL: kCTForegroundColorAttributeName
+C-GLOBAL: kCTParagraphStyleAttributeName
+C-GLOBAL: kCTUnderlineStyleAttributeName
+C-GLOBAL: kCTVerticalFormsAttributeName
+C-GLOBAL: kCTGlyphInfoAttributeName
FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) ;
-FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
\ No newline at end of file
+FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;