]> 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 ffa7e8f6fb463a25fc3d33925dedcca846d85229..44d55f5fcd398ff1e46196bf6f4166b4768c809b 100644 (file)
@@ -1,12 +1,12 @@
 ! Copyright (C) 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: arrays alien alien.c-types alien.data alien.syntax kernel
-destructors accessors fry words hashtables strings sequences
-memoize assocs make 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 ;
+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
@@ -20,17 +20,19 @@ 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 ;
 
@@ -87,7 +89,9 @@ render-loc render-dim ;
     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> ;
 
@@ -115,7 +119,7 @@ render-loc render-dim ;
 :: <line> ( font string -- line )
     [
         line new-disposable
-        font cache-font :> open-font
+        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
@@ -134,8 +138,8 @@ render-loc render-dim ;
         ctline line-rect :> rect
         rect origin>> CGPoint>loc :> (loc)
         rect size>> CGSize>dim :> (dim)
-        (loc) [ floor ] map :> loc
-        (loc) (dim) [ + ceiling ] 2map :> ext
+        (loc) vfloor :> loc
+        (loc) (dim) v+ vceiling :> ext
         ext loc [ - >integer 1 max ] 2map :> dim
 
         loc line render-loc<<
@@ -155,7 +159,7 @@ render-loc render-dim ;
             [ loc set-text-position ]
             [ [ ctline ] dip CTLineDraw ]
         } cleave
-    ] make-bitmap-image ;
+    ] make-bitmap-image retina? get-global >>2x? ;
 
 : line>image ( line -- image )
     dup image>> [ render >>image ] unless image>> ;
@@ -167,4 +171,4 @@ SYMBOL: cached-lines
 : cached-line ( font string -- line )
     cached-lines get-global [ <line> ] 2cache ;
 
-[ <cache-assoc> cached-lines set-global ] "core-text" add-startup-hook
+[ <cache-assoc> cached-lines set-global f retina? set-global ] "core-text" add-startup-hook