Class: Arrolio::Renderer::AccessibilityTagger
- Inherits:
-
Object
- Object
- Arrolio::Renderer::AccessibilityTagger
- Defined in:
- lib/arrolio/renderer/accessibility_tagger.rb
Overview
Builds marked-content property lists for PDF/UA accessibility. Wraps non-text content (images, formulas) with /Alt and /ActualText entries so screen readers can access the semantic meaning even when the visual rendering is a rasterized image or a flattened formula.
In the PDF content stream, marked content looks like:
/MP << /Alt (alternative text) >> BDC
... drawing operators ...
EMC
This class builds the property list dictionaries; the renderer wraps them in BDC/EMC operators when drawing.
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
-
#actual_text_property(text) ⇒ Object
Builds a property list for actual text (the visible text representation of non-text content, e.g. a formula's flattened text).
-
#alt_property(alt_text) ⇒ Object
Builds a property list dictionary for an image's alt text.
-
#combined_property(alt_text: nil, actual_text: nil) ⇒ Object
Combines alt + actual text into a single property list.
-
#initialize(document) ⇒ AccessibilityTagger
constructor
A new instance of AccessibilityTagger.
-
#next_tag ⇒ Object
Next unique marked-content sequence number.
Constructor Details
#initialize(document) ⇒ AccessibilityTagger
Returns a new instance of AccessibilityTagger.
21 22 23 24 |
# File 'lib/arrolio/renderer/accessibility_tagger.rb', line 21 def initialize(document) @document = document @sequence = 0 end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
19 20 21 |
# File 'lib/arrolio/renderer/accessibility_tagger.rb', line 19 def document @document end |
Instance Method Details
#actual_text_property(text) ⇒ Object
Builds a property list for actual text (the visible text representation of non-text content, e.g. a formula's flattened text).
41 42 43 44 45 46 47 48 |
# File 'lib/arrolio/renderer/accessibility_tagger.rb', line 41 def actual_text_property(text) return nil if text.nil? || text.to_s.empty? @document.add( { ActualText: text.to_s }, type: Pdfrb::Model::Cos::Dictionary ) end |
#alt_property(alt_text) ⇒ Object
Builds a property list dictionary for an image's alt text. Returns a Pdfrb reference to the dictionary, or nil if no alt text is provided.
29 30 31 32 33 34 35 36 |
# File 'lib/arrolio/renderer/accessibility_tagger.rb', line 29 def alt_property(alt_text) return nil if alt_text.nil? || alt_text.to_s.empty? @document.add( { Alt: alt_text.to_s }, type: Pdfrb::Model::Cos::Dictionary ) end |
#combined_property(alt_text: nil, actual_text: nil) ⇒ Object
Combines alt + actual text into a single property list. When both are present, the dictionary carries both entries.
52 53 54 55 56 57 58 59 |
# File 'lib/arrolio/renderer/accessibility_tagger.rb', line 52 def combined_property(alt_text: nil, actual_text: nil) props = {} props[:Alt] = alt_text.to_s if alt_text && !alt_text.to_s.empty? props[:ActualText] = actual_text.to_s if actual_text && !actual_text.to_s.empty? return nil if props.empty? @document.add(props, type: Pdfrb::Model::Cos::Dictionary) end |
#next_tag ⇒ Object
Next unique marked-content sequence number. Used by the renderer to tag BDC/EMC pairs so structure-tree entries can reference them.
64 65 66 67 |
# File 'lib/arrolio/renderer/accessibility_tagger.rb', line 64 def next_tag @sequence += 1 :"MC#{@sequence}" end |