-
-
Save Siyfion/ac6c51789ab588302680 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'prawn' | |
require 'prawn/measurement_extensions' | |
require 'mongo' | |
require_relative 'logging' | |
class HealthMarkRenderer | |
include Singleton, Logging | |
def render_health_mark(pdf, health_mark_object, health_marks) | |
# Get the binding's ACTUAL value from the product data | |
bound_attribute = health_mark_object['binding'] | |
# There is no data in the product that will render this item, so ignore it | |
return if bound_attribute.nil? | |
# Check that the data is an ID reference | |
return unless bound_attribute.is_a?(String) | |
health_mark = health_marks.find { |health_mark| health_mark['_id'].to_s == bound_attribute } | |
# Don't render anything, as it requires data | |
return if health_mark['countryCode'].nil? || health_mark['establishmentCode'].nil? | |
x_position = health_mark_object['settings']['x'].to_f | |
y_position = health_mark_object['settings']['y'].to_f | |
rt = health_mark_object['settings']['rt'].to_f | |
width = health_mark_object['settings']['width'].to_f | |
height = health_mark_object['settings']['height'].to_f | |
pdf.rotate(-rt, :origin => [x_position + (width / 2.0), -(y_position + (height / 2.0))]) do | |
# Work out the current ratio of width to height | |
aspect_ratio = width / height | |
# Check the proportions of this control match | |
# the expected (they probably wont!) | |
if aspect_ratio > 2 | |
# The control is too SHORT | |
new_width = height * 2 | |
# Adjust the X value to compensate | |
x_position += (width - new_width) / 2 | |
# Save the new width | |
width = new_width | |
elsif aspect_ratio < 2 | |
# The control is too WIDE | |
new_height = width / 2 | |
# Adjust the Y value to compensate | |
y_position += (height - new_height) / 2 | |
# Save the new height | |
height = new_height | |
end | |
pdf.translate(x_position, -y_position) do | |
# Draw the outlines | |
pdf.stroke do | |
pdf.ellipse([width / 2.0, -height / 2.0], width / 2.0, height / 2.0) | |
end | |
# Set the font size at the maximum allowed | |
pdf.font_size 100 | |
pdf.formatted_text_box( | |
[ | |
text: "#{health_mark['countryCode']}\n#{health_mark['establishmentCode']}\nEC", | |
styles: [:bold], | |
color: '#000000' | |
], | |
at: [width / 10.0, -height / 10.0], | |
width: (width / 10.0) * 8, | |
height: (height / 10.0) * 8, | |
overflow: :shrink_to_fit, | |
align: :center, | |
valign: :center, | |
# Allow the font to go all the way down to the unreadable 1pt setting. | |
min_font_size: 1, | |
disable_wrap_by_char: true | |
) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment