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.utf16 kernel
7 literals locals math math.bitwise math.functions namespaces
8 sequences specialized-arrays windows.errors windows.fonts
9 windows.gdi32 windows.offscreen windows.ole32 windows.types
12 SPECIALIZED-ARRAY: uint32_t
15 TUPLE: script-string < disposable font string metrics ssa size image ;
19 CONSTANT: ssa-dwFlags flags{ SSA_GLYPHS SSA_FALLBACK SSA_TAB }
21 :: >codepoint-index ( str utf16-index -- codepoint-index )
22 0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
24 :: >utf16-index ( str codepoint-index -- utf16-index )
25 0 codepoint-index str subseq utf16n encode length 2 /i ;
29 :: line-offset>x ( n script-string -- x )
30 script-string string>> n >utf16-index :> n-utf16
31 script-string ssa>> ! ssa
32 n script-string string>> length = [
39 { int } [ ScriptStringCPtoX check-ole32-error ] with-out-parameters ;
41 :: x>line-offset ( x script-string -- n trailing )
42 script-string ssa>> ! ssa
44 { int int } [ ScriptStringXtoCP check-ole32-error ] with-out-parameters
45 swap dup 0 < [ script-string string>> swap >codepoint-index ] unless
50 : make-ssa ( dc script-string -- ssa )
51 dup selection? [ string>> ] when
52 utf16n encode ! pString
53 dup length 2 /i ! cString
54 dup 1.5 * 16 + >integer ! cGlyphs -- MSDN says this is "recommended size"
55 -1 ! iCharset -- Unicode
64 [ ScriptStringAnalyse ] keep
65 [ check-ole32-error ] [ |ScriptStringFree void* deref ] bi* ;
67 : set-dc-colors ( dc font -- )
68 dup background>> >rgba alpha>> 1 number= [
69 ! No transparency needed, set colors from the font.
70 [ background>> color>RGB SetBkColor drop ]
71 [ foreground>> color>RGB SetTextColor drop ] 2bi
73 ! Draw white text on black background. The resulting grayscale
74 ! image will be used as transparency mask for the actual color.
76 [ COLOR: black color>RGB SetBkColor drop ]
77 [ COLOR: white color>RGB SetTextColor drop ] bi
80 : selection-start/end ( script-string -- iMinSel iMaxSel )
81 string>> dup selection? [ [ start>> ] [ end>> ] bi ] [ drop 0 0 ] if ;
83 : draw-script-string ( ssa size script-string -- )
89 [ [ { 0 0 } ] dip <RECT> ]
91 [let :> str str selection-start/end
93 str string>> dup selection? [ string>> ] when
101 ScriptStringOut check-ole32-error ;
103 ! The image is a grayscale rendering of a text string. We want the text to
104 ! have the given color. Move the blue channel of the image (any color
105 ! channel will do, since it's grayscale) into its alpha channel, and make
106 ! the entire image a rectangle of the given color with varying
108 :: color-to-alpha ( image color -- image' )
109 color >rgba-components :> alpha
110 [ 255 * round >integer ] tri@ 3byte-array uint32_t deref 24 bits :> rgb
111 image bitmap>> uint32_t cast-array
113 [ [ 0xff bitand alpha * >integer 24 shift rgb bitor ] map! ]
114 [ [ 0xff bitand 24 shift rgb bitor ] map! ]
116 image RGBA >>component-order ;
118 :: render-image ( dc ssa script-string -- image )
119 script-string size>> :> size
121 [ ssa size script-string draw-script-string ] make-bitmap-image
122 script-string font>> [ foreground>> ] [ background>> ] bi
123 >rgba alpha>> 1 number= [ drop ] [ color-to-alpha ] if ;
125 : set-dc-font ( dc font -- )
126 cache-font SelectObject win32-error=0/f ;
128 : ssa-size ( ssa -- dim )
131 [ cx>> ] [ cy>> ] bi 2array ;
133 : dc-metrics ( dc -- metrics )
135 [ GetTextMetrics drop ] keep
138 ! DC limit is default soft-limited to 10,000 per process.
139 : <script-string> ( font string -- script-string )
140 [ script-string new-disposable ] 2dip
141 [ >>font ] [ >>string ] bi*
144 [ over font>> set-dc-font ]
145 [ dc-metrics >>metrics ]
146 [ over string>> make-ssa [ >>ssa ] [ ssa-size >>size ] bi ]
152 M: script-string dispose*
153 ssa>> void* <ref> ScriptStringFree check-ole32-error ;
155 SYMBOL: cached-script-strings
157 : cached-script-string ( font string -- script-string )
158 cached-script-strings get-global [ <script-string> ] 2cache ;
160 : script-string>image ( script-string -- image )
164 [ over font>> [ set-dc-font ] [ set-dc-colors ] 2bi ]
166 dup pick string>> make-ssa
167 dup void* <ref> &ScriptStringFree drop
168 pick render-image >>image
174 STARTUP-HOOK: [ <cache-assoc> cached-script-strings set-global ]