summaryrefslogtreecommitdiff
path: root/support/rfil/lib/rfil/rfi_plugin_context.rb
blob: 08a904b3ef24e512ba5a64cd44c7e0922b88f4f0 (plain)
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
=begin rdoc
Plugin for RFIL to create a typescript usable for ConTeXt.
=end

# :enddoc:

class TypescriptWriterConTeXt < RFIL::RFI::Plugin
  
  def initialize(fontcollection)
    @fc=fontcollection
    super(:context,:typescript)
  end

  STOPTYPESCRIPT="\\stoptypescript\n\n"
  
  def run_plugin
    ret=[]
    str=""
    puts "running context plugin" if @fc.options[:verbose]
    @fc.texenc.each { |e|
      str << typescript(e)
      str << "\n"
    }
    h={}
    h[:type]=:typescript
    h[:filename],h[:contents]=["type-#{@fc.name}.tex",str]
    ret.push(h)
    ret
  end

  # Returns hash: Style, font
  def find_fonts
    ret={}
    @fc.fonts.each { |font|
      ret[""]=font if font.variant==:regular and font.weight==:regular 
#      ret[""]=font if font.variant==:regular and font.weight==:regular and font.style!=:sans
      ret["Bold"]=font if font.variant==:regular and font.weight==:bold
      ret["Italic"]=font if font.variant==:italic and font.weight==:regular
      ret["Caps"]=font if font.variant==:smallcaps and font.weight==:regular
    }
    ret
  end
  def typescript(e)
    contextenc=case e.encname
               when "ECEncoding"
                 "ec"
               when "TS1Encoding"
                 "ts1"
               when "T1Encoding"
                 "tex256"
               when "TeXBase1Encoding"
                 "8r"
               else
                 raise "unknown context encoding: #{e.encname}"
               end
    # i know that this is crap, it's just a start
    contextstyle=case @fc.style
                 when :sans
                   "Sans"
                 when :roman, :serif
                   "Serif"
                 when :typewriter
                   "Typewriter"
                 else
                   raise "unknown style found: #{@fc.style}"
                 end
    tmp = ""
    fontname=@fc.name
    tmp << "\\starttypescript[#{@fc.style}][#{fontname}][name]\n"
    find_fonts.sort{ |a,b| a[0] <=> b[0]}.each { |style,font|
      tmp << "\\definefontsynonym [#{contextstyle}"
      p style
      tmp << "#{style}" if style.length > 0
      tmp << "] [#{fontname}"
      tmp << "-#{style}" if style.length > 0
      tmp << "]\n"
    }
    tmp << STOPTYPESCRIPT

    tmp << "\\starttypescript[#{@fc.style}][#{fontname}][#{contextenc}]\n"
    find_fonts.sort{ |a,b| a[0] <=> b[0]}.each { |style,font|
      tmp << "\\definefontsynonym [#{fontname}"
      tmp << "-#{style}" if style.length > 0
      tmp << "][#{font.tex_fontname(e)}]\n"
    }
    tmp << STOPTYPESCRIPT

    return tmp
  end
end