| 
									
										
										
										
											2009-01-16 20:11:48 -05:00
										 |  |  | ! Copyright (C) 2009 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2014-12-13 19:26:09 -05:00
										 |  |  | USING: accessors alien.c-types alien.data alien.syntax arrays | 
					
						
							|  |  |  | assocs cache colors combinators core-foundation | 
					
						
							|  |  |  | core-foundation.attributed-strings core-foundation.strings | 
					
						
							|  |  |  | core-graphics core-graphics.types core-text.fonts destructors | 
					
						
							|  |  |  | fonts init kernel locals make math math.functions math.order | 
					
						
							|  |  |  | math.vectors memoize namespaces sequences strings ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) | 
					
						
							| 
									
										
										
										
											2009-01-16 20:11:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) | 
					
						
							| 
									
										
										
										
											2009-01-19 17:29:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ) | 
					
						
							| 
									
										
										
										
											2009-01-19 17:29:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position ) | 
					
						
							| 
									
										
										
										
											2009-01-19 17:29:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading ) | 
					
						
							| 
									
										
										
										
											2009-01-19 17:29:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 19:25:30 -04:00
										 |  |  | FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) | 
					
						
							| 
									
										
										
										
											2009-01-19 23:30:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-01 19:28:33 -04:00
										 |  |  | SYMBOL: retina? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 01:22:41 -05:00
										 |  |  | ERROR: not-a-string object ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-01 01:03:32 -04:00
										 |  |  | MEMO: make-attributes ( open-font color -- hashtable )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         kCTForegroundColorAttributeName ,, | 
					
						
							|  |  |  |         kCTFontAttributeName ,, | 
					
						
							|  |  |  |     ] H{ } make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |             dup string? [ not-a-string ] unless
 | 
					
						
							| 
									
										
										
										
											2009-02-09 01:22:41 -05:00
										 |  |  |         ] 2dip
 | 
					
						
							| 
									
										
										
										
											2012-08-01 01:03:32 -04:00
										 |  |  |         make-attributes <CFAttributedString> &CFRelease | 
					
						
							| 
									
										
										
										
											2009-01-19 23:30:32 -05:00
										 |  |  |         CTLineCreateWithAttributedString | 
					
						
							|  |  |  |     ] with-destructors ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-20 22:31:36 -04:00
										 |  |  | TUPLE: line < disposable font string line metrics image loc dim | 
					
						
							|  |  |  | render-loc render-dim ;
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:07:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-21 02:26:50 -05:00
										 |  |  | : typographic-bounds ( line -- width ascent descent leading )
 | 
					
						
							| 
									
										
										
										
											2010-05-23 03:07:47 -04:00
										 |  |  |     { CGFloat CGFloat CGFloat } | 
					
						
							| 
									
										
										
										
											2010-07-16 17:32:05 -04:00
										 |  |  |     [ CTLineGetTypographicBounds ] with-out-parameters ; inline
 | 
					
						
							| 
									
										
										
										
											2009-02-21 02:26:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : 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 | 
					
						
							| 
									
										
										
										
											2012-11-01 19:43:26 -04:00
										 |  |  |         font retina? get-global [ cache-font@2x ] [ cache-font ] if :> open-font | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  |         string open-font font foreground>> <CTLine> |CFRelease :> line | 
					
						
							| 
									
										
										
										
											2012-09-20 22:31:36 -04:00
										 |  |  |         open-font line compute-line-metrics | 
					
						
							|  |  |  |         [ >>metrics ] [ metrics>dim >>dim ] bi
 | 
					
						
							|  |  |  |         font >>font | 
					
						
							|  |  |  |         string >>string | 
					
						
							|  |  |  |         line >>line | 
					
						
							|  |  |  |     ] with-destructors ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: render ( line -- line image )
 | 
					
						
							|  |  |  |     line line>> :> ctline | 
					
						
							|  |  |  |     line string>> :> string
 | 
					
						
							|  |  |  |     line font>> :> font | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-20 22:31:36 -04:00
										 |  |  |     line render-loc>> [ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ctline line-rect :> rect | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  |         rect origin>> CGPoint>loc :> (loc) | 
					
						
							|  |  |  |         rect size>> CGSize>dim :> (dim) | 
					
						
							| 
									
										
										
										
											2014-10-13 17:56:28 -04:00
										 |  |  |         (loc) vfloor :> loc | 
					
						
							|  |  |  |         (loc) (dim) v+ vceiling :> ext | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  |         ext loc [ - >integer 1 max ] 2map :> dim | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-20 22:31:36 -04:00
										 |  |  |         loc line render-loc<< | 
					
						
							|  |  |  |         dim line render-dim<< | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-20 22:31:36 -04:00
										 |  |  |         line metrics>> loc dim line-loc line loc<< | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-20 22:31:36 -04:00
										 |  |  |     ] unless
 | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-20 22:31:36 -04:00
										 |  |  |     line render-loc>> :> loc | 
					
						
							|  |  |  |     line render-dim>> :> dim | 
					
						
							| 
									
										
										
										
											2011-08-27 01:21:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     line dim [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             [ font dim fill-background ] | 
					
						
							|  |  |  |             [ loc dim ctline string fill-selection-background ] | 
					
						
							|  |  |  |             [ loc set-text-position ] | 
					
						
							|  |  |  |             [ [ ctline ] dip CTLineDraw ] | 
					
						
							|  |  |  |         } cleave
 | 
					
						
							| 
									
										
										
										
											2012-11-01 19:28:33 -04:00
										 |  |  |     ] make-bitmap-image retina? get-global >>2x? ;
 | 
					
						
							| 
									
										
										
										
											2011-08-27 01:21:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : line>image ( line -- image )
 | 
					
						
							|  |  |  |     dup image>> [ render >>image ] unless image>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2012-08-03 00:35:28 -04:00
										 |  |  |     cached-lines get-global [ <line> ] 2cache ;
 | 
					
						
							| 
									
										
										
										
											2009-01-19 23:58:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-01 19:28:33 -04:00
										 |  |  | [ <cache-assoc> cached-lines set-global f retina? set-global ] "core-text" add-startup-hook |