]> gitweb.factorcode.org Git - factor.git/blob - basis/windows/uniscribe/uniscribe.factor
77bdc94fc3e553b6453fb9c36aad1f842fd96dde
[factor.git] / basis / windows / uniscribe / uniscribe.factor
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
4 byte-arrays cache classes.struct colors colors.constants
5 combinators destructors fonts images init io.encodings.string
6 io.encodings.utf16n kernel literals locals math math.bitwise
7 namespaces sequences specialized-arrays windows.errors
8 windows.fonts windows.gdi32 windows.offscreen windows.ole32
9 windows.types windows.usp10 ;
10 SPECIALIZED-ARRAY: uint32_t
11 IN: windows.uniscribe
12
13 TUPLE: script-string < disposable font string metrics ssa size image ;
14
15 <PRIVATE
16
17 CONSTANT: ssa-dwFlags flags{ SSA_GLYPHS SSA_FALLBACK SSA_TAB }
18
19 :: line-offset>x ( n script-string -- x )
20     n script-string
21     2dup string>> length = [
22         ssa>> ! ssa
23         ! swap 1 - ! icp
24         swap 0 swap script-string string>> subseq
25         utf16n encode length 2 / >integer 1 - ! icp
26         TRUE ! fTrailing
27     ] [
28         ssa>>
29         ! swap ! icp
30         swap 0 swap script-string string>> subseq
31         utf16n encode length 2 / >integer ! icp
32         FALSE ! fTrailing
33     ] if
34     { int } [ ScriptStringCPtoX check-ole32-error ] with-out-parameters ;
35
36 :: x>line-offset ( x script-string -- n trailing )
37     x script-string
38     ssa>> ! ssa
39     swap ! iX
40     { int int } [ ScriptStringXtoCP check-ole32-error ] with-out-parameters
41     swap 2 * 0 swap script-string string>> utf16n encode subseq
42     utf16n decode length
43     swap ;
44
45 : make-ssa ( dc script-string -- ssa )
46     dup selection? [ string>> ] when
47     !    [ utf16n encode ] ! pString
48     !    [ length ] bi ! cString
49     utf16n encode ! pString
50     dup length 2 / >integer ! cString
51     dup 1.5 * 16 + >integer ! cGlyphs -- MSDN says this is "recommended size"
52     -1 ! iCharset -- Unicode
53     ssa-dwFlags
54     0 ! iReqWidth
55     f ! psControl
56     f ! psState
57     f ! piDx
58     f ! pTabdef
59     f ! pbInClass
60     f void* <ref> ! pssa
61     [ ScriptStringAnalyse ] keep
62     [ check-ole32-error ] [ |ScriptStringFree void* deref ] bi* ;
63
64 : set-dc-colors ( dc font -- )
65     dup background>> >rgba alpha>> 1 number= [
66         ! No transparency needed, set colors from the font.
67         [ background>> color>RGB SetBkColor drop ]
68         [ foreground>> color>RGB SetTextColor drop ] 2bi
69     ] [
70         ! Draw white text on black background. The resulting grayscale
71         ! image will be used as transparency mask for the actual color.
72         drop
73         [ COLOR: black color>RGB SetBkColor drop ]
74         [ COLOR: white color>RGB SetTextColor drop ] bi
75     ] if ;
76
77 : selection-start/end ( script-string -- iMinSel iMaxSel )
78     string>> dup selection? [ [ start>> ] [ end>> ] bi ] [ drop 0 0 ] if ;
79
80 : draw-script-string ( ssa size script-string -- )
81     [
82         0 ! iX
83         0 ! iY
84         ETO_OPAQUE ! uOptions
85     ]
86     [ [ { 0 0 } ] dip <RECT> ]
87     [ selection-start/end ] tri*
88     ! iMinSel
89     ! iMaxSel
90     FALSE ! fDisabled
91     ScriptStringOut check-ole32-error ;
92
93 ! The image is a grayscale rendering of a text string. We want the text to
94 ! have the given color. Move the blue channel of the image (any color
95 ! channel will do, since it's grayscale) into its alpha channel, and make
96 ! the entire image a rectangle of the given color with varying
97 ! transparency.
98 :: color-to-alpha ( image color -- image' )
99     color >rgba-components :> alpha
100     [ 255 * >integer ] tri@ 3byte-array uint32_t deref 24 bits :> rgb
101     image bitmap>> uint32_t cast-array
102         alpha 1 <
103         [ [ 0xff bitand alpha * >integer 24 shift rgb bitor ] map! ]
104         [ [ 0xff bitand                  24 shift rgb bitor ] map! ]
105         if drop
106     image RGBA >>component-order ;
107
108 :: render-image ( dc ssa script-string -- image )
109     script-string size>> :> size
110     size dc
111     [ ssa size script-string draw-script-string ] make-bitmap-image
112     script-string font>> [ foreground>> ] [ background>> ] bi
113     >rgba alpha>> 1 number= [ drop ] [ color-to-alpha ] if ;
114
115 : set-dc-font ( dc font -- )
116     cache-font SelectObject win32-error=0/f ;
117
118 : ssa-size ( ssa -- dim )
119     ScriptString_pSize
120     dup win32-error=0/f
121     [ cx>> ] [ cy>> ] bi 2array ;
122
123 : dc-metrics ( dc -- metrics )
124     TEXTMETRICW <struct>
125     [ GetTextMetrics drop ] keep
126     TEXTMETRIC>metrics ;
127
128 ! DC limit is default soft-limited to 10,000 per process.
129 : <script-string> ( font string -- script-string )
130     [ script-string new-disposable ] 2dip
131         [ >>font ] [ >>string ] bi*
132     [
133         {
134             [ over font>> set-dc-font ]
135             [ dc-metrics >>metrics ]
136             [ over string>> make-ssa [ >>ssa ] [ ssa-size >>size ] bi ]
137         } cleave
138     ] with-memory-dc ;
139
140 PRIVATE>
141
142 M: script-string dispose*
143     ssa>> void* <ref> ScriptStringFree check-ole32-error ;
144
145 SYMBOL: cached-script-strings
146
147 : cached-script-string ( font string -- script-string )
148     cached-script-strings get-global [ <script-string> ] 2cache ;
149
150 : script-string>image ( script-string -- image )
151     dup image>> [
152         [
153             {
154                 [ over font>> [ set-dc-font ] [ set-dc-colors ] 2bi ]
155                 [
156                     dup pick string>> make-ssa
157                     dup void* <ref> &ScriptStringFree drop
158                     pick render-image >>image
159                 ]
160             } cleave
161         ] with-memory-dc
162     ] unless image>> ;
163
164 [ <cache-assoc> cached-script-strings set-global ]
165 "windows.uniscribe" add-startup-hook