Skip to content

Commit 469de4d

Browse files
committed
Fix pdf/ps/svg support and bump cairo to 1.15.12 on Windows.
1 parent 7f9f22c commit 469de4d

File tree

5 files changed

+59
-18
lines changed

5 files changed

+59
-18
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def build_extensions(self):
162162
["/std:c++17", "/EHsc", "/D_USE_MATH_DEFINES",
163163
"/wd4244", "/wd4267"]) # cf. gcc -Wconversion.
164164
ext.libraries += (
165-
["cairo", "freetype"])
165+
["psapi", "cairo", "freetype"])
166166
ext.library_dirs += (
167167
# Windows conda path for FreeType.
168168
[str(Path(sys.prefix, "Library/lib"))])

src/_mplcairo.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "_mplcairo.h"
22

3+
#include "_os.h"
34
#include "_pattern_cache.h"
45
#include "_raqm.h"
56
#include "_util.h"
@@ -1528,11 +1529,15 @@ PYBIND11_MODULE(_mplcairo, m)
15281529
py::getattr(dll, name, py::int_(0)), ctypes.attr("c_void_p"))
15291530
.attr("value").cast<std::optional<uintptr_t>>().value_or(0);
15301531
};
1532+
#else
1533+
auto const& load_ptr = [&](char const* name) -> os::symbol_t {
1534+
return os::dlsym(name);
1535+
};
1536+
#endif
15311537
#define LOAD_API(name) \
15321538
detail::name = reinterpret_cast<decltype(detail::name)>(load_ptr(#name));
15331539
ITER_CAIRO_OPTIONAL_API(LOAD_API)
1534-
#undef LOAD_PTR
1535-
#endif
1540+
#undef LOAD_API
15361541

15371542
FT_CHECK(FT_Init_FreeType, &detail::ft_library);
15381543

src/_os.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
#if defined __linux__ || defined __APPLE__
44
#include <dlfcn.h>
5-
#else
5+
#elif defined _WIN32
6+
#include <memory>
7+
8+
#define NOMINMAX
9+
#include <psapi.h>
610
#include <Windows.h>
711
#endif
812

@@ -28,7 +32,7 @@ char const* dlerror() {
2832
return ::dlerror();
2933
}
3034

31-
#elif _WIN32
35+
#elif defined _WIN32
3236
using library_t = HMODULE;
3337
using symbol_t = FARPROC;
3438

@@ -44,6 +48,22 @@ symbol_t dlsym(library_t handle, char const* symbol) {
4448
return GetProcAddress(handle, symbol);
4549
}
4650

51+
symbol_t dlsym(char const* symbol) {
52+
auto hProcess = GetCurrentProcess();
53+
auto cbNeeded = DWORD{};
54+
EnumProcessModules(hProcess, nullptr, 0, &cbNeeded);
55+
auto n_modules = cbNeeded / sizeof(HMODULE);
56+
auto lphModule = std::unique_ptr<HMODULE[]>(new HMODULE[n_modules]);
57+
if (EnumProcessModules(hProcess, lphModule.get(), cbNeeded, &cbNeeded)) {
58+
for (auto i = 0; i < n_modules; ++i) {
59+
if (auto proc = GetProcAddress(lphModule[i], symbol)) {
60+
return proc;
61+
}
62+
}
63+
}
64+
return nullptr;
65+
}
66+
4767
char const* dlerror() {
4868
return ""; // FIXME
4969
}

src/_os.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#include <iostream>
2+
13
#ifdef _WIN32
4+
#define NOMINMAX
25
#include <Windows.h>
36
#endif
47

@@ -10,6 +13,9 @@ using symbol_t = void*;
1013
#elif defined _WIN32
1114
using library_t = HMODULE;
1215
using symbol_t = FARPROC;
16+
17+
// Like dlsym, but looks through everything listed by EnumProcessModules.
18+
symbol_t dlsym(char const* symbol);
1319
#endif
1420
library_t dlopen(char const* filename);
1521
bool dlclose(library_t handle);

tools/build-windows-wheel.ps1

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
New-Item -ItemType directory build
1+
New-Item -ItemType directory -Force build | Out-Null
22
Set-Location build
33
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
4-
Invoke-WebRequest `
5-
https://github.com/preshing/cairo-windows/releases/download/1.15.10/cairo-windows-1.15.10.zip `
6-
-OutFile cairo-windows-1.15.10.zip
7-
Expand-Archive cairo-windows-1.15.10.zip -DestinationPath .
8-
Invoke-WebRequest `
9-
https://github.com/ubawurinna/freetype-windows-binaries/releases/download/v2.9.1/freetype-2.9.1.zip `
10-
-OutFile freetype-2.9.1.zip
11-
Expand-Archive freetype-2.9.1.zip -DestinationPath freetype-2.9.1
4+
5+
$cairo_url = `
6+
"https://github.com/preshing/cairo-windows/releases/download/1.15.12"
7+
$cairo_name = "cairo-windows-1.15.12"
8+
if (!(Test-Path "$cairo_name")) {
9+
Invoke-WebRequest "$cairo_url/$cairo_name.zip" -OutFile "$cairo_name.zip"
10+
Expand-Archive "$cairo_name.zip" -DestinationPath .
11+
}
12+
13+
$freetype_url = `
14+
"https://github.com/ubawurinna/freetype-windows-binaries/releases/download/v2.9.1"
15+
$freetype_name = "freetype-2.9.1"
16+
if (!(Test-Path "$freetype_name")) {
17+
Invoke-WebRequest "$freetype_url/$freetype_name.zip" -OutFile "$freetype_name.zip"
18+
Expand-Archive "$freetype_name.zip" -DestinationPath "$freetype_name"
19+
}
20+
1221
Set-Location ..
22+
1323
$Env:CL = `
14-
"/I$(Get-Location)\build\cairo-windows-1.15.10\include " `
15-
+ "/I$(Get-Location)\build\freetype-2.9.1\include"
24+
"/I$(Get-Location)\build\$cairo_name\include " `
25+
+ "/I$(Get-Location)\build\$freetype_name\include"
1626
$Env:LINK = `
17-
"/LIBPATH:$(Get-Location)\build\cairo-windows-1.15.10\lib\x64 " `
18-
+ "/LIBPATH:$(Get-Location)\build\freetype-2.9.1\win64"
27+
"/LIBPATH:$(Get-Location)\build\$cairo_name\lib\x64 " `
28+
+ "/LIBPATH:$(Get-Location)\build\$freetype_name\win64"
1929
python -mpip install --upgrade pybind11
2030
python setup.py bdist_wheel

0 commit comments

Comments
 (0)