Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gvloadimage_gdiplus.cpp
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <stdlib.h>
14#include <stddef.h>
15#include <string.h>
16
18#include "gvplugin_gdiplus.h"
19#include <stringapiset.h>
20#include <windows.h>
21#include <gdiplus.h>
22#include <vector>
23
24using namespace Gdiplus;
25
27 delete reinterpret_cast<Image*>(us->data);
28}
29
30// convert a UTF-8 string to UTF-16
31static std::vector<wchar_t> utf8_to_utf16(const char *s) {
32
33 // how much space do we need for the UTF-16 string?
34 const int wide_count = MultiByteToWideChar(CP_UTF8, 0, s, -1, nullptr, 0);
35
36 // translate it
37 std::vector<wchar_t> utf16(wide_count);
38 if (wide_count > 0) {
39 (void)MultiByteToWideChar(CP_UTF8, 0, s, -1, utf16.data(), wide_count);
40 } else {
41 utf16.push_back(0);
42 }
43
44 return utf16;
45}
46
47static Image *gdiplus_loadimage(usershape_t *us) {
48 assert(us);
49 assert(us->name);
50
51 if (us->data && us->datafree != gdiplus_freeimage) {
52 us->datafree(us); /* free incompatible cache data */
53 us->data = nullptr;
54 us->datafree = nullptr;
55 }
56
57 if (!us->data) { /* read file into cache */
59 return nullptr;
60
61 /* create image from the usershape file */
62 const std::vector<wchar_t> filename = utf8_to_utf16(us->name);
63 us->data = Image::FromFile(filename.data());
64
65 /* clean up */
66 if (us->data)
68
70 }
71 return reinterpret_cast<Image*>(us->data);
72}
73
74static void gdiplus_loadimage_gdiplus(GVJ_t * job, usershape_t *us, boxf b, bool)
75{
76 /* get the image from usershape details, then blit it to the context */
77 if (Image *image = gdiplus_loadimage(us)) {
78 assert(job != NULL);
79 auto g = reinterpret_cast<Graphics*>(job->context);
80 g->DrawImage(image, RectF(b.LL.x, b.LL.y, b.UR.x - b.LL.x, b.UR.y - b.LL.y));
81 }
82}
83
87
89 {FORMAT_BMP, "bmp:gdiplus", 8, &engine, nullptr},
90 {FORMAT_GIF, "gif:gdiplus", 8, &engine, nullptr},
91 {FORMAT_JPEG, "jpe:gdiplus", 8, &engine, nullptr},
92 {FORMAT_JPEG, "jpeg:gdiplus", 8, &engine, nullptr},
93 {FORMAT_JPEG, "jpg:gdiplus", 8, &engine, nullptr},
94 {FORMAT_PNG, "png:gdiplus", 8, &engine, nullptr},
95 {0, nullptr, 0, nullptr, nullptr}
96};
node NULL
Definition grammar.y:163
void gvusershape_file_release(usershape_t *us)
bool gvusershape_file_access(usershape_t *us)
static gvloadimage_engine_t engine
static Image * gdiplus_loadimage(usershape_t *us)
static void gdiplus_loadimage_gdiplus(GVJ_t *job, usershape_t *us, boxf b, bool)
static std::vector< wchar_t > utf8_to_utf16(const char *s)
static void gdiplus_freeimage(usershape_t *us)
gvplugin_installed_t gvloadimage_gdiplus_types[]
@ FORMAT_BMP
@ FORMAT_JPEG
Definition gvrender_gd.c:37
@ FORMAT_GIF
Definition gvrender_gd.c:36
@ FORMAT_PNG
Definition gvrender_gd.c:38
T_cell image
Definition htmlparse.y:340
void * context
Definition gvcjob.h:295
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
ingroup plugin_api
Definition gvplugin.h:35
double x
Definition geom.h:29
double y
Definition geom.h:29
const char * name
Definition usershape.h:54
void(* datafree)(usershape_t *us)
Definition usershape.h:65
void * data
Definition usershape.h:63
Definition grammar.c:93