core-text: retina displays should use "2x" fonts.

db4
John Benediktsson 2012-11-01 16:28:33 -07:00
parent 5f1bcb78bd
commit fcce5a2af9
4 changed files with 45 additions and 12 deletions

View 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

View 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) {

View 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

View 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