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