#geos font file as defined by http://zimmers.net/geos/docs/fontfile.txt $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) require 'NativeFileType' require 'C64GeosFile' class C64GeosFont(2*point_size) width end def bytes_in_bitstream(point_size) font_data=@contents[point_size+1] return nil if font_data.nil? font_data[0x01]+font_data[0x02]*100 end def char_height(point_size) font_data=@contents[point_size+1] return nil if font_data.nil? font_data[0x03] end def bitstreams(point_size,char) font_data=@contents[point_size+1] return nil if font_data.nil? bit_stream_index=font_data[0x04]+0x100*font_data[0x05] bit_stream_base=font_data[0x06]+0x100*font_data[0x07] char_pointer=2*((char[0]%0x80)-0x20) start_of_char=font_data[char_pointer+bit_stream_index]+0x100*font_data[char_pointer+bit_stream_index+1] start_of_next_char=font_data[char_pointer+bit_stream_index+2]+0x100*font_data[char_pointer+bit_stream_index+3] # puts "# #{filename} #{point_size} #{char} #{start_of_char} - #{start_of_next_char}" bitstream=[] char_height(point_size).times do |line| bitstream[line]=[] bit_counter=0 start_of_line_bitstream=line*bytes_in_bitstream(point_size)+bit_stream_base (start_of_char..start_of_next_char-1).each do |bit_offset| byte_offset=bit_offset/8 bitmask=0b10000000>>(bit_offset % 8) byte=font_data[start_of_line_bitstream+byte_offset] # puts "%08b:%08b==%08b" % [byte,bitmask,byte & bitmask] bitstream[line][bit_counter]=(((byte & bitmask)!=0)? 1 : 0) bit_counter+=1 end # bitstream[line][bit_counter]="\n" end bitstream end def to_picture require 'PatchedPNG' canvas = PNG::Canvas.new picture_width,picture_height, BACKGROUND_COLOUR draw_sampler(canvas) png = PNG.new canvas result=png.raw_bytes result end def draw_char(canvas,point_size,char,x,y,colour) lines=bitstreams(point_size,char) lines.length.times do |line| lines.length.times do |pixel| canvas[x+pixel,y+line]=colour if lines[line][pixel]==1 end end end def draw_string(canvas,point_size,string,start_x,start_y,colour) x=start_x y=start_y string.each_byte do |b| c=b.chr width=char_width(point_size,c) if (x+width>=picture_width) then x=0 y+=point_size end break if (y+point_size)>=picture_height draw_char(canvas,point_size,c,x,y,colour) x+=width end [x,y] end def meta_data return super unless @contents.kind_of?(Array) && !info_block.nil? @meta_data["geos_font_id"]="$%02x" % (info_block[0x80]+info_block[0x81]*100) @meta_data["geos_font_point_ids"]=(point_ids.collect{|x| '$%04x' % x}).join(",") @meta_data["geos_font_point_sizes"]=point_sizes.join(",") @meta_data["geos_font_lengths"]=(lengths.collect{|x| '$%04x' % x}).join(",") # point_sizes.each do |point_size| # prefix="geos_font_point_size_#{point_size}" # font_data=@contents[point_size+1] # next if font_data.nil? # @meta_data["#{prefix}_line_of_print"]=font_data[0x00] # @meta_data["#{prefix}_bytes_in_bitstream"]=bytes_in_bitstream(point_size) # @meta_data["#{prefix}_character_height"]=char_height(point_size) # @meta_data["#{prefix}_character_width_space"]=char_width(point_size," ") # @meta_data["#{prefix}_character_width_A"]=char_width(point_size,"A") # @meta_data["#{prefix}_bitstream_A"]=bitstreams(point_size,"A") # end super end private def draw_sampler(canvas) raise "no valid point sizes" if point_sizes.length==0 point_size=point_sizes[0] first_letter=filename[0].chr #some fonts have only upper case, some have only lower case, some have both best_case_filename= (char_width(point_size,first_letter.upcase) > char_width(point_size,first_letter.downcase) ? filename.upcase : filename.downcase) x=0 y=0 point_sizes.each do |point_size| (x,y)=draw_string(canvas,point_size,"#{best_case_filename} #{point_size}",x,y,TEXT_COLOUR) x=0 y+=point_size (32..126).each do |b| c=b.chr width=char_width(point_size,c) # puts "#{filename} char #{c} - char width=#{width},x=#{x},y=#{y},#{picture_height}" if (x+width)>=picture_width then # puts "skipping at #{x} - w=#{width}" x=0 y+=point_size end break if (y+point_size)>=picture_height draw_char(canvas,point_size,c,x,y,TEXT_COLOUR) x+=width end x=0 y+=point_size end end end # == Author # Jonno Downes (jonno@jamtronix.com) # # == Copyright # Copyright (c) 2010 Jonno Downes (jonno@jamtronix.com) # #Permission is hereby granted, free of charge, to any person obtaining #a copy of this software and associated documentation files (the #"Software"), to deal in the Software without restriction, including #without limitation the rights to use, copy, modify, merge, publish, #distribute, sublicense, and/or sell copies of the Software, and to #permit persons to whom the Software is furnished to do so, subject to #the following conditions: # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.jukoo