]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
core-text: use cached-lines get-global.
[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.data alien.syntax kernel
4 destructors accessors fry words hashtables strings sequences
5 memoize assocs make math math.order math.vectors math.rectangles
6 math.functions locals init namespaces combinators fonts colors
7 cache core-foundation core-foundation.strings
8 core-foundation.attributed-strings core-foundation.utilities
9 core-graphics core-graphics.types core-text.fonts ;
10 IN: core-text
11
12 TYPEDEF: void* CTLineRef
13
14 C-GLOBAL: CFStringRef kCTFontAttributeName
15 C-GLOBAL: CFStringRef kCTKernAttributeName
16 C-GLOBAL: CFStringRef kCTLigatureAttributeName
17 C-GLOBAL: CFStringRef kCTForegroundColorAttributeName
18 C-GLOBAL: CFStringRef kCTParagraphStyleAttributeName
19 C-GLOBAL: CFStringRef kCTUnderlineStyleAttributeName
20 C-GLOBAL: CFStringRef kCTVerticalFormsAttributeName
21 C-GLOBAL: CFStringRef 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 ERROR: not-a-string object ;
36
37 MEMO: make-attributes ( open-font color -- hashtable )
38     [
39         kCTForegroundColorAttributeName ,,
40         kCTFontAttributeName ,,
41     ] H{ } make ;
42
43 : <CTLine> ( string open-font color -- line )
44     [
45         [
46             dup selection? [ string>> ] when
47             dup string? [ not-a-string ] unless
48         ] 2dip
49         make-attributes <CFAttributedString> &CFRelease
50         CTLineCreateWithAttributedString
51     ] with-destructors ;
52
53 TUPLE: line < disposable line metrics image loc dim rendered-line ;
54
55 TUPLE: rendered-line font string loc dim ;
56 C: <rendered-line> rendered-line
57
58 : typographic-bounds ( line -- width ascent descent leading )
59     { CGFloat CGFloat CGFloat }
60     [ CTLineGetTypographicBounds ] with-out-parameters ; inline
61
62 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
63     {
64         [ >>width ]
65         [ >>ascent ]
66         [ >>descent ]
67         [ >>leading ]
68     } spread ; inline
69
70 : compute-font-metrics ( metrics font -- metrics )
71     [ CTFontGetCapHeight >>cap-height ]
72     [ CTFontGetXHeight >>x-height ]
73     bi ; inline
74
75 : compute-line-metrics ( open-font line -- line-metrics )
76     [ metrics new ] 2dip
77     [ compute-font-metrics ]
78     [ typographic-bounds store-typographic-bounds ] bi*
79     compute-height ;
80
81 : metrics>dim ( bounds -- dim )
82     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
83     [ ceiling >integer ]
84     bi@ 2array ;
85
86 : fill-background ( context font dim -- )
87     [ background>> >rgba-components CGContextSetRGBFillColor ]
88     [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
89     bi-curry* bi ;
90
91 : selection-rect ( dim line selection -- rect )
92     [ start>> ] [ end>> ] bi
93     [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
94     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
95
96 : CGRect-translate-x ( CGRect x -- CGRect' )
97     [ dup CGRect-x ] dip - over set-CGRect-x ;
98
99 :: fill-selection-background ( context loc dim line string -- )
100     string selection? [
101         context string color>> >rgba-components CGContextSetRGBFillColor
102         context dim line string selection-rect
103         loc first CGRect-translate-x
104         CGContextFillRect
105     ] when ;
106
107 : line-rect ( line -- rect )
108     dummy-context CTLineGetImageBounds ;
109
110 : set-text-position ( context loc -- )
111     first2 [ neg ] bi@ CGContextSetTextPosition ;
112
113 :: line-loc ( metrics loc dim -- loc )
114     loc first
115     metrics ascent>> ceiling dim second loc second + - 2array ;
116
117 :: <line> ( font string -- line )
118     [
119         line new-disposable
120
121         font cache-font :> open-font
122         string open-font font foreground>> <CTLine> |CFRelease :> line
123
124         line line-rect :> rect
125         rect origin>> CGPoint>loc :> (loc)
126         rect size>> CGSize>dim :> (dim)
127         (loc) [ floor ] map :> loc
128         (loc) (dim) [ + ceiling ] 2map :> ext
129         ext loc [ - >integer 1 max ] 2map :> dim
130         open-font line compute-line-metrics :> metrics
131
132         line >>line
133
134         font string loc dim <rendered-line> >>rendered-line
135
136         metrics >>metrics
137
138         metrics loc dim line-loc >>loc
139
140         metrics metrics>dim >>dim
141     ] with-destructors ;
142
143 :: render ( line -- line image )
144     line line>> :> ctline
145     line rendered-line>> string>> :> string
146     line rendered-line>> font>> :> font
147     line rendered-line>> loc>> :> loc
148     line rendered-line>> dim>> :> dim
149
150     line dim [
151         {
152             [ font dim fill-background ]
153             [ loc dim ctline string fill-selection-background ]
154             [ loc set-text-position ]
155             [ [ ctline ] dip CTLineDraw ]
156         } cleave
157     ] make-bitmap-image ;
158
159 : line>image ( line -- image )
160     dup image>> [ render >>image ] unless image>> ;
161
162 M: line dispose* line>> CFRelease ;
163
164 SYMBOL: cached-lines
165
166 : cached-line ( font string -- line )
167     cached-lines get-global [ <line> ] 2cache ;
168
169 [ <cache-assoc> cached-lines set-global ] "core-text" add-startup-hook