Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add font family parser #20

Merged
merged 27 commits into from
Feb 4, 2024
Merged

Conversation

LaurenzV
Copy link
Contributor

@LaurenzV LaurenzV commented Feb 3, 2024

Still need to add documentation and maybe some more tests (if you have any suggestions let me know). But I wanted to get some feedback on the technical side first before doing that.

I basically copied the char processing from simplecss since it's also what we need.

Escape sequences are currently not processed (including normal escape sequences like "). I don't think there are many font names with a quotation mark inside, so I think it should be fine. :p

Let me know what you think!

I also rewrote resvg on my branch it it seems to work without problems (one problem I encountered was that in some test files the font "Mplus 1p" was used but it was not put in quotes, which I think is wrong because an ident cannot start with a number. Once I put it in quotes, it worked again with my version.

Some relevant links:
https://www.w3.org/TR/2018/REC-css-fonts-3-20180920/#font-family-prop
https://drafts.csswg.org/css-syntax-3/#typedef-ident-token

@RazrFalcon
Copy link
Collaborator

I honestly not sure at what point escaping should be done. Technically, svgtypes shouldn't know anything about escaping. All XML/CSS escaping should be performed already.
But we do not parse font-family XML property using CSS parser, therefore only XML un-escaping would be performed.
Right now, font-family="Arial" and style="font-family:Arial" are two separate things. Can CSS escapes be used in XML properties? I have no idea.

src/stream.rs Outdated Show resolved Hide resolved
src/stream.rs Outdated Show resolved Hide resolved
src/stream.rs Outdated Show resolved Hide resolved
src/font.rs Show resolved Hide resolved
let ch = self.curr_byte()?;
if ch == b'\'' || ch == b'\"' {
let res = self.parse_string()?;
FontFamily::Named(res.to_string())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return &str?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I should have it take a Cow? because I also need to be able to pass an owned string (see below).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? You're never modifying the input string.

Copy link
Contributor Author

@LaurenzV LaurenzV Feb 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, here, basically when the font name is given as a list of idents instead of a quoted string:

let joined = idents.join(" ");

// TODO: No CSS keyword must be matched as a family name...
match joined.as_str() {
    "serif" => FontFamily::Serif,
    "sans-serif" => FontFamily::SansSerif,
    "cursive" => FontFamily::Cursive,
    "fantasy" => FontFamily::Fantasy,
    "monospace" => FontFamily::Monospace,
    _ => FontFamily::Named(joined),
}

The spec requires us to parse multiple spaces as a single space (which should again probably be done at the CSS parsing level, but since it's relatively straight-forward I just implemented it here).

@RazrFalcon
Copy link
Collaborator

Not to mention that simplecss doesn't even support basic escapes via \ (aka U+005C REVERSE SOLIDUS). It is used not only for Unicode escapes, but also for new-lines. But we support only quotes escaping.

Basically, there is an endless amount of work left in resvg.

@LaurenzV
Copy link
Contributor Author

LaurenzV commented Feb 4, 2024

Can CSS escapes be used in XML properties? I have no idea.

At least from my testing it seems like most browsers recognize them. But not sure if it's mentioned anywhere in the specification.

@RazrFalcon
Copy link
Collaborator

I haven't seen it in the spec either. Very confusing.

src/font.rs Outdated Show resolved Hide resolved
@LaurenzV LaurenzV marked this pull request as ready for review February 4, 2024 17:52
@RazrFalcon RazrFalcon merged commit 3764908 into linebender:master Feb 4, 2024
2 checks passed
@RazrFalcon
Copy link
Collaborator

Thanks!

@LaurenzV
Copy link
Contributor Author

LaurenzV commented Feb 4, 2024

I'm planning on adding the font shorthand as well in the next few days and then I'll update it on resvg.

@LaurenzV LaurenzV deleted the font-family-parsing branch February 4, 2024 18:40
@RazrFalcon
Copy link
Collaborator

Sounds good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants