@shqld/canvas 2.11.2-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +600 -0
- package/binding.gyp +230 -0
- package/browser.js +35 -0
- package/index.js +94 -0
- package/lib/DOMMatrix.js +620 -0
- package/lib/bindings.js +80 -0
- package/lib/canvas.js +113 -0
- package/lib/context2d.js +14 -0
- package/lib/image.js +96 -0
- package/lib/jpegstream.js +41 -0
- package/lib/parse-font.js +101 -0
- package/lib/pattern.js +17 -0
- package/lib/pdfstream.js +35 -0
- package/lib/pngstream.js +42 -0
- package/package.json +71 -0
- package/scripts/install.js +24 -0
- package/src/Backends.cc +18 -0
- package/src/Backends.h +10 -0
- package/src/Canvas.cc +965 -0
- package/src/Canvas.h +96 -0
- package/src/CanvasError.h +23 -0
- package/src/CanvasGradient.cc +123 -0
- package/src/CanvasGradient.h +22 -0
- package/src/CanvasPattern.cc +136 -0
- package/src/CanvasPattern.h +37 -0
- package/src/CanvasRenderingContext2d.cc +3360 -0
- package/src/CanvasRenderingContext2d.h +225 -0
- package/src/Image.cc +1434 -0
- package/src/Image.h +127 -0
- package/src/ImageData.cc +146 -0
- package/src/ImageData.h +27 -0
- package/src/JPEGStream.h +167 -0
- package/src/PNG.h +292 -0
- package/src/Point.h +11 -0
- package/src/Util.h +9 -0
- package/src/backend/Backend.cc +112 -0
- package/src/backend/Backend.h +69 -0
- package/src/backend/ImageBackend.cc +74 -0
- package/src/backend/ImageBackend.h +26 -0
- package/src/backend/PdfBackend.cc +53 -0
- package/src/backend/PdfBackend.h +24 -0
- package/src/backend/SvgBackend.cc +61 -0
- package/src/backend/SvgBackend.h +24 -0
- package/src/bmp/BMPParser.cc +457 -0
- package/src/bmp/BMPParser.h +60 -0
- package/src/bmp/LICENSE.md +24 -0
- package/src/closure.cc +26 -0
- package/src/closure.h +81 -0
- package/src/color.cc +779 -0
- package/src/color.h +30 -0
- package/src/dll_visibility.h +20 -0
- package/src/init.cc +94 -0
- package/src/register_font.cc +408 -0
- package/src/register_font.h +7 -0
- package/types/index.d.ts +484 -0
- package/util/has_lib.js +119 -0
- package/util/win_jpeg_lookup.js +21 -0
package/src/color.h
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|
2
|
+
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include <stdint.h> // node < 7 uses libstdc++ on macOS which lacks complete c++11
|
|
6
|
+
#include <cstdlib>
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* RGBA struct.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
typedef struct {
|
|
13
|
+
double r, g, b, a;
|
|
14
|
+
} rgba_t;
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Prototypes.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
rgba_t
|
|
21
|
+
rgba_create(uint32_t rgba);
|
|
22
|
+
|
|
23
|
+
int32_t
|
|
24
|
+
rgba_from_string(const char *str, short *ok);
|
|
25
|
+
|
|
26
|
+
void
|
|
27
|
+
rgba_to_string(rgba_t rgba, char *buf, size_t len);
|
|
28
|
+
|
|
29
|
+
void
|
|
30
|
+
rgba_inspect(int32_t rgba);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#ifndef DLL_PUBLIC
|
|
2
|
+
|
|
3
|
+
#if defined _WIN32
|
|
4
|
+
#ifdef __GNUC__
|
|
5
|
+
#define DLL_PUBLIC __attribute__ ((dllexport))
|
|
6
|
+
#else
|
|
7
|
+
#define DLL_PUBLIC __declspec(dllexport)
|
|
8
|
+
#endif
|
|
9
|
+
#define DLL_LOCAL
|
|
10
|
+
#else
|
|
11
|
+
#if __GNUC__ >= 4
|
|
12
|
+
#define DLL_PUBLIC __attribute__ ((visibility ("default")))
|
|
13
|
+
#define DLL_LOCAL __attribute__ ((visibility ("hidden")))
|
|
14
|
+
#else
|
|
15
|
+
#define DLL_PUBLIC
|
|
16
|
+
#define DLL_LOCAL
|
|
17
|
+
#endif
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
#endif
|
package/src/init.cc
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|
2
|
+
|
|
3
|
+
#include <cstdio>
|
|
4
|
+
#include <pango/pango.h>
|
|
5
|
+
|
|
6
|
+
#include <cairo.h>
|
|
7
|
+
#if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 10, 0)
|
|
8
|
+
// CAIRO_FORMAT_RGB16_565: undeprecated in v1.10.0
|
|
9
|
+
// CAIRO_STATUS_INVALID_SIZE: v1.10.0
|
|
10
|
+
// CAIRO_FORMAT_INVALID: v1.10.0
|
|
11
|
+
// Lots of the compositing operators: v1.10.0
|
|
12
|
+
// JPEG MIME tracking: v1.10.0
|
|
13
|
+
// Note: CAIRO_FORMAT_RGB30 is v1.12.0 and still optional
|
|
14
|
+
#error("cairo v1.10.0 or later is required")
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
#include "Backends.h"
|
|
18
|
+
#include "Canvas.h"
|
|
19
|
+
#include "CanvasGradient.h"
|
|
20
|
+
#include "CanvasPattern.h"
|
|
21
|
+
#include "CanvasRenderingContext2d.h"
|
|
22
|
+
#include "Image.h"
|
|
23
|
+
#include "ImageData.h"
|
|
24
|
+
|
|
25
|
+
#include <ft2build.h>
|
|
26
|
+
#include FT_FREETYPE_H
|
|
27
|
+
|
|
28
|
+
using namespace v8;
|
|
29
|
+
|
|
30
|
+
// Compatibility with Visual Studio versions prior to VS2015
|
|
31
|
+
#if defined(_MSC_VER) && _MSC_VER < 1900
|
|
32
|
+
#define snprintf _snprintf
|
|
33
|
+
#endif
|
|
34
|
+
|
|
35
|
+
NAN_MODULE_INIT(init) {
|
|
36
|
+
Backends::Initialize(target);
|
|
37
|
+
Canvas::Initialize(target);
|
|
38
|
+
Image::Initialize(target);
|
|
39
|
+
ImageData::Initialize(target);
|
|
40
|
+
Context2d::Initialize(target);
|
|
41
|
+
Gradient::Initialize(target);
|
|
42
|
+
Pattern::Initialize(target);
|
|
43
|
+
|
|
44
|
+
Nan::Set(target, Nan::New<String>("cairoVersion").ToLocalChecked(), Nan::New<String>(cairo_version_string()).ToLocalChecked()).Check();
|
|
45
|
+
#ifdef HAVE_JPEG
|
|
46
|
+
|
|
47
|
+
#ifndef JPEG_LIB_VERSION_MAJOR
|
|
48
|
+
#ifdef JPEG_LIB_VERSION
|
|
49
|
+
#define JPEG_LIB_VERSION_MAJOR (JPEG_LIB_VERSION / 10)
|
|
50
|
+
#else
|
|
51
|
+
#define JPEG_LIB_VERSION_MAJOR 0
|
|
52
|
+
#endif
|
|
53
|
+
#endif
|
|
54
|
+
|
|
55
|
+
#ifndef JPEG_LIB_VERSION_MINOR
|
|
56
|
+
#ifdef JPEG_LIB_VERSION
|
|
57
|
+
#define JPEG_LIB_VERSION_MINOR (JPEG_LIB_VERSION % 10)
|
|
58
|
+
#else
|
|
59
|
+
#define JPEG_LIB_VERSION_MINOR 0
|
|
60
|
+
#endif
|
|
61
|
+
#endif
|
|
62
|
+
|
|
63
|
+
char jpeg_version[10];
|
|
64
|
+
static bool minor_gt_0 = JPEG_LIB_VERSION_MINOR > 0;
|
|
65
|
+
if (minor_gt_0) {
|
|
66
|
+
snprintf(jpeg_version, 10, "%d%c", JPEG_LIB_VERSION_MAJOR, JPEG_LIB_VERSION_MINOR + 'a' - 1);
|
|
67
|
+
} else {
|
|
68
|
+
snprintf(jpeg_version, 10, "%d", JPEG_LIB_VERSION_MAJOR);
|
|
69
|
+
}
|
|
70
|
+
Nan::Set(target, Nan::New<String>("jpegVersion").ToLocalChecked(), Nan::New<String>(jpeg_version).ToLocalChecked()).Check();
|
|
71
|
+
#endif
|
|
72
|
+
|
|
73
|
+
#ifdef HAVE_GIF
|
|
74
|
+
#ifndef GIF_LIB_VERSION
|
|
75
|
+
char gif_version[10];
|
|
76
|
+
snprintf(gif_version, 10, "%d.%d.%d", GIFLIB_MAJOR, GIFLIB_MINOR, GIFLIB_RELEASE);
|
|
77
|
+
Nan::Set(target, Nan::New<String>("gifVersion").ToLocalChecked(), Nan::New<String>(gif_version).ToLocalChecked()).Check();
|
|
78
|
+
#else
|
|
79
|
+
Nan::Set(target, Nan::New<String>("gifVersion").ToLocalChecked(), Nan::New<String>(GIF_LIB_VERSION).ToLocalChecked()).Check();
|
|
80
|
+
#endif
|
|
81
|
+
#endif
|
|
82
|
+
|
|
83
|
+
#ifdef HAVE_RSVG
|
|
84
|
+
Nan::Set(target, Nan::New<String>("rsvgVersion").ToLocalChecked(), Nan::New<String>(LIBRSVG_VERSION).ToLocalChecked()).Check();
|
|
85
|
+
#endif
|
|
86
|
+
|
|
87
|
+
Nan::Set(target, Nan::New<String>("pangoVersion").ToLocalChecked(), Nan::New<String>(PANGO_VERSION_STRING).ToLocalChecked()).Check();
|
|
88
|
+
|
|
89
|
+
char freetype_version[10];
|
|
90
|
+
snprintf(freetype_version, 10, "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
|
|
91
|
+
Nan::Set(target, Nan::New<String>("freetypeVersion").ToLocalChecked(), Nan::New<String>(freetype_version).ToLocalChecked()).Check();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
NODE_MODULE(canvas, init);
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
#include "register_font.h"
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <pango/pangocairo.h>
|
|
5
|
+
#include <pango/pango-fontmap.h>
|
|
6
|
+
#include <pango/pango.h>
|
|
7
|
+
|
|
8
|
+
#ifdef __APPLE__
|
|
9
|
+
#include <CoreText/CoreText.h>
|
|
10
|
+
#elif defined(_WIN32)
|
|
11
|
+
#include <windows.h>
|
|
12
|
+
#include <memory>
|
|
13
|
+
#else
|
|
14
|
+
#include <fontconfig/fontconfig.h>
|
|
15
|
+
#include <pango/pangofc-fontmap.h>
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
#include <ft2build.h>
|
|
19
|
+
#include FT_FREETYPE_H
|
|
20
|
+
#include FT_TRUETYPE_TABLES_H
|
|
21
|
+
#include FT_SFNT_NAMES_H
|
|
22
|
+
#include FT_TRUETYPE_IDS_H
|
|
23
|
+
#ifndef FT_SFNT_OS2
|
|
24
|
+
#define FT_SFNT_OS2 ft_sfnt_os2
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
// OSX seems to read the strings in MacRoman encoding and ignore Unicode entries.
|
|
28
|
+
// You can verify this by opening a TTF with both Unicode and Macroman on OSX.
|
|
29
|
+
// It uses the MacRoman name, while Fontconfig and Windows use Unicode
|
|
30
|
+
#ifdef __APPLE__
|
|
31
|
+
#define PREFERRED_PLATFORM_ID TT_PLATFORM_MACINTOSH
|
|
32
|
+
#define PREFERRED_ENCODING_ID TT_MAC_ID_ROMAN
|
|
33
|
+
#else
|
|
34
|
+
#define PREFERRED_PLATFORM_ID TT_PLATFORM_MICROSOFT
|
|
35
|
+
#define PREFERRED_ENCODING_ID TT_MS_ID_UNICODE_CS
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
// With PangoFcFontMaps (the pango font module on Linux) we're able to add a
|
|
39
|
+
// hook that lets us get perfect matching. Tie the conditions for enabling that
|
|
40
|
+
// feature to one variable
|
|
41
|
+
#if !defined(__APPLE__) && !defined(_WIN32) && PANGO_VERSION_CHECK(1, 47, 0)
|
|
42
|
+
#define PERFECT_MATCHES_ENABLED
|
|
43
|
+
#endif
|
|
44
|
+
|
|
45
|
+
#define IS_PREFERRED_ENC(X) \
|
|
46
|
+
X.platform_id == PREFERRED_PLATFORM_ID && X.encoding_id == PREFERRED_ENCODING_ID
|
|
47
|
+
|
|
48
|
+
#ifdef PERFECT_MATCHES_ENABLED
|
|
49
|
+
// On Linux-like OSes using FontConfig, the PostScript name ranks higher than
|
|
50
|
+
// preferred family and family name since we'll use it to get perfect font
|
|
51
|
+
// matching (see fc_font_map_substitute_hook)
|
|
52
|
+
#define GET_NAME_RANK(X) \
|
|
53
|
+
((IS_PREFERRED_ENC(X) ? 1 : 0) << 2) | \
|
|
54
|
+
((X.name_id == TT_NAME_ID_PS_NAME ? 1 : 0) << 1) | \
|
|
55
|
+
(X.name_id == TT_NAME_ID_PREFERRED_FAMILY ? 1 : 0)
|
|
56
|
+
#else
|
|
57
|
+
#define GET_NAME_RANK(X) \
|
|
58
|
+
((IS_PREFERRED_ENC(X) ? 1 : 0) << 1) | \
|
|
59
|
+
(X.name_id == TT_NAME_ID_PREFERRED_FAMILY ? 1 : 0)
|
|
60
|
+
#endif
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* Return a UTF-8 encoded string given a TrueType name buf+len
|
|
64
|
+
* and its platform and encoding
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
char *
|
|
68
|
+
to_utf8(FT_Byte* buf, FT_UInt len, FT_UShort pid, FT_UShort eid) {
|
|
69
|
+
size_t ret_len = len * 4; // max chars in a utf8 string
|
|
70
|
+
char *ret = (char*)malloc(ret_len + 1); // utf8 string + null
|
|
71
|
+
|
|
72
|
+
if (!ret) return NULL;
|
|
73
|
+
|
|
74
|
+
// In my testing of hundreds of fonts from the Google Font repo, the two types
|
|
75
|
+
// of fonts are TT_PLATFORM_MICROSOFT with TT_MS_ID_UNICODE_CS encoding, or
|
|
76
|
+
// TT_PLATFORM_MACINTOSH with TT_MAC_ID_ROMAN encoding. Usually both, never neither
|
|
77
|
+
|
|
78
|
+
char const *fromcode;
|
|
79
|
+
|
|
80
|
+
if (pid == TT_PLATFORM_MACINTOSH && eid == TT_MAC_ID_ROMAN) {
|
|
81
|
+
fromcode = "MAC";
|
|
82
|
+
} else if (pid == TT_PLATFORM_MICROSOFT && eid == TT_MS_ID_UNICODE_CS) {
|
|
83
|
+
fromcode = "UTF-16BE";
|
|
84
|
+
} else {
|
|
85
|
+
free(ret);
|
|
86
|
+
return NULL;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
GIConv cd = g_iconv_open("UTF-8", fromcode);
|
|
90
|
+
|
|
91
|
+
if (cd == (GIConv)-1) {
|
|
92
|
+
free(ret);
|
|
93
|
+
return NULL;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
size_t inbytesleft = len;
|
|
97
|
+
size_t outbytesleft = ret_len;
|
|
98
|
+
|
|
99
|
+
size_t n_converted = g_iconv(cd, (char**)&buf, &inbytesleft, &ret, &outbytesleft);
|
|
100
|
+
|
|
101
|
+
ret -= ret_len - outbytesleft; // rewind the pointers to their
|
|
102
|
+
buf -= len - inbytesleft; // original starting positions
|
|
103
|
+
|
|
104
|
+
if (n_converted == (size_t)-1) {
|
|
105
|
+
free(ret);
|
|
106
|
+
return NULL;
|
|
107
|
+
} else {
|
|
108
|
+
ret[ret_len - outbytesleft] = '\0';
|
|
109
|
+
return ret;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/*
|
|
114
|
+
* Find a family name in the face's name table, preferring the one the
|
|
115
|
+
* system, fall back to the other
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
char *
|
|
119
|
+
get_family_name(FT_Face face) {
|
|
120
|
+
FT_SfntName name;
|
|
121
|
+
|
|
122
|
+
int best_rank = -1;
|
|
123
|
+
char* best_buf = NULL;
|
|
124
|
+
|
|
125
|
+
for (unsigned i = 0; i < FT_Get_Sfnt_Name_Count(face); ++i) {
|
|
126
|
+
FT_Get_Sfnt_Name(face, i, &name);
|
|
127
|
+
|
|
128
|
+
if (
|
|
129
|
+
name.name_id == TT_NAME_ID_FONT_FAMILY ||
|
|
130
|
+
#ifdef PERFECT_MATCHES_ENABLED
|
|
131
|
+
name.name_id == TT_NAME_ID_PS_NAME ||
|
|
132
|
+
#endif
|
|
133
|
+
name.name_id == TT_NAME_ID_PREFERRED_FAMILY
|
|
134
|
+
) {
|
|
135
|
+
int rank = GET_NAME_RANK(name);
|
|
136
|
+
|
|
137
|
+
if (rank > best_rank) {
|
|
138
|
+
char *buf = to_utf8(name.string, name.string_len, name.platform_id, name.encoding_id);
|
|
139
|
+
if (buf) {
|
|
140
|
+
best_rank = rank;
|
|
141
|
+
if (best_buf) free(best_buf);
|
|
142
|
+
best_buf = buf;
|
|
143
|
+
|
|
144
|
+
#ifdef PERFECT_MATCHES_ENABLED
|
|
145
|
+
// Prepend an '@' to the postscript name
|
|
146
|
+
if (name.name_id == TT_NAME_ID_PS_NAME) {
|
|
147
|
+
std::string best_buf_modified = "@";
|
|
148
|
+
best_buf_modified += best_buf;
|
|
149
|
+
free(best_buf);
|
|
150
|
+
best_buf = strdup(best_buf_modified.c_str());
|
|
151
|
+
}
|
|
152
|
+
#endif
|
|
153
|
+
} else {
|
|
154
|
+
free(buf);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return best_buf;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
PangoWeight
|
|
164
|
+
get_pango_weight(FT_UShort weight) {
|
|
165
|
+
switch (weight) {
|
|
166
|
+
case 100: return PANGO_WEIGHT_THIN;
|
|
167
|
+
case 200: return PANGO_WEIGHT_ULTRALIGHT;
|
|
168
|
+
case 300: return PANGO_WEIGHT_LIGHT;
|
|
169
|
+
#if PANGO_VERSION >= PANGO_VERSION_ENCODE(1, 36, 7)
|
|
170
|
+
case 350: return PANGO_WEIGHT_SEMILIGHT;
|
|
171
|
+
#endif
|
|
172
|
+
case 380: return PANGO_WEIGHT_BOOK;
|
|
173
|
+
case 400: return PANGO_WEIGHT_NORMAL;
|
|
174
|
+
case 500: return PANGO_WEIGHT_MEDIUM;
|
|
175
|
+
case 600: return PANGO_WEIGHT_SEMIBOLD;
|
|
176
|
+
case 700: return PANGO_WEIGHT_BOLD;
|
|
177
|
+
case 800: return PANGO_WEIGHT_ULTRABOLD;
|
|
178
|
+
case 900: return PANGO_WEIGHT_HEAVY;
|
|
179
|
+
case 1000: return PANGO_WEIGHT_ULTRAHEAVY;
|
|
180
|
+
default: return PANGO_WEIGHT_NORMAL;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
PangoStretch
|
|
185
|
+
get_pango_stretch(FT_UShort width) {
|
|
186
|
+
switch (width) {
|
|
187
|
+
case 1: return PANGO_STRETCH_ULTRA_CONDENSED;
|
|
188
|
+
case 2: return PANGO_STRETCH_EXTRA_CONDENSED;
|
|
189
|
+
case 3: return PANGO_STRETCH_CONDENSED;
|
|
190
|
+
case 4: return PANGO_STRETCH_SEMI_CONDENSED;
|
|
191
|
+
case 5: return PANGO_STRETCH_NORMAL;
|
|
192
|
+
case 6: return PANGO_STRETCH_SEMI_EXPANDED;
|
|
193
|
+
case 7: return PANGO_STRETCH_EXPANDED;
|
|
194
|
+
case 8: return PANGO_STRETCH_EXTRA_EXPANDED;
|
|
195
|
+
case 9: return PANGO_STRETCH_ULTRA_EXPANDED;
|
|
196
|
+
default: return PANGO_STRETCH_NORMAL;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
PangoStyle
|
|
201
|
+
get_pango_style(FT_Long flags) {
|
|
202
|
+
if (flags & FT_STYLE_FLAG_ITALIC) {
|
|
203
|
+
return PANGO_STYLE_ITALIC;
|
|
204
|
+
} else {
|
|
205
|
+
return PANGO_STYLE_NORMAL;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
#ifdef _WIN32
|
|
210
|
+
std::unique_ptr<wchar_t[]>
|
|
211
|
+
u8ToWide(const char* str) {
|
|
212
|
+
int iBufferSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, (wchar_t*)NULL, 0);
|
|
213
|
+
if(!iBufferSize){
|
|
214
|
+
return nullptr;
|
|
215
|
+
}
|
|
216
|
+
std::unique_ptr<wchar_t[]> wpBufWString = std::unique_ptr<wchar_t[]>{ new wchar_t[static_cast<size_t>(iBufferSize)] };
|
|
217
|
+
if(!MultiByteToWideChar(CP_UTF8, 0, str, -1, wpBufWString.get(), iBufferSize)){
|
|
218
|
+
return nullptr;
|
|
219
|
+
}
|
|
220
|
+
return wpBufWString;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static unsigned long
|
|
224
|
+
stream_read_func(FT_Stream stream, unsigned long offset, unsigned char* buffer, unsigned long count){
|
|
225
|
+
HANDLE hFile = reinterpret_cast<HANDLE>(stream->descriptor.pointer);
|
|
226
|
+
DWORD numberOfBytesRead;
|
|
227
|
+
OVERLAPPED overlapped;
|
|
228
|
+
overlapped.Offset = offset;
|
|
229
|
+
overlapped.OffsetHigh = 0;
|
|
230
|
+
overlapped.hEvent = NULL;
|
|
231
|
+
if(!ReadFile(hFile, buffer, count, &numberOfBytesRead, &overlapped)){
|
|
232
|
+
return 0;
|
|
233
|
+
}
|
|
234
|
+
return numberOfBytesRead;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
static void
|
|
238
|
+
stream_close_func(FT_Stream stream){
|
|
239
|
+
HANDLE hFile = reinterpret_cast<HANDLE>(stream->descriptor.pointer);
|
|
240
|
+
CloseHandle(hFile);
|
|
241
|
+
}
|
|
242
|
+
#endif
|
|
243
|
+
|
|
244
|
+
/*
|
|
245
|
+
* Return a PangoFontDescription that will resolve to the font file
|
|
246
|
+
*/
|
|
247
|
+
|
|
248
|
+
PangoFontDescription *
|
|
249
|
+
get_pango_font_description(unsigned char* filepath) {
|
|
250
|
+
FT_Library library;
|
|
251
|
+
FT_Face face;
|
|
252
|
+
PangoFontDescription *desc = pango_font_description_new();
|
|
253
|
+
#ifdef _WIN32
|
|
254
|
+
// FT_New_Face use fopen.
|
|
255
|
+
// Unable to find the file when supplied the multibyte string path on the Windows platform and throw error "Could not parse font file".
|
|
256
|
+
// This workaround fixes this by reading the font file uses win32 wide character API.
|
|
257
|
+
std::unique_ptr<wchar_t[]> wFilepath = u8ToWide((char*)filepath);
|
|
258
|
+
if(!wFilepath){
|
|
259
|
+
return NULL;
|
|
260
|
+
}
|
|
261
|
+
HANDLE hFile = CreateFileW(
|
|
262
|
+
wFilepath.get(),
|
|
263
|
+
GENERIC_READ,
|
|
264
|
+
FILE_SHARE_READ,
|
|
265
|
+
NULL,
|
|
266
|
+
OPEN_EXISTING,
|
|
267
|
+
FILE_ATTRIBUTE_NORMAL,
|
|
268
|
+
NULL
|
|
269
|
+
);
|
|
270
|
+
if(!hFile){
|
|
271
|
+
return NULL;
|
|
272
|
+
}
|
|
273
|
+
LARGE_INTEGER liSize;
|
|
274
|
+
if(!GetFileSizeEx(hFile, &liSize)) {
|
|
275
|
+
CloseHandle(hFile);
|
|
276
|
+
return NULL;
|
|
277
|
+
}
|
|
278
|
+
FT_Open_Args args;
|
|
279
|
+
args.flags = FT_OPEN_STREAM;
|
|
280
|
+
FT_StreamRec stream;
|
|
281
|
+
stream.base = NULL;
|
|
282
|
+
stream.size = liSize.QuadPart;
|
|
283
|
+
stream.pos = 0;
|
|
284
|
+
stream.descriptor.pointer = hFile;
|
|
285
|
+
stream.read = stream_read_func;
|
|
286
|
+
stream.close = stream_close_func;
|
|
287
|
+
args.stream = &stream;
|
|
288
|
+
if (
|
|
289
|
+
!FT_Init_FreeType(&library) &&
|
|
290
|
+
!FT_Open_Face(library, &args, 0, &face)) {
|
|
291
|
+
#else
|
|
292
|
+
if (!FT_Init_FreeType(&library) && !FT_New_Face(library, (const char*)filepath, 0, &face)) {
|
|
293
|
+
#endif
|
|
294
|
+
TT_OS2 *table = (TT_OS2*)FT_Get_Sfnt_Table(face, FT_SFNT_OS2);
|
|
295
|
+
if (table) {
|
|
296
|
+
char *family = get_family_name(face);
|
|
297
|
+
|
|
298
|
+
if (!family) {
|
|
299
|
+
pango_font_description_free(desc);
|
|
300
|
+
FT_Done_Face(face);
|
|
301
|
+
FT_Done_FreeType(library);
|
|
302
|
+
|
|
303
|
+
return NULL;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
pango_font_description_set_family_static(desc, family);
|
|
307
|
+
pango_font_description_set_weight(desc, get_pango_weight(table->usWeightClass));
|
|
308
|
+
pango_font_description_set_stretch(desc, get_pango_stretch(table->usWidthClass));
|
|
309
|
+
pango_font_description_set_style(desc, get_pango_style(face->style_flags));
|
|
310
|
+
|
|
311
|
+
FT_Done_Face(face);
|
|
312
|
+
FT_Done_FreeType(library);
|
|
313
|
+
|
|
314
|
+
return desc;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
pango_font_description_free(desc);
|
|
318
|
+
|
|
319
|
+
return NULL;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
#ifdef PERFECT_MATCHES_ENABLED
|
|
323
|
+
static void
|
|
324
|
+
fc_font_map_substitute_hook(FcPattern *pat, gpointer data) {
|
|
325
|
+
FcChar8 *family;
|
|
326
|
+
|
|
327
|
+
for (int i = 0; FcPatternGetString(pat, FC_FAMILY, i, &family) == FcResultMatch; i++) {
|
|
328
|
+
if (family[0] == '@') {
|
|
329
|
+
FcPatternAddString(pat, FC_POSTSCRIPT_NAME, (FcChar8 *)family + 1);
|
|
330
|
+
FcPatternRemove(pat, FC_FAMILY, i);
|
|
331
|
+
i -= 1;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
#endif
|
|
336
|
+
|
|
337
|
+
/*
|
|
338
|
+
* Register font with the OS
|
|
339
|
+
*/
|
|
340
|
+
|
|
341
|
+
bool
|
|
342
|
+
register_font(unsigned char *filepath) {
|
|
343
|
+
bool success;
|
|
344
|
+
|
|
345
|
+
#ifdef __APPLE__
|
|
346
|
+
CFURLRef filepathUrl = CFURLCreateFromFileSystemRepresentation(NULL, filepath, strlen((char*)filepath), false);
|
|
347
|
+
success = CTFontManagerRegisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
|
|
348
|
+
#elif defined(_WIN32)
|
|
349
|
+
std::unique_ptr<wchar_t[]> wFilepath = u8ToWide((char*)filepath);
|
|
350
|
+
if(wFilepath){
|
|
351
|
+
success = AddFontResourceExW(wFilepath.get(), FR_PRIVATE, 0) != 0;
|
|
352
|
+
}else{
|
|
353
|
+
success = false;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
#else
|
|
357
|
+
success = FcConfigAppFontAddFile(FcConfigGetCurrent(), (FcChar8 *)(filepath));
|
|
358
|
+
#endif
|
|
359
|
+
|
|
360
|
+
if (!success) return false;
|
|
361
|
+
|
|
362
|
+
// Tell Pango to throw away the current FontMap and create a new one. This
|
|
363
|
+
// has the effect of registering the new font in Pango by re-looking up all
|
|
364
|
+
// font families.
|
|
365
|
+
pango_cairo_font_map_set_default(NULL);
|
|
366
|
+
|
|
367
|
+
#ifdef PERFECT_MATCHES_ENABLED
|
|
368
|
+
PangoFontMap* map = pango_cairo_font_map_get_default();
|
|
369
|
+
PangoFcFontMap* fc_map = PANGO_FC_FONT_MAP(map);
|
|
370
|
+
pango_fc_font_map_set_default_substitute(fc_map, fc_font_map_substitute_hook, NULL, NULL);
|
|
371
|
+
#endif
|
|
372
|
+
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/*
|
|
377
|
+
* Deregister font from the OS
|
|
378
|
+
* Note that Linux (FontConfig) can only dereregister ALL fonts at once.
|
|
379
|
+
*/
|
|
380
|
+
|
|
381
|
+
bool
|
|
382
|
+
deregister_font(unsigned char *filepath) {
|
|
383
|
+
bool success;
|
|
384
|
+
|
|
385
|
+
#ifdef __APPLE__
|
|
386
|
+
CFURLRef filepathUrl = CFURLCreateFromFileSystemRepresentation(NULL, filepath, strlen((char*)filepath), false);
|
|
387
|
+
success = CTFontManagerUnregisterFontsForURL(filepathUrl, kCTFontManagerScopeProcess, NULL);
|
|
388
|
+
#elif defined(_WIN32)
|
|
389
|
+
std::unique_ptr<wchar_t[]> wFilepath = u8ToWide((char*)filepath);
|
|
390
|
+
if(wFilepath){
|
|
391
|
+
success = RemoveFontResourceExW(wFilepath.get(), FR_PRIVATE, 0) != 0;
|
|
392
|
+
}else{
|
|
393
|
+
success = false;
|
|
394
|
+
}
|
|
395
|
+
#else
|
|
396
|
+
FcConfigAppFontClear(FcConfigGetCurrent());
|
|
397
|
+
success = true;
|
|
398
|
+
#endif
|
|
399
|
+
|
|
400
|
+
if (!success) return false;
|
|
401
|
+
|
|
402
|
+
// Tell Pango to throw away the current FontMap and create a new one. This
|
|
403
|
+
// has the effect of deregistering the font in Pango by re-looking up all
|
|
404
|
+
// font families.
|
|
405
|
+
pango_cairo_font_map_set_default(NULL);
|
|
406
|
+
|
|
407
|
+
return true;
|
|
408
|
+
}
|