googlefonts / picosvg

Helps simplify SVG files. Intended for use as part of a font build.
Apache License 2.0
142 stars 12 forks source link

Possible to ignore vendor style? #293

Closed NightFurySL2001 closed 1 year ago

NightFurySL2001 commented 1 year ago

Sometimes SVG generated by Inkscape contains vendor-specific extensions starting with -, such as Inkscape's -inkscape-font-specification and -inkscape-stroke.

anthrotype commented 1 year ago

hi! what do you exactly mean here by "support"? What would you like picosvg to do with those?

NightFurySL2001 commented 1 year ago

Sorry for using the term "support", I was quoting wrongly from the W3C page. It would be best if picosvg can ignore vendor-specific extensions. (also if possibly, any CSS properties that are not supported).

Quote (4.1.2.1 Vendor-specific extensions):

Thus typical CSS implementations may not recognize such properties and may ignore them according to the rules for handling parsing errors.

Quote (4.2 Rules for handling parsing errors):

In some cases, user agents must ignore part of an illegal style sheet. This specification defines ignore to mean that the user agent parses the illegal part (in order to find its beginning and end), but otherwise acts as if it had not been there. CSS 2.1 reserves for future updates of CSS all property:value combinations and @-keywords that do not contain an identifier beginning with dash or underscore. Implementations must ignore such combinations (other than those introduced by future updates of CSS).

anthrotype commented 1 year ago

yes, sounds reasonable. Would you like to work on a PR?

NightFurySL2001 commented 1 year ago

Actually after checking, it seems that unknown styles are safely ignored properly, which I modified the topic wrongly.

Inspecting the error code, it seems that the source of error is not from picosvg but from etree:

 Exception in thread Thread-1 (build_font_thread):
 Traceback (most recent call last):
   File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
     self.run()
   File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run
     self._target(*self._args, **self._kwargs)
   File "d:\Desktop\coding\svg2font\main_ui.py", line 197, in build_font_thread
     logs = build_font(
   File "d:\Desktop\coding\svg2font\newfontbuild.py", line 253, in build_font
     glyph, glyphadv = getOutlineFromSVG(svg_fulltext, metrics.upm, metrics.ascender, picoNormalize)
   File "d:\Desktop\coding\svg2font\newfontbuild.py", line 83, in getOutlineFromSVG
     picosvg.topicosvg(inplace=True, allow_text=True)
   File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg.py", line 1356, in topicosvg
     self.apply_style_attributes(inplace=True)
   File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg.py", line 471, in apply_style_attributes
     self._apply_styles(el)
   File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg.py", line 454, in _apply_styles
     parse_css_declarations(el.attrib.pop("style", ""), el.attrib)
   File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg_meta.py", line 170, in parse_css_declarations
     output[property_name] = value.strip()
   File "src\lxml\etree.pyx", line 2447, in lxml.etree._Attrib.__setitem__
   File "src\lxml\apihelpers.pxi", line 586, in lxml.etree._setAttributeValue
   File "src\lxml\apihelpers.pxi", line 1764, in lxml.etree._attributeValidOrRaise
 ValueError: Invalid attribute name '-inkscape-font-specification'

Any values that start with dash are not accepted as etree attribute.

The only method I can come up so far to resolve this would be to filter everything starting with dash before this line: https://github.com/googlefonts/picosvg/blob/84c649c5a15e3c441ed85f9fc8f1d3c49c526ad9/src/picosvg/svg_meta.py#L169

if (property_names is None or property_name in property_names) and not property_name.startswith("-"):
NightFurySL2001 commented 1 year ago

It seems like this might be a bug with lxml module. https://bugs.launchpad.net/lxml/+bug/2020379

anthrotype commented 1 year ago

I don't think it's a bug, the XML references says at https://www.w3.org/TR/xml/#sec-common-syn

The first character of a Name must be a NameStartChar, and any other characters must be NameChars

The NameStartChar does not include hyphen "-" (0x002D).

anthrotype commented 1 year ago

the link you posted about vendor specific extensions https://www.w3.org/TR/CSS2/syndata.html#vendor-keywords is about CSS spec, not XML one. When picosvg copies inlined CSS style attributes to svg element's attribute with the same name, since XML can't contain such hyphen prefixed attributes, we can't store them as such so we have to skip them, leave them un-parsed.

NightFurySL2001 commented 1 year ago

I see. I found conflicting resources on whether dash are allowed to start in XML files (without looking at W3C 😅) and so made a mistake there. The bug at lxml can't be closed though so will need to wait for them to react.