Skip to content

Commit

Permalink
Handle non-positive text size and zero font width limit (#175)
Browse files Browse the repository at this point in the history
* Handle text limit of 0

* Add test

* Handle text with non-positive font size
  • Loading branch information
jonmmease authored Jul 27, 2024
1 parent 100165c commit 9483777
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vl-convert-rs/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ import('{url}').then((sg) => {{
// weight may be string like "bold" or number like 600.
// Convert number form to string
let weight = String(item.fontWeight);
let weight = item.fontWeight == null? null: String(item.fontWeight);
let size = sg.fontSize(item);
let family = sg.fontFamily(item);
Expand All @@ -332,7 +332,7 @@ import('{url}').then((sg) => {{
}}, null, 2);
let fullWidth = op_text_width(text_info);
return Math.min(fullWidth, item.limit ?? fullWidth)
return item.limit > 0? Math.min(fullWidth, item.limit): fullWidth
}};
}})
"#,
Expand Down
5 changes: 5 additions & 0 deletions vl-convert-rs/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ pub fn op_text_width(#[string] text_info_str: String) -> Result<f64, AnyError> {
Err(err) => bail!("Failed to deserialize text info: {}", err.to_string()),
};

// Return width zero for text with non-positive size
if text_info.size <= 0.0 {
return Ok(0.0);
}

// Return width zero for empty strings and missing text
match &text_info.text {
Some(Value::String(text)) => {
Expand Down
1 change: 1 addition & 0 deletions vl-convert-rs/tests/test_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ mod test_png_no_theme {
case("geoScale", 1.0),
case("table_heatmap", 1.0),
case("long_text_lable", 1.0),
case("gh_174", 1.0),
)]
fn test(
name: &str,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
159 changes: 159 additions & 0 deletions vl-convert-rs/tests/vl-specs/gh_174.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"config": {
"view": {"continuousWidth": 300, "continuousHeight": 300},
"legend": {"layout": {"expr": "{\"bottom\": {\"anchor\": \"middle\"}}"}},
"locale": {
"number": {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", " €"]
}
}
},
"layer": [
{
"mark": {"type": "bar"},
"encoding": {
"color": {
"field": "Sexo",
"legend": {
"labelFontSize": 12,
"labelLimit": 0,
"labelOffset": 2,
"offset": 15,
"orient": "bottom",
"symbolSize": 50
},
"scale": {"range": ["#B07BD7", "#B1D6AF", "#BF9000"]},
"sort": null,
"title": null,
"type": "nominal"
},
"xOffset": {"field": "Sexo", "sort": null, "type": "nominal"},
"y": {
"axis": {
"domain": false,
"labelAngle": 0,
"labelExpr": "split(datum.label, '\\n')",
"labelFontSize": 12,
"labelLimit": 0,
"labelPadding": 8,
"ticks": false,
"titleFontSize": 12,
"titlePadding": 8
},
"field": "value",
"title": "Porcentaje",
"type": "quantitative"
}
},
"transform": [{"filter": "datum.Sexo != 'Diferencia'"}]
},
{
"mark": {"type": "line", "strokeWidth": 3},
"encoding": {
"color": {
"field": "Sexo",
"legend": {
"labelFontSize": 12,
"labelLimit": 0,
"labelOffset": 2,
"offset": 15,
"orient": "bottom",
"symbolSize": 50
},
"scale": {"range": ["#B07BD7", "#B1D6AF", "#BF9000"]},
"sort": null,
"title": null,
"type": "nominal"
},
"y": {
"axis": {
"domain": false,
"labelExpr": "split(datum.label, '\\n')",
"labelFontSize": 12,
"labelLimit": 0,
"labelPadding": 8,
"ticks": false,
"titleFontSize": 12,
"titlePadding": 8
},
"field": "value",
"title": "Brecha de género",
"type": "quantitative"
}
},
"transform": [{"filter": "datum.Sexo == 'Diferencia'"}]
},
{
"mark": {"type": "text", "dy": -11, "fontSize": 10.666666666666666},
"encoding": {
"text": {"field": "value", "format": ".1f", "type": "quantitative"},
"xOffset": {"field": "Sexo", "sort": null, "type": "nominal"},
"y": {
"axis": null,
"field": "value",
"title": null,
"type": "quantitative"
}
},
"transform": [{"filter": "datum.Sexo != 'Diferencia'"}]
}
],
"data": {"name": "data-d9816b078d38723c826db8427f81174e"},
"encoding": {
"x": {
"axis": {
"domain": false,
"labelAngle": 0,
"labelExpr": "split(datum.label, '\\n')",
"labelFontSize": 12,
"labelLimit": 0,
"labelPadding": 8,
"ticks": false,
"titleFontSize": 12,
"titlePadding": 8
},
"field": "Categoría",
"sort": null,
"title": null,
"type": "nominal"
}
},
"height": 250,
"resolve": {"scale": {"y": "independent"}},
"width": 500,
"$schema": "https://vega.github.io/schema/vega-lite/v5.17.0.json",
"datasets": {
"data-d9816b078d38723c826db8427f81174e": [
{"Categoría": "Mala", "Sexo": "Mujeres", "value": 4.888938288354638},
{"Categoría": "Regular", "Sexo": "Mujeres", "value": 17.20105746322346},
{"Categoría": "Buena", "Sexo": "Mujeres", "value": 47.73831733721285},
{"Categoría": "Muy buena", "Sexo": "Mujeres", "value": 22.48178472726535},
{"Categoría": "Excelente", "Sexo": "Mujeres", "value": 7.689902183943703},
{"Categoría": "Mala", "Sexo": "Hombres", "value": 3.290678903385191},
{"Categoría": "Regular", "Sexo": "Hombres", "value": 13.19356745004503},
{"Categoría": "Buena", "Sexo": "Hombres", "value": 45.60874073590572},
{"Categoría": "Muy buena", "Sexo": "Hombres", "value": 26.48511010933306},
{"Categoría": "Excelente", "Sexo": "Hombres", "value": 11.421902801331},
{"Categoría": "Mala", "Sexo": "Diferencia", "value": 1.598259384969446},
{
"Categoría": "Regular",
"Sexo": "Diferencia",
"value": 4.007490013178437
},
{"Categoría": "Buena", "Sexo": "Diferencia", "value": 2.129576601307129},
{
"Categoría": "Muy buena",
"Sexo": "Diferencia",
"value": -4.003325382067715
},
{
"Categoría": "Excelente",
"Sexo": "Diferencia",
"value": -3.732000617387293
}
]
}
}

0 comments on commit 9483777

Please sign in to comment.