summaryrefslogtreecommitdiffstats
path: root/kdvi/TeXFont_PFB.cpp
blob: 7db8e17143245cee723f450c4ce185c78b402cc5 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// TeXFont_PFB.cpp
//
// Part of KDVI - A DVI previewer for the KDE desktop environemt 
//
// (C) 2003 Stefan Kebekus
// Distributed under the GPL

// This file is compiled only if the FreeType library is present on
// the system

// Add header files alphabetically

#include <config.h>

#include <kdebug.h>
#include <klocale.h>
#include <tqimage.h>

#include "fontpool.h"

#ifdef HAVE_FREETYPE

#include "glyph.h"
#include "TeXFont_PFB.h"

//#define DEBUG_PFB 1


TeXFont_PFB::TeXFont_PFB(TeXFontDefinition *parent, fontEncoding *enc, double slant)
  : TeXFont(parent)
{
#ifdef DEBUG_PFB
  if (enc != 0)
    kdDebug(4300) << "TeXFont_PFB::TeXFont_PFB( parent=" << parent << ", encoding=" << enc->encodingFullName << " )" << endl;
  else
    kdDebug(4300) << "TeXFont_PFB::TeXFont_PFB( parent=" << parent << ", encoding=0 )" << endl;
#endif

  fatalErrorInFontLoading = false;

  int error = FT_New_Face( parent->font_pool->FreeType_library, parent->filename.local8Bit(), 0, &face );
  
  if ( error == FT_Err_Unknown_File_Format ) {
    errorMessage = i18n("The font file %1 could be opened and read, but its font format is unsupported.").tqarg(parent->filename);
    kdError(4300) << errorMessage << endl;
    fatalErrorInFontLoading = true;
    return;
  } else 
    if ( error ) {
      errorMessage = i18n("The font file %1 is broken, or it could not be opened or read.").tqarg(parent->filename);
      kdError(4300) << errorMessage << endl;
      fatalErrorInFontLoading = true;
      return;
    }

  // Take care of slanting, and transform all characters in the font, if necessary.
  if (slant != 0.0) {
    // Construct a transformation matrix for vertical shear which will
    // be used to transform the characters.
    transformationMatrix.xx = 0x10000;
    transformationMatrix.xy = (FT_Fixed)(slant * 0x10000);
    transformationMatrix.yx = 0;
    transformationMatrix.yy = 0x10000;
    
    FT_Set_Transform( face, &transformationMatrix, 0);
  }
  
  if (face->family_name != 0)
    parent->fullFontName = face->family_name;

  // Finally, we need to set up the charMap array, which maps TeX
  // character codes to glyph indices in the font. (Remark: the
  // charMap, and the font encoding procedure is necessary, because
  // TeX is only able to address character codes 0-255 while
  // e.g. Type1 fonts may contain several thousands of characters)
  if (enc != 0) {
    parent->fullEncodingName = enc->encodingFullName.remove(TQString::fromLatin1( "Encoding" ));
    parent->fullEncodingName = enc->encodingFullName.remove(TQString::fromLatin1( "encoding" ));

    // An encoding vector is given for this font, i.e. an array of
    // character names (such as: 'parenleft' or 'dotlessj'). We use
    // the FreeType library function 'FT_Get_Name_Index()' to
    // associate glyph indices to those names.
#ifdef DEBUG_PFB
    kdDebug(4300) << "Trying to associate glyph indices to names from the encoding vector." << endl;
#endif
    for(int i=0; i<256; i++) {
      charMap[i] = FT_Get_Name_Index( face, (FT_String *)(enc->glyphNameVector[i].ascii()) );
#ifdef DEBUG_PFB
      kdDebug(4300) << i << ": " << enc->glyphNameVector[i] << ", GlyphIndex=" <<  charMap[i] << endl;
#endif
    }
  } else {
    // If there is no encoding vector available, we check if the font
    // itself contains a charmap that could be used. An admissible
    // charMap will be stored under platform_id=7 and encoding_id=2.
    FT_CharMap  found = 0;
    for (int n = 0; n<face->num_charmaps; n++ ) {
      FT_CharMap charmap = face->charmaps[n];
      if ( charmap->platform_id == 7 && charmap->encoding_id == 2 ) {
	found = charmap;
	break;
      }
    }
    
    if ((found != 0) && (FT_Set_Charmap( face, found ) == 0)) {
      // Feed the charMap array with the charmap data found in the
      // previous step.
#ifdef DEBUG_PFB
      kdDebug(4300) << "No encoding given: using charmap platform=7, encoding=2 that is contained in the font." << endl;
#endif
      for(int i=0; i<256; i++) 
	charMap[i] = FT_Get_Char_Index( face, i );
    } else {
      if ((found == 0) && (face->charmap != 0)) {
#ifdef DEBUG_PFB
	kdDebug(4300) << "No encoding given: using charmap platform=" << face->charmap->platform_id <<
	  ", encoding=" << face->charmap->encoding_id << " that is contained in the font." << endl;
#endif
	for(int i=0; i<256; i++) 
	  charMap[i] = FT_Get_Char_Index( face, i );
      } else {
	// As a last resort, we use the identity map.
#ifdef DEBUG_PFB
	kdDebug(4300) << "No encoding given, no suitable charmaps found in the font: using identity charmap." << endl;
#endif
	for(int i=0; i<256; i++) 
	  charMap[i] = i;
      }
    }
  }
}


