]> gitweb.factorcode.org Git - factor.git/commitdiff
core-text: retina displays should use "2x" fonts.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 1 Nov 2012 23:28:33 +0000 (16:28 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 1 Nov 2012 23:28:33 +0000 (16:28 -0700)
basis/core-text/core-text.factor
basis/core-text/fonts/fonts.factor
basis/ui/backend/cocoa/views/views.factor
basis/ui/text/core-text/core-text.factor

index ffa7e8f6fb463a25fc3d33925dedcca846d85229..6f9a7d9dbd9ffb81546bff940bc167e503047f9d 100644 (file)
@@ -32,6 +32,8 @@ FUNCTION: double CTLineGetTypographicBounds ( CTLineRef line, CGFloat* ascent, C
 
 FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
 
+SYMBOL: retina?
+
 ERROR: not-a-string object ;
 
 MEMO: make-attributes ( open-font color -- hashtable )
@@ -115,7 +117,7 @@ render-loc render-dim ;
 :: <line> ( font string -- line )
     [
         line new-disposable
-        font cache-font :> open-font
+        font retina? [ 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
@@ -155,7 +157,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 +169,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
index fd030cfdf8d131b65c035a5fd4ce66c6ec8663da..43a527de0be568f5a14d64341340aac4109e262f 100644 (file)
@@ -101,6 +101,9 @@ MEMO:: (cache-font) ( name size traits -- open-font )
 : cache-font ( font -- open-font )
     [ name>> ] [ size>> ] [ font-traits ] tri (cache-font) ;
 
+: cache-font@2x ( font -- open-font )
+    [ name>> ] [ size>> 2 * ] [ font-traits ] tri (cache-font) ;
+
 MEMO: (cache-font-metrics) ( name size traits -- metrics )
     [ metrics new ] 3dip
     (cache-font) {
index 29ff18c786a9afe6fd4c03b753cb9acb8bdde18a..ad49c67bb5ef32107065fbd00895d91be55785b2 100644 (file)
@@ -4,9 +4,10 @@ USING: accessors alien alien.c-types alien.data alien.strings
 arrays assocs cocoa cocoa.application cocoa.classes
 cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.types
 cocoa.views combinators core-foundation.strings core-graphics
-core-graphics.types io.encodings.utf8 kernel locals math
-math.rectangles namespaces opengl sequences threads ui.gadgets
-ui.gadgets.private ui.gadgets.worlds ui.gestures ui.private ;
+core-graphics.types core-text io.encodings.utf8 kernel locals
+math math.rectangles namespaces opengl sequences threads
+ui.gadgets ui.gadgets.private ui.gadgets.worlds ui.gestures
+ui.private ;
 IN: ui.backend.cocoa.views
 
 : send-mouse-moved ( view event -- )
@@ -306,7 +307,9 @@ CLASS: FactorView < NSOpenGLView NSTextInput
 
     METHOD: void prepareOpenGL [
         self 1 -> setWantsBestResolutionOpenGLSurface:
-        self -> backingScaleFactor gl-scale-factor set-global
+        self -> backingScaleFactor
+        [ gl-scale-factor set-global ]
+        [ 1.0 > retina? set-global ] bi
     ]
 
     ! Initialization
index ddde9fe7e36af0a830f375c6ae5464a2b5aa4347..3f66ae60cd41aef1aee01d6cb6cdac99951587a9 100644 (file)
@@ -9,33 +9,58 @@ IN: ui.text.core-text
 
 SINGLETON: core-text-renderer
 
+<PRIVATE
+
+: unscale ( m -- n )
+    retina? get-global [ 2.0 / ] when ; inline
+
+: scale ( m -- n )
+    retina? get-global [ 2.0 * ] when ; inline
+
+: scale-dim ( dim -- dim' )
+    retina? get-global [ [ 2.0 / ] map ] when ; inline
+
+: scale-metrics ( metrics -- metrics' )
+    retina? get-global [
+        clone
+            [ 2.0 / ] change-width
+            [ 2.0 / ] change-ascent
+            [ 2.0 / ] change-descent
+            [ 2.0 / ] change-height
+            [ 2.0 / ] change-leading
+            [ 2.0 / ] change-cap-height
+            [ 2.0 / ] change-x-height
+    ] when ; inline
+
+PRIVATE>
+
 M: core-text-renderer string-dim
     [ " " string-dim { 0 1 } v* ]
-    [ cached-line dim>> ]
+    [ cached-line dim>> scale-dim ]
     if-empty ;
 
 M: core-text-renderer flush-layout-cache
     cached-lines get-global purge-cache ;
 
 M: core-text-renderer string>image ( font string -- image loc )
-    cached-line [ line>image ] [ loc>> ] bi ;
+    cached-line [ line>image ] [ loc>> scale-dim ] bi ;
 
 M: core-text-renderer x>offset ( x font string -- n )
     [ 2drop 0 ] [
         cached-line line>>
-        swap 0 <CGPoint> CTLineGetStringIndexForPosition
+        swap scale 0 <CGPoint> CTLineGetStringIndexForPosition
     ] if-empty ;
 
 M: core-text-renderer offset>x ( n font string -- x )
     cached-line line>> swap f
-    CTLineGetOffsetForStringIndex ;
+    CTLineGetOffsetForStringIndex unscale ;
 
 M: core-text-renderer font-metrics ( font -- metrics )
     cache-font-metrics ;
 
 M: core-text-renderer line-metrics ( font string -- metrics )
     [ " " line-metrics clone 0 >>width ]
-    [ cached-line metrics>> ]
+    [ cached-line metrics>> scale-metrics ]
     if-empty ;
 
 core-text-renderer font-renderer set-global