1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data arrays assocs cache
4 classes.struct combinators destructors fonts init io.encodings.string
5 io.encodings.utf16n kernel literals locals math namespaces sequences
6 windows.errors windows.fonts windows.gdi32 windows.offscreen
7 windows.ole32 windows.types windows.usp10 ;
10 TUPLE: script-string < disposable font string metrics ssa size image ;
12 : line-offset>x ( n script-string -- x )
13 2dup string>> length = [
22 { int } [ ScriptStringCPtoX check-ole32-error ] with-out-parameters ;
24 : x>line-offset ( x script-string -- n trailing )
27 { int int } [ ScriptStringXtoCP check-ole32-error ] with-out-parameters ;
31 CONSTANT: ssa-dwFlags flags{ SSA_GLYPHS SSA_FALLBACK SSA_TAB }
33 : make-ssa ( dc script-string -- ssa )
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 [ check-ole32-error ] [ |ScriptStringFree void* deref ] 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 ( ssa size script-string -- )
63 [ [ { 0 0 } ] dip <RECT> ]
64 [ selection-start/end ] tri*
68 ScriptStringOut check-ole32-error ;
70 :: render-image ( dc ssa script-string -- image )
71 script-string size>> :> size
73 [ ssa size script-string draw-script-string ] make-bitmap-image ;
75 : set-dc-font ( dc font -- )
76 cache-font SelectObject win32-error=0/f ;
78 : ssa-size ( ssa -- dim )
81 [ cx>> ] [ cy>> ] bi 2array ;
83 : dc-metrics ( dc -- metrics )
85 [ GetTextMetrics drop ] keep
88 ! DC limit is default soft-limited to 10,000 per process.
89 : <script-string> ( font string -- script-string )
90 [ script-string new-disposable ] 2dip
91 [ >>font ] [ >>string ] bi*
94 [ over font>> set-dc-font ]
95 [ dc-metrics >>metrics ]
96 [ over string>> make-ssa [ >>ssa ] [ ssa-size >>size ] bi ]
102 M: script-string dispose*
103 ssa>> void* <ref> ScriptStringFree check-ole32-error ;
105 SYMBOL: cached-script-strings
107 : cached-script-string ( font string -- script-string )
108 cached-script-strings get-global [ <script-string> ] 2cache ;
110 : script-string>image ( script-string -- image )
114 [ over font>> [ set-dc-font ] [ set-dc-colors ] 2bi ]
116 dup pick string>> make-ssa
117 dup void* <ref> &ScriptStringFree drop
118 pick render-image >>image
124 [ <cache-assoc> &dispose cached-script-strings set-global ]
125 "windows.uniscribe" add-startup-hook