Jump to content

Wikipedia:SVG help

From Wikipedia, the free encyclopedia

SVG help

Scalable Vector Graphics is a commonly used file format for providing a geometrical description of an image using basic objects such as labels, circles, lines, curves and polygons. An image can be reduced or enlarged to an arbitrary size, and will not suffer image data loss, nor will it become pixelated. SVG makes an excellent format for artwork, diagrams and drawings. SVG images are defined in XML text files. This means that they can be searched, indexed, scripted, and compressed. Since they are XML files, SVG images can be edited with any text editor, but SVG-based drawing programs are also available.

However, the rendering engine used by wiki is not perfect, and may cause the image to be shown incorrectly, or differently from how it is displayed in your vector editor of choice. This page enables authors experiencing problems with SVG graphics to obtain some help in getting their images into wiki the way they intend.

Things we can help with


Understanding SVG

  • Questions about the SVG format

Using SVG appropriately

  • When to (or not to) use SVG

What you see is not what you get

  • Missing objects from files
  • Random filled boxes in the image
  • Images that are the wrong size
  • Font inconsistencies
  • Other weird and wonderful bugs

Something new

  • Questions that you can't find a better place for

General suggestions[edit]

The following is a list of common vector editors and suggested settings for compatibility. The user guide of your chosen program may also be valuable in troubleshooting.

Browsers[edit]

Most modern browsers can render SVGs in view-only mode. It may be convenient to set a browser as the default SVG viewer, but you can also paste the file path into the address bar. Please note that even if your browser renders an SVG correctly, it may render differently on Wikipedia.

Text editors[edit]

SVG documents are text files, and can be edited like any other text file. Specifically, SVGs are XML documents, which have special support in many text editors. One potential disadvantage to editing SVGs by hand is that previewing your changes is more cumbersome. A rudimentary solution would be to open the SVG file in a browser while working on it, and manually refreshing the document to see the result of changes.

Inkscape[edit]

There is a simple work-around for the scarcities of librsvg. The operation "Stroke to Path", to be found under Menu>Path in Inkscape or via Ctrl+Alt+C, can be applied to all of the objects that are not rendered correctly. To keep the SVGs editable, this should only be done to the files intended for upload, and these files can be deleted afterwards.

As of February 2014, the objects that must be modified to render correctly by librsvg include:

  • Lines with arrow heads (the arrows need to be converted)
  • Text, that has been transformed, e.g. "Text on Path"
  • Compound objects created with the binary path tools (union, intersect etc.)

OpenOffice.org[edit]

OpenOffice.org SVG files may require manual modification before being uploaded to Wikipedia. To achieve this:

  • Change all fonts to Wikipedia supported fonts as mentioned before. (E.g. change "Sans embedded" to "DejaVu Sans".)
  • Add "px" to all font-size references. (E.g. change "font-size:100" to "font-size:100px".)
  • Remove all additional x coordinate references in tspan elements. (E.g. change <tspan x="17583 17917 " y="10943"> to <tspan x="17583" y="10943">.)
  • [Not required for OO 2.3.0] Explicitly colour all text (e.g. black) by replacing relevant "stroke:none;fill:none" instances with "stroke:none;fill:rgb(0,0,0)" (note that simply explicitly colouring text black in OpenOffice 3.2.1 does not appear to work).

NB: Vector graphics line widths may also need to be set explicitly in OpenOffice.org Draw.

SVG code replacement guide (executing replace all using Nedit regular expressions)[edit]

Original text Replacement text
Sans embedded DejaVu Sans
tspan x="([0-9]*) ([0-9 ]*)" tspan x="\1"

This SVG export procedure has been tested using OO 2.3.0 and OO 3.2.1 with a simple .odg candidate.

Microsoft Powerpoint[edit]

It is easiest to save entire slides from Powerpoint as svg rather than saving multiselected objects with right click / save as picture, because for entire slides the boundaries will be cropped to the size of the slide and the background will be set to white. To save slides as svg, use file / save as / browse / save as type --> svg.

If your slide has text, open the exported svg into Notepad and replace all the existing font-family specifications with font-family="Liberation Sans,sans-serif". This specification will make Wikimedia render SVG fonts similarly to how browsers render Wikipedia fonts.

Text can also get chopped up on export, which causes rendering and localization bugs. To fix this, search for all </text> tags and make sure the tag isn't embedded in another text tag that's on the same line of text. Most embedded text tags can simply be deleted; however, but if content needs to be subscripted or subscripted, then replace the text tag with a tspan specifiction. For subscripts, use <tspan font-size="smaller" baseline-shift="sub">. For superscripts, use <tspan font-size="smaller" baseline-shift="super">.

Microsoft Visio[edit]

Generating SVG charts from Microsoft Excel[edit]

General concept above; examples below.
Warming stripes
(five examples compared)
Warming stripes bar chart
Bar chart (vertical)
Pie chart
Line charts
Scatterplot

I've uploaded .xlsx (Microsoft Excel) spreadsheets that automatically generate XML code for charts in SVG format.

You simply paste or enter your data into the spreadsheet, and specify image dimensions, number of grid lines, font sizes, etc. The spreadsheet instantly and automatically generates a column of XML code that you simply copy and paste into a text editor and save as an ".svg" file. The spreadsheets produce lean SVG code, avoiding the "extra stuff" that Inkscape inserts. They should save you time in creating SVG charts.

Feedback and suggestions on my talk page are welcome. RCraig09 (talk) 23:41, 19 February 2021 (UTC)[reply]

Click HOW TO for detailed explanation.
Click HERE (else HERE) to download the spreadsheets.
  1. Warming stripes — Accepts a single dataset and converts to SVG code portraying Ed Hawkins' warming stripes graphics. User chooses vertical or horizontal stripes; normal or reverse data ordering; or from a variety of geometric shapes (updated 17 May 2023). . . . . Click here to see examples of warming stripes embedded in different shapes.
  2. Warming stripes bar chart — Accepts a single dataset and creates a conventional bar chart whose individual bars/columns are coloured according to Dr. Hawkins' warming stripes colour scheme. Alternate option: choose one colour for ascending bars and another colour for descending bars. (updated 28 August 2023)
  3. Line charts — Accepts up to six datasets. (updated 30 August 2023)
  4. Vertical bar charts (column charts) — Accepts up to six datasets. Toggle between clustered and stacked charts; user can adjust "Yfloor"—the Y level (usually=0) from which columns rise or fall; user chooses to keep or ignore negative input values. (updated 27 August 2023)
  5. Horizontal bar charts — Accepts up to six datasets. Toggle between clustered and stacked charts; user can adjust "Yfloor"—the value (usually=0) from which bars extend; user chooses to keep or ignore negative input values. (updated 27 August 2023)
  6. Scatter plots — Accepts up to five datasets. (updated 28 August 2023)
  7. Pie charts — Accepts a single dataset of up to 36 items. (updated 17 May 2023)
  8. Variable-width bar charts — Accepts up to six datasets; is like "Vertical bar charts", above, but user can choose different widths for different bars. (updated 27 August 2023)

Text guidance[edit]

Use text over paths[edit]

Converting text in an SVG file into paths (shapes) increases file size, prevents localization/accessibility features, and complicates edits/modifications down the line. It is therefore generally disfavored, with some exceptions, such as text-based logos. The Wikimedia text renderer can introduce bugs as its fonts may differ from browser fonts or SVG editor fonts, but following the guidance below should minimize these errors.

font-family property[edit]

Fallback fonts

Due to copyright restrictions, MediaWiki cannot use proprietary fonts that are commonly found on several proprietary operating systems. Fonts such as Geneva require licensing fees to distribute. rsvg will not be able to locate such fonts, and the text will fail to appear in the rendered image. There are three solutions to this issue:

  • One can substitute a font that is available on Wikipedia. This approach facilitates editability.
  • One can specify a generic font-family such as "sans-serif", "serif", or "monospace", but this can lead to inconsistent rendering. It is better to specify a font available on Wikipedia (such as Liberation Sans) with fallback fonts such as: font-family="Liberation Sans,Arial,Helvetica,sans-serif", in which you define a font-list with similar fonts that at least contain one font for each Operating System such as Wikimedia (e.g. Liberation Sans), Windows (e.g. Arial), Linux (e.g. Liberation Sans), Mac (e.g. Helvetica).
  • Since local rendering should be as close as possible to Wikipedia, it should use locally the same font as it will have on Wikipedia, if available. Therefore always define a Wikimedia-font first. Also, Wikimedia has synonyms for substituting fonts, such as "Arial" for "Liberation Sans"; therefore font-family="Arial,DejaVu Sans" will be rendered by "Liberation Sans" and not (as expected) by "DejaVu Sans". (This is because "Liberation Sans" has the same letter size as "Arial" [metric-compatible], so it is less likely to mess up the text flow.)
  • Group the text, create a copy, and convert the copy to paths. Then either:
    1. move the original, editable non-path text into a separate editable text layer that you make transparent (warning: this might be removed by SVG optimizers), or
    2. move the original, editable non-path text outside the visible area (example: File:Essigsäuresynthesen.svg).

