Giter Site home page Giter Site logo

plotters-svg's People

Contributors

38 avatar dependabot-preview[bot] avatar facorread avatar matprec avatar nuald avatar serzhiio avatar sloshwoven avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sftse facorread

plotters-svg's Issues

svg draw_text do not preserve spaces

Hello,

this code

use plotters::prelude::*;

fn main() {

    let root_drawing_area = SVGBackend::new("images/0.1.svg", (1159, 539))
        .into_drawing_area();
    root_drawing_area.fill(&WHITE).unwrap();
    let text_style = TextStyle::from(("monospace", 25).into_font());
    root_drawing_area.draw_text("|___|___|", &text_style, (100,100)).unwrap();
    root_drawing_area.draw_text("|   |   |", &text_style, (100,150)).unwrap();
    root_drawing_area.draw_text("|   |   |", &text_style, (100,200)).unwrap();
}

produce next image:
image

while all vertical bars should be aligned.

Looks like the problem is in the svg text tag: If I change from

<text x="100" y="150" dy="0.76em" text-anchor="start" font-family="monospace" font-size="20.161290322580644" opacity="1" fill="#000000">
|   |   |
</text>

to

<text x="100" y="150" dy="0.76em" text-anchor="start" font-family="monospace" font-size="20.161290322580644" opacity="1" fill="#000000" xml:space="preserve">
|   |   |
</text>

(note xml:space="preserve" inside text tag) then it looks better

Reduce SVG size

For example this histogram is 13kB, while it could be 3kB (compressed via the first site i found):

sample histogram

In fact, looking at the XML code, it does look very repetitive.

<svg width="1152" height="648" viewBox="0 0 1152 648" xmlns="http://www.w3.org/2000/svg">
<rect x="0" y="0" width="1151" height="647" opacity="1" fill="#FFFFFF" stroke="none"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="81" y1="582" x2="81" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="113" y1="582" x2="113" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="146" y1="582" x2="146" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="179" y1="582" x2="179" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="212" y1="582" x2="212" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="244" y1="582" x2="244" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="277" y1="582" x2="277" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="310" y1="582" x2="310" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="343" y1="582" x2="343" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="376" y1="582" x2="376" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="408" y1="582" x2="408" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="441" y1="582" x2="441" y2="4"/>
<line opacity="0.1" stroke="#000000" stroke-width="1" x1="474" y1="582" x2="474" y2="4"/>
...

An example optimization is to make this line list a <path> instead (see also path commands):

<svg width="1152" height="648" viewBox="0 0 1152 648" xmlns="http://www.w3.org/2000/svg">
<rect x="0" y="0" width="1151" height="647" opacity="1" fill="#FFFFFF" stroke="none"/>
<path opacity="0.1" stroke="#000000" stroke-width="1" d="M81,582 V4 M113,582 V4 M146,582 V4 ..."/>

To enable this kind of optimizations, I suggest making SVGBackend work by appending SVG elements to an array, and only write to its output on DrawingBackend::present, so that this kind of pattern can be minified.

Willing to make a patch for this, what do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.