Scribus
Open source desktop publishing at your fingertips
scface_ps.h
1 /*
2  For general Scribus (>=1.3.2) copyright and licensing information please refer
3  to the COPYING file provided with the program. Following this notice may exist
4  a copyright and/or license notice that predates the release of Scribus 1.3.2
5  for which a new license (GPL+exception) is in place.
6  */
7 #ifndef SCFACE_PS_H
8 #define SCFACE_PS_H
9 
10 #include <QString>
11 #include <QStringList>
12 #include <QFont>
13 #include <QMap>
14 
15 #include <ft2build.h>
16 #include FT_FREETYPE_H
17 #include FT_OUTLINE_H
18 #include FT_GLYPH_H
19 
20 #include "scribusapi.h"
21 #include "fpointarray.h"
22 #include "scconfig.h"
23 
24 
25 /*
26  Class ScFace_postscript
27  Subclass of ScFace, for PostScript fonts that could possibly have a .afm file
28  associated with them for metrics information.
29 */
30 
31 class ScFace_postscript : public FtFace
32 {
33  public:
34  ScFace_postscript(QString fam, QString sty, QString alt, QString scname, QString psname, QString path, int face) :
35  FtFace(fam,sty,alt,scname,psname,path,face)
36  {
37  isFixedPitch = false;
38  typeCode = ScFace::TYPE1;
39  }
40 
41  virtual QStringList findFontMetrics(const QString& fontPath) const;
42  virtual QStringList findFontMetrics(const QString& baseDir, const QString& baseName) const;
43  virtual bool loadFontMetrics(FT_Face face, const QString& fontPath) const;
44 
45  virtual void load() const // routine by Franz Schmid - modified by Alastair M. Robinson
46  {
47  FtFace::load();
48 // bool error;
49  FT_Face face = ftFace();
50  if (!face)
51  {
52  const_cast<ScFace_postscript*>(this)->usable = false;
53  qDebug("%s", QObject::tr("Font %1 is broken (no Face), discarding it").arg(fontFile).toLocal8Bit().constData());
54  return;
55  }
56  if (loadFontMetrics(face, fontFile))
57  {
58  // re-initialize: ScFaceData::load() just clears caches,
59  // FtFace::load() skips FT_New_Face if m_face is already defined.
60  // dont mind checking glyphs again for now (PS files have only 255 glyphs max, anyway)
61  FtFace::load();
62  }
63 // Ascent = tmp.setNum(face->ascender);
64 // Descender = tmp.setNum(face->descender);
65 // CapHeight = Ascent;
66 // ItalicAngle = "0";
67 // FontBBox = tmp.setNum(face->bbox.xMin)+" "+tmp2.setNum(face->bbox.yMin)+" "+tmp3.setNum(face->bbox.xMax)+" "+tmp4.setNum(face->bbox.yMax);
68 /*
69  setBestEncoding(face);
70  gindex = 0;
71  charcode = FT_Get_First_Char( face, &gindex );
72  int goodGlyph = 0;
73  int invalidGlyph = 0;
74  while ( gindex != 0 )
75  {
76  error = FT_Load_Glyph( face, gindex, FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP );
77  if (error)
78  {
79  ++invalidGlyph;
80  sDebug(QObject::tr("Font %1 has broken glyph %2 (charcode %3)").arg(fontPath()).arg(gindex).arg(charcode));
81  charcode = FT_Get_Next_Char( face, charcode, &gindex );
82  continue;
83  }
84  ++goodGlyph;
85  qreal ww = face->glyph->metrics.horiAdvance / uniEM;
86  if (face->glyph->format == FT_GLYPH_FORMAT_PLOTTER)
87  isStroked = true;
88  error = false;
89  outlines = traceChar(face, charcode, 10, &x, &y, &error);
90  if (!error)
91  {
92  CharWidth.insert(charcode, ww);
93  GRec.Outlines = outlines.copy();
94  GRec.x = x;
95  GRec.y = y;
96  GlyphArray.insert(charcode, GRec);
97  }
98  charcode = FT_Get_Next_Char( face, charcode, &gindex );
99  }
100  */
101  }
102 };
103 
104 /*
105  Class ScFace_pfb
106  Subclass of ScFace, specifically for Adobe type 1 .pfb fonts.
107  Implements: RealName() and EmbedFont().
108 */
109 
111 {
112  public:
113  ScFace_pfb(QString fam, QString sty, QString alt, QString scname, QString psname, QString path, int face) :
114  ScFace_postscript(fam,sty,alt,scname,psname,path,face)
115  {
116  formatCode = ScFace::PFB;
117  }
118 
119  virtual bool EmbedFont(QByteArray &str) const
120  {
121  QByteArray bb;
122  RawData(bb);
123  QString tmp2 = "";
124  if ((bb.size() > 2) && (bb[0] == char(0x80)) && (static_cast<int>(bb[1]) == 1))
125  {
126  QString tmp3="";
127  QString tmp4 = "";
128  int posi,cxxc=0;
129  for (posi = 6; posi < bb.size(); ++posi)
130  {
131  if ((bb[posi] == char(0x80)) && (posi+1 < bb.size()) && (static_cast<int>(bb[posi+1]) == 2))
132  break;
133  str += bb[posi];
134  }
135  int ulen;
136  if (posi+6 < bb.size())
137  {
138  ulen = bb[posi+2] & 0xff;
139  ulen |= (bb[posi+3] << 8) & 0xff00;
140  ulen |= (bb[posi+4] << 16) & 0xff0000;
141  ulen |= (bb[posi+5] << 24) & 0xff000000;
142  posi += 6;
143  if (posi + ulen > bb.size())
144  ulen = bb.size() - posi - 1;
145  char linebuf[80];
146  cxxc=0;
147  for (int j = 0; j < ulen; ++j)
148  {
149  unsigned char u=bb[posi];
150  linebuf[cxxc]=((u >> 4) & 15) + '0';
151  if(u>0x9f) linebuf[cxxc]+='a'-':';
152  ++cxxc;
153  u&=15; linebuf[cxxc]=u + '0';
154  if(u>0x9) linebuf[cxxc]+='a'-':';
155  ++posi;
156  ++cxxc;
157  if (cxxc > 72)
158  {
159  linebuf[cxxc++]='\n';
160  linebuf[cxxc++]=0;
161  str += linebuf;
162  cxxc = 0;
163  }
164  }
165  linebuf[cxxc]=0;
166  str += linebuf;
167  str += "\n";
168  }
169  posi += 6;
170  for (int j = posi; j < bb.size(); ++j)
171  {
172  if ((bb[j] == static_cast<char>(0x80)) && (j+1 < bb.size()) && (static_cast<int>(bb[j+1]) == 3))
173  break;
174  if(bb[j]=='\r')
175  str+="\n";
176  else
177  str += bb[j];
178  }
179  str += "\n";
180  cxxc = 0;
181  return true;
182  }
183  else
184  {
185  qDebug("%s", QObject::tr("Font %1 cannot be read, no embedding").arg(fontFile).toLatin1().constData());
186  return false;
187  }
188  }
189 };
190 
191 /*
192  Class ScFace_pfa
193  Subclass of ScFace, specifically for Adobe type 1 and type 3 .pfa fonts.
194  Implements: RealName() and EmbedFont().
195 */
196 
198 {
199  public:
200  ScFace_pfa(QString fam, QString sty, QString alt, QString scname, QString psname, QString path, int face) :
201  ScFace_postscript(fam,sty,alt,scname,psname,path,face)
202  {
203  formatCode = ScFace::PFA;
204  }
205  virtual bool EmbedFont(QByteArray &str) const
206  {
207  QByteArray bb;
208  RawData(bb);
209  if (bb.size() > 2 && bb[0] == '%' && bb[1] == '!')
210  {
211  // this is ok since bb will not contain '\0'
212  str.append(bb);
213  return true;
214  }
215  qDebug("%s", QObject::tr("Font %1 cannot be read, no embedding").arg(fontFile).toLatin1().constData());
216  return false;
217  }
218 };
219 
220 
221 #endif
Definition: scface_ps.h:31
Base Class FtFace provides an ScFace private implementation for Freetype based fonts. Subclasses are ScFace_ps and ScFace_ttf.
Definition: ftface.h:53
Definition: scface_ps.h:197
Definition: scface_ps.h:110