]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
basis/extra: replace "/ >integer" with "/i" in a few places.
[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 classes 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 io.encodings.utf16n io.encodings.string ;
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 SYMBOL: retina?
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             string check-instance
48         ] 2dip
49         make-attributes <CFAttributedString> &CFRelease
50         CTLineCreateWithAttributedString
51     ] with-destructors ;
52
53 TUPLE: line < disposable font string line metrics image loc dim
54 render-loc render-dim ;
55
56 : typographic-bounds ( line -- width ascent descent leading )
57     { CGFloat CGFloat CGFloat }
58     [ CTLineGetTypographicBounds ] with-out-parameters ; inline
59
60 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
61     {
62         [ >>width ]
63         [ >>ascent ]
64         [ >>descent ]
65         [ >>leading ]
66     } spread ; inline
67
68 : compute-font-metrics ( metrics font -- metrics )
69     [ CTFontGetCapHeight >>cap-height ]
70     [ CTFontGetXHeight >>x-height ]
71     bi ; inline
72
73 : compute-line-metrics ( open-font line -- line-metrics )
74     [ metrics new ] 2dip
75     [ compute-font-metrics ]
76     [ typographic-bounds store-typographic-bounds ] bi*
77     compute-height ;
78
79 : metrics>dim ( bounds -- dim )
80     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi 2array ;
81
82 : fill-background ( context font dim -- )
83     [ background>> >rgba-components CGContextSetRGBFillColor ]
84     [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
85     bi-curry* bi ;
86
87 : selection-rect ( dim line selection -- rect )
88     [let [ start>> ] [ end>> ] [ string>> ] tri :> ( start end string )
89      start end [ 0 swap string subseq utf16n encode length 2 /i ] bi@
90     ]
91     [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
92     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
93
94 : CGRect-translate-x ( CGRect x -- CGRect' )
95     [ dup CGRect-x ] dip - over set-CGRect-x ;
96
97 :: fill-selection-background ( context loc dim line string -- )
98     string selection? [
99         context string color>> >rgba-components CGContextSetRGBFillColor
100         context dim line string selection-rect
101         loc first CGRect-translate-x
102         CGContextFillRect
103     ] when ;
104
105 : line-rect ( line -- rect )
106     dummy-context CTLineGetImageBounds ;
107
108 : set-text-position ( context loc -- )
109     first2 [ neg ] bi@ CGContextSetTextPosition ;
110
111 :: line-loc ( metrics loc dim -- loc )
112     loc first
113     metrics ascent>> ceiling dim second loc second + - 2array ;
114
115 :: <line> ( font string -- line )
116     [
117         line new-disposable
118         font retina? get-global [ cache-font@2x ] [ cache-font ] if :> open-font
119         string open-font font foreground>> <CTLine> |CFRelease :> line
120         open-font line compute-line-metrics
121         [ >>metrics ] [ metrics>dim >>dim ] bi
122         font >>font
123         string >>string
124         line >>line
125     ] with-destructors ;
126
127 :: render ( line -- line image )
128     line line>> :> ctline
129     line string>> :> string
130     line font>> :> font
131
132     line render-loc>> [
133
134         ctline line-rect :> rect
135         rect origin>> CGPoint>loc :> (loc)
136         rect size>> CGSize>dim :> (dim)
137         (loc) vfloor :> loc
138         (loc) (dim) v+ vceiling :> ext
139         ext loc [ - >integer 1 max ] 2map :> dim
140
141         loc line render-loc<<
142         dim line render-dim<<
143
144         line metrics>> loc dim line-loc line loc<<
145
146     ] unless
147
148     line render-loc>> :> loc
149     line render-dim>> :> dim
150
151     line dim [
152         {
153             [ font dim fill-background ]
154             [ loc dim ctline string fill-selection-background ]
155             [ loc set-text-position ]
156             [ [ ctline ] dip CTLineDraw ]
157         } cleave
158     ] make-bitmap-image retina? get-global >>2x? ;
159
160 : line>image ( line -- image )
161     dup image>> [ render >>image ] unless image>> ;
162
163 M: line dispose* line>> CFRelease ;
164
165 SYMBOL: cached-lines
166
167 : cached-line ( font string -- line )
168     cached-lines get-global [ <line> ] 2cache ;
169
170 [ <cache-assoc> cached-lines set-global f retina? set-global ] "core-text" add-startup-hook