Add support for rendering selected text to ui.text

db4
Slava Pestov 2009-02-07 18:05:11 -06:00
parent c04ee453e3
commit 176629055e
3 changed files with 32 additions and 6 deletions

View File

@ -34,6 +34,7 @@ FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
: <CTLine> ( string open-font color -- line ) : <CTLine> ( string open-font color -- line )
[ [
[ dup selection? [ string>> ] when ] 2dip
[ [
kCTForegroundColorAttributeName set kCTForegroundColorAttributeName set
kCTFontAttributeName set kCTFontAttributeName set
@ -53,6 +54,25 @@ TUPLE: line font line metrics dim bitmap age refs disposed ;
[ ceiling >fixnum ] [ ceiling >fixnum ]
bi@ 2array ; bi@ 2array ;
: fill-background ( context font dim -- )
[ background>> >rgba-components CGContextSetRGBFillColor ]
[ [ 0 0 ] dip first2 <CGRect> CGContextFillRect ]
bi-curry* bi ;
: selection-rect ( dim line selection -- rect )
[ start>> ] [ end>> ] bi
[ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
[ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
:: fill-selection-background ( context dim line string -- )
string selection? [
context string color>> >rgba-components CGContextSetRGBFillColor
context dim line string selection-rect CGContextFillRect
] when ;
: set-text-position ( context metrics -- )
[ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
:: <line> ( font string -- line ) :: <line> ( font string -- line )
[ [
[let* | open-font [ font cache-font CFRetain |CFRelease ] [let* | open-font [ font cache-font CFRetain |CFRelease ]
@ -61,10 +81,10 @@ TUPLE: line font line metrics dim bitmap age refs disposed ;
dim [ metrics bounds>dim ] | dim [ metrics bounds>dim ] |
dim [ dim [
{ {
[ font background>> >rgba-components CGContextSetRGBFillColor ] [ font dim fill-background ]
[ 0 0 dim first2 <CGRect> CGContextFillRect ] [ dim line string fill-selection-background ]
[ 0 metrics descent>> ceiling CGContextSetTextPosition ] [ metrics set-text-position ]
[ line swap CTLineDraw ] [ [ line ] dip CTLineDraw ]
} cleave } cleave
] with-bitmap-context ] with-bitmap-context
[ open-font line metrics dim ] dip 0 0 f [ open-font line metrics dim ] dip 0 0 f

View File

@ -56,3 +56,7 @@ italic?
12 >>size ; 12 >>size ;
TUPLE: metrics width ascent descent leading ; TUPLE: metrics width ascent descent leading ;
TUPLE: selection string start end color ;
C: <selection> selection

View File

@ -1,7 +1,7 @@
! Copyright (C) 2009 Slava Pestov. ! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel arrays sequences math math.order opengl opengl.gl USING: kernel arrays sequences math math.order opengl opengl.gl
strings fonts colors ; strings fonts colors accessors ;
IN: ui.text IN: ui.text
<PRIVATE <PRIVATE
@ -56,6 +56,8 @@ GENERIC# draw-text 1 ( font text loc -- )
M: string draw-text draw-string ; M: string draw-text draw-string ;
M: selection draw-text draw-string ;
M: sequence draw-text M: sequence draw-text
[ [
[ [