r-lib / svglite

A lightweight svg graphics device for R
https://svglite.r-lib.org
180 stars 39 forks source link

Using <style> element and inline style attributes #39

Closed yixuan closed 9 years ago

yixuan commented 9 years ago

A <style> element will be added when svg_new_page() is called. This globally defines several default attributes, including fill color, stroke color, line end shape, line join shape etc.

Styling properties for a single shape object will be put in an inline style attribute, which override the global ones in <style> when necessary. These properties are written only when they are different from the default values. For example, the following R code

plot.new()
set.seed(123)
symbols(runif(4), runif(4), circles = rep(0.05, 4), inches = FALSE, add = TRUE,
        fg  = c(1, 2, 1, 2),
        bg  = c(1, 1, NA, NA))

will generate SVG file

<?xml version='1.0' encoding='UTF-8' ?>
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 720.00 576.00'>
<defs>
  <style type='text/css'><![CDATA[
    line, polyline, path, rect, circle {
      fill: none;
      stroke: #000000;
      stroke-linecap: round;
      stroke-linejoin: round;
      stroke-miterlimit: 10.00;
    }
  ]]></style>
</defs>
<rect width='100%' height='100%' style='fill: #FFFFFF;'/>
<circle cx='631.63' cy='368.03' r='43.80' style='stroke-width: 0.75; fill: #000000;' />
<circle cx='109.00' cy='162.40' r='43.80' style='stroke-width: 0.75; stroke: #FF0000; fill: #000000;' />
<circle cx='390.81' cy='318.18' r='43.80' style='stroke-width: 0.75;' />
<circle cx='603.57' cy='123.51' r='43.80' style='stroke-width: 0.75; stroke: #FF0000;' />
</svg>

This strategy in general could further reduce the file size of plots. Using the benchmark example in README, the file size is now reduced from ~93K to ~73K.

codecov-io commented 9 years ago

Current coverage is 92.81%

Merging #39 into master will increase coverage by +0.44% as of e6ec2b4

@@            master     #39   diff @@
======================================
  Files            6       6       
  Stmts          328     376    +48
  Branches         0       0       
  Methods          0       0       
======================================
+ Hit            303     349    +46
  Partial          0       0       
- Missed          25      27     +2

Review entire Coverage Diff as of e6ec2b4

Powered by Codecov. Updated on successful CI builds.

hadley commented 9 years ago

Apart from that small comment, this looks great!

yixuan commented 9 years ago

I think it's now ready.

hadley commented 9 years ago

Awesome - thanks!