1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
# Utils.rb
=begin
Copyright (C) 2006 Vincent Fourmond
This file is part of Tioga.
Tioga is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published
by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Tioga is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with Tioga; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
=end
# This file is meant to receive useful functions not strictly speaking
# part of the graphical library, but that can come in handy for it.
module Tioga
# The Utils module contains some useful functions that can help Tioga
# users. They are not strictly speaking part of the graphical library,
# but provides functionnalities Tioga users will certainly need at
# some point.
module Utils
# This function returns a string that is suitable for inclusion in
# a TeX document: all nasty characters are escaped properly. It will
# not work however if you redefine TeX character classes, but then
# you should know what you're doing.
def tex_quote_text(text)
a = text.gsub("\\", "\\BS")
a.gsub!(/([{}$%#_^~])/) do
"\\#{$1}"
end
return a
end
module_function :tex_quote_text
class ::String
def quotex
Tioga::Utils.tex_quote_text(self)
end
end
# Dimension conversion constants taken straight from the TeXbook
DIMENSION_CONVERSION = {
"pt" => (72.0/72.27),
"bp" => 1.0,
"in" => 72.0,
"cm" => (72.0/2.54),
"mm" => (72.0/25.4),
}
# Returns the value of the given TeX dimension in postscript points.
def self.tex_dimension_to_bp(dim)
for unit, val in DIMENSION_CONVERSION
if dim =~ /^\s*([+-]?[\d.]+)\s*#{unit}$/
return $1.to_f * val
end
end
# We take it to be centimeters by default ????
if dim =~ /^\s*([+-]?[\d.]+)\s*$/
warn "tex_dimension_to_bp: No dimension was specified, " +
"using centimeters"
return $1.to_f * DIMENSION_CONVERSION["cm"]
end
raise "'#{dim}' is not a valid TeX dimension"
end
def tex_dimension_to_bp(dim)
return Utils::tex_dimension_to_bp(dim)
end
end
# This module is used internally by Tioga for handling the hash
# arguments that most of the functions take
module HashArguments
def check_dict(dict,names,str)
dict.each_key do |name|
if names[name] == nil
raise "Sorry: Invalid dictionary key for #{str} (#{name})."
end
end
end
def set_if_given(name, dict)
val = dict[name]
return if val == nil
eval "self." + name + " = val"
end
def alt_names(dict, name1, name2)
val = dict[name1]
val = dict[name2] if val == nil
return val
end
def get_if_given_else_use_default_dict(dict, name, default_dict)
if dict != nil
val = dict[name]
return val if val != nil
end
val = default_dict[name]
if val == nil
raise "Sorry: failed to find value for '#{name}' in the defaults dictionary."
end
return val
end
def get_if_given_else_default(dict, name, default)
return default if dict == nil
val = dict[name]
return val if val != nil
return default
end
def complain_if_missing_numeric_arg(dict, name, alt_name, who_called)
val = dict[name]
val = dict[alt_name] if val == nil
if val == nil
raise "Sorry: Must supply '#{name}' in call to '#{who_called}'"
end
if !(val.kind_of?Numeric)
raise "Sorry: Must supply numeric value for '#{name}' in call to '#{who_called}'"
end
return val
end
def check_pair(ary, name, who_called)
return false if ary == nil
if !(ary.kind_of?(Array) || ary.kind_of?(Dobjects::Dvector)) and ary.size == 2
raise "Sorry: '#{name}' must be array [x,y] for #{who_called}."
end
return true
end
def get_dvec(dict, name, who_called)
val = dict[name]
if val == nil || !(val.kind_of? Dobjects::Dvector)
raise "Sorry: '#{name}' must be a Dvector for '#{who_called}'"
end
return val
end
end
end
|