| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2009-08-26 15:58:15 -04:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2009-10-09 05:46:29 -04:00
										 |  |  | core-text.fonts ;
 | 
					
						
							| 
									
										
										
										
											2009-01-16 20:11:48 -05:00
										 |  |  | IN: core-text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TYPEDEF: void* CTLineRef | 
					
						
							| 
									
										
										
										
											2009-01-21 00:06:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-09 05:46:29 -04:00
										 |  |  | C-GLOBAL: CFStringRef kCTFontAttributeName | 
					
						
							|  |  |  | C-GLOBAL: CFStringRef kCTKernAttributeName | 
					
						
							|  |  |  | C-GLOBAL: CFStringRef kCTLigatureAttributeName | 
					
						
							|  |  |  | C-GLOBAL: CFStringRef kCTForegroundColorAttributeName | 
					
						
							|  |  |  | C-GLOBAL: CFStringRef kCTParagraphStyleAttributeName | 
					
						
							|  |  |  | C-GLOBAL: CFStringRef kCTUnderlineStyleAttributeName | 
					
						
							|  |  |  | C-GLOBAL: CFStringRef kCTVerticalFormsAttributeName | 
					
						
							|  |  |  | C-GLOBAL: CFStringRef 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-08-24 03:26:13 -04:00
										 |  |  | TUPLE: line < disposable line metrics image loc dim ;
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:07:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-21 02:26:50 -05:00
										 |  |  | : typographic-bounds ( line -- width ascent descent leading )
 | 
					
						
							| 
									
										
										
										
											2009-01-19 23:30:32 -05:00
										 |  |  |     0 <CGFloat> 0 <CGFloat> 0 <CGFloat> | 
					
						
							| 
									
										
										
										
											2009-02-21 02:26:50 -05:00
										 |  |  |     [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@ ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
 | 
					
						
							| 
									
										
										
										
											2009-02-15 03:13:16 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         [ >>width ] | 
					
						
							| 
									
										
										
										
											2009-02-21 02:26:50 -05:00
										 |  |  |         [ >>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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2009-02-14 22:51:54 -05:00
										 |  |  |     [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:05:11 -05:00
										 |  |  |     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 21:33:27 -05:00
										 |  |  | : CGRect-translate-x ( CGRect x -- CGRect' )
 | 
					
						
							|  |  |  |     [ dup CGRect-x ] dip - over set-CGRect-x ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  | :: fill-selection-background ( context loc dim line string -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:05:11 -05:00
										 |  |  |     string selection? [ | 
					
						
							|  |  |  |         context string color>> >rgba-components CGContextSetRGBFillColor | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  |         context dim line string selection-rect | 
					
						
							| 
									
										
										
										
											2009-02-28 21:33:27 -05:00
										 |  |  |         loc first CGRect-translate-x | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  |         CGContextFillRect | 
					
						
							| 
									
										
										
										
											2009-02-07 19:05:11 -05:00
										 |  |  |     ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  | : 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 ;
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:05:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 21:32:49 -05:00
										 |  |  | :: <line> ( font string -- line )
 | 
					
						
							| 
									
										
										
										
											2009-01-19 23:30:32 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-08-24 03:26:13 -04:00
										 |  |  |         line new-disposable | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 21:33:27 -05:00
										 |  |  |         [let* | open-font [ font cache-font ] | 
					
						
							| 
									
										
										
										
											2009-01-30 04:36:39 -05:00
										 |  |  |                 line [ string open-font font foreground>> <CTLine> |CFRelease ] | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 rect [ line line-rect ] | 
					
						
							| 
									
										
										
										
											2009-08-29 13:22:55 -04:00
										 |  |  |                 (loc) [ rect origin>> CGPoint>loc ] | 
					
						
							|  |  |  |                 (dim) [ rect size>> CGSize>dim ] | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  |                 (ext) [ (loc) (dim) v+ ] | 
					
						
							|  |  |  |                 loc [ (loc) [ floor ] map ] | 
					
						
							|  |  |  |                 ext [ (loc) (dim) [ + ceiling ] 2map ] | 
					
						
							| 
									
										
										
										
											2009-08-26 15:58:15 -04:00
										 |  |  |                 dim [ ext loc [ - >integer 1 max ] 2map ] | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  |                 metrics [ open-font line compute-line-metrics ] | | 
					
						
							| 
									
										
										
										
											2009-08-24 03:26:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             line >>line | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             metrics >>metrics | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:07:51 -05:00
										 |  |  |             dim [ | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2009-02-07 19:05:11 -05:00
										 |  |  |                     [ font dim fill-background ] | 
					
						
							| 
									
										
										
										
											2009-02-20 21:53:51 -05:00
										 |  |  |                     [ loc dim line string fill-selection-background ] | 
					
						
							|  |  |  |                     [ loc set-text-position ] | 
					
						
							| 
									
										
										
										
											2009-02-07 19:05:11 -05:00
										 |  |  |                     [ [ line ] dip CTLineDraw ] | 
					
						
							| 
									
										
										
										
											2009-01-29 23:07:51 -05:00
										 |  |  |                 } cleave
 | 
					
						
							| 
									
										
										
										
											2009-08-24 03:26:13 -04:00
										 |  |  |             ] make-bitmap-image >>image | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             metrics loc dim line-loc >>loc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             metrics metrics>dim >>dim | 
					
						
							| 
									
										
										
										
											2009-01-29 23:07:51 -05:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2009-01-19 23:30:32 -05:00
										 |  |  |     ] with-destructors ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 21:33:27 -05:00
										 |  |  | M: line dispose* line>> CFRelease ;
 | 
					
						
							| 
									
										
										
										
											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-08-26 15:58:15 -04:00
										 |  |  | [ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook |