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.order math.vectors math.rectangles 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
12 TYPEDEF: void* CTLineRef
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
23 FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) ;
25 FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
27 FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ) ;
29 FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position ) ;
31 FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading ) ;
33 FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
35 ERROR: not-a-string object ;
37 : <CTLine> ( string open-font color -- line )
40 dup selection? [ string>> ] when
41 dup string? [ not-a-string ] unless
44 kCTForegroundColorAttributeName set
45 kCTFontAttributeName set
46 ] H{ } make-assoc <CFAttributedString> &CFRelease
47 CTLineCreateWithAttributedString
50 TUPLE: line < disposable line metrics image loc dim ;
52 : typographic-bounds ( line -- width ascent descent leading )
53 0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
54 [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@ ; inline
56 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
64 : compute-font-metrics ( metrics font -- metrics )
65 [ CTFontGetCapHeight >>cap-height ]
66 [ CTFontGetXHeight >>x-height ]
69 : compute-line-metrics ( open-font line -- line-metrics )
71 [ compute-font-metrics ]
72 [ typographic-bounds store-typographic-bounds ] bi*
75 : metrics>dim ( bounds -- dim )
76 [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
80 : fill-background ( context font dim -- )
81 [ background>> >rgba-components CGContextSetRGBFillColor ]
82 [ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
85 : selection-rect ( dim line selection -- rect )
86 [ start>> ] [ end>> ] bi
87 [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
88 [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
90 : CGRect-translate-x ( CGRect x -- CGRect' )
91 [ dup CGRect-x ] dip - over set-CGRect-x ;
93 :: fill-selection-background ( context loc dim line string -- )
95 context string color>> >rgba-components CGContextSetRGBFillColor
96 context dim line string selection-rect
97 loc first CGRect-translate-x
101 : line-rect ( line -- rect )
102 dummy-context CTLineGetImageBounds ;
104 : set-text-position ( context loc -- )
105 first2 [ neg ] bi@ CGContextSetTextPosition ;
107 :: line-loc ( metrics loc dim -- loc )
109 metrics ascent>> ceiling dim second loc second + - 2array ;
111 :: <line> ( font string -- line )
115 font cache-font :> open-font
116 string open-font font foreground>> <CTLine> |CFRelease :> line
118 line line-rect :> rect
119 rect origin>> CGPoint>loc :> (loc)
120 rect size>> CGSize>dim :> (dim)
121 (loc) (dim) v+ :> (ext)
122 (loc) [ floor ] map :> loc
123 (loc) (dim) [ + ceiling ] 2map :> ext
124 ext loc [ - >integer 1 max ] 2map :> dim
125 open-font line compute-line-metrics :> metrics
133 [ font dim fill-background ]
134 [ loc dim line string fill-selection-background ]
135 [ loc set-text-position ]
136 [ [ line ] dip CTLineDraw ]
138 ] make-bitmap-image >>image
140 metrics loc dim line-loc >>loc
142 metrics metrics>dim >>dim
145 M: line dispose* line>> CFRelease ;
149 : cached-line ( font string -- line )
150 cached-lines get [ <line> ] 2cache ;
152 [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook