]> gitweb.factorcode.org Git - factor.git/blob - basis/windows/uniscribe/uniscribe.factor
Fixes for out parameter changes
[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: kernel assocs math sequences fry io.encodings.string
4 io.encodings.utf16n accessors arrays combinators destructors
5 cache namespaces init fonts alien.c-types alien.data
6 windows.usp10 windows.offscreen windows.gdi32 windows.ole32
7 windows.types windows.fonts opengl.textures locals
8 windows.errors classes.struct ;
9 IN: windows.uniscribe
10
11 TUPLE: script-string < disposable font string metrics ssa size image ;
12
13 : line-offset>x ( n script-string -- x )
14     2dup string>> length = [
15         ssa>> ! ssa
16         swap 1 - ! icp
17         TRUE ! fTrailing
18     ] [
19         ssa>>
20         swap ! icp
21         FALSE ! fTrailing
22     ] if
23     { int } [ ScriptStringCPtoX ole32-error ] [ ] with-out-parameters ;
24
25 : x>line-offset ( x script-string -- n trailing )
26     ssa>> ! ssa
27     swap ! iX
28     { int int } [ ScriptStringXtoCP ole32-error ] [ ] with-out-parameters ;
29
30 <PRIVATE
31
32 : make-script-string ( dc string -- script-string )
33     dup selection? [ string>> ] when
34     [ utf16n encode ] ! pString
35     [ length ] bi ! cString
36     dup 1.5 * 16 + >integer ! cGlyphs -- MSDN says this is "recommended size"
37     -1 ! iCharset -- Unicode
38     SSA_GLYPHS ! dwFlags
39     0 ! iReqWidth
40     f ! psControl
41     f ! psState
42     f ! piDx
43     f ! pTabdef
44     f ! pbInClass
45     f <void*> ! pssa
46     [ ScriptStringAnalyse ] keep
47     [ ole32-error ] [ |ScriptStringFree *void* ] bi* ;
48
49 : set-dc-colors ( dc font -- )
50     [ background>> color>RGB SetBkColor drop ]
51     [ foreground>> color>RGB SetTextColor drop ] 2bi ;
52
53 : selection-start/end ( script-string -- iMinSel iMaxSel )
54     string>> dup selection? [ [ start>> ] [ end>> ] bi ] [ drop 0 0 ] if ;
55
56 : (draw-script-string) ( script-string -- )
57     [
58         ssa>> ! ssa
59         0 ! iX
60         0 ! iY
61         ETO_OPAQUE ! uOptions
62     ]
63     [ [ { 0 0 } ] dip size>> <RECT> ]
64     [ selection-start/end ] tri
65     ! iMinSel
66     ! iMaxSel
67     FALSE ! fDisabled
68     ScriptStringOut ole32-error ;
69
70 : draw-script-string ( dc script-string -- )
71     [ font>> set-dc-colors ] keep (draw-script-string) ;
72
73 :: make-script-string-image ( dc script-string -- image )
74     script-string size>> dc
75     [ dc script-string draw-script-string ] make-bitmap-image ;
76
77 : set-dc-font ( dc font -- )
78     cache-font SelectObject win32-error=0/f ;
79
80 : script-string-size ( script-string -- dim )
81     ssa>> ScriptString_pSize
82     dup win32-error=0/f
83     [ cx>> ] [ cy>> ] bi 2array ;
84
85 : dc-metrics ( dc -- metrics )
86     TEXTMETRICW <struct>
87     [ GetTextMetrics drop ] keep
88     TEXTMETRIC>metrics ;
89
90 : <script-string> ( font string -- script-string )
91     [ script-string new-disposable ] 2dip
92         [ >>font ] [ >>string ] bi*
93     [
94         {
95             [ over font>> set-dc-font ]
96             [ dc-metrics >>metrics ]
97             [ over string>> make-script-string >>ssa ]
98             [ drop dup script-string-size >>size ]
99             [ over make-script-string-image >>image ]
100         } cleave
101     ] with-memory-dc ;
102
103 PRIVATE>
104
105 M: script-string dispose*
106     ssa>> <void*> ScriptStringFree ole32-error ;
107
108 SYMBOL: cached-script-strings
109
110 : cached-script-string ( font string -- script-string )
111     cached-script-strings get-global [ <script-string> ] 2cache ;
112
113 [ <cache-assoc> &dispose cached-script-strings set-global ]
114 "windows.uniscribe" add-startup-hook