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