FUNCTION: CGRect CTLineGetImageBounds ( CTLineRef line, CGContextRef context ) ;
+SYMBOL: retina?
+
ERROR: not-a-string object ;
MEMO: make-attributes ( open-font color -- hashtable )
:: <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
[ 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>> ;
: 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
: 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) {
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 -- )
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
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