1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien.c-types alien.data arrays assocs
5 byte-arrays cache classes.struct colors combinators destructors
6 fonts images init io.encodings.string io.encodings.utf16n kernel
7 literals locals math math.bitwise namespaces sequences
8 specialized-arrays windows.errors windows.fonts windows.gdi32
9 windows.offscreen windows.ole32 windows.types windows.usp10 ;
11 SPECIALIZED-ARRAY: uint32_t
14 TUPLE: script-string < disposable font string metrics ssa size image ;
18 CONSTANT: ssa-dwFlags flags{ SSA_GLYPHS SSA_FALLBACK SSA_TAB }
20 :: >codepoint-index ( str utf16-index -- codepoint-index )
21 0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
23 :: >utf16-index ( str codepoint-index -- utf16-index )
24 0 codepoint-index str subseq utf16n encode length 2 /i ;
28 :: line-offset>x ( n script-string -- x )
29 script-string string>> n >utf16-index :> n-utf16
30 script-string ssa>> ! ssa
31 n script-string string>> length = [
38 { int } [ ScriptStringCPtoX check-ole32-error ] with-out-parameters ;
40 :: x>line-offset ( x script-string -- n trailing )
41 script-string ssa>> ! ssa
43 { int int } [ ScriptStringXtoCP check-ole32-error ] with-out-parameters
44 swap dup 0 < [ script-string string>> swap >codepoint-index ] unless
49 : make-ssa ( dc script-string -- ssa )
50 dup selection? [ string>> ] when
51 utf16n encode ! pString
52 dup length 2 /i ! cString
53 dup 1.5 * 16 + >integer ! cGlyphs -- MSDN says this is "recommended size"
54 -1 ! iCharset -- Unicode
63 [ ScriptStringAnalyse ] keep
64 [ check-ole32-error ] [ |ScriptStringFree void* deref ] bi* ;
66 : set-dc-colors ( dc font -- )
67 dup background>> >rgba alpha>> 1 number= [
68 ! No transparency needed, set colors from the font.
69 [ background>> color>RGB SetBkColor drop ]
70 [ foreground>> color>RGB SetTextColor drop ] 2bi
72 ! Draw white text on black background. The resulting grayscale
73 ! image will be used as transparency mask for the actual color.
75 [ COLOR: black color>RGB SetBkColor drop ]
76 [ COLOR: white color>RGB SetTextColor drop ] bi
79 : selection-start/end ( script-string -- iMinSel iMaxSel )
80 string>> dup selection? [ [ start>> ] [ end>> ] bi ] [ drop 0 0 ] if ;
82 : draw-script-string ( ssa size script-string -- )
88 [ [ { 0 0 } ] dip <RECT> ]
90 [let :> str str selection-start/end
92 str string>> dup selection? [ string>> ] when
100 ScriptStringOut check-ole32-error ;
102 ! The image is a grayscale rendering of a text string. We want the text to
103 ! have the given color. Move the blue channel of the image (any color
104 ! channel will do, since it's grayscale) into its alpha channel, and make
105 ! the entire image a rectangle of the given color with varying
107 :: color-to-alpha ( image color -- image' )
108 color >rgba-components :> alpha
109 [ 255 * >integer ] tri@ 3byte-array uint32_t deref 24 bits :> rgb
110 image bitmap>> uint32_t cast-array
112 [ [ 0xff bitand alpha * >integer 24 shift rgb bitor ] map! ]
113 [ [ 0xff bitand 24 shift rgb bitor ] map! ]
115 image RGBA >>component-order ;
117 :: render-image ( dc ssa script-string -- image )
118 script-string size>> :> size
120 [ ssa size script-string draw-script-string ] make-bitmap-image
121 script-string font>> [ foreground>> ] [ background>> ] bi
122 >rgba alpha>> 1 number= [ drop ] [ color-to-alpha ] if ;
124 : set-dc-font ( dc font -- )
125 cache-font SelectObject win32-error=0/f ;
127 : ssa-size ( ssa -- dim )
130 [ cx>> ] [ cy>> ] bi 2array ;
132 : dc-metrics ( dc -- metrics )
134 [ GetTextMetrics drop ] keep
137 ! DC limit is default soft-limited to 10,000 per process.
138 : <script-string> ( font string -- script-string )
139 [ script-string new-disposable ] 2dip
140 [ >>font ] [ >>string ] bi*
143 [ over font>> set-dc-font ]
144 [ dc-metrics >>metrics ]
145 [ over string>> make-ssa [ >>ssa ] [ ssa-size >>size ] bi ]
151 M: script-string dispose*
152 ssa>> void* <ref> ScriptStringFree check-ole32-error ;
154 SYMBOL: cached-script-strings
156 : cached-script-string ( font string -- script-string )
157 cached-script-strings get-global [ <script-string> ] 2cache ;
159 : script-string>image ( script-string -- image )
163 [ over font>> [ set-dc-font ] [ set-dc-colors ] 2bi ]
165 dup pick string>> make-ssa
166 dup void* <ref> &ScriptStringFree drop
167 pick render-image >>image
173 [ <cache-assoc> cached-script-strings set-global ]
174 "windows.uniscribe" add-startup-hook