]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/ui/text/core-text/core-text.factor
io.encodings.utf16: add a utf16n word for native utf16 type.
[factor.git] / basis / ui / text / core-text / core-text.factor
index 7636349b87221f1085894a09fd3d8e619edca2be..7b328e787b7e2325d846b3cce6e9eaf1c3f75f32 100644 (file)
@@ -1,10 +1,9 @@
 ! Copyright (C) 2009, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: assocs accessors alien core-graphics.types core-text
-core-text.fonts kernel hashtables namespaces sequences ui.text
-ui.text.private destructors combinators core-foundation
-core-foundation.strings math math.vectors init colors
-colors.constants cache arrays images ;
+USING: accessors cache core-graphics.types core-text
+core-text.fonts io.encodings.string io.encodings.utf16 kernel
+locals math math.vectors namespaces sequences ui.text
+ui.text.private ;
 IN: ui.text.core-text
 
 SINGLETON: core-text-renderer
@@ -12,13 +11,13 @@ SINGLETON: core-text-renderer
 <PRIVATE
 
 : unscale ( m -- n )
-    retina? get-global [ 2 /i ] when ; inline
+    retina? get-global [ 2.0 / ] when ; inline
 
 : scale ( m -- n )
-    retina? get-global [ 2 * ] when ; inline
+    retina? get-global [ 2.0 * ] when ; inline
 
 : scale-dim ( dim -- dim' )
-    retina? get-global [ [ 2 /i ] map ] when ; inline
+    retina? get-global [ [ 2.0 / ] map ] when ; inline
 
 : scale-metrics ( metrics -- metrics' )
     retina? get-global [
@@ -45,14 +44,19 @@ M: core-text-renderer flush-layout-cache
 M: core-text-renderer string>image ( font string -- image loc )
     cached-line [ line>image ] [ loc>> scale-dim ] bi ;
 
-M: core-text-renderer x>offset ( x font string -- n )
+M:: core-text-renderer x>offset ( x font string -- n )
+    x font string
     [ 2drop 0 ] [
         cached-line line>>
         swap scale 0 <CGPoint> CTLineGetStringIndexForPosition
-    ] if-empty ;
+    ] if-empty
+    2 * 0 swap string utf16n encode subseq
+    utf16n decode length ;
 
-M: core-text-renderer offset>x ( n font string -- x )
-    cached-line line>> swap f
+M:: core-text-renderer offset>x ( n font string -- x )
+    font string cached-line line>> 
+    0 n string subseq utf16n encode length 2 / >integer
+    f
     CTLineGetOffsetForStringIndex unscale ;
 
 M: core-text-renderer font-metrics ( font -- metrics )