<commit_before># This is free and unencumbered software released into the public domain.
# See the `UNLICENSE` file or <http://unlicense.org/> for more details.


require 'prawn'

require 'rqrcode'
require 'securerandom'

module Prawn::QRCodes
	# Adds a QR code to the PDF.
	#
	# @param [String, RQRCode, Numeric, #to_s] code the content of
	#     the QR code
	# @param [Hash] opts the options to generate and to draw the
	#     QR code with
	# @option opts [Boolean] :border draw a border around the QR code
	# @option opts [Symbol] :position horizontal position on the
	#     PDF page: `:left`, `:center`, `:right`
	# @option opts [Symbol] :vposition vertial position on the PDF
	#     page: `:top`, `:center`, `:bottom`
	# @option opts [Array<Numeric, Numeric>] :fit width and height
	#     inside which fit the QR code
	# @option opts [Symbol] :encoding the QR encoding to use:
	#     `:utf8`, `:ascii`, `:number`, `:kanji`, `:url`
	# @option opts [Symbol] :level the error correction level: `:l`,
	#     `:m`, `:q` or `:h`
	# @option opts [Fixnum] :size force a certain QR module size
	#
	# @example Generating and adding a QR code for "Hello world"
	#
	#	pdf.qrcode "Hello world"
	#
	# @example Same QR code but printed as a centred 200x200 square
	#
	#	pdf.qrcode "Hello world", :fit => [200, 200], :position => :center

	def qrcode(code, opts = {})
		if code.is_a? RQRCode
			code = code
		elsif code.is_a? String
			opts[:encoding] = qrcode_best_encoding(code) unless opts[:encoding]
			code = RQRCode::QRCode.new(code, opts)
		elsif code.is_a? Fixnum
			opts[:encoding] = :number unless opts[:encoding]
			code = RQRCode::QRCode.new(code.to_s, opts)
		elsif code.respond_to(:to_s)
			str = code.to_s
			opts[:encoding] = qrcode_best_encoding(string) unless opts[:encoding]
			code = RQRCode::QRCode.new(str, opts)
		else
			msg = code.class.to_s + " cannot be converted to QR code"
			raise ArgumentError.new(msg)
		end

		qrcode_draw(code, opts)
	end

private

include Prawn::Images # FIXME: remove this hack, see https://github.com/sandal/prawn/issues/266

	def qrcode_draw(qr, opts)
		size = qrcode_default_size(qr)
		w, h = calc_image_dimensions(size, opts)
		x, y = image_position(w, h, opts)

		canvas do
			bounding_box([x, y], :width => w, :height => h) do
				stroke_bounds if opts[:border]

				qr_stamp = "qr_module" + SecureRandom::hex(2)
				qrcode_create_stamp(qr, qr_stamp)

				dot_size = qrcode_dotsize(qr)
				y = bounds.height<commit_msg>Refactored object-to-QR conversion sequence<commit_after># This is free and unencumbered software released into the public domain.
# See the `UNLICENSE` file or <http://unlicense.org/> for more details.


require 'prawn'

require 'rqrcode'
require 'securerandom'

module Prawn::QRCodes
	# Adds a QR code to the PDF.
	#
	# @param [String, RQRCode, Numeric, #to_s] code the content of
	#     the QR code
	# @param [Hash] opts the options to generate and to draw the
	#     QR code with
	# @option opts [Boolean] :border draw a border around the QR code
	# @option opts [Symbol] :position horizontal position on the
	#     PDF page: `:left`, `:center`, `:right`
	# @option opts [Symbol] :vposition vertial position on the PDF
	#     page: `:top`, `:center`, `:bottom`
	# @option opts [Array<Numeric, Numeric>] :fit width and height
	#     inside which fit the QR code
	# @option opts [Symbol] :encoding the QR encoding to use:
	#     `:utf8`, `:ascii`, `:number`, `:kanji`, `:url`
	# @option opts [Symbol] :level the error correction level: `:l`,
	#     `:m`, `:q` or `:h`
	# @option opts [Fixnum] :size force a certain QR module size
	#
	# @example Generating and adding a QR code for "Hello world"
	#
	#	pdf.qrcode "Hello world"
	#
	# @example Same QR code but printed as a centred 200x200 square
	#
	#	pdf.qrcode "Hello world", :fit => [200, 200], :position => :center

	def qrcode(code, opts = {})
		if code.is_a? RQRCode
			code = code
		else
			if code.is_a? Fixnum
				code = code.to_s
				opts[:encoding] = :number unless opts[:encoding]
			elsif code.is_a? String || code.respond_to(:to_s)
				code = code.to_s
				opts[:encoding] = qrcode_best_encoding(code) unless opts[:encoding]
			else
				msg = code.class.to_s + " cannot be converted to QR code"
				raise ArgumentError.new(msg)
			end

			code = RQRCode::QRCode.new(code, opts)
		end

		qrcode_draw(code, opts)
	end

private

include Prawn::Images # FIXME: remove this hack, see https://github.com/sandal/prawn/issues/266

	def qrcode_draw(qr, opts)
		size = qrcode_default_size(qr)
		w, h = calc_image_dimensions(size, opts)
		x, y = image_position(w, h, opts)

		canvas do
			bounding_box([x, y], :width => w, :height => h) do
				stroke_bounds if opts[:border]

				qr_stamp = "qr_module" + SecureRandom::hex(2)
				qrcode_create_stamp(qr, qr_stamp)

				dot_size = qrcode_dotsize(qr)
				y = bounds.height
{'fraction_non_alphanumeric': 0.12603305785123967, 'fraction_numerical': 0.0070247933884297524, 'mean_word_length': 3.788328387734916, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4585034', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1710, 'n_words': 557}