From 0b72959009ba637eeb3709053e6cb494638dc106 Mon Sep 17 00:00:00 2001 From: Drahoslav Date: Wed, 10 Jan 2018 23:00:32 +0100 Subject: [PATCH] Implement PathFontMode and make it default --- draw2dsvg/converters.go | 9 +++++---- draw2dsvg/gc.go | 14 ++++++++++---- draw2dsvg/svg.go | 19 ++++++++++++++++--- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/draw2dsvg/converters.go b/draw2dsvg/converters.go index 7fc187b..b4440a1 100644 --- a/draw2dsvg/converters.go +++ b/draw2dsvg/converters.go @@ -87,11 +87,11 @@ func toSvgPathDesc(p *draw2d.Path) string { // dirty hack to ensure whole arc is drawn // if start point equals end point if sweep == 1 { - x += 0.0001 * sinfi - y += 0.0001 * -cosfi + x += 0.01 * sinfi + y += 0.01 * -cosfi } else { - x += 0.0001 * sinfi - y += 0.0001 * cosfi + x += 0.01 * sinfi + y += 0.01 * cosfi } // rx ry x-axis-rotation large-arc-flag sweep-flag x y @@ -153,6 +153,7 @@ func optiSprintf(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) } +// TODO needs test, since it is not quiet right func getPrec(num float64, better bool) int { max := 3 eps := 0.0005 diff --git a/draw2dsvg/gc.go b/draw2dsvg/gc.go index b39a1cd..30fba26 100644 --- a/draw2dsvg/gc.go +++ b/draw2dsvg/gc.go @@ -247,6 +247,16 @@ func (gc *GraphicContext) drawPaths(drawType drawType, paths ...*draw2d.Path) { // Add text element to svg and returns its expected width func (gc *GraphicContext) drawString(text string, drawType drawType, x, y float64) float64 { + switch gc.svg.FontMode { + case PathFontMode: + w := gc.CreateStringPath(text, x, y) + gc.drawPaths(drawType) + gc.Current.Path.Clear() + return w + case SvgFontMode: + gc.embedSvgFont(text) + } + // create elements svgText := Text{} group := gc.newGroup(drawType) @@ -258,10 +268,6 @@ func (gc *GraphicContext) drawString(text string, drawType drawType, x, y float6 svgText.Y = y svgText.FontFamily = gc.Current.FontData.Name - if gc.svg.FontMode == SvgFontMode { - gc.embedSvgFont(text) - } - // attach to group group.Texts = []*Text{&svgText} left, _, right, _ := gc.GetStringBounds(text) diff --git a/draw2dsvg/svg.go b/draw2dsvg/svg.go index e1cccbe..2f15bd6 100644 --- a/draw2dsvg/svg.go +++ b/draw2dsvg/svg.go @@ -11,11 +11,24 @@ import ( type FontMode int +// Modes of font handling in svg const ( + // Does nothing special + // Makes sense only for common system fonts SysFontMode FontMode = 1 << iota - LinkFontMode + + // Links font files in css def + // Requires distribution of font files with outputed svg + LinkFontMode // TODO implement + + // Embeds glyphs definition in svg file itself in svg font format + // Has poor browser support SvgFontMode - CssFontMode + + // Embeds font definiton in svg file itself in woff format as part of css def + CssFontMode // TODO implement + + // Converts texts to paths PathFontMode ) @@ -33,7 +46,7 @@ func NewSvg() *Svg { return &Svg{ Xmlns: "http://www.w3.org/2000/svg", FillStroke: FillStroke{Fill: "none", Stroke: "none"}, - FontMode: SvgFontMode, + FontMode: PathFontMode, } }