1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs cache combinators fonts kernel
4 math math.order namespaces opengl opengl.gl opengl.textures
5 sequences strings system ui.gadgets.worlds vocabs
13 : world-text-handle ( world -- handle )
14 dup text-handle>> [ <cache-assoc> >>text-handle ] unless
17 HOOK: flush-layout-cache font-renderer ( -- )
19 [ flush-layout-cache ] flush-layout-cache-hook set-global
21 HOOK: string-dim font-renderer ( font string -- dim )
23 : string-width ( font string -- w ) string-dim first ; inline
25 : string-height ( font string -- h ) string-dim second ; inline
27 HOOK: free-fonts font-renderer ( world -- )
29 : combine-text-dim ( dim1 dim2 -- dim3 )
30 [ first2 ] bi@ swapd [ max ] [ + ] 2bi* 2array ; inline
34 HOOK: x>offset font-renderer ( x font string -- n )
36 HOOK: offset>x font-renderer ( n font string -- x )
38 GENERIC: text-dim ( font text -- dim )
40 M: string text-dim string-dim ;
43 [ { 0 0 } ] 2dip [ string-dim combine-text-dim ] with each ;
45 : text-width ( font text -- w ) text-dim first ;
47 : text-height ( font text -- h ) text-dim second ;
49 HOOK: font-metrics font-renderer ( font -- metrics )
51 HOOK: line-metrics font-renderer ( font string -- metrics )
53 HOOK: string>image font-renderer ( font string -- image loc )
57 : string-empty? ( obj -- ? )
58 dup selection? [ string>> ] when empty? ;
60 : draw-string ( font string -- )
61 dup string-empty? [ 2drop ] [
62 world get world-text-handle
63 [ string>image <texture> ] 2cache
69 GENERIC: draw-text ( font text -- )
71 M: string draw-text draw-string ;
73 M: selection draw-text draw-string ;
79 [ [ 0.0 ] 2dip string-height 0.0 glTranslated ] 2bi
83 USE-MACOSX: ui.text.core-text
85 USE-UNIX: ui.text.pango
87 USE-WINDOWS: ui.text.uniscribe