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