]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
Merge branch 'master' into new_ui
[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.functions locals init namespaces combinators fonts colors cache
6 core-foundation core-foundation.strings core-foundation.attributed-strings
7 core-foundation.utilities core-graphics core-graphics.types
8 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 disposed ;
50
51 : compute-line-metrics ( open-font line -- line-metrics )
52     [
53         [ metrics new ] dip
54         [ CTFontGetCapHeight >>cap-height ]
55         [ CTFontGetXHeight >>x-height ]
56         bi
57     ] dip
58     0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
59     [ CTLineGetTypographicBounds ] 3keep
60     {
61         [ >>width ]
62         [ *CGFloat >>ascent ]
63         [ *CGFloat >>descent ]
64         [ *CGFloat >>leading ]
65     } spread
66     dup compute-height ;
67
68 : bounds>dim ( bounds -- dim )
69     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
70     [ ceiling >integer ]
71     bi@ 2array ;
72
73 : fill-background ( context font dim -- )
74     [ background>> >rgba-components CGContextSetRGBFillColor ]
75     [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
76     bi-curry* bi ;
77
78 : selection-rect ( dim line selection -- rect )
79     [ start>> ] [ end>> ] bi
80     [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
81     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
82
83 :: fill-selection-background ( context dim line string -- )
84     string selection? [
85         context string color>> >rgba-components CGContextSetRGBFillColor
86         context dim line string selection-rect CGContextFillRect
87     ] when ;
88
89 : set-text-position ( context metrics -- )
90     [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
91
92 :: <line> ( font string -- line )
93     [
94         [let* | open-font [ font cache-font CFRetain |CFRelease ]
95                 line [ string open-font font foreground>> <CTLine> |CFRelease ]
96                 metrics [ open-font line compute-line-metrics ]
97                 dim [ metrics bounds>dim ] |
98             open-font line metrics
99             dim [
100                 {
101                     [ font dim fill-background ]
102                     [ dim line string fill-selection-background ]
103                     [ metrics set-text-position ]
104                     [ [ line ] dip CTLineDraw ]
105                 } cleave
106             ] make-bitmap-image
107         ]
108         f line boa
109     ] with-destructors ;
110
111 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
112
113 SYMBOL: cached-lines
114
115 : cached-line ( font string -- line )
116     cached-lines get [ <line> ] 2cache ;
117
118 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook