]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/core-text/core-text.factor
io.encodings.utf16: add a utf16n word for native utf16 type.
[factor.git] / basis / core-text / core-text.factor
index 39d298476ddcd1cb9036c1c8f83812de1abddfad..44d55f5fcd398ff1e46196bf6f4166b4768c809b 100644 (file)
@@ -1,71 +1,84 @@
 ! 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.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 ;
+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 io.encodings.string io.encodings.utf16 kernel locals
+make math math.functions math.order math.vectors memoize
+namespaces sequences strings ;
 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
+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
 
-FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string ) ;
+FUNCTION: CTLineRef CTLineCreateWithAttributedString ( CFAttributedStringRef string )
 
-FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context ) ;
+FUNCTION: void CTLineDraw ( CTLineRef line, CGContextRef context )
 
-FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ) ;
+FUNCTION: CGFloat CTLineGetOffsetForStringIndex ( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset )
 
-FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position ) ;
+FUNCTION: CFIndex CTLineGetStringIndexForPosition ( CTLineRef line, CGPoint position )
 
-FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading ) ;
+FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, CGFloat* descent, CGFloat* leading )
 
-FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
+FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context )
+
+SYMBOL: retina?
 
 ERROR: not-a-string object ;
 
+MEMO: make-attributes ( open-font color -- hashtable )
+    [
+        kCTForegroundColorAttributeName ,,
+        kCTFontAttributeName ,,
+    ] H{ } make ;
+
 : <CTLine> ( string open-font color -- line )
     [
         [
             dup selection? [ string>> ] when
             dup string? [ not-a-string ] unless
         ] 2dip
-        [
-            kCTForegroundColorAttributeName set
-            kCTFontAttributeName set
-        ] H{ } make-assoc <CFAttributedString> &CFRelease
+        make-attributes <CFAttributedString> &CFRelease
         CTLineCreateWithAttributedString
     ] with-destructors ;
 
-TUPLE: line font line metrics image disposed ;
+TUPLE: line < disposable font string line metrics image loc dim
+render-loc render-dim ;
 
-: compute-line-metrics ( open-font line -- line-metrics )
-    [
-        [ metrics new ] dip
-        [ CTFontGetCapHeight >>cap-height ]
-        [ CTFontGetXHeight >>x-height ]
-        bi
-    ] dip
-    0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
-    [ CTLineGetTypographicBounds ] 3keep
+: typographic-bounds ( line -- width ascent descent leading )
+    { CGFloat CGFloat CGFloat }
+    [ CTLineGetTypographicBounds ] with-out-parameters ; inline
+
+: store-typographic-bounds ( metrics width ascent descent leading -- metrics )
     {
         [ >>width ]
-        [ *CGFloat >>ascent ]
-        [ *CGFloat >>descent ]
-        [ *CGFloat >>leading ]
-    } spread
-    dup compute-height ;
+        [ >>ascent ]
+        [ >>descent ]
+        [ >>leading ]
+    } spread ; inline
+
+: compute-font-metrics ( metrics font -- metrics )
+    [ CTFontGetCapHeight >>cap-height ]
+    [ CTFontGetXHeight >>x-height ]
+    bi ; inline
 
-: bounds>dim ( bounds -- dim )
+: compute-line-metrics ( open-font line -- line-metrics )
+    [ metrics new ] 2dip
+    [ compute-font-metrics ]
+    [ typographic-bounds store-typographic-bounds ] bi*
+    compute-height ;
+
+: metrics>dim ( bounds -- dim )
     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
     [ ceiling >integer ]
     bi@ 2array ;
@@ -76,43 +89,86 @@ TUPLE: line font line metrics image disposed ;
     bi-curry* bi ;
 
 : selection-rect ( dim line selection -- rect )
-    [ start>> ] [ end>> ] bi
+    [let [ start>> ] [ end>> ] [ string>> ] tri :> ( start end string )
+     start end [ 0 swap string subseq utf16n encode length 2 / >integer ] bi@
+    ]
     [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
 
-:: fill-selection-background ( context dim line string -- )
+: 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 CGContextFillRect
+        context dim line string selection-rect
+        loc first CGRect-translate-x
+        CGContextFillRect
     ] when ;
 
-: set-text-position ( context metrics -- )
-    [ 0 ] dip descent>> ceiling CGContextSetTextPosition ;
+: 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 )
     [
-        [let* | open-font [ font cache-font CFRetain |CFRelease ]
-                line [ string open-font font foreground>> <CTLine> |CFRelease ]
-                metrics [ open-font line compute-line-metrics ]
-                dim [ metrics bounds>dim ] |
-            open-font line metrics
-            dim [
-                {
-                    [ font dim fill-background ]
-                    [ dim line string fill-selection-background ]
-                    [ metrics set-text-position ]
-                    [ [ line ] dip CTLineDraw ]
-                } cleave
-            ] make-bitmap-image
-        ]
-        f line boa
+        line new-disposable
+        font retina? get-global [ cache-font@2x ] [ cache-font ] if :> open-font
+        string open-font font foreground>> <CTLine> |CFRelease :> line
+        open-font line compute-line-metrics
+        [ >>metrics ] [ metrics>dim >>dim ] bi
+        font >>font
+        string >>string
+        line >>line
     ] with-destructors ;
 
-M: line dispose* [ font>> CFRelease ] [ line>> CFRelease ] bi ;
+:: render ( line -- line image )
+    line line>> :> ctline
+    line string>> :> string
+    line font>> :> font
+
+    line render-loc>> [
+
+        ctline line-rect :> rect
+        rect origin>> CGPoint>loc :> (loc)
+        rect size>> CGSize>dim :> (dim)
+        (loc) vfloor :> loc
+        (loc) (dim) v+ vceiling :> ext
+        ext loc [ - >integer 1 max ] 2map :> dim
+
+        loc line render-loc<<
+        dim line render-dim<<
+
+        line metrics>> loc dim line-loc line loc<<
+
+    ] unless
+
+    line render-loc>> :> loc
+    line render-dim>> :> dim
+
+    line dim [
+        {
+            [ font dim fill-background ]
+            [ loc dim ctline string fill-selection-background ]
+            [ loc set-text-position ]
+            [ [ ctline ] dip CTLineDraw ]
+        } cleave
+    ] make-bitmap-image retina? get-global >>2x? ;
+
+: line>image ( line -- image )
+    dup image>> [ render >>image ] unless image>> ;
+
+M: line dispose* line>> CFRelease ;
 
 SYMBOL: cached-lines
 
 : cached-line ( font string -- line )
-    cached-lines get [ <line> ] 2cache ;
+    cached-lines get-global [ <line> ] 2cache ;
 
-[ <cache-assoc> cached-lines set-global ] "core-text" add-init-hook
\ No newline at end of file
+[ <cache-assoc> cached-lines set-global f retina? set-global ] "core-text" add-startup-hook