summaryrefslogtreecommitdiff
path: root/lib/str-to-bit.c
blob: 1f60b2220bee9dea07131cdf92164645f1b378fd (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
/*
# str-to-bit.c: typeset a text string in some font, producing a bitmap.
#
# 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 "c-ctype.h"
#include "font.h"


/* Turn the C string TEXT into a bitmap, taking the characters from the
   font FONT_NAME at resolution DPI.  Any space characters in TEXT turn
   into the natural interword space for this font.  No escapes are
   recognized in TEXT.  */

bitmap_type
string_to_bitmap (string text, string font_name, unsigned dpi)
{
  bitmap_type b;
  unsigned this_char;
  char_info_type *chars[strlen (text)];
  font_info_type font = get_font (font_name, dpi);
  int width = 0, height = 0, depth = 0;
  int x = 0, y = 0;  /* Our current position.  */
  real tfm_space
    = TFM_SAFE_FONTDIMEN (FONT_TFM_FONT (font), TFM_SPACE_PARAMETER, 0);
  int font_space = POINTS_TO_PIXELS (tfm_space, dpi);

  for (this_char = 0; this_char < strlen (text); this_char++)
    {
      /* Turn any kind of space character into a normal interword space.  */
      if (ISSPACE (text[this_char]))
        width += font_space;
      else
        {
	  chars[this_char] = get_char (font_name, text[this_char]);
          
          /* If the character doesn't exist in this font, just keep
             going.  */
          if (chars[this_char] == NULL)
            continue;
            
          /* The set width should be equal to the left side bearing plus
             the bitmap width plus the right side bearing.  */
	  width += CHAR_SET_WIDTH (*chars[this_char]);
	  height = MAX (height, CHAR_HEIGHT (*chars[this_char]));
	  depth = MAX (depth, CHAR_DEPTH (*chars[this_char]));
        }
    }

  /* Unless the image is nonnegative both horizontally and vertically,
     it is invisible.  */
  if (width <= 0 || (height + depth + 1 <= 0))
    {
      BITMAP_WIDTH (b) = 0;
      BITMAP_HEIGHT (b) = 0;
      BITMAP_BITS (b) = NULL;
      return b;
    }

  /* The image is going to be visible.  */
  b = new_bitmap ((dimensions_type) { height + depth + 1, width });
  
  for (this_char = 0; this_char < strlen (text); this_char++)
    {
      if (ISSPACE (text[this_char]))
        x += font_space;
      else
        {
          int char_x, char_y;
          char_info_type c;
          
          if (chars[this_char] == NULL)
            continue;
          
          c = *chars[this_char];

          x += CHAR_LSB (c);
          x = MAX (x, 0);  /* In case the lsb was negative.  */

          /* Copy the character image to the bitmap we are building, one
             column at a time, from top to bottom.  */
          for (char_x = 0; char_x < CHAR_BITMAP_WIDTH (c); char_x++, x++)
            for (char_y = 0, y = height - CHAR_HEIGHT (c);
                 char_y < CHAR_BITMAP_HEIGHT (c);
                 char_y++, y++)
              BITMAP_PIXEL (b, y, x)
                = BITMAP_PIXEL (CHAR_BITMAP (c), char_y, char_x);
          
          x += CHAR_RSB (c);
        }
    }

  close_font (font_name);
  
  return b;
}