-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg2jfx.clj
235 lines (217 loc) · 5.68 KB
/
svg2jfx.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
(ns
svg2jfx
"Make a GUI node from an SVG"
(:require [cljfx.api :as fx]
[cljfx.composite :as com])
(:import #_[javafx.embed.swing JFXPanel]
com.kitfox.svg.SVGCache
[afester.javafx.svg SvgLoader]))
(set! *warn-on-reflection* true)
;; ## Converting SVGs to JavaFX objects
;; Takes a string and turns it into an input stream
(defn string->stream
([^java.lang.String s] (string->stream s "UTF-8"))
([^java.lang.String s
^java.lang.String encoding]
(-> s
(.getBytes encoding)
(java.io.ByteArrayInputStream.))))
;;
(defn svg2batik-fn
"Use the FranzXaver library to turn an SVG
into a JavaFX compatible Group Node
.. (which shows up as a picture)
This is using Batik under the hood somehow"
[^java.lang.String svg-xml-string
scale-x
scale-y]
(println
"Yo"
" scale x: "
scale-x
" scale y: "
scale-y)
(fn
[]
(let [^javafx.scene.Group
img (.loadSvg
(SvgLoader.)
^java.io.ByteArrayInputStream
(string->stream
svg-xml-string))]
(.setScaleX
img
scale-x)
(.setScaleY
img
scale-y)
img)))
(defn
batik-load
[svg-string]
(.loadSvg
(SvgLoader.)
^java.io.ByteArrayInputStream
(string->stream
svg-string)))
(defn
batik-scale
[^javafx.scene.Group batik-group
scale-x
scale-y]
(.setScaleX
batik-group
scale-x)
(.setScaleY
batik-group
scale-y)
batik-group)
(defn
xml
"Converts an SVG string to a JavaFX Group-like object
`:svg` for the svg string
`:width` for an optional width parameter
NOTE: The library seems to work weird with widths
if you specify the display width with `:width`
then the `<svg>` tag shouldn't specify its own height/width"
[{:keys [fx/context
svg
scale-x
scale-y]}]
{:fx/type fx/ext-instance-factory
:create (svg2batik-fn
svg
scale-x
scale-y)
#_(girod2jfx-fn
svg
width)})
#_#_params (LoaderParameters.) ;; seemingly unused
(defn-
salamander-buffered-image
"How to turn an SVG XML string to an image
Uses `svgSalamander`"
[svg-xml-str
width
height]
(let [universe (com.kitfox.svg.SVGCache/getSVGUniverse)
uri (.loadSVG
universe
(java.io.StringReader.
svg-xml-str)
"unused-placeholder")
diagram (.getDiagram
universe
uri)
buffered-image (java.awt.image.BufferedImage.
width
height
java.awt.image.BufferedImage/TYPE_4BYTE_ABGR)
graphics-2d (.createGraphics
buffered-image)]
(.setRenderingHint
graphics-2d
java.awt.RenderingHints/KEY_ANTIALIASING
java.awt.RenderingHints/VALUE_ANTIALIAS_ON)
(.render
diagram
graphics-2d)
(.removeDocument
universe
uri)
buffered-image))
#_
(javax.imageio.ImageIO/write
(salamander-buffered-image
(slurp
"small-map-notext.svg")
100
100)
"jpg"
(java.io.File.
"buff.jpg"))
(defn-
salamander2jfx-image
"How to turn an SVG XML string to an image
Uses `svgSalamander`"
[svg-xml-str
width]
(->
svg-xml-str
(salamander-buffered-image
width
width) ;;height
((fn
[buff-img]
(println
"Writing image to disk")
(javax.imageio.ImageIO/write
^java.awt.image.BufferedImage buff-img
"jpg"
(java.io.File.
"buff.jpg"))
buff-img))
(javafx.embed.swing.SwingFXUtils/toFXImage
nil)))
(defn
xml2img
[{:keys [fx/context
svg
width]}]
{:fx/type :scroll-pane
;; :fit-to-width true
;; :fit-to-height true
:content {:fx/type :image-view
;; :fit-height width
:image (salamander2jfx-image
svg
width)
#_ (girod2jfx-image
svg
width)}})
#_
{:fx/type :stack-pane
;; :preserve-ratio true
:children []
:style {:-fx-background-repeat "stretch"
:-fx-background-position "center center";
:-fx-background-image (let [#_#_params (LoaderParameters.)
image (->
^String svg
(SVGLoader/load))]
(.snapshot
image
"jpg"
(java.io.File.
"test.txt"))
(spit "test.svg" svg)
(.toImage
image))}}
#_
(set! (.
params
applyViewportPosition)
false)
#_#_ (set! (.
params
width)
100)
(set! (.
params
centerImage)
true)
#_
(def props
(merge
fx.shape/props
(composite/props SVGPath
:content [:setter lifecycle/scalar]
:fill-rule [:setter lifecycle/scalar :coerce (coerce/enum FillRule)
:default :non-zero])))
#_
(def lifecycle
(lifecycle/annotate
(composite/describe SVGPath
:ctor []
:props props)
:svg-path))