]> gitweb.factorcode.org Git - factor.git/blob - basis/core-text/core-text.factor
Fix conflict
[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 images endian 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 disposed ;
50
51 : compute-line-metrics ( line -- line-metrics )
52     0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
53     [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
54     metrics boa ;
55
56 : bounds>dim ( bounds -- dim )
57     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
58     [ ceiling >fixnum ]
59     bi@ 2array ;
60
61 : fill-background ( context font dim -- )
62     [ background>> >rgba-components CGContextSetRGBFillColor ]
63     [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
64     bi-curry* bi ;
65
66 : selection-rect ( dim line selection -- rect )
67     [ start>> ] [ end>> ] bi
68     [ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
69     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
70
71 :: fill-selection-background ( context dim line string -- )
72     string selection? [
73         context string color>> >rgba-components CGContextSetRGBFillColor
74         context dim line string selection-rect CGContextFillRect
75     ] when ;
76
77 : set-text-position ( context metrics -- )
78     [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
79
80 : <line-image> ( dim bitmap -- image )
81     <image>
82         swap >>bitmap
83         swap >>dim
84         BGRA >>component-order
85         native-endianness >>byte-order ;
86
87 :: <line> ( font string -- line )
88     [
89         [let* | open-font [ font cache-font CFRetain |CFRelease ]
90                 line [ string open-font font foreground>> <CTLine> |CFRelease ]
91                 metrics [ line compute-line-metrics ]
92                 dim [ metrics bounds>dim ] |
93             dim [
94                 {
95                     [ font dim fill-background ]
96                     [ dim line string fill-selection-background ]
97                     [ metrics set-text-position ]
98                     [ [ line ] dip CTLineDraw ]
99                 } cleave
100             ] with-bitmap-context
101             [ open-font line metrics dim ] dip <line-image>
102         ]
103         f line boa
104     ] with-destructors ;
105
106 M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
107
108 SYMBOL: cached-lines
109
110 : cached-line ( font string -- line )
111     cached-lines get [ <line> ] 2cache ;
112
113 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook