factor/basis/core-text/core-text.factor

154 lines
4.9 KiB
Factor
Raw Normal View History

2009-01-16 20:11:48 -05:00
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays alien alien.c-types alien.syntax kernel destructors
accessors fry words hashtables strings sequences memoize assocs math
math.order math.vectors math.rectangles math.functions locals init
namespaces combinators fonts colors cache 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
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 ) ;
FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
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
ERROR: not-a-string object ;
: <CTLine> ( string open-font color -- line )
2009-01-19 23:30:32 -05:00
[
[
dup selection? [ string>> ] when
dup string? [ not-a-string ] unless
] 2dip
[
kCTForegroundColorAttributeName set
kCTFontAttributeName set
] H{ } make-assoc <CFAttributedString> &CFRelease
2009-01-19 23:30:32 -05:00
CTLineCreateWithAttributedString
] with-destructors ;
TUPLE: line < disposable line metrics image loc dim ;
: typographic-bounds ( line -- width ascent descent leading )
2009-01-19 23:30:32 -05:00
0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@ ; inline
: store-typographic-bounds ( metrics width ascent descent leading -- metrics )
2009-02-15 03:13:16 -05:00
{
[ >>width ]
[ >>ascent ]
[ >>descent ]
[ >>leading ]
} spread ; inline
: compute-font-metrics ( metrics font -- metrics )
[ CTFontGetCapHeight >>cap-height ]
[ CTFontGetXHeight >>x-height ]
bi ; inline
: compute-line-metrics ( open-font line -- line-metrics )
[ metrics new ] 2dip
[ compute-font-metrics ]
[ typographic-bounds store-typographic-bounds ] bi*
compute-height ;
2009-01-19 23:30:32 -05:00
: metrics>dim ( bounds -- dim )
2009-01-19 23:30:32 -05:00
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
2009-02-15 03:13:16 -05:00
[ ceiling >integer ]
2009-01-19 23:30:32 -05:00
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
2009-02-14 22:51:54 -05:00
[ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
[ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
: CGRect-translate-x ( CGRect x -- CGRect' )
[ dup CGRect-x ] dip - over set-CGRect-x ;
:: fill-selection-background ( context loc dim line string -- )
string selection? [
context string color>> >rgba-components CGContextSetRGBFillColor
context dim line string selection-rect
loc first CGRect-translate-x
CGContextFillRect
] when ;
: line-rect ( line -- rect )
dummy-context CTLineGetImageBounds ;
: set-text-position ( context loc -- )
first2 [ neg ] bi@ CGContextSetTextPosition ;
:: line-loc ( metrics loc dim -- loc )
loc first
metrics ascent>> ceiling dim second loc second + - 2array ;
:: <line> ( font string -- line )
2009-01-19 23:30:32 -05:00
[
line new-disposable
[let* | open-font [ font cache-font ]
line [ string open-font font foreground>> <CTLine> |CFRelease ]
rect [ line line-rect ]
(loc) [ rect origin>> CGPoint>loc ]
(dim) [ rect size>> CGSize>dim ]
(ext) [ (loc) (dim) v+ ]
loc [ (loc) [ floor ] map ]
ext [ (loc) (dim) [ + ceiling ] 2map ]
dim [ ext loc [ - >integer 1 max ] 2map ]
metrics [ open-font line compute-line-metrics ] |
line >>line
metrics >>metrics
dim [
{
[ font dim fill-background ]
[ loc dim line string fill-selection-background ]
[ loc set-text-position ]
[ [ line ] dip CTLineDraw ]
} cleave
] make-bitmap-image >>image
metrics loc dim line-loc >>loc
metrics metrics>dim >>dim
]
2009-01-19 23:30:32 -05:00
] with-destructors ;
M: line dispose* line>> CFRelease ;
2009-01-27 00:11:45 -05:00
SYMBOL: cached-lines
: cached-line ( font string -- line )
cached-lines get [ <line> ] 2cache ;
[ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook