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 images.normalization alien.c-types locals
6 windows windows.usp10 windows.offscreen windows.gdi32
7 windows.ole32 windows.types windows.fonts ;
10 TUPLE: script-string metrics ssa size image string disposed ;
12 : make-script-string ( dc string -- script-string )
13 [ utf16n encode ] ! pString
14 [ length ] bi ! cString
15 dup 1.5 * 16 + >integer ! cGlyphs -- MSDN says this is "recommended size"
16 -1 ! iCharset -- Unicode
25 [ ScriptStringAnalyse ] keep
26 [ ole32-error ] [ |ScriptStringFree *void* ] bi* ;
28 : draw-script-string ( script-string -- )
37 ScriptStringOut ole32-error ;
39 : set-dc-font ( dc font -- )
40 [ cache-font SelectObject win32-error=0/f ]
41 [ background>> color>RGB SetBkColor drop ]
42 [ foreground>> color>RGB SetTextColor drop ] 2tri ;
44 : script-string-size ( ssa -- dim )
47 [ SIZE-cx ] [ SIZE-cy ] bi 2array ;
49 : dc-metrics ( dc -- metrics )
50 "TEXTMETRICW" <c-object> [ GetTextMetrics drop ] keep
53 :: <script-string> ( font string -- script-string )
54 #! Comments annotate BOA constructor arguments
57 dc dc-metrics ! metrics
58 dc string make-script-string dup :> ssa ! ssa
59 dup script-string-size ! size
60 dup dc [ ssa draw-script-string ] make-bitmap-image
61 normalize-image ! image
66 : text-position ( script-string -- loc ) drop { 0 0 } ;
68 M: script-string dispose* ssa>> <void*> ScriptStringFree ole32-error ;
70 SYMBOL: cached-script-strings
72 : cached-script-string ( string font -- script-string )
73 cached-script-strings get-global [ <script-string> ] 2cache ;
75 [ <cache-assoc> cached-script-strings set-global ]
76 "windows.uniscribe" add-init-hook
78 : line-offset>x ( n script-string -- x )
79 2dup string>> length = [
88 0 <int> [ ScriptStringCPtoX ole32-error ] keep *int ;
90 : x>line-offset ( x script-string -- n trailing )
95 [ ScriptStringXtoCP ole32-error ] 2keep [ *int ] bi@ ;