For ease of subsequent editing and significantly smaller file sizes, substituting the font with an available font is recommended. Many common fonts have non-proprietary alternatives that are similar in typographical style, resulting in minimal disruption to existing images during substitution. For a list of fonts available in Wikipedia, see available fonts on Meta.

Wikimedia has default fonts, and will use Liberation Serif for Times New Roman and Liberation Sans for Arial. For further fallbacks see c:Help:SVG#fallback.

Fonts that are available on Wikimedia servers may or may not be available on a visitor's machine. If the placement or appearance of text in the image is important and there is uncertainty about which fonts are installed on a visitor's machine, then converting text into path information may be necessary.

font-size property[edit]

Rendering anomalies may occur in thumbnail views if small font sizes are used. Declaring font-size="35" (or larger) appears to avoid this problem, depending on the pixel size of your graphic.

Fonts should be sized so that they look good as thumbnails on wikipedia pages and so they are easy to view on smartphones. Smartphones are how most people access wikipedia. Thumbnails are how most other people see images, as very few people click images and zoom them in to see them. Avoid using or creating images with fonts that are too small to be legible in thumbnail or smartphone views.

The font size that appears in a thumbnail is a combination of svg width, thumbnail width, and font size. To match wikipedia font size as a thumbnail, use font-size = (63/(your upright value))*((your svg width)/1000). For instance, if the thumbnail will be scaled up to thumb upright=1.35 and your image has svg width of 960, set font-size in the svg to (63/1.35)*(960/1000) = 44.8.

Background colors[edit]

Transparent backgrounds are fine, but do not think your image will always be displayed on a light or white background. The wikipedia smartphone app, for instance, renders images on a black background, so if you show black text on a transparent background it will be invisible.

Testing for problems[edit]

The following SVG checkers may help you to detect SVG problems before you upload:

None of these checkers are foolproof, so it's wise to validate images. You can preliminarily validate images by uploading them to the shared, temporary-use location Test.svg, though other users may overwrite your image at any time.

To force refresh images in your browser use Ctrl+F5 (Shift-Reload on Mac), so that you see the latest upload and not cached image copies (this is necessary even on wikimedia upload pages).

Rendering SVG files[edit]

On Wikipedia SVGs are interpreted by the librsvg-library to create PNG previews at different image sizes (to rasterize them). That library only recognizes a subset of all valid SVG syntax, and may render your SVG without many features.

In order to bypass these deficiencies in the library, there are certain parameters that need to be formatted in specific ways or be assigned a workaround value in order for librsvg to accurately render views of your SVG file. Some issues are enumerated below, but be sure to test your SVG files before and after uploading them.

<mask> parameter maskUnits="userSpaceOnUse"[edit]

The librsvg-library does not interpret the value of "userSpaceOnUse" for the parameter maskUnits correctly. To bypass this issue, replace maskUnits="userSpaceOnUse" with maskUnits="-10% -10% 120% 120%", and the SVG mask will render properly on Wikipedia.

parameter stroke-dasharray[edit]

The librsvg-library does not accept a stroke-dasharray parameter with values separated by spaces. Replace all spaces with commas to bypass this issue:
Example: replace stroke-dasharray="2 3 2 4" with stroke-dasharray="2,3,2,4"

Use xlink:href=, not href= alone, in <use ___/> statements[edit]

