Get text styles working with core-text binding

db4
Slava Pestov 2009-01-21 00:39:32 -06:00
parent 9f76476d26
commit 7c1fcb3b98
3 changed files with 48 additions and 40 deletions

View File

@ -6,19 +6,19 @@ arrays kernel generalizations math accessors
combinators hashtables ; combinators hashtables ;
IN: core-text.tests IN: core-text.tests
: test-font ( -- object ) : test-font ( name -- object )
"Helvetica" kCTFontNameAttribute associate <CTFont> ; kCTFontFamilyNameAttribute associate <CTFont> ;
[ ] [ test-font CFRelease ] unit-test [ ] [ "Helvetica" test-font CFRelease ] unit-test
[ ] [ [ ] [
[ [
kCTFontAttributeName test-font &CFRelease 2array 1array kCTFontAttributeName "Helvetica" test-font &CFRelease 2array 1array
<CFDictionary> &CFRelease drop <CFDictionary> &CFRelease drop
] with-destructors ] with-destructors
] unit-test ] unit-test
: test-typographic-bounds ( string -- ? ) : test-typographic-bounds ( string font -- ? )
[ [
test-font &CFRelease <CTLine> &CFRelease test-font &CFRelease <CTLine> &CFRelease
line-typographic-bounds { line-typographic-bounds {
@ -29,6 +29,8 @@ IN: core-text.tests
} cleave and and and } cleave and and and
] with-destructors ; ] with-destructors ;
[ t ] [ "Hello world" test-typographic-bounds ] unit-test [ t ] [ "Hello world" "Helvetica" test-typographic-bounds ] unit-test
[ t ] [ "日本語" test-typographic-bounds ] unit-test [ t ] [ "Hello world" "Chicago" test-typographic-bounds ] unit-test
[ t ] [ "日本語" "Helvetica" test-typographic-bounds ] unit-test

View File

@ -21,6 +21,15 @@ TYPEDEF: void* CTFontDescriptorRef
>> >>
! CTFontSymbolicTraits
: kCTFontItalicTrait ( -- n ) 0 2^ ; inline
: kCTFontBoldTrait ( -- n ) 1 2^ ; inline
: kCTFontExpandedTrait ( -- n ) 5 2^ ; inline
: kCTFontCondensedTrait ( -- n ) 6 2^ ; inline
: kCTFontMonoSpaceTrait ( -- n ) 10 2^ ; inline
: kCTFontVerticalTrait ( -- n ) 11 2^ ; inline
: kCTFontUIOptimizedTrait ( -- n ) 12 2^ ; inline
C-GLOBAL: kCTFontSymbolicTrait C-GLOBAL: kCTFontSymbolicTrait
C-GLOBAL: kCTFontWeightTrait C-GLOBAL: kCTFontWeightTrait
C-GLOBAL: kCTFontWidthTrait C-GLOBAL: kCTFontWidthTrait
@ -88,6 +97,14 @@ FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, C
FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ; FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
FUNCTION: CTFontRef CTFontCreateCopyWithSymbolicTraits (
CTFontRef font,
CGFloat size,
CGAffineTransform* matrix,
uint32_t symTraitValue,
uint32_t symTraitMask
) ;
: <CTLine> ( string font -- line ) : <CTLine> ( string font -- line )
[ [
kCTFontAttributeName associate <CFAttributedString> &CFRelease kCTFontAttributeName associate <CFAttributedString> &CFRelease

View File

@ -2,9 +2,8 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: assocs accessors alien core-graphics.types core-text kernel USING: assocs accessors alien core-graphics.types core-text kernel
hashtables namespaces sequences ui.gadgets.worlds ui.render hashtables namespaces sequences ui.gadgets.worlds ui.render
opengl opengl.gl destructors combinators combinators.smart opengl opengl.gl destructors combinators core-foundation
core-foundation core-foundation.dictionaries core-foundation.numbers core-foundation.strings io.styles memoize math ;
core-foundation.strings io.styles memoize ;
IN: ui.cocoa.text IN: ui.cocoa.text
SINGLETON: core-text-renderer SINGLETON: core-text-renderer
@ -16,44 +15,34 @@ CONSTANT: font-names
{ "serif" "Times" } { "serif" "Times" }
} }
: (bold) ( -- ) 1.0 kCTFontWeightTrait set ; : font-name ( string -- string' )
font-names at-default ;
: (italic) ( -- ) 1.0 kCTFontSlantTrait set ; : (bold) ( x -- y ) kCTFontBoldTrait bitor ; inline
: font-traits ( style -- dictionary ) : (italic) ( x -- y ) kCTFontItalicTrait bitor ; inline
[
{
{ plain [ ] }
{ bold [ (bold) ] }
{ italic [ (italic) ] }
{ bold-italic [ (bold) (italic) ] }
} case
] H{ } make-assoc ;
: font-name-attr ( name -- ) : font-traits ( style -- mask )
font-names at-default kCTFontNameAttribute set ; [ 0 ] dip {
{ plain [ ] }
{ bold [ (bold) ] }
{ italic [ (italic) ] }
{ bold-italic [ (bold) (italic) ] }
} case ;
: font-traits-attr ( style -- ) : apply-font-traits ( font style -- font' )
font-traits kCTFontTraitsAttribute set ; [ drop ] [ [ 0.0 f ] dip font-traits dup ] 2bi
CTFontCreateCopyWithSymbolicTraits
: font-size-attr ( size -- ) dup [ [ CFRelease ] dip ] [ drop ] if ;
kCTFontSizeAttribute set ;
MEMO: cache-font ( font -- open-font )
: font-attrs ( font -- dictionary )
[ [
[ [
[ [ first font-name <CFString> &CFRelease ] [ third ] bi
[ font-name-attr ] f CTFontCreateWithName
[ font-traits-attr ] ] [ second ] bi apply-font-traits
[ font-size-attr ]
tri*
] input<sequence
] H{ } make-assoc
] with-destructors ; ] with-destructors ;
MEMO: cache-font ( font -- open-font )
font-attrs <CTFont> ;
M: core-text-renderer open-font M: core-text-renderer open-font
dup alien? [ cache-font ] unless ; dup alien? [ cache-font ] unless ;