2009-01-16 20:11:48 -05:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-01-21 00:06:23 -05:00
|
|
|
USING: arrays alien alien.c-types alien.syntax kernel
|
|
|
|
destructors words parser accessors fry words hashtables
|
|
|
|
sequences memoize assocs math math.functions locals init
|
2009-01-29 23:07:51 -05:00
|
|
|
namespaces combinators colors core-foundation
|
|
|
|
core-foundation.strings core-foundation.attributed-strings
|
|
|
|
core-foundation.utilities core-graphics core-graphics.types ;
|
2009-01-16 20:11:48 -05:00
|
|
|
IN: core-text
|
|
|
|
|
|
|
|
TYPEDEF: void* CTLineRef
|
2009-01-16 23:37:56 -05:00
|
|
|
TYPEDEF: void* CTFontRef
|
2009-01-21 00:06:23 -05:00
|
|
|
TYPEDEF: void* CTFontDescriptorRef
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-16 23:37:56 -05:00
|
|
|
<<
|
|
|
|
|
|
|
|
: C-GLOBAL:
|
|
|
|
CREATE-WORD
|
|
|
|
dup name>> '[ _ f dlsym *void* ]
|
|
|
|
(( -- value )) define-declared ; parsing
|
|
|
|
|
|
|
|
>>
|
|
|
|
|
2009-01-21 01:39:32 -05:00
|
|
|
! CTFontSymbolicTraits
|
|
|
|
: kCTFontItalicTrait ( -- n ) 0 2^ ; inline
|
|
|
|
: kCTFontBoldTrait ( -- n ) 1 2^ ; inline
|
|
|
|
: kCTFontExpandedTrait ( -- n ) 5 2^ ; inline
|
|
|
|
: kCTFontCondensedTrait ( -- n ) 6 2^ ; inline
|
|
|
|
: kCTFontMonoSpaceTrait ( -- n ) 10 2^ ; inline
|
|
|
|
: kCTFontVerticalTrait ( -- n ) 11 2^ ; inline
|
|
|
|
: kCTFontUIOptimizedTrait ( -- n ) 12 2^ ; inline
|
|
|
|
|
2009-01-21 00:06:23 -05:00
|
|
|
C-GLOBAL: kCTFontSymbolicTrait
|
|
|
|
C-GLOBAL: kCTFontWeightTrait
|
|
|
|
C-GLOBAL: kCTFontWidthTrait
|
|
|
|
C-GLOBAL: kCTFontSlantTrait
|
|
|
|
|
|
|
|
C-GLOBAL: kCTFontNameAttribute
|
|
|
|
C-GLOBAL: kCTFontDisplayNameAttribute
|
|
|
|
C-GLOBAL: kCTFontFamilyNameAttribute
|
|
|
|
C-GLOBAL: kCTFontStyleNameAttribute
|
|
|
|
C-GLOBAL: kCTFontTraitsAttribute
|
|
|
|
C-GLOBAL: kCTFontVariationAttribute
|
|
|
|
C-GLOBAL: kCTFontSizeAttribute
|
|
|
|
C-GLOBAL: kCTFontMatrixAttribute
|
|
|
|
C-GLOBAL: kCTFontCascadeListAttribute
|
|
|
|
C-GLOBAL: kCTFontCharacterSetAttribute
|
|
|
|
C-GLOBAL: kCTFontLanguagesAttribute
|
|
|
|
C-GLOBAL: kCTFontBaselineAdjustAttribute
|
|
|
|
C-GLOBAL: kCTFontMacintoshEncodingsAttribute
|
|
|
|
C-GLOBAL: kCTFontFeaturesAttribute
|
|
|
|
C-GLOBAL: kCTFontFeatureSettingsAttribute
|
|
|
|
C-GLOBAL: kCTFontFixedAdvanceAttribute
|
|
|
|
C-GLOBAL: kCTFontOrientationAttribute
|
|
|
|
|
|
|
|
FUNCTION: CTFontDescriptorRef CTFontDescriptorCreateWithAttributes (
|
|
|
|
CFDictionaryRef attributes
|
|
|
|
) ;
|
|
|
|
|
|
|
|
FUNCTION: CTFontRef CTFontCreateWithName (
|
|
|
|
CFStringRef name,
|
|
|
|
CGFloat size,
|
|
|
|
CGAffineTransform* matrix
|
|
|
|
) ;
|
|
|
|
|
|
|
|
FUNCTION: CTFontRef CTFontCreateWithFontDescriptor (
|
|
|
|
CTFontDescriptorRef descriptor,
|
|
|
|
CGFloat size,
|
|
|
|
CGAffineTransform* matrix
|
|
|
|
) ;
|
|
|
|
|
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-01-21 01:39:32 -05:00
|
|
|
FUNCTION: CTFontRef CTFontCreateCopyWithSymbolicTraits (
|
|
|
|
CTFontRef font,
|
|
|
|
CGFloat size,
|
|
|
|
CGAffineTransform* matrix,
|
|
|
|
uint32_t symTraitValue,
|
|
|
|
uint32_t symTraitMask
|
|
|
|
) ;
|
|
|
|
|
2009-01-21 20:34:42 -05:00
|
|
|
: <CTLine> ( string font color -- line )
|
2009-01-19 23:30:32 -05:00
|
|
|
[
|
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-01-29 23:07:51 -05:00
|
|
|
TUPLE: line font line bounds dim bitmap age refs disposed ;
|
|
|
|
|
2009-01-19 23:30:32 -05:00
|
|
|
TUPLE: typographic-bounds width ascent descent leading ;
|
|
|
|
|
|
|
|
: line-typographic-bounds ( line -- typographic-bounds )
|
|
|
|
0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
|
|
|
|
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
|
|
|
|
typographic-bounds boa ;
|
|
|
|
|
|
|
|
: bounds>dim ( bounds -- dim )
|
|
|
|
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
|
|
|
|
[ ceiling >fixnum ]
|
|
|
|
bi@ 2array ;
|
|
|
|
|
2009-01-29 23:07:51 -05:00
|
|
|
:: <line> ( string font foreground background -- line )
|
2009-01-19 23:30:32 -05:00
|
|
|
[
|
2009-01-29 23:07:51 -05:00
|
|
|
[let* | font [ font CFRetain |CFRelease ]
|
|
|
|
line [ string font foreground <CTLine> |CFRelease ]
|
|
|
|
bounds [ line line-typographic-bounds ]
|
|
|
|
dim [ bounds bounds>dim ] |
|
|
|
|
dim [
|
|
|
|
{
|
|
|
|
[ background >rgba-components CGContextSetRGBFillColor ]
|
|
|
|
[ 0 0 dim first2 <CGRect> CGContextFillRect ]
|
|
|
|
[ 0 bounds descent>> CGContextSetTextPosition ]
|
|
|
|
[ line swap CTLineDraw ]
|
|
|
|
} cleave
|
|
|
|
] with-bitmap-context
|
|
|
|
[ font line bounds dim ] dip 0 0 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
|
|
|
: ref/unref-line ( line n -- )
|
|
|
|
'[ _ + ] change-refs 0 >>age drop ;
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
: ref-line ( line -- ) 1 ref/unref-line ;
|
|
|
|
: unref-line ( line -- ) -1 ref/unref-line ;
|
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-01-27 00:11:45 -05:00
|
|
|
: cached-line ( string font -- line )
|
2009-01-29 23:07:51 -05:00
|
|
|
black white 4array cached-lines get [ first4 <line> ] cache ;
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-29 23:07:51 -05:00
|
|
|
CONSTANT: max-line-age 10
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
: age ( obj -- ? )
|
|
|
|
[ 1+ ] change-age age>> max-line-age >= ;
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
: age-line ( line -- ? )
|
|
|
|
#! Outputs t whether the line is dead.
|
|
|
|
dup refs>> 0 = [ age ] [ drop f ] if ;
|
|
|
|
|
|
|
|
: age-assoc ( assoc quot -- assoc' )
|
|
|
|
'[ nip @ ] assoc-partition
|
|
|
|
[ values dispose-each ] dip ;
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
: age-lines ( -- )
|
|
|
|
cached-lines global [ [ age-line ] age-assoc ] change-at ;
|
2009-01-19 23:58:09 -05:00
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
[ H{ } clone cached-lines set-global ] "core-text" add-init-hook
|