File: vtkFontConfigFreeTypeTools.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (213 lines) | stat: -rw-r--r-- 6,454 bytes parent folder | download
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkFontConfigFreeTypeTools.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkFontConfigFreeTypeTools.h"

#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkTextProperty.h"

#ifndef _MSC_VER
# include <stdint.h>
#endif

#include <fontconfig/fontconfig.h>

vtkStandardNewMacro(vtkFontConfigFreeTypeTools)

namespace
{
// The FreeType face requester callback:
static FT_Error vtkFontConfigFreeTypeToolsFaceRequester(FTC_FaceID face_id,
                                        FT_Library lib,
                                        FT_Pointer request_data,
                                        FT_Face* face)
{
  // Get a pointer to the current vtkFontConfigFreeTypeTools object
  vtkFontConfigFreeTypeTools *self =
    reinterpret_cast<vtkFontConfigFreeTypeTools*>(request_data);

  // Map the ID to a text property
  vtkSmartPointer<vtkTextProperty> tprop =
      vtkSmartPointer<vtkTextProperty>::New();
  self->MapIdToTextProperty(reinterpret_cast<intptr_t>(face_id), tprop);

  bool faceIsSet =
      self->GetForceCompiledFonts() || tprop->GetFontFamily() == VTK_FONT_FILE
      ? false : self->LookupFaceFontConfig(tprop, lib, face);

  // Fall back to compiled fonts if lookup fails/compiled fonts are forced:
  if (!faceIsSet)
  {
    faceIsSet = self->Superclass::LookupFace(tprop, lib, face);
  }

  if (!faceIsSet)
  {
    return static_cast<FT_Error>(1);
  }

  if ( tprop->GetOrientation() != 0.0 )
  {
    // FreeType documentation says that the transform should not be set
    // but we cache faces also by transform, so that there is a unique
    // (face, orientation) cache entry
    FT_Matrix matrix;
    float angle = vtkMath::RadiansFromDegrees( tprop->GetOrientation() );
    matrix.xx = (FT_Fixed)( cos(angle) * 0x10000L);
    matrix.xy = (FT_Fixed)(-sin(angle) * 0x10000L);
    matrix.yx = (FT_Fixed)( sin(angle) * 0x10000L);
    matrix.yy = (FT_Fixed)( cos(angle) * 0x10000L);
    FT_Set_Transform(*face, &matrix, NULL);
  }

  return static_cast<FT_Error>(0);
}
} // end anon namespace

void vtkFontConfigFreeTypeTools::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

vtkFontConfigFreeTypeTools::vtkFontConfigFreeTypeTools()
{
}

vtkFontConfigFreeTypeTools::~vtkFontConfigFreeTypeTools()
{
}

FT_Error vtkFontConfigFreeTypeTools::CreateFTCManager()
{
  return FTC_Manager_New(*this->GetLibrary(),
                         this->MaximumNumberOfFaces,
                         this->MaximumNumberOfSizes,
                         this->MaximumNumberOfBytes,
                         vtkFontConfigFreeTypeToolsFaceRequester,
                         static_cast<FT_Pointer>(this),
                         this->CacheManager);
}

bool vtkFontConfigFreeTypeTools::LookupFaceFontConfig(vtkTextProperty *tprop,
                                                      FT_Library lib,
                                                      FT_Face *face)
{
  if (!FcInit())
  {
    return false;
  }

  // Query tprop
  const FcChar8 *family = reinterpret_cast<const FcChar8*>(
        tprop->GetFontFamilyAsString());
  const double pointSize = static_cast<double>(tprop->GetFontSize());
  const int weight = tprop->GetBold() ? FC_WEIGHT_BOLD : FC_WEIGHT_MEDIUM;
  const int slant = tprop->GetItalic() ? FC_SLANT_ITALIC : FC_SLANT_ROMAN;

  // Build pattern
  FcPattern *pattern = FcPatternCreate();
  FcPatternAddString(pattern, FC_FAMILY, family);
  FcPatternAddDouble(pattern, FC_SIZE, pointSize);
  FcPatternAddInteger(pattern, FC_WEIGHT, weight);
  FcPatternAddInteger(pattern, FC_SLANT, slant);
  FcPatternAddBool(pattern, FC_SCALABLE, true);

  // Prefer fonts that have at least greek characters:
  FcCharSet *charSet = FcCharSetCreate();
  FcCharSetAddChar(charSet, static_cast<FcChar32>(948)); // lowercase delta
  FcPatternAddCharSet(pattern, FC_CHARSET, charSet);

  // Replace common font names, e.g. arial, times, etc -> sans, serif, etc
  FcConfigSubstitute(NULL, pattern, FcMatchPattern);

  // Fill in any missing defaults:
  FcDefaultSubstitute(pattern);

  // Match pattern
  FcResult result;
  FcFontSet *fontMatches = FcFontSort(NULL, pattern, false, NULL, &result);
  FcPatternDestroy(pattern);
  pattern = NULL;
  if (!fontMatches || fontMatches->nfont == 0)
  {
    if (fontMatches)
    {
      FcFontSetDestroy(fontMatches);
    }
    return false;
  }

  // Grab the first match that is scalable -- even though we've requested
  // scalable fonts in the match, FC seems to not weigh that option very heavily
  FcPattern *match = NULL;
  for (int i = 0; i < fontMatches->nfont; ++i)
  {
    match = fontMatches->fonts[i];

    // Ensure that the match is scalable
    FcBool isScalable;
    if (FcPatternGetBool(match, FC_SCALABLE, 0, &isScalable) != FcResultMatch ||
        !isScalable)
    {
      continue;
    }

    FcCharSet *currentFontCharSet;
    if (FcPatternGetCharSet(match, FC_CHARSET, 0, &currentFontCharSet)
        != FcResultMatch ||
        FcCharSetIntersectCount(charSet, currentFontCharSet) == 0)
    {
      continue;
    }

    break;
  }

  if (!match)
  {
    FcFontSetDestroy(fontMatches);
    FcCharSetDestroy(charSet);
    return false;
  }

  // Get filename. Do not free the filename string -- it is owned by FcPattern
  // "match". Likewise, do not use the filename after match is freed.
  FcChar8 *filename;
  result = FcPatternGetString(match, FC_FILE, 0, &filename);

  FT_Error error = FT_New_Face(lib, reinterpret_cast<const char*>(filename), 0,
                               face);

  if (!error)
  {
    vtkDebugWithObjectMacro(vtkFreeTypeTools::GetInstance(),
                            <<"Loading system font: "
                            << reinterpret_cast<const char*>(filename));
  }

  FcCharSetDestroy(charSet);
  charSet = NULL;
  FcFontSetDestroy(fontMatches);
  fontMatches = NULL;

  if (error)
  {
    return false;
  }

  return true;
}