diff --git a/docs/src/gallery/forms.md b/docs/src/gallery/forms.md index 7234bae9..9540ed58 100644 --- a/docs/src/gallery/forms.md +++ b/docs/src/gallery/forms.md @@ -181,6 +181,23 @@ img = compose(context(units=UnitBox(0,0,2,2)), ) ``` +```@example +using Compose +set_default_graphic_size(10cm,8cm) + +# This graphic illustrates text alignment +txt = [x*"\n"*y for x in ["hleft", "hcenter","hright"], + y in ["vtop","vcenter","vbottom"] ] +x = repeat(0.1w.*[1,5,9], outer=3) +y = repeat(0.1h.*[1,5,9], inner=3) +xp = repeat([hleft,hcenter,hright], outer=3) +yp = repeat([vtop,vcenter,vbottom], inner=3) + +img = compose(context(), + (context(), circle(x, y, [0.01]), fill("red")), + text(x, y, txt, xp, yp), fontsize(14pt) +) +``` diff --git a/src/svg.jl b/src/svg.jl index 600eb4cf..2445be3a 100644 --- a/src/svg.jl +++ b/src/svg.jl @@ -87,22 +87,13 @@ svg_fmt_color(c::Color) = string("#", hex(c)) svg_fmt_color(c::Nothing) = "none" # Replace newlines in a string with the appropriate SVG tspan tags. -function svg_newlines(input::AbstractString, x::Float64) - xpos = svg_fmt_float(x) - newline_count = 0 +function svg_newlines(input::AbstractString) output = IOBuffer() - lastpos = 1 - for mat in eachmatch(r"\n", input) - write(output, input[lastpos:mat.offset-1]) - newline_count += 1 - write(output, """""") - lastpos = mat.offset + length(mat.match) + inputs = split(input,"\n") + write(output, inputs[1]) + for mat in inputs[2:end] + write(output, """""", mat, "") end - write(output, input[lastpos:end]) - for _ in 1:newline_count - write(output, "") - end - return String(take!(output)) end @@ -958,12 +949,16 @@ function draw(img::SVG, prim::TextPrimitive, idx::Int) # NOTE: "dominant-baseline" is the correct way to vertically center text # in SVG, but implementations are pretty inconsistent (chrome in particular # does a really bad job). We fake it by shifting by some reasonable amount. + nlines = length(split(prim.value,"\n"))-1 if prim.valign === vcenter - print(img.out, " dy=\"0.35em\"") +# print(img.out, " dy=\"0.35em\"") + print(img.out, " dy=\"$(svg_fmt_float(0.35-0.6*nlines))em\"") #print(img.out, " style=\"dominant-baseline:central\"") elseif prim.valign === vtop print(img.out, " dy=\"0.6em\"") #print(img.out, " style=\"dominant-baseline:text-before-edge\"") + elseif prim.valign === vbottom + print(img.out, " dy=\"$(svg_fmt_float(-1.2*nlines))em\"") end if abs(prim.rot.theta) > 1e-4 || sum(abs.(prim.offset)) > 1e-4mm @@ -988,7 +983,7 @@ function draw(img::SVG, prim::TextPrimitive, idx::Int) end @printf(img.out, ">%s\n", - svg_newlines(pango_to_svg(prim.value), prim.position[1].value)) + svg_newlines(pango_to_svg(prim.value))) img.indentation -= 1 indent(img)