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