00001 class TtfTable;
00002 class GdcTtf;
00003
00004 class LgiClass GdcFontMetrics {
00005 public:
00006 long tmHeight;
00007 long tmAscent;
00008 long tmDescent;
00009 long tmInternalLeading;
00010 long tmExternalLeading;
00011 long tmAveCharWidth;
00012 long tmMaxCharWidth;
00013 long tmWeight;
00014 long tmOverhang;
00015 long tmDigitizedAspectX;
00016 long tmDigitizedAspectY;
00017 char tmFirstChar;
00018 char tmLastChar;
00019 char tmDefaultChar;
00020 char tmBreakChar;
00021 char tmItalic;
00022 char tmUnderlined;
00023 char tmStruckOut;
00024 char tmPitchAndFamily;
00025 char tmCharSet;
00026 };
00027
00028 class LgiClass GTypeFace {
00029 protected:
00030 uint Flags;
00031 uint Height;
00032 uint Width;
00033 uint Escapement;
00034 uint Orientation;
00035 uint Weight;
00036 uchar CharSet;
00037 uchar OutPrecision;
00038 uchar ClipPrecision;
00039 uchar Quality;
00040 uchar PitchAndFamily;
00041 char FaceName[33];
00042
00043
00044 COLOUR ForeCol;
00045 COLOUR BackCol;
00046 bool Trans;
00047
00048 int TabSize;
00049
00050 public:
00051 GTypeFace()
00052 {
00053 ForeCol = 0;
00054 BackCol = 0xFFFFFF;
00055 Trans = FALSE;
00056 TabSize = 0;
00057 }
00058
00059 virtual ~GTypeFace() {}
00060
00061 virtual void Colour(COLOUR Fore, COLOUR Back = 0xFFFFFFFF) { ForeCol = Fore; BackCol = Back; }
00062 void Fore(COLOUR f) { ForeCol = f; }
00063 COLOUR Fore() { return ForeCol; }
00064 void Back(COLOUR b) { BackCol = b; }
00065 COLOUR Back() { return BackCol; }
00066 void Transparent(bool t) { Trans = t; }
00067
00068 void SetTabs(int tabsize)
00069 {
00070 TabSize = tabsize;
00071 }
00072
00073 virtual bool Load(GFile &F) { return FALSE; }
00074 virtual bool Save(GFile &F) { return FALSE; }
00075 };
00076
00077 typedef unsigned long TTF_FIXED;
00078 typedef signed short TTF_FWORD;
00079 typedef unsigned short TTF_UFWORD;
00080 typedef signed short TTF_F2DOT14;
00081
00082 class LgiClass TtfFileHeader {
00083 public:
00084 TTF_FIXED Version;
00085 ushort NumTables;
00086 ushort SearchRange;
00087 ushort EntrySelector;
00088 ushort RangeShift;
00089
00090 bool Read(GFile &F);
00091 bool Write(GFile &F);
00092 void Dump();
00093 };
00094
00095 class LgiClass TtfTable {
00096 public:
00097 char Tag[4];
00098 ulong CheckSum;
00099 ulong Offset;
00100 ulong Length;
00101
00102 void *Table;
00103
00104 virtual bool Read(GFile &F);
00105 virtual bool Write(GFile &F);
00106 virtual void Dump();
00107 };
00108
00109 class LgiClass TtfObj {
00110 public:
00111 GdcTtf *Parent;
00112 TtfObj() { Parent = 0; }
00113 void *FindTag(char *t);
00114 };
00115
00116 class LgiClass TtfHeader : public TtfObj {
00117 public:
00118 ulong Version;
00119 ulong Revision;
00120 ulong CheckSumAdjustment;
00121 ulong MagicNumber;
00122 ushort Flags;
00123
00124
00125
00126
00127
00128 ushort UnitsPerEm;
00129 quad InternationalDate;
00130 quad Modified;
00131 TTF_FWORD xMin;
00132 TTF_FWORD yMin;
00133 TTF_FWORD xMax;
00134 TTF_FWORD yMax;
00135 ushort MacStyle;
00136
00137
00138 ushort LowestRecPPEM;
00139 short FontDirectionHint;
00140
00141
00142
00143
00144 short IndexToLocFormat;
00145
00146 short GlyphDataFormat;
00147
00148 bool Read(GFile &F);
00149 bool Write(GFile &F);
00150 void Dump();
00151 };
00152
00153 class LgiClass TtfMaxProfile : public TtfObj {
00154 public:
00155 ulong Version;
00156 ushort NumGlyphs;
00157 ushort MaxPoints;
00158 ushort MaxContours;
00159 ushort MaxCompositePoints;
00160 ushort MaxCompositeContours;
00161 ushort MaxZones;
00162 ushort MaxTwilightPoints;
00163 ushort MaxStorage;
00164 ushort MaxFunctionDefs;
00165 ushort MaxInstructionDefs;
00166 ushort MaxStackElements;
00167 ushort MaxSizeOfInstructions;
00168 ushort MaxComponentElements;
00169 ushort MaxComponentDepth;
00170
00171 bool Read(GFile &F);
00172 bool Write(GFile &F);
00173 void Dump();
00174 };
00175
00176 class LgiClass TtfLocation : public TtfObj {
00177
00178 int Entries;
00179 int Type;
00180 void *Data;
00181
00182 public:
00183 TtfLocation();
00184 virtual ~TtfLocation();
00185
00186 int operator [](int i);
00187 bool Read(GFile &F);
00188 bool Write(GFile &F);
00189 void Dump();
00190 };
00191
00192 class LgiClass DataList {
00193
00194 int Alloc;
00195
00196 public:
00197 int Size;
00198 double *Data;
00199
00200 DataList()
00201 {
00202 Size = 0;
00203 Alloc = 0;
00204 Data = 0;
00205 }
00206
00207 virtual ~DataList()
00208 {
00209 DeleteArray(Data);
00210 }
00211
00212 void AddValue(double n)
00213 {
00214 if (Size >= Alloc)
00215 {
00216 int NewAlloc = (Size + 16) & (~0xF);
00217 double *Temp = new double[NewAlloc];
00218 if (Temp)
00219 {
00220 memcpy(Temp, Data, sizeof(double)*Size);
00221 DeleteArray(Data);
00222 Data = Temp;
00223 Alloc = NewAlloc;
00224 }
00225 else
00226 {
00227 return;
00228 }
00229 }
00230
00231 if (Size > 0)
00232 {
00233 for (int i=0; i<Size; i++)
00234 {
00235 if (Data[i] > n)
00236 {
00237 memmove(Data + i + 1, Data + i, sizeof(double)*(Size-i));
00238 Data[i] = n;
00239 Size++;
00240 return;
00241 }
00242 }
00243 Data[Size] = n;
00244 }
00245 else
00246 {
00247 Data[0] = n;
00248 }
00249
00250 Size++;
00251 }
00252 };
00253
00254 class LgiClass TtfContour {
00255
00256 class CPt {
00257 public:
00258 double x, y;
00259 };
00260
00261
00262 int *x, *y;
00263 uchar *Flags;
00264 int XOff, YOff;
00265
00266
00267 int Points;
00268 int Alloc;
00269 CPt *Point;
00270
00271 bool SetPoints(int p);
00272 void Bezier(CPt *p, double Threshold = 0.5);
00273
00274 public:
00275 TtfContour();
00276 ~TtfContour();
00277
00278 void Setup(int *x, int *y, uchar *Flags, int MinX, int MinY);
00279 bool Create(int Pts, double XScale, double YScale);
00280 bool RasterX(int Size, DataList *List);
00281 bool RasterY(int Size, DataList *List);
00282
00283 void DebugDraw(GSurface *pDC, int Sx, int Sy);
00284 };
00285
00286 class LgiClass TtfGlyph : public TtfObj {
00287
00288 int Points;
00289 uchar *Temp;
00290
00291 public:
00292 TtfGlyph();
00293 ~TtfGlyph();
00294
00295 short Contours;
00296
00297 TTF_FWORD xMin;
00298 TTF_FWORD yMin;
00299 TTF_FWORD xMax;
00300 TTF_FWORD yMax;
00301
00302
00303 ushort *EndPtsOfContours;
00304 ushort InstructionLength;
00305 uchar *Instructions;
00306 uchar *Flags;
00307 int *X;
00308 int *Y;
00309
00310
00311
00312
00313 int GetX() { return xMax - xMin; }
00314 int GetY() { return yMax - yMin; }
00315
00316 bool Read(GFile &F);
00317 bool Write(GFile &F);
00318 void Dump();
00319 void Draw(GSurface *pDC, int x, int y, int Scale);
00320 int DrawEm(GSurface *pDC, int X, int Y, int EmUnits, double PixelsPerEm);
00321 bool Rasterize( GSurface *pDC,
00322 GRect *pDest,
00323 double xppem,
00324 double yppem,
00325 int BaseLine);
00326 };
00327
00328 class LgiClass TtfMap {
00329 public:
00330 TtfMap() {}
00331 virtual ~TtfMap() {}
00332 virtual int operator[](int i) { return 0; }
00333 virtual bool Read(GFile &F) { return FALSE; }
00334 virtual bool Write(GFile &F) { return FALSE; }
00335 virtual void Dump() {}
00336 };
00337
00338 class LgiClass TtfCMapTable {
00339 public:
00340 TtfCMapTable();
00341 ~TtfCMapTable();
00342
00343 ushort PlatformID;
00344 ushort EncodingID;
00345 ulong Offset;
00346
00347 ushort Format;
00348 TtfMap *Map;
00349
00350 bool Read(GFile &F);
00351 bool Write(GFile &F);
00352 void Dump();
00353 };
00354
00355 class LgiClass TtfCMapByteEnc : public TtfMap {
00356
00357 ushort Format;
00358 ushort Length;
00359 ushort Version;
00360 uchar Map[256];
00361
00362 public:
00363 int operator[](int i);
00364 bool Read(GFile &F);
00365 bool Write(GFile &F);
00366 void Dump();
00367 };
00368
00369 class LgiClass TtfCMapHighByte : public TtfMap {
00370
00371 class SubHeader {
00372 public:
00373 ushort FirstCode;
00374 ushort EntryCount;
00375 short IdDelta;
00376 ushort IdRangeOffset;
00377 };
00378
00379 ushort Format;
00380 ushort Length;
00381 ushort Version;
00382 ushort subHeaderKeys[256];
00383
00384 SubHeader *Header;
00385 ushort *GlyphIndexArray;
00386
00387 public:
00388 int operator[](int i) { return 0; }
00389 bool Read(GFile &F) { return FALSE; }
00390 bool Write(GFile &F) { return FALSE; }
00391 void Dump() {}
00392 };
00393
00394 class LgiClass TtfCMapSegDelta : public TtfMap {
00395
00396 int SegCount;
00397 int IdCount;
00398
00399 ushort Format;
00400 ushort Length;
00401 ushort Version;
00402 ushort SegCountX2;
00403 ushort SearchRange;
00404 ushort EntrySelector;
00405 ushort RangeShift;
00406 ushort *EndCount;
00407 ushort *StartCount;
00408 short *IdDelta;
00409 ushort *IdRangeOffset;
00410
00411
00412
00413 public:
00414 TtfCMapSegDelta();
00415 ~TtfCMapSegDelta();
00416
00417 int operator[](int i);
00418 bool Read(GFile &F);
00419 bool Write(GFile &F);
00420 void Dump();
00421 };
00422
00423 class LgiClass TtfCMap : public TtfObj {
00424
00425 TtfMap *Fravorite;
00426
00427 public:
00428 ushort Version;
00429 ushort Tables;
00430 TtfCMapTable *Table;
00431
00432 TtfCMap();
00433 ~TtfCMap();
00434 int operator[](int i) { return (Fravorite) ? (*Fravorite)[i] : 0; }
00435 bool Read(GFile &F);
00436 bool Write(GFile &F);
00437 void Dump();
00438 };
00439
00440 class LgiClass TtfRaster : public TtfObj {
00441
00442 friend class GSurface;
00443
00444 int XPixelsPerEm;
00445 int YPixelsPerEm;
00446 int Glyphs;
00447
00448 public:
00449 int *BaseLine;
00450 GRect *pSource;
00451 GSurface *pDC;
00452
00453 TtfRaster();
00454 ~TtfRaster();
00455
00456 double GetXPixelsPerEm() { return XPixelsPerEm; }
00457 double GetYPixelsPerEm() { return YPixelsPerEm; }
00458 bool Rasterize(double xPPEm, double yPPEm, int OverSample);
00459 int DrawChar(GSurface *pDC, int x, int y, int Char);
00460 };
00461
00462 class LgiClass GdcTtf : public GTypeFace {
00463
00464 friend class TtfObj;
00465
00466 protected:
00467 int Tables;
00468 TtfTable *TableList;
00469 TtfTable *FindTag(char *t);
00470 TtfTable *SeekTag(char *t, GFile *F);
00471
00472 TtfHeader Header;
00473 TtfMaxProfile Profile;
00474 TtfLocation Location;
00475 TtfGlyph **Glyph;
00476 TtfCMap Map;
00477
00478 int Rasters;
00479 TtfRaster **Raster;
00480 TtfRaster *FindRaster(int Point, int XDpi = 96, int YDpi = -1);
00481
00482 public:
00483 GdcTtf();
00484 virtual ~GdcTtf();
00485
00486 virtual bool Load(GFile &F);
00487 virtual bool Save(GFile &F);
00488 virtual bool Rasterize( int Point,
00489 int StyleFlags,
00490 int OverSample = 2,
00491 int XDpi = 96,
00492 int YDpi = -1);
00493
00494 void Test(GSurface *pDC);
00495 virtual void Size(int *x, int *y, char *Str, int Len = -1, int Flags = 0);
00496 int X(char *Str, int Len = -1, int Flags = 0);
00497 int Y(char *Str, int Len = -1, int Flags = 0);
00498
00499
00500 virtual bool SelectPoint(int Pt) { return FALSE; }
00501 virtual void Text(GSurface *pDC, int x, int y, char *Str, int Len = -1);
00502 };
00503
00504 #define LFONT_LIGHT FW_LIGHT
00505 #define LFONT_NORMAL FW_NORMAL
00506 #define LFONT_BOLD FW_BOLD
00507
00508 class LgiClass GFont : public GdcTtf {
00509
00510 HFONT hFont;
00511 float *Widths;
00512
00513 void SetWidths();
00514
00515 public:
00516 GFont();
00517 ~GFont();
00518
00519 HFONT Handle() { return hFont; }
00520
00521 bool Load(GFile &F) { return TRUE; }
00522 bool Save(GFile &F) { return FALSE; }
00523
00524 bool Create(char *Face,
00525 int Height,
00526 int Width = 0,
00527 int Weight = LFONT_NORMAL,
00528 uint32 Italic = false,
00529 uint32 Underline = false,
00530 uint32 StrikeOut = false,
00531 uint32 CharSet = ANSI_CHARSET,
00532 int Escapement = 0,
00533 int Orientation = 0,
00534 uint32 OutputPrecision = OUT_DEFAULT_PRECIS,
00535 uint32 ClipPrecision = CLIP_DEFAULT_PRECIS,
00536 uint32 Quality = ANTIALIASED_QUALITY,
00537 uint32 PitchAndFamily = DEFAULT_PITCH);
00538
00539 bool CreateFont(LOGFONT *LogFont);
00540
00541 void Text( GSurface *pDC,
00542 int x, int y,
00543 char *Str,
00544 int Len = -1,
00545 GRect *r = NULL);
00546 void Size(int *x, int *y, char *Str, int Len = -1, int Flags = 0);
00547 int CharAt(int x, char *Str, int Len = -1);
00548 };
00549
00550
00551