summaryrefslogtreecommitdiff
path: root/fontconvert/output-epsf.c
blob: 6b6ce638c2d9951c2d748c877bba2a511c7a7709 (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
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
# output-epsf.c: output EPS files.
#
# Copyright (C) 1992, 2011 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#include "config.h"

#include "output-epsf.h"


/* The base part of the fontname to output to.  */
static string output_name;


/* It's a fatal error if NAME has a suffix, since that could mean
   overwriting the same file several times with different information.  */

void
epsf_start_output (const_string name)
{
  if (find_suffix (name) != NULL)
    FATAL ("You can't specify a suffix and EPS output");
}


/* Write a single character to the file `output_name-CODE.eps',
   where CODE is the character code.  */

/* Output the lower eight bits of VALUE to FILE as two hex digits.  For
   reasons I don't understand, when I write these same expressions in
   the arguments to the fprintf, values less than 128 are copied into
   the high nybble; that is, 1 is output as `11'.  */
#define OUTPUT_HEX(file, value)						\
  do									\
    {  one_byte v1 = (value) & 0xf0;					\
       one_byte v2 = (value) & 0xf;					\
       fprintf (file, "%x%x", v1 >> 4, v2);				\
    }									\
  while (0)

void
epsf_output_char (char_info_type c)
{
  unsigned row;
  bitmap_type b = CHAR_BITMAP (c);
  unsigned height = BITMAP_HEIGHT (b);
  unsigned width = BITMAP_WIDTH (b);
  charcode_type code = CHARCODE (c);
  string title = concat3 (output_name, "-", itoa (code));
  string eps_name = concat (title, ".eps");
  FILE *eps = xfopen (eps_name, "w");
  
  /* Write the header.  */
  fputs ("%!PS-Adobe-3.0 EPSF-3.0\n", eps);
  fprintf (eps, "%%%%BoundingBox: 0 0 %u %u\n",
           BITMAP_WIDTH (b), BITMAP_HEIGHT (b));
  fprintf (eps, "%%%%Title: (%s)\n", title);
  fputs ("%%Creator: fontconvert -epsf\n", eps);
  fprintf (eps, "%%%%CreationDate: %s\n", now ());
  fputs ("%%EndComments\n", eps);
  
  /* Write the bitmap as an image.  */
  
  /* First arrange to scale the image to its original size.  */
  fprintf (eps, "%u %u scale\n", width, height);
  
  /* Push the imagemask parameters.  */
  fprintf (eps, "%u %u  true  [%u 0 0 -%u 0 %u]\n",
           width, height, width, height, height);

  /* Output the bits.  */
  fputs ("{<", eps);
  for (row = 0; row < height; row++)
    {
      unsigned col;
      one_byte byte = 0;
      unsigned bit = 7;

      for (col = 0; col < width; col++)
        {
          byte |= BITMAP_PIXEL (b, row, col) << bit;
          
          /* Output eight bits at a time, since imagemask expects that.  */
          if (bit == 0)
            {
              OUTPUT_HEX (eps, byte);
              byte = 0;
              bit = 8;
            }
          
          /* Decrement `bit' after, so that the test `bit == 0' above is
             true at the right time.  */
          bit--;
        }
      
      /* If the width isn't a multiple of eight, have to output the
         remainder.  */
      if (bit < 7)
        OUTPUT_HEX (eps, byte);
      
      /* In any case, output a newline, just to make the output easier
         to read, in case somebody wants to read it.  */
      fputs ("\n", eps);
    }
  fputs (">}\nimagemask\n", eps);
  
  /* Write the trailer.  */
  fputs ("showpage\n", eps);
  fputs ("%%EOF\n", eps);
  
  xfclose (eps, eps_name);
  free (eps_name);
  free (title);
}


/* We don't have anything to do at the end.  */

void
epsf_finish_output (void)
{
}