]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/text/text.factor
factor: more top level forms.
[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: 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
6 vocabs.platforms ;
7 IN: ui.text
8
9 <PRIVATE
10
11 SYMBOL: font-renderer
12
13 : world-text-handle ( world -- handle )
14     dup text-handle>> [ <cache-assoc> >>text-handle ] unless
15     text-handle>> ;
16
17 HOOK: flush-layout-cache font-renderer ( -- )
18
19 [ flush-layout-cache ] flush-layout-cache-hook set-global
20
21 HOOK: string-dim font-renderer ( font string -- dim )
22
23 : string-width ( font string -- w ) string-dim first ; inline
24
25 : string-height ( font string -- h ) string-dim second ; inline
26
27 HOOK: free-fonts font-renderer ( world -- )
28
29 : combine-text-dim ( dim1 dim2 -- dim3 )
30     [ first2 ] bi@ swapd [ max ] [ + ] 2bi* 2array ; inline
31
32 PRIVATE>
33
34 HOOK: x>offset font-renderer ( x font string -- n )
35
36 HOOK: offset>x font-renderer ( n font string -- x )
37
38 GENERIC: text-dim ( font text -- dim )
39
40 M: string text-dim string-dim ;
41
42 M: array text-dim
43     [ { 0 0 } ] 2dip [ string-dim combine-text-dim ] with each ;
44
45 : text-width ( font text -- w ) text-dim first ;
46
47 : text-height ( font text -- h ) text-dim second ;
48
49 HOOK: font-metrics font-renderer ( font -- metrics )
50
51 HOOK: line-metrics font-renderer ( font string -- metrics )
52
53 HOOK: string>image font-renderer ( font string -- image loc )
54
55 <PRIVATE
56
57 : string-empty? ( obj -- ? )
58     dup selection? [ string>> ] when empty? ;
59
60 : draw-string ( font string -- )
61     dup string-empty? [ 2drop ] [
62         world get world-text-handle
63         [ string>image <texture> ] 2cache
64         draw-texture
65     ] if ;
66
67 PRIVATE>
68
69 GENERIC: draw-text ( font text -- )
70
71 M: string draw-text draw-string ;
72
73 M: selection draw-text draw-string ;
74
75 M: array draw-text
76     [
77         [
78             [ draw-string ]
79             [ [ 0.0 ] 2dip string-height 0.0 glTranslated ] 2bi
80         ] with each
81     ] do-matrix ;
82
83 USE-MACOSX: ui.text.core-text
84 <!MACOSX
85     USE-UNIX: ui.text.pango
86 !MACOSX>
87 USE-WINDOWS: ui.text.uniscribe