2009-01-16 20:11:48 -05:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-12 04:58:05 -05:00
|
|
|
USING: arrays alien alien.c-types alien.syntax kernel destructors
|
|
|
|
accessors fry words hashtables strings sequences memoize assocs math
|
|
|
|
math.functions locals init namespaces combinators fonts colors cache
|
|
|
|
images endian core-foundation core-foundation.strings
|
|
|
|
core-foundation.attributed-strings core-foundation.utilities
|
|
|
|
core-graphics core-graphics.types core-text.fonts core-text.utilities ;
|
2009-01-16 20:11:48 -05:00
|
|
|
IN: core-text
|
|
|
|
|
|
|
|
TYPEDEF: void* CTLineRef
|
2009-01-21 00:06:23 -05:00
|
|
|
|
2009-01-16 23:37:56 -05:00
|
|
|
C-GLOBAL: kCTFontAttributeName
|
|
|
|
C-GLOBAL: kCTKernAttributeName
|
|
|
|
C-GLOBAL: kCTLigatureAttributeName
|
|
|
|
C-GLOBAL: kCTForegroundColorAttributeName
|
|
|
|
C-GLOBAL: kCTParagraphStyleAttributeName
|
|
|
|
C-GLOBAL: kCTUnderlineStyleAttributeName
|
|
|
|
C-GLOBAL: kCTVerticalFormsAttributeName
|
|
|
|
C-GLOBAL: kCTGlyphInfoAttributeName
|
2009-01-16 20:11:48 -05:00
|
|
|
|
|
|
|
FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) ;
|
|
|
|
|
2009-01-16 23:37:56 -05:00
|
|
|
FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
|
2009-01-19 17:29:52 -05:00
|
|
|
|
|
|
|
FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ) ;
|
|
|
|
|
|
|
|
FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position ) ;
|
|
|
|
|
|
|
|
FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading ) ;
|
|
|
|
|
|
|
|
FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
|
2009-01-19 23:30:32 -05:00
|
|
|
|
2009-02-09 01:22:41 -05:00
|
|
|
ERROR: not-a-string object ;
|
|
|
|
|
2009-01-30 04:36:39 -05:00
|
|
|
: <CTLine> ( string open-font color -- line )
|
2009-01-19 23:30:32 -05:00
|
|
|
[
|
2009-02-09 01:22:41 -05:00
|
|
|
[
|
|
|
|
dup selection? [ string>> ] when
|
|
|
|
dup string? [ not-a-string ] unless
|
|
|
|
] 2dip
|
2009-01-21 20:34:42 -05:00
|
|
|
[
|
|
|
|
kCTForegroundColorAttributeName set
|
|
|
|
kCTFontAttributeName set
|
|
|
|
] H{ } make-assoc <CFAttributedString> &CFRelease
|
2009-01-19 23:30:32 -05:00
|
|
|
CTLineCreateWithAttributedString
|
|
|
|
] with-destructors ;
|
|
|
|
|
2009-02-12 04:58:05 -05:00
|
|
|
TUPLE: line font line metrics image disposed ;
|
2009-01-29 23:07:51 -05:00
|
|
|
|
2009-02-01 21:32:49 -05:00
|
|
|
: compute-line-metrics ( line -- line-metrics )
|
2009-01-19 23:30:32 -05:00
|
|
|
0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
|
|
|
|
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
|
2009-02-02 00:58:05 -05:00
|
|
|
metrics boa ;
|
2009-01-19 23:30:32 -05:00
|
|
|
|
|
|
|
: bounds>dim ( bounds -- dim )
|
|
|
|
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
|
|
|
|
[ ceiling >fixnum ]
|
|
|
|
bi@ 2array ;
|
|
|
|
|
2009-02-07 19:05:11 -05:00
|
|
|
: 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 ;
|
|
|
|
|
2009-02-12 04:58:05 -05:00
|
|
|
: <line-image> ( dim bitmap -- image )
|
|
|
|
<image>
|
|
|
|
swap >>bitmap
|
|
|
|
swap >>dim
|
|
|
|
BGRA >>component-order
|
|
|
|
native-endianness >>byte-order ;
|
|
|
|
|
2009-02-01 21:32:49 -05:00
|
|
|
:: <line> ( font string -- line )
|
2009-01-19 23:30:32 -05:00
|
|
|
[
|
2009-01-30 04:36:39 -05:00
|
|
|
[let* | open-font [ font cache-font CFRetain |CFRelease ]
|
|
|
|
line [ string open-font font foreground>> <CTLine> |CFRelease ]
|
2009-02-01 21:32:49 -05:00
|
|
|
metrics [ line compute-line-metrics ]
|
2009-02-02 00:58:05 -05:00
|
|
|
dim [ metrics bounds>dim ] |
|
2009-01-29 23:07:51 -05:00
|
|
|
dim [
|
|
|
|
{
|
2009-02-07 19:05:11 -05:00
|
|
|
[ font dim fill-background ]
|
|
|
|
[ dim line string fill-selection-background ]
|
|
|
|
[ metrics set-text-position ]
|
|
|
|
[ [ line ] dip CTLineDraw ]
|
2009-01-29 23:07:51 -05:00
|
|
|
} cleave
|
|
|
|
] with-bitmap-context
|
2009-02-12 04:58:05 -05:00
|
|
|
[ open-font line metrics dim ] dip <line-image>
|
2009-01-29 23:07:51 -05:00
|
|
|
]
|
2009-02-10 23:05:13 -05:00
|
|
|
f line boa
|
2009-01-19 23:30:32 -05:00
|
|
|
] with-destructors ;
|
|
|
|
|
2009-01-29 23:07:51 -05:00
|
|
|
M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
SYMBOL: cached-lines
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-02-01 21:32:49 -05:00
|
|
|
: cached-line ( font string -- line )
|
2009-01-30 04:36:39 -05:00
|
|
|
cached-lines get [ <line> ] 2cache ;
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-02-10 03:45:43 -05:00
|
|
|
[ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook
|