From 449f710fd6977307892df0bae6dd4e1c35b12b46 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Thu, 17 Apr 2025 15:47:26 -0700 Subject: [PATCH 1/3] add a test case for duplicate SVG 'font-family' Gitlab: !4298 Reported-by: FeRD (Frank Dana) --- tests/test_regression.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_regression.py b/tests/test_regression.py index 685bf13531..707d87dbb5 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -6335,3 +6335,29 @@ def test_lock_graph(): # now try this with a large integer for the locking operation output = run(["gvpr", "-f", program2], input=src) assert output == "0\n1\n", "locking a graph using a large integer did not work" + + +@pytest.mark.xfail() +def test_duplicate_font_family(): + """ + SVG output should not contain duplicate `font-family` items + https://gitlab.com/graphviz/graphviz/-/merge_requests/4298 + """ + + # a sample graph to exercise font families + source = textwrap.dedent( + """\ + graph G { + graph [fontnames=svg]; + N [label="node" fontname="Helvetica"]; + } + """ + ) + + # convert this to SVG + svg = dot("svg", source=source) + + # extract font families + for ff in re.findall(r'font-family="(?P[^"]*)', svg): + families = [f.strip() for f in ff.split(",")] + assert len(families) == len(set(families)), "duplicate font families listed" -- GitLab From dffcb6e8b59c2ae50fa476b8cb738b862b78f0f4 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Thu, 17 Apr 2025 16:29:37 -0400 Subject: [PATCH 2/3] SVG output: Don't double names with fontnames=svg When a graph is rendered to SVG by the core_svg renderer, having the `fontnames` attribute set to `svg` will use the chosen font's `svg_font_family` alias as the `font-family` name in the resulting `` tags. Previously, the code was always following the `family` name with the `svg_font_family` name as a fallback, meaning that `fontnames=svg` always caused doubled `font-family=` attributes in `` tags. (e.g. ``.) To avoid that redundancy, only output the second (fallback) font family name if it's **not** the `family` name. Note: This doesn't necessarily eliminate ALL redundancy -- if the user's chosen font is `sans-serif`, for example, then that font's `svg_font_family` will likely be `sans-Serif`... but if they're not using `fontnames=svg`, that may still be output into the SVG code as `font-family="sans-serif,sans-Serif"`. This change only prevents the redundancy that is guaranteed to occur when `family` and `svg_font_family` hold the **same string pointer value**, not just an equivalent string of characters. (Which is something that can only happen when `fontnames=svg`.) --- plugin/core/gvrender_core_svg.c | 2 +- tests/test_regression.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/core/gvrender_core_svg.c b/plugin/core/gvrender_core_svg.c index 1bac43a08d..f20307f269 100644 --- a/plugin/core/gvrender_core_svg.c +++ b/plugin/core/gvrender_core_svg.c @@ -481,7 +481,7 @@ static void svg_textspan(GVJ_t * job, pointf p, textspan_t * span) stretch = pA->stretch; gvprintf(job, " font-family=\"%s", family); - if (pA->svg_font_family) + if (pA->svg_font_family && pA->svg_font_family != family) gvprintf(job, ",%s", pA->svg_font_family); gvputc(job, '"'); if (weight) diff --git a/tests/test_regression.py b/tests/test_regression.py index 707d87dbb5..bec38a2d65 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -6337,7 +6337,6 @@ def test_lock_graph(): assert output == "0\n1\n", "locking a graph using a large integer did not work" -@pytest.mark.xfail() def test_duplicate_font_family(): """ SVG output should not contain duplicate `font-family` items -- GitLab From b43c845609c0495fb5edc04c72392149ec10fd44 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Thu, 17 Apr 2025 16:13:30 -0700 Subject: [PATCH 3/3] add a changelog entry for a previous commit Gitlab: !4298 Reported-by: FeRD (Frank Dana) --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b348a7980..99a8ceba0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -106,6 +106,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 2.38.0 to the behavior Graphviz 2.36.0 and prior had. The difference is most apparent when using a non-default `dpi` setting. #2669 - The Tcl bindings have been updated for compatibility with Tcl 9. #2668 +- The SVG output format (`-Tsvg`, `-Tsvg_inline`) no longer duplicates font + families. ### Fixed -- GitLab