1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors cache core-graphics.types core-text
4 core-text.fonts io.encodings.string io.encodings.utf16n kernel
5 math math.vectors namespaces opengl sequences ui.text
8 SINGLETON: core-text-renderer
12 : scale-dim ( dim -- dim' )
13 gl-scale-factor get-global [ [ gl-unscale ] map ] when ; inline
15 : scale-metrics ( metrics -- metrics' )
16 gl-scale-factor get-global [
18 [ gl-unscale ] change-width
19 [ gl-unscale ] change-ascent
20 [ gl-unscale ] change-descent
21 [ gl-unscale ] change-height
22 [ gl-unscale ] change-leading
23 [ gl-unscale ] change-cap-height
24 [ gl-unscale ] change-x-height
29 M: core-text-renderer string-dim
30 [ " " string-dim { 0 1 } v* ]
31 [ cached-line dim>> scale-dim ]
34 M: core-text-renderer flush-layout-cache
35 cached-lines get-global purge-cache ;
37 M: core-text-renderer string>image
38 cached-line [ line>image ] [ loc>> scale-dim ] bi ;
40 M:: core-text-renderer x>offset ( x font string -- n )
44 swap gl-scale 0 <CGPoint> CTLineGetStringIndexForPosition
45 2 * 0 swap string utf16n encode subseq
49 M:: core-text-renderer offset>x ( n font string -- x )
50 font string cached-line line>>
51 0 n string subseq utf16n encode length 2 /i
53 CTLineGetOffsetForStringIndex gl-unscale ;
55 M: core-text-renderer font-metrics
58 M: core-text-renderer line-metrics
59 [ " " line-metrics clone 0 >>width ]
60 [ cached-line metrics>> scale-metrics ]
63 core-text-renderer font-renderer set-global