File: TypeFace.cpp

package info (click to toggle)
pymol 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,288 kB
  • sloc: cpp: 476,472; python: 76,538; ansic: 29,510; javascript: 6,792; sh: 47; makefile: 24
file content (186 lines) | stat: -rw-r--r-- 5,091 bytes parent folder | download | duplicates (2)
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

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2005 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/

#include "MemoryDebug.h"
#include "Base.h"

#include "TypeFace.h"

#ifdef _PYMOL_FREETYPE

#include <ft2build.h>
#include FT_FREETYPE_H

struct _CType {
  FT_Library library;
};

struct _CTypeFace {
  PyMOLGlobals *G;
  FT_Face Face;
  float LastSize;
};

int TypeFaceCharacterNew(CTypeFace * I, CharFngrprnt * fprnt, float size)
{
  FT_GlyphSlot slot = I->Face->glyph;   /* a small shortcut */
  if(I->LastSize != size) {
    I->LastSize = size;
    FT_Set_Char_Size(I->Face,   /* handle to face object */
                     0,         /* char_width in 1/64th of points */
                     (int) (size * 64), /* char_height in 1/64th of points */
                     72,        /* horizontal device resolution */
                     72);       /* vertical device resolution */

  }
  if(!FT_Load_Char(I->Face, fprnt->u.i.ch, FT_LOAD_RENDER))
    return CharacterNewFromBytemap(I->G,
                                   slot->bitmap.width,
                                   slot->bitmap.rows,
                                   -slot->bitmap.pitch,
                                   slot->bitmap.buffer +
                                   ((slot->bitmap.rows - 1) * slot->bitmap.pitch),
                                   (float) -slot->bitmap_left,
                                   (float) slot->bitmap.rows - slot->bitmap_top,
                                   slot->advance.x / 64.0F, fprnt);
  else
    return 0;
}

CTypeFace *TypeFaceLoad(PyMOLGlobals * G, unsigned char *dat, unsigned int len)
{
  CType *I = G->Type;
  int ok = true;
  CTypeFace *result = pymol::calloc<CTypeFace>(1);
  if(result) {
    FT_Error error = FT_New_Memory_Face(I->library, dat, len, 0, &result->Face);
    result->G = G;
    if(error)
      ok = false;
    else {
      result->LastSize = 12.0F; /* default size */
      error = FT_Set_Char_Size(result->Face,    /* handle to face object */
                               0,       /* char_width in 1/64th of points */
                               (int) (result->LastSize * 64),   /* char_height in 1/64th of points */
                               72,      /* horizontal device resolution */
                               72);     /* vertical device resolution */

      if(error) {
        ok = false;
      } else {
        error = FT_Select_Charmap(result->Face, FT_ENCODING_UNICODE);
      }
    }
  }
  if(!ok) {
    FreeP(result);
  }
  return result;
}

float TypeFaceGetKerning(CTypeFace * I, unsigned int last, unsigned int current,
                         float size)
{
  float result = 0.0F;
  FT_UInt glyph_index, previous;
  /*  FT_Bool use_kerning = FT_HAS_KERNING( I->Face ); */
  FT_Bool use_kerning = 1;
  if(I->LastSize != size) {
    I->LastSize = size;
    FT_Set_Char_Size(I->Face,   /* handle to face object */
                     0,         /* char_width in 1/64th of points */
                     (int) (size * 64), /* char_height in 1/64th of points */
                     72,        /* horizontal device resolution */
                     72);       /* vertical device resolution */

  }
  if(use_kerning) {
    previous = FT_Get_Char_Index(I->Face, last);
    glyph_index = FT_Get_Char_Index(I->Face, current);
    if(previous && glyph_index) {
      FT_Vector delta;
      FT_Get_Kerning(I->Face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
      result = delta.x / 64.0F;
    }
  }
  return result;
}

void TypeFaceFree(CTypeFace * I)
{
  FT_Done_Face(I->Face);
  FreeP(I);
}

int TypeInit(PyMOLGlobals * G)
{
  CType *I;
  if((I = (G->Type = pymol::calloc<CType>(1)))) {
    FT_Error error = FT_Init_FreeType(&I->library);
    return !error;
  }
  return 0;
}

void TypeFree(PyMOLGlobals * G)
{
  CType *I = G->Type;
  FT_Done_FreeType(I->library);
  FreeP(G->Type);
}

float TypeFaceGetDescender(CTypeFace * I){
  return fabs(I->Face->descender/(float)I->Face->height);
}

#else

int TypeFaceCharacterNew(CTypeFace * I, CharFngrprnt * fprnt, float size)
{
  return 0;
}

float TypeFaceGetKerning(CTypeFace * I,
                         unsigned int last, unsigned int current, float size)
{
  return 0.0F;
}

CTypeFace *TypeFaceLoad(PyMOLGlobals * G, unsigned char *dat, unsigned int len)
{
  return NULL;
}

void TypeFaceFree(CTypeFace * face)
{
}

int TypeInit(PyMOLGlobals * G)
{
  return 1;
}

void TypeFree(PyMOLGlobals * G)
{
}

float TypeFaceGetDescender(CTypeFace * I){
  return 0.f;
}

#endif