TeXFont_PFB::~TeXFont_PFB()
{
  FT_Done_Face( face );
}


glyph *TeXFont_PFB::getGlyph(TQ_UINT16 ch, bool generateCharacterPixmap, const TQColor& color)
{
#ifdef DEBUG_PFB
  kdDebug(4300) << "TeXFont_PFB::getGlyph( ch=" << ch << ", '" << (char)(ch) << "', generateCharacterPixmap=" << generateCharacterPixmap << " )" << endl;
#endif
  
  // Paranoia checks
  if (ch >= TeXFontDefinition::max_num_of_chars_in_font) {
    kdError(4300) << "TeXFont_PFB::getGlyph(): Argument is too big." << endl;
    return glyphtable;
  }

  // This is the address of the glyph that will be returned.
  struct glyph *g = glyphtable+ch;


  if (fatalErrorInFontLoading == true)
    return g;
  
  if ((generateCharacterPixmap == true) && ((g->shrunkenCharacter.isNull()) || (color != g->color)) ) {
    int error;
    unsigned int res =  (unsigned int)(parent->displayResolution_in_dpi/parent->enlargement +0.5);
    g->color = color;

    // Character height in 1/64th of points (reminder: 1 pt = 1/72 inch)
    // Only approximate, may vary from file to file!!!! @@@@@

    long int characterSize_in_printers_points_by_64 = (long int)((64.0*72.0*parent->scaled_size_in_DVI_units*parent->font_pool->getCMperDVIunit())/2.54 + 0.5 ); 
    error = FT_Set_Char_Size(face, 0, characterSize_in_printers_points_by_64, res, res );
    if (error) {
      TQString msg = i18n("FreeType reported an error when setting the character size for font file %1.").tqarg(parent->filename);
      if (errorMessage.isEmpty())
	errorMessage = msg;
      kdError(4300) << msg << endl;
      g->shrunkenCharacter.resize(1,1);
      g->shrunkenCharacter.fill(TQColor(255, 255, 255));
      return g;
    }
    
    // load glyph image into the slot and erase the previous one
    if (parent->font_pool->getUseFontHints() == true)
      error = FT_Load_Glyph(face, charMap[ch], FT_LOAD_DEFAULT ); 
    else
      error = FT_Load_Glyph(face, charMap[ch], FT_LOAD_NO_HINTING );

    if (error) {
      TQString msg = i18n("FreeType is unable to load glyph #%1 from font file %2.").tqarg(ch).tqarg(parent->filename);
      if (errorMessage.isEmpty())
	errorMessage = msg;
      kdError(4300) << msg << endl;
      g->shrunkenCharacter.resize(1,1);
      g->shrunkenCharacter.fill(TQColor(255, 255, 255));
      return g;
    }
    
    // convert to an anti-aliased bitmap
    error = FT_Render_Glyph( face->glyph, ft_render_mode_normal );
    if (error) {
      TQString msg = i18n("FreeType is unable to render glyph #%1 from font file %2.").tqarg(ch).tqarg(parent->filename);
      if (errorMessage.isEmpty())
	errorMessage = msg;
      kdError(4300) << msg << endl;
      g->shrunkenCharacter.resize(1,1);
      g->shrunkenCharacter.fill(TQColor(255, 255, 255));
      return g;
    }
    
    FT_GlyphSlot slot = face->glyph;

    if ((slot->bitmap.width == 0) || (slot->bitmap.rows == 0)) {
      if (errorMessage.isEmpty())
	errorMessage = i18n("Glyph #%1 is empty.").tqarg(ch);
      kdError(4300) << i18n("Glyph #%1 from font file %2 is empty.").tqarg(ch).tqarg(parent->filename) << endl;
      g->shrunkenCharacter.resize( 15, 15 );
      g->shrunkenCharacter.fill(TQColor(255, 0, 0));
      g->x2 = 0;
      g->y2 = 15;
    } else {
      TQImage imgi(slot->bitmap.width, slot->bitmap.rows, 32);
      imgi.setAlphaBuffer(true);

      // Do TQPixmaps fully support the alpha channel? If yes, we use
      // that. Otherwise, use other routines as a fallback
      if (parent->font_pool->TQPixmapSupportsAlpha) {
	// If the alpha channel is properly supported, we set the
	// character glyph to a colored rectangle, and define the
	// character outline only using the alpha channel. That
	// ensures good quality rendering for overlapping characters.
	uchar *srcScanLine = slot->bitmap.buffer;
	for(int row=0; row<slot->bitmap.rows; row++) {
	  uchar *destScanLine = imgi.scanLine(row);
	  for(int col=0; col<slot->bitmap.width; col++) {
	    destScanLine[4*col+0] = color.blue();
	    destScanLine[4*col+1] = color.green();
	    destScanLine[4*col+2] = color.red();
	    destScanLine[4*col+3] = srcScanLine[col];
	  }
	  srcScanLine += slot->bitmap.pitch;
	}
      } else {
	// If the alpha channel is not supported... QT seems to turn
	// the alpha channel into a crude bitmap which is used to mask
	// the resulting TQPixmap. In this case, we define the
	// character outline using the image data, and use the alpha
	// channel only to store "maximally opaque" or "completely
	// transparent" values. When characters are rendered,
	// overlapping characters are no longer correctly drawn, but
	// quality is still sufficient for most purposes. One notable
	// exception is output from the gftodvi program, which will be
	// partially unreadable.
	TQ_UINT16 rInv = 0xFF - color.red();
	TQ_UINT16 gInv = 0xFF - color.green();
	TQ_UINT16 bInv = 0xFF - color.blue();
	
	for(TQ_UINT16 y=0; y<slot->bitmap.rows; y++) {
	  TQ_UINT8 *srcScanLine = slot->bitmap.buffer + y*slot->bitmap.pitch;
	  unsigned int *destScanLine = (unsigned int *)imgi.scanLine(y);
	  for(TQ_UINT16 col=0; col<slot->bitmap.width; col++) {
	    TQ_UINT16 data =  *srcScanLine;
	    // The value stored in "data" now has the following meaning:
	    // data = 0 -> white; data = 0xff -> use "color"
	    *destScanLine = tqRgba(0xFF - (rInv*data + 0x7F) / 0xFF,
				  0xFF - (gInv*data + 0x7F) / 0xFF,
				  0xFF - (bInv*data + 0x7F) / 0xFF,
				  (data > 0x03) ? 0xff : 0x00);
	    destScanLine++;
	    srcScanLine++;
	  }
	}
      }
      
      g->shrunkenCharacter.convertFromImage (imgi, 0);
      g->x2 = -slot->bitmap_left;
      g->y2 = slot->bitmap_top;
    }
  }
  
  // Load glyph width, if that hasn't been done yet.
  if (g->dvi_advance_in_units_of_design_size_by_2e20 == 0) {
    int error = FT_Load_Glyph(face, charMap[ch], FT_LOAD_NO_SCALE);
    if (error) {
      TQString msg = i18n("FreeType is unable to load metric for glyph #%1 from font file %2.").tqarg(ch).tqarg(parent->filename);
      if (errorMessage.isEmpty())
	errorMessage = msg;
      kdError(4300) << msg << endl;
      g->dvi_advance_in_units_of_design_size_by_2e20 =  1;
    }
    g->dvi_advance_in_units_of_design_size_by_2e20 =  (TQ_INT32)(((TQ_INT64)(1<<20) * (TQ_INT64)face->glyph->metrics.horiAdvance) / (TQ_INT64)face->units_per_EM);
  }
  
  return g;
}

#endif // HAVE_FREETYPE