In Wikimedia projects, <use xlink:href="#myobjectname" transform="translate(10,10)" /> will render properly if you have specified xmlns:xlink="http://www.w3.org/1999/xlink". Be aware that href= by itself will not work on Wikimedia projects even though it might render properly if directly read by your browser.

CSS selectors or identifiers must start with a letter or hyphen[edit]

http://w3.org/TR/css-syntax-3 states, "Property names and at-rule names are always identifiers, which have to start with a letter or a hyphen..."

Common problems[edit]

Misaligned text[edit]

The latest thumbnail-image-maker (named rsvg) unfortunately has a bug which misaligns centre- or right-aligned text tags containing tspan tags on the same line.

SVG with assorted workarounds for T97233

While developers work on a solution, here are some remedies in decreasing usefulness:

  1. Left-align affected text tags with text-anchor="start"
  2. Specify either the y or dy attribute for affected tspan tags
  3. If using multiple tspan tags on a line for:
  4. Merge affected tspan tags, even unitalicising variables etc if above methods fail
  5. Convert text into shapes (last resort, as the text can no longer be amended, copied or read by screen-readers)

flowRoot does not appear[edit]

a picture containing SVG1.2-valid flowRoot

If a black box appears, read c:User:JoKalliauer/RepairFlowRoot how to solve this issue, but do not remove those objects since they might contain text. The workarounds that one can employ are either not to use flowed text (by using the text tool without creating a text field), or convert the text to normal text (by Text-editor or sed-command, or with Inkscape-GUI or with a Inkscape-batch), but to stroke the text using "object to path", since path-text is not recommended and increases file-size.

Missing embedded JPEG images[edit]

Normal image
Broken image

When a raster graphic is embedded in an SVG it is encoded into base64 data. That data is then assigned a MIME type in the <image> element. In the case of an embedded JPEG, the MIME type is "image/jpeg". Older versions of Inkscape (and possibly other editors) assigned the MIME type "image/jpg". While Inkscape and most web browsers will display such an SVG image just fine, the MediaWiki software that rasterizes the SVG file will have trouble with it. Not recognizing the MIME type "image/jpg" there will simply be an empty space where the image is supposed to be. The fix is to open the SVG file in a text editor, find the <image> element, locate "image/jpg", change it to "image/jpeg" and re-save. At right is an example of this problem. The Commons SVG Checker looks for this problem; see Commons:Commons:Commons SVG Checker/KnownBugs#Checks for details.

Though Web browsers cope with image tags without width and height specified, librsvg ignores such images.

Further issues[edit]

Further issues can be found at c:Librsvg_bugs or at Commons:Commons:Commons SVG Checker/KnownBugs, and examples can be found at c:Category:Pictures_demonstrating_a_librsvg_bug. However most issues (for files <1MB) can be fixed using https://svgworkaroundbot.toolforge.org/ (enable "run svgcleaner" and enable "run scour" before clicking convert), for a more detailed list check c:User:SVGWorkaroundBot.

Assistance[edit]

If you have a tricky SVG file with a problem not described, or can't quite figure out what the previous section was talking about, you can simply ask for assistance by posting a quick note hereafter that outlines the problem, as well as providing links to the files that are exhibiting these problems. Don't forget to sign your name with four tilde symbols (~~~~) and an editor will attempt to reply here to help!

When you are happy that a request has been fulfilled, just leave a note so that the request can be archived later, as needed.

An alternative source of help is Commons:Graphics village pump.

Current requests[edit]

Create a new request

[edit]

This is my first time uploading an SVG file. I just uploaded File:Avatar Studios logo.svg and I am certain this file isn't entirely free. I want to resize it appropriately, similar to File:DreamWorks Animation SKG logo with fishing boy.svg. Any advice? Zingo156 (talk) 19:37, 23 May 2024 (UTC)[reply]

@Zingo156: It's not an SVG, it's a PNG that is pretending to be an SVG. Resize the PNG so that it is 281 x 255 instead of 444 x 561, then upload it as a true .PNG, and you will find that it is small enough to satisfy WP:IMAGERES. --Redrose64 🌹 (talk) 20:06, 23 May 2024 (UTC)[reply]

How to make SVG print on multiple predefined pages from a browser[edit]

An example print-and-play game

