]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/core-text/core-text.factor
basis: use lint.vocabs tool to trim using lists
[factor.git] / basis / core-text / core-text.factor
index 99849c16667d977efd8335d483e97b4f5a080b1f..d50f69040b9fc78cd2d1f9ea0b1263027fdadd2f 100644 (file)
@@ -1,57 +1,59 @@
 ! 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 ;
+USING: accessors alien.c-types alien.data alien.syntax arrays
+assocs cache classes colors combinators core-foundation
+core-foundation.attributed-strings core-foundation.strings
+core-graphics core-graphics.types core-text.fonts destructors
+fonts io.encodings.string io.encodings.utf16 kernel make
+math math.functions math.vectors namespaces opengl 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 )
 
-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
+            string check-instance
         ] 2dip
-        [
-            kCTForegroundColorAttributeName set
-            kCTFontAttributeName set
-        ] H{ } make-assoc <CFAttributedString> &CFRelease
+        make-attributes <CFAttributedString> &CFRelease
         CTLineCreateWithAttributedString
     ] with-destructors ;
 
-TUPLE: line < disposable line metrics image loc dim ;
+TUPLE: line < disposable font string line metrics image loc dim
+render-loc render-dim render-ext ;
 
 : typographic-bounds ( line -- width ascent descent leading )
-    0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
-    [ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@ ; inline
+    { CGFloat CGFloat CGFloat }
+    [ CTLineGetTypographicBounds ] with-out-parameters ; inline
 
 : store-typographic-bounds ( metrics width ascent descent leading -- metrics )
     {
@@ -74,8 +76,7 @@ TUPLE: line < disposable line metrics image loc dim ;
 
 : metrics>dim ( bounds -- dim )
     [ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
-    [ ceiling >integer ]
-    bi@ 2array ;
+    ceiling >integer 2array ;
 
 : fill-background ( context font dim -- )
     [ background>> >rgba-components CGContextSetRGBFillColor ]
@@ -83,8 +84,10 @@ TUPLE: line < disposable line metrics image loc dim ;
     bi-curry* bi ;
 
 : selection-rect ( dim line selection -- rect )
-    [ start>> ] [ end>> ] bi
-    [ f CTLineGetOffsetForStringIndex round ] bi-curry@ bi
+    [let [ start>> ] [ end>> ] [ string>> ] tri :> ( start end string )
+     start end [ 0 swap string subseq utf16n encode length 2 /i ] bi@
+    ]
+    [ f CTLineGetOffsetForStringIndex ] bi-curry@ bi
     [ drop nip 0 ] [ swap - swap second ] 3bi <CGRect> ;
 
 : CGRect-translate-x ( CGRect x -- CGRect' )
@@ -106,48 +109,67 @@ TUPLE: line < disposable line metrics image loc dim ;
 
 :: line-loc ( metrics loc dim -- loc )
     loc first
-    metrics ascent>> ceiling dim second loc second + - 2array ;
+    metrics ascent>> dim second loc second + - 1 - 2array ;
+
+: load-2x? ( -- ? )
+    gl-scale-factor get-global [ 1.0 > ] [ f ] if* ;
 
 :: <line> ( font string -- line )
     [
         line new-disposable
+        font load-2x? [ 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 ;
 
-        [let* | open-font [ font cache-font ]
-                line [ string open-font font foreground>> <CTLine> |CFRelease ]
+:: render ( line -- line image )
+    line line>> :> ctline
+    line string>> :> string
+    line font>> :> font
 
-                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 render-loc>> [
 
-            line >>line
+        ctline line-rect :> rect
+        rect origin>> CGPoint>loc :> (loc)
+        rect size>> CGSize>dim :> (dim)
 
-            metrics >>metrics
+        (loc) vfloor :> loc
+        (dim) vceiling :> dim
+        dim [ >integer 1 + ] map :> ext
 
-            dim [
-                {
-                    [ font dim fill-background ]
-                    [ loc dim line string fill-selection-background ]
-                    [ loc set-text-position ]
-                    [ [ line ] dip CTLineDraw ]
-                } cleave
-            ] make-bitmap-image >>image
+        loc line render-loc<<
+        dim line render-dim<<
+        ext line render-ext<<
 
-            metrics loc dim line-loc >>loc
+        line metrics>> loc dim line-loc line loc<<
 
-            metrics metrics>dim >>dim
-        ]
-    ] with-destructors ;
+    ] unless
+
+    line render-loc>> :> loc
+    line render-dim>> :> dim
+    line render-ext>> :> ext
+
+    line ext [
+        {
+            [ font ext fill-background ]
+            [ loc first 0 2array dim first ext second 2array ctline string fill-selection-background ]
+            [ loc set-text-position ]
+            [ [ ctline ] dip CTLineDraw ]
+        } cleave
+    ] make-bitmap-image load-2x? >>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
+STARTUP-HOOK: [ <cache-assoc> cached-lines set-global ]