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