Skip to content

Commit

Permalink
Support middle function of dominant-baseline for <text>
Browse files Browse the repository at this point in the history
  • Loading branch information
henry-hsieh authored and mogest committed Dec 16, 2023
1 parent 5140da1 commit d6e71f6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

- `<text>`, `<tspan>` and `<tref>` 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)

- <tt>&lt;svg&gt;</tt>, <tt>&lt;g&gt;</tt> and <tt>&lt;symbol&gt;</tt>

Expand Down
5 changes: 3 additions & 2 deletions lib/prawn/svg/elements/text_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 7 additions & 0 deletions lib/prawn/svg/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions lib/prawn/svg/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit d6e71f6

Please sign in to comment.