]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/text/text.factor
Make "foo.private" require load foo instead.
[factor.git] / basis / ui / text / text.factor
1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel arrays assocs sequences math math.order cache
4 opengl opengl.gl opengl.textures strings fonts colors accessors
5 namespaces ui.gadgets.worlds ;
6 IN: ui.text
7
8 <PRIVATE
9
10 SYMBOL: font-renderer
11
12 : world-text-handle ( world -- handle )
13     dup text-handle>> [ <cache-assoc> >>text-handle ] unless
14     text-handle>> ;
15
16 HOOK: flush-layout-cache font-renderer ( -- )
17
18 [ flush-layout-cache ] flush-layout-cache-hook set-global
19
20 HOOK: string-dim font-renderer ( font string -- dim )
21
22 HOOK: string-width font-renderer ( font string -- w )
23
24 HOOK: string-height font-renderer ( font string -- h )
25
26 M: object string-dim [ string-width ] [ string-height ] 2bi 2array ;
27
28 M: object string-width string-dim first ;
29
30 M: object string-height string-dim second ;
31
32 HOOK: free-fonts font-renderer ( world -- )
33
34 : combine-text-dim ( dim1 dim2 -- dim3 )
35     [ [ first ] bi@ max ]
36     [ [ second ] bi@ + ]
37     2bi 2array ;
38
39 PRIVATE>
40
41 HOOK: x>offset font-renderer ( x font string -- n )
42
43 HOOK: offset>x font-renderer ( n font string -- x )
44
45 GENERIC: text-dim ( font text -- dim )
46
47 M: string text-dim string-dim ;
48
49 M: array text-dim
50     [ { 0 0 } ] 2dip [ string-dim combine-text-dim ] with each ;
51
52 : text-width ( font text -- w ) text-dim first ;
53
54 : text-height ( font text -- h ) text-dim second ;
55
56 HOOK: font-metrics font-renderer ( font -- metrics )
57
58 HOOK: line-metrics font-renderer ( font string -- metrics )
59
60 HOOK: string>image font-renderer ( font string -- image loc )
61
62 <PRIVATE
63
64 : string-empty? ( obj -- ? )
65     dup selection? [ string>> ] when empty? ;
66
67 : draw-string ( font string -- )
68     dup string-empty? [ 2drop ] [
69         world get world-text-handle
70         [ string>image <texture> ] 2cache
71         draw-texture
72     ] if ;
73
74 PRIVATE>
75
76 GENERIC: draw-text ( font text -- )
77
78 M: string draw-text draw-string ;
79
80 M: selection draw-text draw-string ;
81
82 M: array draw-text
83     [
84         [
85             [ draw-string ]
86             [ [ 0.0 ] 2dip string-height 0.0 glTranslated ] 2bi
87         ] with each
88     ] do-matrix ;
89
90 USING: vocabs system combinators ;
91
92 {
93     { [ os macosx? ] [ "core-text" ] }
94     { [ os windows? ] [ "uniscribe" ] }
95     { [ os unix? ] [ "pango" ] }
96 } cond "ui.text." prepend require