]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
Add foreground and background color slots to font tuple
[factor.git] / basis / core-text / core-text.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays alien alien.c-types alien.syntax kernel
4 destructors accessors fry words hashtables
5 sequences memoize assocs math math.functions locals init
6 namespaces combinators fonts colors core-foundation
7 core-foundation.strings core-foundation.attributed-strings
8 core-foundation.utilities core-graphics core-graphics.types
9 core-text.fonts core-text.utilities ;
10 IN: core-text
11
12 TYPEDEF: void* CTLineRef
13
14 C-GLOBAL: kCTFontAttributeName
15 C-GLOBAL: kCTKernAttributeName
16 C-GLOBAL: kCTLigatureAttributeName
17 C-GLOBAL: kCTForegroundColorAttributeName
18 C-GLOBAL: kCTParagraphStyleAttributeName
19 C-GLOBAL: kCTUnderlineStyleAttributeName
20 C-GLOBAL: kCTVerticalFormsAttributeName
21 C-GLOBAL: kCTGlyphInfoAttributeName
22
23 FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) ;
24
25 FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
26
27 FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ) ;
28
29 FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position ) ;
30
31 FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading ) ;
32
33 FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
34
35 : <CTLine> ( string open-font color -- line )
36     [
37         [
38             kCTForegroundColorAttributeName set
39             kCTFontAttributeName set
40         ] H{ } make-assoc <CFAttributedString> &CFRelease
41         CTLineCreateWithAttributedString
42     ] with-destructors ;
43
44 TUPLE: line font line bounds dim bitmap age refs disposed ;
45
46 TUPLE: typographic-bounds width ascent descent leading ;
47
48 : line-typographic-bounds ( line -- typographic-bounds )
49     0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
50     [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
51     typographic-bounds boa ;
52
53 : bounds>dim ( bounds -- dim )
54     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
55     [ ceiling >fixnum ]
56     bi@ 2array ;
57
58 :: <line> ( string font -- line )
59     [
60         [let* | open-font [ font cache-font CFRetain |CFRelease ]
61                 line [ string open-font font foreground>> <CTLine> |CFRelease ]
62                 bounds [ line line-typographic-bounds ]
63                 dim [ bounds bounds>dim ] |
64             dim [
65                 {
66                     [ font background>> >rgba-components CGContextSetRGBFillColor ]
67                     [ 0 0 dim first2 <CGRect> CGContextFillRect ]
68                     [ 0 bounds descent>> CGContextSetTextPosition ]
69                     [ line swap CTLineDraw ]
70                 } cleave
71             ] with-bitmap-context
72             [ open-font line bounds dim ] dip 0 0 f
73         ]
74         line boa
75     ] with-destructors ;
76
77 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
78
79 : ref/unref-line ( line n -- )
80     '[ _ + ] change-refs 0 >>age drop ;
81
82 : ref-line ( line -- ) 1 ref/unref-line ;
83 : unref-line ( line -- ) -1 ref/unref-line ;
84
85 SYMBOL: cached-lines
86
87 : cached-line ( string font -- line )
88     cached-lines get [ <line> ] 2cache ;
89
90 CONSTANT: max-line-age 10
91
92 : age ( obj -- ? )
93     [ 1+ ] change-age age>> max-line-age >= ;
94
95 : age-line ( line -- ? )
96     #! Outputs t whether the line is dead.
97     dup refs>> 0 = [ age ] [ drop f ] if ;
98
99 : age-assoc ( assoc quot -- assoc' )
100     '[ nip @ ] assoc-partition
101     [ values dispose-each ] dip ;
102
103 : age-lines ( -- )
104     cached-lines global [ [ age-line ] age-assoc ] change-at ;
105
106 [ H{ } clone cached-lines set-global ] "core-text" add-init-hook