From fcce5a2af9f9b4dcf3474295cf5ef1422b881435 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 1 Nov 2012 16:28:33 -0700 Subject: [PATCH] core-text: retina displays should use "2x" fonts. --- basis/core-text/core-text.factor | 8 ++++-- basis/core-text/fonts/fonts.factor | 3 ++ basis/ui/backend/cocoa/views/views.factor | 11 ++++--- basis/ui/text/core-text/core-text.factor | 35 +++++++++++++++++++---- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/basis/core-text/core-text.factor b/basis/core-text/core-text.factor index ffa7e8f6fb..6f9a7d9dbd 100644 --- a/basis/core-text/core-text.factor +++ b/basis/core-text/core-text.factor @@ -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 ; :: ( 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>> |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 [ ] 2cache ; -[ cached-lines set-global ] "core-text" add-startup-hook +[ cached-lines set-global f retina? set-global ] "core-text" add-startup-hook diff --git a/basis/core-text/fonts/fonts.factor b/basis/core-text/fonts/fonts.factor index fd030cfdf8..43a527de0b 100644 --- a/basis/core-text/fonts/fonts.factor +++ b/basis/core-text/fonts/fonts.factor @@ -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) { diff --git a/basis/ui/backend/cocoa/views/views.factor b/basis/ui/backend/cocoa/views/views.factor index 29ff18c786..ad49c67bb5 100644 --- a/basis/ui/backend/cocoa/views/views.factor +++ b/basis/ui/backend/cocoa/views/views.factor @@ -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 diff --git a/basis/ui/text/core-text/core-text.factor b/basis/ui/text/core-text/core-text.factor index ddde9fe7e3..3f66ae60cd 100644 --- a/basis/ui/text/core-text/core-text.factor +++ b/basis/ui/text/core-text/core-text.factor @@ -9,33 +9,58 @@ IN: ui.text.core-text SINGLETON: core-text-renderer + + 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 CTLineGetStringIndexForPosition + swap scale 0 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