Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Use Base64 For Embedding Raster Data In Source File

DZone's Guide to

Use Base64 For Embedding Raster Data In Source File

·
Free Resource
When we create a simple script with gui, it should be nice to puts the icons in the source script,
so whe can diffuse the code without any packaging.

This code create a ruby source which declare some file-content coded in ASCII/.base64
In the source, he put get_icon_filename(name) which put content to a temporary file and return
the filename created :


# usage: ruby gene.rb ../angel.png  face_crying.png face_smile_big.png > icons64.rb
require 'base64'
require 'tmpdir'

str=<
  
    #{fname} / #{$icons[name].size}" if $DEBUG
  File.open(fname,"wb") { |f| f.write($icons[name].unpack('m')) } unless File.exists?(fname)
  fname
end
EEND

puts str

STDERR.puts "#{lvar.join(", ")} done size=#{str.size}."

  

This generate a code like :

require 'base64'
require 'tmpdir'
$icons={}

$icons['angel']=<
  
    #{fname} / #{$icons[name].size}" if $DEBUG
  File.open(fname,"wb") { |f| f.write($icons[name].unpack('m')) } unless File.exists?(fname)
  fname
end

  

Here a little Shoes app for use raster content

require 'green_shoes'

<< "contents of 'icons64.rb' here" >>

Shoes.app do
  stack {
	background "#A0A0B0".."#A0A0FF"
	$icons.keys.each { |name|
			para name
			image get_icon_filename(name)
	}
  }
end



Or a Ruiby app (see https://github.com/raubarede/Ruiby):


require_relative 'Ruiby/lib/ruiby.rb'
<< "contents of 'icons64.rb' here" >>

class X < Ruiby_gtk
	def component()
	  stack do
		$icons.keys.each do |n| 
			flow { 
                           slot(label(n))
                           slot(label("#"+get_icon_filename(n) )) 
			}
		end
	  end
	end
end
Ruiby.start { X.new("test",100,100) }


Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}