summaryrefslogtreecommitdiff
path: root/splash/SplashT1FontFile.cc
blob: 1832a916d9ae3730e9f95501737bd98ff6622c7a (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
//========================================================================
//
// SplashT1FontFile.cc
//
//========================================================================

//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
// Copyright (C) 2009 Albert Astals Cid <aacid@kde.org>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

#include <config.h>

#if HAVE_T1LIB_H

#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif

#include <string.h>
#include <t1lib.h>
#include "goo/GooString.h"
#include "goo/gfile.h"
#include "goo/gmem.h"
#include "SplashT1FontEngine.h"
#include "SplashT1Font.h"
#include "SplashT1FontFile.h"

//------------------------------------------------------------------------
// SplashT1FontFile
//------------------------------------------------------------------------

SplashFontFile *SplashT1FontFile::loadType1Font(SplashT1FontEngine *engineA,
						SplashFontFileID *idA,
						SplashFontSrc *src,
						const char **encA) {
  int t1libIDA;
  const char **encTmp;
  char *encStrTmp;
  int encStrSize;
  char *encPtr;
  int i;

  GooString *fileNameA;
  SplashFontSrc *newsrc = NULL;
  SplashFontFile *ff;

  if (! src->isFile) {
    GooString *tmpFileName;
    FILE *tmpFile;
    if (!openTempFile(&tmpFileName, &tmpFile, "wb"))
      return NULL;
    fwrite(src->buf, 1, src->bufLen, tmpFile);
    fclose(tmpFile);
    newsrc = new SplashFontSrc;
    newsrc->setFile(tmpFileName, gTrue);
    src = newsrc;
    delete tmpFileName;
  }
  fileNameA = src->fileName;
  // load the font file
  if ((t1libIDA = T1_AddFont(fileNameA->getCString())) < 0) {
    delete newsrc;
    return NULL;
  }
  T1_LoadFont(t1libIDA);

  // reencode it
  encStrSize = 0;
  for (i = 0; i < 256; ++i) {
    if (encA[i]) {
      encStrSize += strlen(encA[i]) + 1;
    }
  }
  encTmp = (const char **)gmallocn(257, sizeof(char *));
  encStrTmp = (char *)gmallocn(encStrSize, sizeof(char));
  encPtr = encStrTmp;
  for (i = 0; i < 256; ++i) {
    if (encA[i]) {
      strcpy(encPtr, encA[i]);
      encTmp[i] = encPtr;
      encPtr += strlen(encPtr) + 1;
    } else {
      encTmp[i] = ".notdef";
    }
  }
  encTmp[256] = "custom";
  T1_ReencodeFont(t1libIDA, (char **)encTmp);

  ff = new SplashT1FontFile(engineA, idA, src,
			      t1libIDA, encTmp, encStrTmp);
  if (newsrc)
    newsrc->unref();
  return ff;
}

SplashT1FontFile::SplashT1FontFile(SplashT1FontEngine *engineA,
				   SplashFontFileID *idA,
				   SplashFontSrc *srcA,
				   int t1libIDA, const char **encA, char *encStrA):
  SplashFontFile(idA, srcA)
{
  engine = engineA;
  t1libID = t1libIDA;
  enc = encA;
  encStr = encStrA;
}

SplashT1FontFile::~SplashT1FontFile() {
  gfree(encStr);
  gfree(enc);
  T1_DeleteFont(t1libID);
}

SplashFont *SplashT1FontFile::makeFont(SplashCoord *mat,
				       SplashCoord *textMat) {
  SplashFont *font;

  font = new SplashT1Font(this, mat, textMat);
  font->initCache();
  return font;
}

#endif // HAVE_T1LIB_H