To illustrate the concept of print-and-play games, I mocked up an example in SVG. As rsvg does not render some of the emoji, I wish to view the SVG in a compatible browser and print to PDF as shown. I wish the pages to break at predefined points so that the printout can be cut into cards. How can I do so?

My workaround was to build in a switch to select which page to render, so that I can manually print one page at a time. Merging and optimising the soft-copies yielded the uploaded file.

Thanks, cmɢʟeeτaʟκ 23:16, 6 June 2024 (UTC)[reply]

Rendering issue[edit]

This logo is not rendered correctly but appears fine on browsers. Can someone help me fix it? Thanks. 0x0a (talk) 06:13, 7 June 2024 (UTC)[reply]

It's not really an SVG image; it's a PNG that some fancy clipping and masking has been applied to. PNGs are not infinitely scalable. Also, it appears to have some East Asian characters in use for id names, these might not work as expected. --Redrose64 🌹 (talk) 07:50, 7 June 2024 (UTC)[reply]
It seems PNG was used to make the complex gradient, similar to File:CIExy1931.svg. As SVG lacks non-radial or non-linear gradients, and the gradient is smooth enough to not show pixelation when zoomed in, that seems a valid use of embedded bitmaps.
I'll investigate why there's a large gap between the shapes, though there are still hairline cracks in the browser rendering due to commons:Librsvg_bugs#Hairline_cracks. cmɢʟeeτaʟκ 15:43, 7 June 2024 (UTC)[reply]
@0x0a: Further to the above, see c:Help:SVG#Document declaration. Your logo lacks the XML Prolog, i.e. the
<?xml version="1.0" encoding="UTF-8"?>
line, and in the <svg> tag, the version="1.1" attribute is absent. --Redrose64 🌹 (talk) 17:26, 8 June 2024 (UTC)[reply]
It works. Thank you! 0x0a (talk) 04:37, 9 June 2024 (UTC)[reply]

Embedded text is not rendered correctly in svg file[edit]

Hello, can someone please fix the file propionate pathway.svg. The text is no longer rendering correctly, probably due to switch element translations. Everything worked fine in the beginning, unfortunately I can't tell when it stopped working. Some letters/words are pushed into each other. Thank you. Abvdj (talk) 16:41, 8 June 2024 (UTC)[reply]

Remake
@Abvdj: I've remade the SVG as it was incredibly bloated, making it difficult to debug. Is it better? cmɢʟeeτaʟκ 12:55, 10 June 2024 (UTC)[reply]
@Cmglee: Thank you for your attempt and your effort. I just uploaded my different versions of the svg file to “Test.svg” to find out which language caused the error. Surprisingly, everything has now worked correctly again. So could you please put my last version back in, I've put so much work into the alignment, proportions, font and spacing. I know that a few boxes need to be lengthened, but I still wanted to wait and see if native speakers have a better and shorter translation. Abvdj (talk) 22:15, 10 June 2024 (UTC)[reply]
I've also put in a lot of time today to remake the diagram and copy all the languages over. The original at 173 KB is unnecessarily large: my version is just 15 KB (8.7% the size), which makes it much easier for future editors to update. cmɢʟeeτaʟκ 23:51, 10 June 2024 (UTC)[reply]
@Cmglee: Why didn't you ask before you took on so much work? There was a misunderstanding. I rather thought that someone was adding a line of code or changing/adding a variable. Maybe something with backup fonts or something similar. A small job. The file size thing is certainly good, but at the same time it locked out the majority who aren't pros at creating/editing SVGs using a text editor. Adding more translations may still be easy for future editors, but adding more arrows, boxes etc. will be more difficult. Maybe I'm wrong, but now I can't just open it with Inkscape and make the needed adjustments. For example, the proportions are not right, which makes it unpleasant to look at the diagram. It looks distorted, as if a small image had been scaled up but not proportionally. The ellipse instead of a circle in the "citric acid cycle" and the small height compared to the length of the text boxes with larger font size intensify this effect even more. Currently the arrowheads are also not rendered correctly, in the original there was a distance of 3 px between arrows and text boxes. The arrow from "methylmalonic acid" to "L-methylmalonyl-CoA" should not be rounded in the corner, since all other arrows are straight, it makes it inharmonious. Also, texts “ATP”, “CoA” & “Acyl-CoA synthetase family member 3 (ACSF3)” are not center aligned to the previously mentioned arrow. I would help with that, now I don't know how. Abvdj (talk) 17:06, 12 June 2024 (UTC)[reply]
If there original works, just revert my change and link to the above justification. cmɢʟeeτaʟκ 19:20, 12 June 2024 (UTC)[reply]

