1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel assocs math sequences fry io.encodings.string
4 io.encodings.utf16n accessors arrays combinators destructors
5 cache namespaces init fonts alien.c-types windows.usp10
6 windows.offscreen windows.gdi32 windows.ole32 windows.types
7 windows.fonts opengl.textures locals windows.errors
11 TUPLE: script-string < disposable font string metrics ssa size image ;
13 : line-offset>x ( n script-string -- x )
14 2dup string>> length = [
23 0 <int> [ ScriptStringCPtoX ole32-error ] keep *int ;
25 : x>line-offset ( x script-string -- n trailing )
30 [ ScriptStringXtoCP ole32-error ] 2keep [ *int ] bi@ ;
34 : make-script-string ( dc string -- script-string )
35 dup selection? [ string>> ] when
36 [ utf16n encode ] ! pString
37 [ length ] bi ! cString
38 dup 1.5 * 16 + >integer ! cGlyphs -- MSDN says this is "recommended size"
39 -1 ! iCharset -- Unicode
48 [ ScriptStringAnalyse ] keep
49 [ ole32-error ] [ |ScriptStringFree *void* ] bi* ;
51 : set-dc-colors ( dc font -- )
52 [ background>> color>RGB SetBkColor drop ]
53 [ foreground>> color>RGB SetTextColor drop ] 2bi ;
55 : selection-start/end ( script-string -- iMinSel iMaxSel )
56 string>> dup selection? [ [ start>> ] [ end>> ] bi ] [ drop 0 0 ] if ;
58 : (draw-script-string) ( script-string -- )
65 [ [ { 0 0 } ] dip size>> <RECT> ]
66 [ selection-start/end ] tri
70 ScriptStringOut ole32-error ;
72 : draw-script-string ( dc script-string -- )
73 [ font>> set-dc-colors ] keep (draw-script-string) ;
75 :: make-script-string-image ( dc script-string -- image )
76 script-string size>> dc
77 [ dc script-string draw-script-string ] make-bitmap-image ;
79 : set-dc-font ( dc font -- )
80 cache-font SelectObject win32-error=0/f ;
82 : script-string-size ( script-string -- dim )
83 ssa>> ScriptString_pSize
85 [ cx>> ] [ cy>> ] bi 2array ;
87 : dc-metrics ( dc -- metrics )
89 [ GetTextMetrics drop ] keep
92 : <script-string> ( font string -- script-string )
93 [ script-string new-disposable ] 2dip
94 [ >>font ] [ >>string ] bi*
97 [ over font>> set-dc-font ]
98 [ dc-metrics >>metrics ]
99 [ over string>> make-script-string >>ssa ]
100 [ drop dup script-string-size >>size ]
101 [ over make-script-string-image >>image ]
107 M: script-string dispose*
108 ssa>> <void*> ScriptStringFree ole32-error ;
110 SYMBOL: cached-script-strings
112 : cached-script-string ( font string -- script-string )
113 cached-script-strings get-global [ <script-string> ] 2cache ;
115 [ <cache-assoc> &dispose cached-script-strings set-global ]
116 "windows.uniscribe" add-startup-hook