Skip to content

Commit

Permalink
Merge pull request #95 from naitoh/fix_convertHTMLColorToDec
Browse files Browse the repository at this point in the history
convertHTMLColorToDec was changed to deprecated. use convert_html_color_to_dec_array instead.
  • Loading branch information
naitoh authored Dec 25, 2023
2 parents 50741d9 + 1245bbe commit 212dba0
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 57 deletions.
8 changes: 4 additions & 4 deletions example/rails/app/controllers/example012_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ def index
pdf.text(185, 249, 'Arrows')
pdf.set_line_style(style5)
pdf.set_fill_color(255, 0, 0)
pdf.Arrow(x0=200, y0=280, x1=185, y1=266, head_style=0, arm_size=5, arm_angle=15)
pdf.Arrow(x0=200, y0=280, x1=190, y1=263, head_style=1, arm_size=5, arm_angle=15)
pdf.Arrow(x0=200, y0=280, x1=195, y1=261, head_style=2, arm_size=5, arm_angle=15)
pdf.Arrow(x0=200, y0=280, x1=200, y1=260, head_style=3, arm_size=5, arm_angle=15)
pdf.arrow(x0=200, y0=280, x1=185, y1=266, head_style=0, arm_size=5, arm_angle=15)
pdf.arrow(x0=200, y0=280, x1=190, y1=263, head_style=1, arm_size=5, arm_angle=15)
pdf.arrow(x0=200, y0=280, x1=195, y1=261, head_style=2, arm_size=5, arm_angle=15)
pdf.arrow(x0=200, y0=280, x1=200, y1=260, head_style=3, arm_size=5, arm_angle=15)

# - . - . - . - . - . - . - . - . - . - . - . - . - . - . -

