]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
Clean up Core Text rendering code, and factor our basis/cache and basis/opengl/textur...
[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
4 destructors accessors fry words hashtables strings
5 sequences memoize assocs math math.functions locals init
6 namespaces combinators fonts colors cache core-foundation
7 core-foundation.strings core-foundation.attributed-strings
8 core-foundation.utilities core-graphics core-graphics.types
9 core-text.fonts core-text.utilities ;
10 IN: core-text
11
12 TYPEDEF: void* CTLineRef
13
14 C-GLOBAL: kCTFontAttributeName
15 C-GLOBAL: kCTKernAttributeName
16 C-GLOBAL: kCTLigatureAttributeName
17 C-GLOBAL: kCTForegroundColorAttributeName
18 C-GLOBAL: kCTParagraphStyleAttributeName
19 C-GLOBAL: kCTUnderlineStyleAttributeName
20 C-GLOBAL: kCTVerticalFormsAttributeName
21 C-GLOBAL: 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 ERROR: not-a-string object ;
36
37 : <CTLine> ( string open-font color -- line )
38     [
39         [
40             dup selection? [ string>> ] when
41             dup string? [ not-a-string ] unless
42         ] 2dip
43         [
44             kCTForegroundColorAttributeName set
45             kCTFontAttributeName set
46         ] H{ } make-assoc <CFAttributedString> &CFRelease
47         CTLineCreateWithAttributedString
48     ] with-destructors ;
49
50 TUPLE: line font line metrics dim bitmap age disposed ;
51
52 : compute-line-metrics ( line -- line-metrics )
53     0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
54     [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
55     metrics boa ;
56
57 : bounds>dim ( bounds -- dim )
58     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
59     [ ceiling >fixnum ]
60     bi@ 2array ;
61
62 : fill-background ( context font dim -- )
63     [ background>> >rgba-components CGContextSetRGBFillColor ]
64     [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
65     bi-curry* bi ;
66
67 : selection-rect ( dim line selection -- rect )
68     [ start>> ] [ end>> ] bi
69     [ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
70     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
71
72 :: fill-selection-background ( context dim line string -- )
73     string selection? [
74         context string color>> >rgba-components CGContextSetRGBFillColor
75         context dim line string selection-rect CGContextFillRect
76     ] when ;
77
78 : set-text-position ( context metrics -- )
79     [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
80
81 :: <line> ( font string -- line )
82     [
83         [let* | open-font [ font cache-font CFRetain |CFRelease ]
84                 line [ string open-font font foreground>> <CTLine> |CFRelease ]
85                 metrics [ line compute-line-metrics ]
86                 dim [ metrics bounds>dim ] |
87             dim [
88                 {
89                     [ font dim fill-background ]
90                     [ dim line string fill-selection-background ]
91                     [ metrics set-text-position ]
92                     [ [ line ] dip CTLineDraw ]
93                 } cleave
94             ] with-bitmap-context
95             [ open-font line metrics dim ] dip 0 f
96         ]
97         line boa
98     ] with-destructors ;
99
100 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
101
102 SYMBOL: cached-lines
103
104 : cached-line ( font string -- line )
105     cached-lines get [ <line> ] 2cache ;
106
107 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook