]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
Merge OneEyed's patch
[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 font line metrics image loc dim disposed ;
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 :: fill-selection-background ( context loc dim line string -- )
90     string selection? [
91         context string color>> >rgba-components CGContextSetRGBFillColor
92         context dim line string selection-rect
93         dup CGRect-x loc first - over set-CGRect-x
94         CGContextFillRect
95     ] when ;
96
97 : line-rect ( line -- rect )
98     dummy-context CTLineGetImageBounds ;
99
100 : set-text-position ( context loc -- )
101     first2 [ neg ] bi@ CGContextSetTextPosition ;
102
103 :: line-loc ( metrics loc dim -- loc )
104     loc first
105     metrics ascent>> ceiling dim second loc second + - 2array ;
106
107 :: <line> ( font string -- line )
108     [
109         [let* | open-font [ font cache-font CFRetain |CFRelease ]
110                 line [ string open-font font foreground>> <CTLine> |CFRelease ]
111
112                 rect [ line line-rect ]
113                 (loc) [ rect CGRect-origin CGPoint>loc ]
114                 (dim) [ rect CGRect-size CGSize>dim ]
115                 (ext) [ (loc) (dim) v+ ]
116                 loc [ (loc) [ floor ] map ]
117                 ext [ (loc) (dim) [ + ceiling ] 2map ]
118                 dim [ ext loc [ - >integer ] 2map ]
119                 metrics [ open-font line compute-line-metrics ] |
120             open-font line metrics
121             dim [
122                 {
123                     [ font dim fill-background ]
124                     [ loc dim line string fill-selection-background ]
125                     [ loc set-text-position ]
126                     [ [ line ] dip CTLineDraw ]
127                 } cleave
128             ] make-bitmap-image
129             metrics loc dim line-loc
130             metrics metrics>dim
131         ]
132         f line boa
133     ] with-destructors ;
134
135 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
136
137 SYMBOL: cached-lines
138
139 : cached-line ( font string -- line )
140     cached-lines get [ <line> ] 2cache ;
141
142 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook