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 ;
10 TUPLE: script-string font string metrics ssa size image disposed ;
12 : line-offset>x ( n script-string -- x )
13 2dup string>> length = [
22 0 <int> [ ScriptStringCPtoX ole32-error ] keep *int ;
24 : x>line-offset ( x script-string -- n trailing )
29 [ ScriptStringXtoCP ole32-error ] 2keep [ *int ] bi@ ;
33 : make-script-string ( dc string -- script-string )
34 dup selection? [ string>> ] when
35 [ utf16n encode ] ! pString
36 [ length ] bi ! cString
37 dup 1.5 * 16 + >integer ! cGlyphs -- MSDN says this is "recommended size"
38 -1 ! iCharset -- Unicode
47 [ ScriptStringAnalyse ] keep
48 [ ole32-error ] [ |ScriptStringFree *void* ] bi* ;
50 : set-dc-colors ( dc font -- )
51 [ background>> color>RGB SetBkColor drop ]
52 [ foreground>> color>RGB SetTextColor drop ] 2bi ;
54 : selection-start/end ( script-string -- iMinSel iMaxSel )
55 string>> dup selection? [ [ start>> ] [ end>> ] bi ] [ drop 0 0 ] if ;
57 : (draw-script-string) ( script-string -- )
64 [ [ { 0 0 } ] dip size>> <RECT> ]
65 [ selection-start/end ] tri
69 ScriptStringOut ole32-error ;
71 : draw-script-string ( dc script-string -- )
72 [ font>> set-dc-colors ] keep (draw-script-string) ;
74 :: make-script-string-image ( dc script-string -- image )
75 script-string size>> dc
76 [ dc script-string draw-script-string ] make-bitmap-image ;
78 : set-dc-font ( dc font -- )
79 cache-font SelectObject win32-error=0/f ;
81 : script-string-size ( script-string -- dim )
82 ssa>> ScriptString_pSize
84 [ SIZE-cx ] [ SIZE-cy ] bi 2array ;
86 : dc-metrics ( dc -- metrics )
87 "TEXTMETRICW" <c-object>
88 [ GetTextMetrics drop ] keep
91 : <script-string> ( font string -- script-string )
92 [ script-string new ] 2dip
93 [ >>font ] [ >>string ] bi*
96 [ over font>> set-dc-font ]
97 [ dc-metrics >>metrics ]
98 [ over string>> make-script-string >>ssa ]
99 [ drop dup script-string-size >>size ]
100 [ over make-script-string-image >>image ]
106 M: script-string dispose*
107 ssa>> <void*> ScriptStringFree ole32-error ;
109 SYMBOL: cached-script-strings
111 : cached-script-string ( font string -- script-string )
112 cached-script-strings get-global [ <script-string> ] 2cache ;
114 [ <cache-assoc> cached-script-strings set-global ]
115 "windows.uniscribe" add-init-hook