]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
io.encodings.utf16: add a utf16n word for native utf16 type.
[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 io.encodings.string io.encodings.utf16 kernel locals
8 make math math.functions math.order math.vectors memoize
9 namespaces sequences strings ;
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 ERROR: not-a-string object ;
38
39 MEMO: make-attributes ( open-font color -- hashtable )
40     [
41         kCTForegroundColorAttributeName ,,
42         kCTFontAttributeName ,,
43     ] H{ } make ;
44
45 : <CTLine> ( string open-font color -- line )
46     [
47         [
48             dup selection? [ string>> ] when
49             dup string? [ not-a-string ] unless
50         ] 2dip
51         make-attributes <CFAttributedString> &CFRelease
52         CTLineCreateWithAttributedString
53     ] with-destructors ;
54
55 TUPLE: line < disposable font string line metrics image loc dim
56 render-loc render-dim ;
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     [let [ start>> ] [ end>> ] [ string>> ] tri :> ( start end string )
93      start end [ 0 swap string subseq utf16n encode length 2 / >integer ] bi@
94     ]
95     [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
96     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
97
98 : CGRect-translate-x ( CGRect x -- CGRect' )
99     [ dup CGRect-x ] dip - over set-CGRect-x ;
100
101 :: fill-selection-background ( context loc dim line string -- )
102     string selection? [
103         context string color>> >rgba-components CGContextSetRGBFillColor
104         context dim line string selection-rect
105         loc first CGRect-translate-x
106         CGContextFillRect
107     ] when ;
108
109 : line-rect ( line -- rect )
110     dummy-context CTLineGetImageBounds ;
111
112 : set-text-position ( context loc -- )
113     first2 [ neg ] bi@ CGContextSetTextPosition ;
114
115 :: line-loc ( metrics loc dim -- loc )
116     loc first
117     metrics ascent>> ceiling dim second loc second + - 2array ;
118
119 :: <line> ( font string -- line )
120     [
121         line new-disposable
122         font retina? get-global [ cache-font@2x ] [ cache-font ] if :> open-font
123         string open-font font foreground>> <CTLine> |CFRelease :> line
124         open-font line compute-line-metrics
125         [ >>metrics ] [ metrics>dim >>dim ] bi
126         font >>font
127         string >>string
128         line >>line
129     ] with-destructors ;
130
131 :: render ( line -- line image )
132     line line>> :> ctline
133     line string>> :> string
134     line font>> :> font
135
136     line render-loc>> [
137
138         ctline line-rect :> rect
139         rect origin>> CGPoint>loc :> (loc)
140         rect size>> CGSize>dim :> (dim)
141         (loc) vfloor :> loc
142         (loc) (dim) v+ vceiling :> ext
143         ext loc [ - >integer 1 max ] 2map :> dim
144
145         loc line render-loc<<
146         dim line render-dim<<
147
148         line metrics>> loc dim line-loc line loc<<
149
150     ] unless
151
152     line render-loc>> :> loc
153     line render-dim>> :> dim
154
155     line dim [
156         {
157             [ font dim fill-background ]
158             [ loc dim ctline string fill-selection-background ]
159             [ loc set-text-position ]
160             [ [ ctline ] dip CTLineDraw ]
161         } cleave
162     ] make-bitmap-image retina? get-global >>2x? ;
163
164 : line>image ( line -- image )
165     dup image>> [ render >>image ] unless image>> ;
166
167 M: line dispose* line>> CFRelease ;
168
169 SYMBOL: cached-lines
170
171 : cached-line ( font string -- line )
172     cached-lines get-global [ <line> ] 2cache ;
173
174 [ <cache-assoc> cached-lines set-global f retina? set-global ] "core-text" add-startup-hook