You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
plot: Changing the default font for plots is unintuitive and/or not possible #702
Rending the plot panic'ed, because Font objects are used unchecked in places using Extent().
It is not clear at all, what fonts can even be used for plot.DefaultFont and that it needs to happen before calling plot.New(). Digging through the code reveals there was a built-in map, which is currently being removed.
It is possible to load a custom font such as Arial, by extending the vg.FontDirs global. In addition, the default font cache needs to be manually extended with a mapping for "Arial" to a font object loaded via vg.MakeFont().
However, this will still fail later in the SVG renderer, because it has a hard-coded mapping of typefaces to CSS style strings. This mapping cannot currently be extended. If a font is not part of this mapping, it will at least throw a proper error.
What version of Go and Gonum/plot are you using?
From go.sum, it looks like I am using v0.9.0.
Does this issue reproduce with the current master?
(compared to v0.8.0's way, it's not too dissimilar: see here)
svg fonts
as for the latter issue, it's "just" an overlook :)
I will say that ITSMT vg/vgsvg actually never handled 3rd-party fonts and just played tricks (ie: replacing your use of Arial with the "equivalent" Liberation font, for example).
it seems like SVG can embed fonts, like PDF does.
we could add that feature to produce something along the lines of:
<svgxmlns="http://www.w3.org/2000/svg"width="450"height="150"font-size="24"text-anchor="middle">
<defs>
<style>
@font-face{font-family:"Roboto Condensed";src:url(data:font/ttf;charset=utf-8;base64,your_base64_encoded_long_string) format("ttf"); font-weight:normal;font-style:normal; }@font-face{font-family:"Open Sans";src:url(data:font/ttf;charset=utf-8;base64,your_base64_encoded_long_string) format("ttf"); font-weight:normal;font-style:normal; }@font-face{font-family:"Anonymous Pro";src:url(data:font/ttf;charset=utf-8;base64,your_base64_encoded_long_string) format("ttf"); font-weight:normal;font-style:normal; }
</style>
</defs>
<textfont-family="Roboto Condensed"x="190"y="32.92">
This is a Roboto Condensed font
</text>
<textfont-family="Open Sans"x="190"y="82.92">
This is a Open Sans font
</text>
<textfont-family="Anonymous Pro"x="190"y="132.92">
This is a Anonymous Pro font
</text>
</svg>
so:
add an embed fonts field to vgsvg.Canvas, default to false
if embed is false and vgsvg.Canvas.FillString encounters an unknown font, either:
panic, or
embed font
(I'd err on panic, to lead to reproducible results of the produced plot file.)
if embed is true, eagerly embed all fonts, even the "known" ones.
A distinction should be made between installed fonts and custom fonts. Why can't vg.FontDirs, upon first use, be initialized with platform-specific default font locations?
Then of course there is the problem with mapping font names to font files. Either you can scan the system font directories and build the mapping, but of course that will take time. Or there could at least be some "fuzzy logic". Most font files follow a common naming scheme. The actual font file scanning could be delayed until the naming scheme fails for the first time.
Implementing the above would go a long way. Then you could really do just
plot.DefaultFont= font.Font{Typeface: "Arial"}
This is what I tried first, so I'd consider that "intuitive". ;-)
For the case that you have a font file outside the system's installed fonts, and you want to import it into gonum, you would have to do what you describe in your snippet. I think that's fine, but it really shouldn't be necessary for system fonts.
svg fonts
I ended up replacing the font family name in the SVG without any embedding. I agree embedding should be manually enabled. In the SVG renderer, I think it doesn't need to have the built-in mapping at all. It should all be able to be generated from the information in font.Font{}.
Actually, I take back what I said about custom font files. Instead of your snippet, I'd much prefer this in pkg font:
// AddFontFile parses the file and makes the contained font available via the default font cachefuncAddFontFile(pathstring) (*Font, error) {
ttf, err:=os.ReadFile(path)
iferr!=nil {
nil, err
}
fontTTF, err:=opentype.Parse(ttf)
iferr!=nil {
returnnil, fmt.Errorf("failed to parse '%s' as font: %w", path, err)
}
font:=Font{<name, style, weight, etc. frompulledfromfontTTF>}
DefaultCache.Add([]Face{
{
Font: font,
Face: fontTTF,
},
})
return&font, nil
}
A distinction should be made between installed fonts and custom fonts. Why can't vg.FontDirs, upon first use, be initialized with platform-specific default font locations?
I don't think that should be the default behaviour.
I believe it should be user opt-in as crawling a filesystem is an operation that may not be completely cheap.
that said, I am open to add a few convenience methods/funcs to, say, font.Cache:
font.Cache.AddFrom(fname string) (Font, error)
same but with the raw bytes instead ?
easy addition of a whole TTF collection (.ttc or .otc files)
In the SVG renderer, I think it doesn't need to have the built-in mapping at all. It should all be able to be generated from the information in font.Font{}.
you're right.
a fresh pair of eyes is always welcomed.
Activity
stippi2 commentedon Jun 10, 2021
This is the code I've had to use (on macOS) to get as far as the error in the SVG renderer:
sbinet commentedon Jun 10, 2021
thanks for the report.
there are 2 issues in this issue:
font
as for the former, I'd say it's a lack of examples (or discoverability of these examples, such as: this one) or familiarity with the new API.
as hinted in:
your snippet of code in #702 (comment) would translate to:
(compared to
v0.8.0's way, it's not too dissimilar: see here)svg fonts
as for the latter issue, it's "just" an overlook :)
I will say that ITSMT
vg/vgsvgactually never handled 3rd-party fonts and just played tricks (ie: replacing your use of Arial with the "equivalent" Liberation font, for example).it seems like SVG can embed fonts, like PDF does.
we could add that feature to produce something along the lines of:
so:
add an
embedfonts field tovgsvg.Canvas, default tofalseif
embedisfalseandvgsvg.Canvas.FillStringencounters an unknown font, either:(I'd err on
panic, to lead to reproducible results of the produced plot file.)if
embedistrue, eagerly embed all fonts, even the "known" ones.thoughts?
stippi2 commentedon Jun 11, 2021
Just my 2 cents, of course:
font
A distinction should be made between installed fonts and custom fonts. Why can't
vg.FontDirs, upon first use, be initialized with platform-specific default font locations?Then of course there is the problem with mapping font names to font files. Either you can scan the system font directories and build the mapping, but of course that will take time. Or there could at least be some "fuzzy logic". Most font files follow a common naming scheme. The actual font file scanning could be delayed until the naming scheme fails for the first time.
Implementing the above would go a long way. Then you could really do just
This is what I tried first, so I'd consider that "intuitive". ;-)
For the case that you have a font file outside the system's installed fonts, and you want to import it into gonum, you would have to do what you describe in your snippet. I think that's fine, but it really shouldn't be necessary for system fonts.
svg fonts
I ended up replacing the font family name in the SVG without any embedding. I agree embedding should be manually enabled. In the SVG renderer, I think it doesn't need to have the built-in mapping at all. It should all be able to be generated from the information in
font.Font{}.stippi2 commentedon Jun 11, 2021
Actually, I take back what I said about custom font files. Instead of your snippet, I'd much prefer this in pkg
font:sbinet commentedon Jun 11, 2021
I don't think that should be the default behaviour.
I believe it should be user opt-in as crawling a filesystem is an operation that may not be completely cheap.
that said, I am open to add a few convenience methods/funcs to, say,
font.Cache:font.Cache.AddFrom(fname string) (Font, error).ttcor.otcfiles)you're right.
a fresh pair of eyes is always welcomed.
vg/vgsvg: drop fontMap, use informations from font.Font
vg/vgsvg: drop fontMap, use informations from font.Font
vg/vgsvg: drop fontMap, use informations from font.Font
vg/vgsvg: drop fontMap, use informations from font.Font
vg/vgsvg: drop fontMap, use informations from font.Font
font: introduce Cache.AddFrom convenience method
font: introduce Cache.AddFrom convenience method
vg/vgsvg: drop fontMap, use informations from font.Font
vg/vgsvg: drop fontMap, use informations from font.Font
font: introduce Cache.AddFrom convenience method
font: introduce Cache.AddFrom convenience method
font: introduce Cache.AddFrom convenience method
font: introduce Cache.AddFrom convenience method
sbinet commentedon Sep 30, 2021
Is it because the font example is not in a very discoverable place:
https://pkg.go.dev/gonum.org/v1/plot@v0.10.0/vg#example-package-AddFont
Or because the example isn't helpful enough?
sbinet commentedon Sep 30, 2021
On top of that, I guess it also makes more sense to now move that example to the 'plot/font' package.