File: Utils.rb

package info (click to toggle)
libtioga-ruby 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,460 kB
  • ctags: 3,986
  • sloc: ansic: 38,451; ruby: 16,774; sh: 172; makefile: 111
file content (85 lines) | stat: -rw-r--r-- 2,641 bytes parent folder | download
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
#  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
end