Skip to content

Commit

Permalink
Created the Fonts Library!!!
Browse files Browse the repository at this point in the history
At the moment the fonts library holds only the 14 standard fonts, but
in the future, it might be extended for more fonts and maybe Unicode
support.

My Mission: page numbers and textboxes in different languages.
  • Loading branch information
Myst authored and Myst committed Sep 8, 2014
1 parent 0b27346 commit 9b646e3
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 29,426 deletions.
32 changes: 17 additions & 15 deletions lib/combine_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
require "combine_pdf/combine_pdf_operations.rb"
require "combine_pdf/combine_pdf_basic_writer.rb"
require "combine_pdf/combine_pdf_decrypt.rb"
require "combine_pdf/combine_pdf_fonts.rb"
require "combine_pdf/combine_pdf_filter.rb"
require "combine_pdf/combine_pdf_parser.rb"
require "combine_pdf/combine_pdf_pdf.rb"

require "combine_pdf/font_metrics/courier-bold_metrics.rb"
require "combine_pdf/font_metrics/courier-boldoblique_metrics.rb"
require "combine_pdf/font_metrics/courier-oblique_metrics.rb"
require "combine_pdf/font_metrics/courier_metrics.rb"
require "combine_pdf/font_metrics/helvetica-bold_metrics.rb"
require "combine_pdf/font_metrics/helvetica-boldoblique_metrics.rb"
require "combine_pdf/font_metrics/helvetica-oblique_metrics.rb"
require "combine_pdf/font_metrics/helvetica_metrics.rb"
require "combine_pdf/font_metrics/symbol_metrics.rb"
require "combine_pdf/font_metrics/times-bold_metrics.rb"
require "combine_pdf/font_metrics/times-bolditalic_metrics.rb"
require "combine_pdf/font_metrics/times-italic_metrics.rb"
require "combine_pdf/font_metrics/times-roman_metrics.rb"
require "combine_pdf/font_metrics/zapfdingbats_metrics.rb"
# # will be removed one font support and font library is completed.
# require "combine_pdf/font_metrics/courier-bold_metrics.rb"
# require "combine_pdf/font_metrics/courier-boldoblique_metrics.rb"
# require "combine_pdf/font_metrics/courier-oblique_metrics.rb"
# require "combine_pdf/font_metrics/courier_metrics.rb"
# require "combine_pdf/font_metrics/helvetica-bold_metrics.rb"
# require "combine_pdf/font_metrics/helvetica-boldoblique_metrics.rb"
# require "combine_pdf/font_metrics/helvetica-oblique_metrics.rb"
# require "combine_pdf/font_metrics/helvetica_metrics.rb"
# require "combine_pdf/font_metrics/symbol_metrics.rb"
# require "combine_pdf/font_metrics/times-bold_metrics.rb"
# require "combine_pdf/font_metrics/times-bolditalic_metrics.rb"
# require "combine_pdf/font_metrics/times-italic_metrics.rb"
# require "combine_pdf/font_metrics/times-roman_metrics.rb"
# require "combine_pdf/font_metrics/zapfdingbats_metrics.rb"
# require "combine_pdf/font_metrics/metrics_dictionary.rb"

require "combine_pdf/font_metrics/metrics_dictionary.rb"



Expand Down
46 changes: 27 additions & 19 deletions lib/combine_pdf/combine_pdf_basic_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,29 @@ def textbox(text, properties = {})

self
end

def dimensions_of(text, font_name, size = 1000)
Fonts.get_font(font_name).dimensions_of text, size
end
# this method returns the size for which the text fits the requested metrices
# the size is type Float and is rather exact
# if the text cannot fit such a small place, returns zero (0).
# maximum font size possible is set to 100,000 - which should be big enough for anything
# text:: the text to fit
# font:: the font name. @see font
# length:: the length to fit
# height:: the height to fit (optional - normally length is the issue)
def fit_text(text, font, length, height = 10000000)
size = 100000
size_array = [size]
metrics = Fonts.get_font(font).dimensions_of text, size
if metrics[0] > length
size_array << size * length/metrics[0]
end
if metrics[1] > height
size_array << size * height/metrics[1]
end
size_array.min
end
protected

# accessor (getter) for the :Resources element of the page
Expand Down Expand Up @@ -289,33 +311,19 @@ def contents
# - :Symbol
# - :ZapfDingbats
def set_font(font = :Helvetica)
# refuse any other fonts that arn't basic standard fonts
allow_fonts = [ :"Times-Roman",
:"Times-Bold",
:"Times-Italic",
:"Times-BoldItalic",
:Helvetica,
:"Helvetica-Bold",
:"Helvetica-BoldOblique",
:"Helvetica-Oblique",
:Courier,
:"Courier-Bold",
:"Courier-Oblique",
:"Courier-BoldOblique",
:Symbol,
:ZapfDingbats ]
raise "set_font(font) accepts only one of the 14 standards fonts - wrong font!" unless allow_fonts.include? font
# if the font exists, return it's name
resources[:Font] ||= {}
resources[:Font].each do |k,v|
if v.is_a?(Hash) && v[:Type] == :Font && v[:BaseFont] == font
return k
end
end
# create font object
font_object = { Type: :Font, Subtype: :Type1, BaseFont: font}
# set a secure name for the font
name = (SecureRandom.urlsafe_base64(9)).to_sym
# get font object
font_object = Fonts.get_font(font)
# return false if the font wan't found in the library.
return false unless font_object
# add object to reasource
resources[:Font][name] = font_object
#return name
Expand Down
174 changes: 174 additions & 0 deletions lib/combine_pdf/combine_pdf_fonts.rb

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions lib/combine_pdf/combine_pdf_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,19 @@ def << (obj)
return self #return self object for injection chaining (pdf << page << page << page)
end

# and page numbers to the PDF
# LATIN ONLY - NO UNICODE SUPPORT YET
# add page numbers to the PDF
# options:: a Hash of options setting the behavior and format of the page numbers:
# - :number_format a string representing the format for page number. defaults to ' - %d - '.
# - :number_format a string representing the format for page number. defaults to ' - %s - ' (allows for letter numbering as well, such as "a", "b"...).
# - :number_location an Array containing the location for the page numbers, can be :top, :buttom, :top_left, :top_right, :bottom_left, :bottom_right. defaults to [:top, :buttom].
# - :start_at a Fixnum that sets the number for first page number. defaults to 1.
# - :start_at a Fixnum that sets the number for first page number. also accepts a letter ("a") for letter numbering. defaults to 1.
# - :margin_from_height a number (PDF points) for the top and buttom margins. defaults to 45.
# - :margin_from_side a number (PDF points) for the left and right margins. defaults to 15.
# the options Hash can also take all the options for PDFWriter.textbox.
# defaults to font_name: :Helvetica, font_size: 12 and no box (:border_width => 0, :box_color => nil).
def number_pages(options = {})
opt = {
number_format: ' - %d - ',
number_format: ' - %s - ',
number_location: [:top, :bottom],
start_at: 1,
font_size: 12,
Expand Down Expand Up @@ -351,7 +352,7 @@ def number_pages(options = {})
stamp.textbox text, {x: x, y: y }.merge(opt)
end
page << stamp
page_number += 1
page_number = page_number.succ
end
end

Expand Down
Loading

0 comments on commit 9b646e3

Please sign in to comment.