From d6e71f6d951fe27aa67904d432a21a76490b3bdc Mon Sep 17 00:00:00 2001 From: Henry Hsieh Date: Sat, 9 Dec 2023 19:07:06 +0800 Subject: [PATCH] Support middle function of dominant-baseline for --- README.md | 2 +- lib/prawn/svg/elements/text_component.rb | 5 +++-- lib/prawn/svg/interface.rb | 7 +++++++ lib/prawn/svg/properties.rb | 1 + 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b7ffe7b..60eeeef 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ prawn-svg supports most but not all of the full SVG 1.1 specification. It curre implementation of elliptical arc is a bit rough at the moment. - ``, `` and `` with attributes `x`, `y`, `dx`, `dy`, `rotate`, 'textLength', 'lengthAdjust', and with extra properties - `text-anchor`, `text-decoration` (underline only), `font-size`, `font-family`, `font-weight`, `font-style`, `letter-spacing` + `text-anchor`, `text-decoration` (underline only), `font-size`, `font-family`, `font-weight`, `font-style`, `letter-spacing`, `dominant-baseline` (middle only) - <svg>, <g> and <symbol> diff --git a/lib/prawn/svg/elements/text_component.rb b/lib/prawn/svg/elements/text_component.rb index 1c5ce98..1beb012 100644 --- a/lib/prawn/svg/elements/text_component.rb +++ b/lib/prawn/svg/elements/text_component.rb @@ -41,12 +41,13 @@ def apply font = select_font apply_font(font) if font - # text_anchor isn't a Prawn option; we have to do some math to support it - # and so we handle this in Prawn::SVG::Interface#rewrite_call_arguments + # text_anchor and dominant_baseline aren't Prawn options; we have to do some math to support them + # and so we handle them in Prawn::SVG::Interface#rewrite_call_arguments opts = { size: computed_properties.numerical_font_size, style: font && font.subfamily, text_anchor: computed_properties.text_anchor, + dominant_baseline: computed_properties.dominant_baseline, } opts[:decoration] = computed_properties.text_decoration unless computed_properties.text_decoration == 'none' diff --git a/lib/prawn/svg/interface.rb b/lib/prawn/svg/interface.rb index aad977b..22f1d9a 100644 --- a/lib/prawn/svg/interface.rb +++ b/lib/prawn/svg/interface.rb @@ -149,6 +149,13 @@ def rewrite_call_arguments(prawn, call, arguments, kwarguments) at[0] = @cursor[0] if at[0] == :relative at[1] = @cursor[1] if at[1] == :relative + case options.delete(:dominant_baseline) + when 'middle' + height = prawn.font.height + at[1] -= height / 2.0 + @cursor = [at[0], at[1]] + end + if offset = options.delete(:offset) at[0] += offset[0] at[1] -= offset[1] diff --git a/lib/prawn/svg/properties.rb b/lib/prawn/svg/properties.rb index 1fc5422..b66a412 100644 --- a/lib/prawn/svg/properties.rb +++ b/lib/prawn/svg/properties.rb @@ -38,6 +38,7 @@ class Prawn::SVG::Properties "stroke-width" => Config.new("1", true), "text-anchor" => Config.new("start", true, %w(inherit start middle end), true), 'text-decoration' => Config.new('none', true, %w(inherit none underline), true), + "dominant-baseline" => Config.new("auto", true, %w(inherit auto middle), true), }.freeze PROPERTIES.each do |name, value|