Path elements don't appear, except directly in Chrome and Safari (not in Firefox)[edit]

1990- Growth in share of population that is foreign-born - by country

The file description page, and Wikipedia renderings, do not render 14 differently-colored trapezoidal paths that should be visible in front of the 14 yellow rectangles. Clicking through to the renderings in Chrome and Safari, the trapezoids show up as they should. However, the trapezoids do not show up using Firefox. ( ~ scratches head ~ )

Adding opacity="1.0" did not help. Could the difference between relative and absolute coordinates possibly be the problem?
* Yellow rectangles: <g fill="#ffa" stroke-width="0.4" stroke="black">

Trapezoids:

<g stroke="black" stroke-width="0.1" opacity="1.0">
<path fill="#cdf" d="M152,625 V467.2, L465,362 V625 z"/>
etc etc
</g>

Update 1: the problem is definitely in the trapezoids group itself, as they are not showing up at File:Test:svg after I purposely removed the yellow-rectangles group.

Update 2: Notice how, at File:Test:svg, some of the "File history" thumbnails show the colored trapezoids, but the main display shows an old version.

(Yes, I've cleared my browser cache, many times.)

Any help would be appreciated! —RCraig09 (talk) 17:45, 17 June 2024 (UTC)[reply]

This is the code:
<g stroke="black" stroke-width="0.1" opacity="1.0">
<path fill="#cdf" d="M152,625 V467.2, L465,362 V625 z"/> 
<path fill="#ceb" d="M467,625 V484.78, L545,292 V625 z"/> 
<path fill="#fcb" d="M547,625 V520, L610,380 V625 z"/> 
<path fill="#fb0" d="M612,625 V449.61, L672,397 V625 z"/> 
<path fill="#e8e" d="M674,625 V589.93, L718,362 V625 z"/> 
<path fill="#db8" d="M720,625 V572.36, L775,432 V625 z"/> 
<path fill="#fa5" d="M777,625 V589.85, L855,502 V625 z"/> 
<path fill="#cdf" d="M857,625 V485, L873,380 V625 z"/> 
<path fill="#ceb" d="M875,625 V607.5, L992,590 V625 z"/> 
<path fill="#fcb" d="M994,625 V467.5, L1004,275 V625 z"/> 
<path fill="#fb0" d="M1006,625 V397.11, L1017,327 V625 z"/> 
<path fill="#e8e" d="M1019,625 V623.23, L1067,572 V625 z"/> 
<path fill="#db8" d="M1069,625 V519.76, L1079,397 V625 z"/> 
<path fill="#fa5" d="M1081,625 V537.5, L1086,415 V625 z"/> 
</g>
In Firefox 127.0 they are there, but extremely narrow - they have the appearance of vertical lines of varying height, the thickness of the line corresponds with the very narrow stroke-width: one-tenth of a pixel, when your canvas is 1200px wide, is barely perceptible. At that sort of scale, the black is largely lost as the colour of the adjacent pixels swamps these lines. --Redrose64 🌹 (talk) 07:59, 18 June 2024 (UTC)[reply]
@RCraig09: you have a spurious extra comma on every d= attribute in that block. Change <path fill="#cdf" d="M152,625 V467.2, L465,362 V625 z"/> to <path fill="#cdf" d="M152,625 V467.2 L465,362 V625 z"/> and similarly for all of the others. --Redrose64 🌹 (talk) 08:03, 18 June 2024 (UTC)[reply]
D'oh! I have committed a rookie error. Many thanks, User:Redrose64. —RCraig09 (talk) 14:22, 18 June 2024 (UTC)[reply]
Interesting. Ordinarily, commas and spaces are equivalent in path d attributes but here it makes a difference. cmɢʟeeτaʟκ 11:18, 19 June 2024 (UTC)[reply]
Resolved