Expand Down
Binary file added example/rails/public/gif_test_msk_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/rails/public/image_demo.webp
Binary file not shown.
Binary file added example/rails/public/webp_test_alpha.webp
Binary file not shown.
101 changes: 48 additions & 53 deletions lib/rbpdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,18 +530,9 @@ def initialize(orientation = 'P', unit = 'mm', format = 'A4', unicode = true, e
@href ||= {}
@fontlist ||= []
getFontsList()
@fgcolor = ActiveSupport::OrderedHash.new
@fgcolor['R'] = 0
@fgcolor['G'] = 0
@fgcolor['B'] = 0
@strokecolor = ActiveSupport::OrderedHash.new
@strokecolor['R'] = 0
@strokecolor['G'] = 0
@strokecolor['B'] = 0
@bgcolor = ActiveSupport::OrderedHash.new
@bgcolor['R'] = 255
@bgcolor['G'] = 255
@bgcolor['B'] = 255
@fgcolor = [0, 0, 0]
@strokecolor = [0, 0, 0]
@bgcolor = [255, 255, 255]
@extgstates ||= []

# user's rights
Expand Down Expand Up @@ -2300,20 +2291,15 @@ def SetDrawColor(col1=0, col2=-1, col3=-1, col4=-1)
if (col2 == -1) and (col3 == -1) and (col4 == -1)
# Grey scale
@draw_color = sprintf('%.3f G', col1 / 255.0)
@strokecolor['G'] = col1
@strokecolor = [col1]
elsif col4 == -1
# RGB
@draw_color = sprintf('%.3f %.3f %.3f RG', col1 / 255.0, col2 / 255.0, col3 / 255.0)
@strokecolor['R'] = col1
@strokecolor['G'] = col2
@strokecolor['B'] = col3
@strokecolor = [col1, col2, col3]
else
# CMYK
@draw_color = sprintf('%.3f %.3f %.3f %.3f K', col1 / 100.0, col2 / 100.0, col3 / 100.0, col4 / 100.0)
@strokecolor['C'] = col1
@strokecolor['M'] = col2
@strokecolor['Y'] = col3
@strokecolor['K'] = col4
@strokecolor = [col1, col2, col3, col4]
end
if (@page>0)
out(@draw_color + ' ')
Expand Down Expand Up @@ -2373,20 +2359,15 @@ def SetFillColor(col1=0, col2=-1, col3=-1, col4=-1)
if (col2 == -1) and (col3 == -1) and (col4 == -1)
# Grey scale
@fill_color = sprintf('%.3f g', col1 / 255.0)
@bgcolor['G'] = col1
@bgcolor = [col1]
elsif col4 == -1
# RGB
@fill_color = sprintf('%.3f %.3f %.3f rg', col1 / 255.0, col2 / 255.0, col3 / 255.0)
@bgcolor['R'] = col1
@bgcolor['G'] = col2
@bgcolor['B'] = col3
@bgcolor = [col1, col2, col3]
else
# CMYK
@fill_color = sprintf('%.3f %.3f %.3f %.3f k', col1 / 100.0, col2 / 100.0, col3 / 100.0, col4 / 100.0)
@bgcolor['C'] = col1
@bgcolor['M'] = col2
@bgcolor['Y'] = col3
@bgcolor['K'] = col4
@bgcolor = [col1, col2, col3, col4]
end

@color_flag = (@fill_color != @text_color)
Expand Down Expand Up @@ -2463,20 +2444,16 @@ def SetTextColor(col1=0, col2=-1, col3=-1, col4=-1)
if (col2 == -1) and (col3 == -1) and (col4 == -1)
# Grey scale
@text_color = sprintf('%.3f g', col1 / 255.0)
@fgcolor['G'] = col1
@fgcolor = [col1]
elsif col4 == -1
# RGB
@text_color = sprintf('%.3f %.3f %.3f rg', col1 / 255.0, col2 / 255.0, col3 / 255.0)
@fgcolor['R'] = col1
@fgcolor['G'] = col2
@fgcolor['B'] = col3
@fgcolor = [col1, col2, col3]

else
# CMYK
@text_color = sprintf('%.3f %.3f %.3f %.3f k', col1 / 100.0, col2 / 100.0, col3 / 100.0, col4 / 100.0)
@fgcolor['C'] = col1
@fgcolor['M'] = col2
@fgcolor['Y'] = col3
@fgcolor['K'] = col4
@fgcolor = [col1, col2, col3, col4]
end
@color_flag = (@fill_color != @text_color)
end
Expand Down Expand Up @@ -8672,7 +8649,16 @@ def getHtmlAnchorPosition(anchor)
# [@return array] RGB color or empty array in case of error.
# [@access public]
#
def convertHTMLColorToDec(color = "#FFFFFF")
def convert_html_color_to_dec_array(color = "#FFFFFF")
returncolor = _convert_html_color_to_dec(color)
if returncolor.is_a? Hash
returncolor.values
else
returncolor
end
end

private def _convert_html_color_to_dec(color = "#FFFFFF")
color = color.gsub(/[\s]*/, '') # remove extra spaces
color = color.downcase
if !(dotpos = color.index('.')).nil?
Expand All @@ -8682,7 +8668,6 @@ def convertHTMLColorToDec(color = "#FFFFFF")
if color.length == 0
return []
end
returncolor = ActiveSupport::OrderedHash.new
# RGB ARRAY
if color[0,3] == 'rgb'
codes = color.sub(/^rgb\(/, '')
Expand All @@ -8697,6 +8682,7 @@ def convertHTMLColorToDec(color = "#FFFFFF")
if color[0,4] == 'cmyk'
codes = color.sub(/^cmyk\(/, '')
codes = codes.gsub(')', '')
returncolor = codes.split(',', 4)
returncolor[0] = returncolor[0].to_i
returncolor[1] = returncolor[1].to_i
returncolor[2] = returncolor[2].to_i
Expand All @@ -8715,6 +8701,7 @@ def convertHTMLColorToDec(color = "#FFFFFF")
color_code = color.sub(/^#/, "")
end
# RGB VALUE
returncolor = {}
case color_code.length
when 3
# three-digit hexadecimal representation
Expand All @@ -8734,6 +8721,11 @@ def convertHTMLColorToDec(color = "#FFFFFF")
end
return returncolor
end

def convertHTMLColorToDec(color = "#FFFFFF")
warn("#{__callee__} is deprecated, use convert_html_color_to_dec_array instead.", uplevel: 1)
_convert_html_color_to_dec(color)
end
alias_method :convert_html_color_to_dec, :convertHTMLColorToDec

#
Expand Down Expand Up @@ -8858,7 +8850,7 @@ def Rotate(angle, x="", y="")
tm[5] = y - tm[0] * y - tm[1] * x

# generate the transformation matrix
Transform(tm)
transform(tm)
end
alias_method :rotate, :Rotate

Expand All @@ -8869,7 +8861,7 @@ def Rotate(angle, x="", y="")
# [@since 2.1.000 (2008-01-07)]
# [@see] StartTransform(), StopTransform()
#
def Transform(tm)
def transform(tm)
out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm', tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]))
# add tranformation matrix
@transfmatrix[@transfmatrix_key].push 'a' => tm[0], 'b' => tm[1], 'c' => tm[2], 'd' => tm[3], 'e' => tm[4], 'f' => tm[5]
Expand All @@ -8878,7 +8870,7 @@ def Transform(tm)
@transfmrk[@page] = @pagelen[@page]
end
end
protected :Transform
protected :transform

# END TRANSFORMATIONS SECTION -------------------------

Expand Down Expand Up @@ -8955,6 +8947,7 @@ def SetLineStyle(style)
end
if !style['dash'].nil?
dash = style['dash']
phase = style['phase'].to_i
dash_string = ''
if dash != 0 and dash != ''
if dash.is_a?(String) && dash =~ /^.+,/
Expand All @@ -8969,8 +8962,10 @@ def SetLineStyle(style)
end
dash_string << sprintf("%.2f", v.to_f)
}
else
phase = 0
end
phase = 0

@linestyle_dash = sprintf("[%s] %.2f d", dash_string, phase)
out(@linestyle_dash)
end
Expand Down Expand Up @@ -9074,13 +9069,13 @@ def outCurveY(x1, y1, x3, y3)
# [@param float :y1] Ordinate of first point
# [@param float :x2] Abscissa of second point
# [@param float :y2]] Ordinate of second point
# [@param hash :style] Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
# [@param hash :style] Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty Hash).
# [@access public]
# [@since 1.0]
# [@see] SetLineWidth(), SetDrawColor(), SetLineStyle()
#
def Line(x1, y1, x2, y2, style=nil)
if style.is_a? Hash
def Line(x1, y1, x2, y2, style={})
if style.is_a?(Hash) && !style.empty?
SetLineStyle(style)
end
outPoint(x1, y1)
Expand Down Expand Up @@ -10647,15 +10642,15 @@ def getAnnotOptFromJSProp(prop)
if prop['fillColor'].is_a? Array
opt['mk']['bg'] = prop['fillColor']
else
opt['mk']['bg'] = convertHTMLColorToDec(prop['fillColor'])
opt['mk']['bg'] = convert_html_color_to_dec_array(prop['fillColor'])
end
end
# strokeColor: Specifies the stroke color for a field that is used to stroke the rectangle of the field with a line as large as the line width.
if prop['strokeColor']
if prop['strokeColor'].is_a? Array
opt['mk']['bc'] = prop['strokeColor']
else
opt['mk']['bc'] = convertHTMLColorToDec(prop['strokeColor'])
opt['mk']['bc'] = convert_html_color_to_dec_array(prop['strokeColor'])
end
end
# rotation: The rotation of a widget in counterclockwise increments.
Expand Down Expand Up @@ -12860,7 +12855,7 @@ def getHtmlDomArray(html)
dom[key]['fill'] = ((@textrendermode % 2) == 0)
dom[key]['clip'] = (@textrendermode > 3)
dom[key]['line-height'] = @cell_height_ratio
dom[key]['bgcolor'] = ActiveSupport::OrderedHash.new
dom[key]['bgcolor'] = []
dom[key]['fgcolor'] = @fgcolor.dup # color
dom[key]['strokecolor'] = @strokecolor.dup

Expand Down Expand Up @@ -13087,13 +13082,13 @@ def getHtmlDomArray(html)
end
# font color
if !empty_string(dom[key]['style']['color'])
dom[key]['fgcolor'] = convertHTMLColorToDec(dom[key]['style']['color'])
dom[key]['fgcolor'] = convert_html_color_to_dec_array(dom[key]['style']['color'])
elsif dom[key]['value'] == 'a'
dom[key]['fgcolor'] = @html_link_color_array
end
# background color
if !empty_string(dom[key]['style']['background-color'])
dom[key]['bgcolor'] = convertHTMLColorToDec(dom[key]['style']['background-color'])
dom[key]['bgcolor'] = convert_html_color_to_dec_array(dom[key]['style']['background-color'])
end
# text-decoration
if !dom[key]['style']['text-decoration'].nil?
Expand Down Expand Up @@ -13256,17 +13251,17 @@ def getHtmlDomArray(html)
end
# set foreground color attribute
if !empty_string(dom[key]['attribute']['color'])
dom[key]['fgcolor'] = convertHTMLColorToDec(dom[key]['attribute']['color'])
dom[key]['fgcolor'] = convert_html_color_to_dec_array(dom[key]['attribute']['color'])
elsif (dom[key]['style'].nil? or dom[key]['style']['color'].nil?) and (dom[key]['value'] == 'a')
dom[key]['fgcolor'] = @html_link_color_array
end
# set background color attribute
if !empty_string(dom[key]['attribute']['bgcolor'])
dom[key]['bgcolor'] = convertHTMLColorToDec(dom[key]['attribute']['bgcolor'])
dom[key]['bgcolor'] = convert_html_color_to_dec_array(dom[key]['attribute']['bgcolor'])
end
# set stroke color attribute
if !empty_string(dom[key]['attribute']['strokecolor'])
dom[key]['strokecolor'] = convertHTMLColorToDec(dom[key]['attribute']['strokecolor'])
dom[key]['strokecolor'] = convert_html_color_to_dec_array(dom[key]['attribute']['strokecolor'])
end
# check for width attribute
if !dom[key]['attribute']['width'].nil?
Expand Down
80 changes: 80 additions & 0 deletions test/rbpdf_graphic_func_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2011-2023 NAITOH Jun
# Released under the MIT license
# http://www.opensource.org/licenses/MIT

require 'test_helper'

class RbpdfPageTest < Test::Unit::TestCase
class MYPDF < RBPDF
def putshaders
super
end

def getPageBuffer(page)
super
end
end

test "set_line_style test" do
pdf = MYPDF.new
pdf.add_page()
page = pdf.get_page
contents = pdf.getPageBuffer(page)
before_size = contents.split("\n").size
pdf.putshaders
pdf.set_line_style({'width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => '1,2,3,4', 'phase' => 1, 'color' => [255, 0, 0]})

content = []
contents = pdf.getPageBuffer(page)
contents.each_line {|line| content.push line.chomp }

assert_equal "0.28 w", content[before_size] # 'width' => 0.1
assert_equal "0 J", content[before_size + 1] # 'cap' => 'butt'
assert_equal "0 j", content[before_size + 2] # 'join' => 'miter
assert_equal "[1.00 2.00 3.00 4.00] 1.00 d", content[before_size + 3] # 'dash' => '1,2,3,4', 'phase' => 1
assert_equal "1.000 0.000 0.000 RG ", content[before_size + 4] # 'color' => [255, 0, 0]
end

test "line without style test" do
pdf = MYPDF.new
pdf.add_page()
page = pdf.get_page
contents = pdf.getPageBuffer(page)
before_size = contents.split("\n").size
pdf.putshaders
pdf.line(5, 10, 80, 30)
content = []
contents = pdf.getPageBuffer(page)
contents.each_line {|line| content.push line.chomp }

assert_equal "14.17 813.54 m", content[before_size] # outPoint(x1, y1)
assert_equal "226.77 756.85 l", content[before_size + 1] # outLine(x2, y2)
assert_equal "S", content[before_size + 2]
end

test "line with style test" do
pdf = MYPDF.new
pdf.add_page()
page = pdf.get_page
contents = pdf.getPageBuffer(page)
before_size = contents.split("\n").size
pdf.putshaders
style = {'width' => 0.1, 'cap' => 'round', 'join' => 'bevel', 'dash' => 0, 'color' => [0, 255, 0]}
pdf.line(5, 10, 80, 30, style)
content = []
contents = pdf.getPageBuffer(page)
contents.each_line {|line| content.push line.chomp }

# set_line_style start
assert_equal "0.28 w", content[before_size] # 'width' => 0.1
assert_equal "1 J", content[before_size + 1] # 'cap' => 'round'
assert_equal "2 j", content[before_size + 2] # 'join' => 'bevel
assert_equal "[] 0.00 d", content[before_size + 3] # 'dash' => 0
assert_equal "0.000 1.000 0.000 RG ", content[before_size + 4] # 'color' => [0, 255, 0]
# set_line_style end

assert_equal "14.17 813.54 m", content[before_size + 5] # outPoint(x1, y1)
assert_equal "226.77 756.85 l", content[before_size + 6] # outLine(x2, y2)
assert_equal "S", content[before_size + 7]
end
end
Loading

0 comments on commit 212dba0

Please sign in to comment.