]> gitweb.factorcode.org Git - factor.git/blob - basis/windows/uniscribe/uniscribe.factor
colors: merge colors.constants and colors.hex.
[factor.git] / basis / windows / uniscribe / uniscribe.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3
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 ;
10
11 SPECIALIZED-ARRAY: uint32_t
12 IN: windows.uniscribe
13
14 TUPLE: script-string < disposable font string metrics ssa size image ;
15
16 <PRIVATE
17
18 CONSTANT: ssa-dwFlags flags{ SSA_GLYPHS SSA_FALLBACK SSA_TAB }
19
20 :: >codepoint-index ( str utf16-index -- codepoint-index )
21     0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
22
23 :: >utf16-index ( str codepoint-index -- utf16-index )
24     0 codepoint-index str subseq utf16n encode length 2 /i ;
25
26 PRIVATE>
27
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 = [
32         n-utf16 1 - ! icp
33         TRUE ! fTrailing
34     ] [
35         n-utf16 ! icp
36         FALSE ! fTrailing
37     ] if
38     { int } [ ScriptStringCPtoX check-ole32-error ] with-out-parameters ;
39
40 :: x>line-offset ( x script-string -- n trailing )
41     script-string ssa>> ! ssa
42     x ! iX
43     { int int } [ ScriptStringXtoCP check-ole32-error ] with-out-parameters
44     swap dup 0 < [ script-string string>> swap >codepoint-index ] unless
45     swap ;
46
47 <PRIVATE
48
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
55     ssa-dwFlags
56     0 ! iReqWidth
57     f ! psControl
58     f ! psState
59     f ! piDx
60     f ! pTabdef
61     f ! pbInClass
62     f void* <ref> ! pssa
63     [ ScriptStringAnalyse ] keep
64     [ check-ole32-error ] [ |ScriptStringFree void* deref ] bi* ;
65
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
71     ] [
72         ! Draw white text on black background. The resulting grayscale
73         ! image will be used as transparency mask for the actual color.
74         drop
75         [ COLOR: black color>RGB SetBkColor drop ]
76         [ COLOR: white color>RGB SetTextColor drop ] bi
77     ] if ;
78
79 : selection-start/end ( script-string -- iMinSel iMaxSel )
80     string>> dup selection? [ [ start>> ] [ end>> ] bi ] [ drop 0 0 ] if ;
81
82 : draw-script-string ( ssa size script-string -- )
83     [
84         0 ! iX
85         0 ! iY
86         ETO_OPAQUE ! uOptions
87     ]
88     [ [ { 0 0 } ] dip <RECT> ]
89     [
90         [let :> str str selection-start/end
91          [
92              str string>> dup selection? [ string>> ] when
93              swap >utf16-index
94          ] bi@
95         ]
96     ] tri*
97     ! iMinSel
98     ! iMaxSel
99     FALSE ! fDisabled
100     ScriptStringOut check-ole32-error ;
101
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
106 ! transparency.
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
111         alpha 1 <
112         [ [ 0xff bitand alpha * >integer 24 shift rgb bitor ] map! ]
113         [ [ 0xff bitand                  24 shift rgb bitor ] map! ]
114         if drop
115     image RGBA >>component-order ;
116
117 :: render-image ( dc ssa script-string -- image )
118     script-string size>> :> size
119     size dc
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 ;
123
124 : set-dc-font ( dc font -- )
125     cache-font SelectObject win32-error=0/f ;
126
127 : ssa-size ( ssa -- dim )
128     ScriptString_pSize
129     dup win32-error=0/f
130     [ cx>> ] [ cy>> ] bi 2array ;
131
132 : dc-metrics ( dc -- metrics )
133     TEXTMETRICW <struct>
134     [ GetTextMetrics drop ] keep
135     TEXTMETRIC>metrics ;
136
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*
141     [
142         {
143             [ over font>> set-dc-font ]
144             [ dc-metrics >>metrics ]
145             [ over string>> make-ssa [ >>ssa ] [ ssa-size >>size ] bi ]
146         } cleave
147     ] with-memory-dc ;
148
149 PRIVATE>
150
151 M: script-string dispose*
152     ssa>> void* <ref> ScriptStringFree check-ole32-error ;
153
154 SYMBOL: cached-script-strings
155
156 : cached-script-string ( font string -- script-string )
157     cached-script-strings get-global [ <script-string> ] 2cache ;
158
159 : script-string>image ( script-string -- image )
160     dup image>> [
161         [
162             {
163                 [ over font>> [ set-dc-font ] [ set-dc-colors ] 2bi ]
164                 [
165                     dup pick string>> make-ssa
166                     dup void* <ref> &ScriptStringFree drop
167                     pick render-image >>image
168                 ]
169             } cleave
170         ] with-memory-dc
171     ] unless image>> ;
172
173 [ <cache-assoc> cached-script-strings set-global ]
174 "windows.uniscribe" add-startup-hook