|
1
|
+/**************************************************************************
|
|
2
|
+* file: dmpimageinfo.cpp
|
|
3
|
+
|
|
4
|
+* Author: qingxiongf
|
|
5
|
+* Date: 2022-01-04 10:50:35
|
|
6
|
+* Email: qingxiongf@chinadci.com
|
|
7
|
+* copyright: 广州城市信息研究所有限公司
|
|
8
|
+***************************************************************************/
|
|
9
|
+#include <unistd.h>
|
|
10
|
+#include <dirent.h>
|
|
11
|
+#include <stdlib.h>
|
|
12
|
+#include <sys/stat.h>
|
|
13
|
+#include <map>
|
|
14
|
+#include <set>
|
|
15
|
+#include <fontconfig/fontconfig.h>
|
|
16
|
+
|
|
17
|
+#include <boost/foreach.hpp>
|
|
18
|
+#include <boost/lexical_cast.hpp>
|
|
19
|
+#include <boost/algorithm/string.hpp>
|
|
20
|
+#include <boost/date_time.hpp>
|
|
21
|
+#include <boost/property_tree/ptree.hpp>
|
|
22
|
+#include <boost/property_tree/json_parser.hpp>
|
|
23
|
+#include <boost/typeof/typeof.hpp>
|
|
24
|
+#include "dmplogger.h"
|
|
25
|
+#include "dmpserverresponse.h"
|
|
26
|
+#include "dmpserverrequest.h"
|
|
27
|
+
|
|
28
|
+#include "dmpimageinfo.h"
|
|
29
|
+
|
|
30
|
+namespace DmpMapping
|
|
31
|
+{
|
|
32
|
+ void getTypefaceList(const DmpServerContext &context)
|
|
33
|
+ {
|
|
34
|
+ FcConfig *config = FcInitLoadConfigAndFonts();
|
|
35
|
+ FcPattern *pat = FcPatternCreate();
|
|
36
|
+ FcObjectSet *os = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, (char *)0);
|
|
37
|
+ FcFontSet *fs = FcFontList(config, pat, os);
|
|
38
|
+ if (fs == nullptr)
|
|
39
|
+ {
|
|
40
|
+ context.response()->writeJson("{\"error\":\"load FcFontSet failed\"}");
|
|
41
|
+ return;
|
|
42
|
+ }
|
|
43
|
+ //printf("Total matching fonts: %d\n", fs->nfont);
|
|
44
|
+ boost::property_tree::ptree ptDoc;
|
|
45
|
+ boost::property_tree::ptree ptRoot;
|
|
46
|
+
|
|
47
|
+ std::map<std::string, std::string> mapFont;
|
|
48
|
+ mapFont["SimSun"] = "宋体";
|
|
49
|
+ mapFont["NSimSun"] = "新宋体";
|
|
50
|
+ mapFont["SimHei"] = "黑体";
|
|
51
|
+ mapFont["FangSong"] = "仿宋";
|
|
52
|
+ mapFont["KaiTi"] = "楷体";
|
|
53
|
+ mapFont["LiSu"] = "隶书";
|
|
54
|
+ mapFont["YouYuan"] = "幼圆";
|
|
55
|
+ mapFont["STXihei"] = "华文细黑";
|
|
56
|
+ mapFont["STKaiti"] = "华文楷体";
|
|
57
|
+ mapFont["STSong"] = "华文宋体";
|
|
58
|
+ mapFont["STZhongsong"] = "华文中宋";
|
|
59
|
+ mapFont["STFangsong"] = "华文仿宋";
|
|
60
|
+ mapFont["FZShuTi"] = "方正舒体";
|
|
61
|
+ mapFont["FZYaoti"] = "方正姚体";
|
|
62
|
+ mapFont["STCaiyun"] = "华文彩云";
|
|
63
|
+ mapFont["STHupo"] = "华文琥珀";
|
|
64
|
+ mapFont["STLiti"] = "华文隶书";
|
|
65
|
+ mapFont["STXingkai"] = "华文行楷";
|
|
66
|
+ mapFont["STXinwei"] = "华文新魏";
|
|
67
|
+ mapFont["Microsoft YaHei"] = "微软雅黑";
|
|
68
|
+ mapFont["Microsoft JhengHei"] = "微軟正黑體";
|
|
69
|
+ mapFont["DengXian"] = "等线";
|
|
70
|
+ mapFont["MingLiU"] = "細明體";
|
|
71
|
+ mapFont["MingLiU_HKSCS"] = "細明體_HKSCS";
|
|
72
|
+ mapFont["MingLiU"] = "細明體";
|
|
73
|
+
|
|
74
|
+ std::set<std::string> setFont;
|
|
75
|
+ char buff[1000];
|
|
76
|
+ for (int i = 0; fs && i < fs->nfont; ++i)
|
|
77
|
+ {
|
|
78
|
+
|
|
79
|
+ FcPattern *font = fs->fonts[i];
|
|
80
|
+ FcChar8 *file, *style, *family, *prgname;
|
|
81
|
+ if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch &&
|
|
82
|
+ FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch &&
|
|
83
|
+ FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch)
|
|
84
|
+ {
|
|
85
|
+ sprintf(buff, "%s", family);
|
|
86
|
+ if (setFont.find(buff) == setFont.end())
|
|
87
|
+ {
|
|
88
|
+
|
|
89
|
+ setFont.insert(buff);
|
|
90
|
+ if (mapFont.find(buff) != mapFont.end())
|
|
91
|
+ {
|
|
92
|
+ boost::property_tree::ptree ptNode;
|
|
93
|
+ ptNode.put("", buff);
|
|
94
|
+ ptRoot.push_back(std::make_pair("", ptNode));
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ for (std::set<std::string>::iterator iter = setFont.begin();
|
|
101
|
+ iter != setFont.end(); iter++)
|
|
102
|
+ {
|
|
103
|
+ if (mapFont.find(*iter) == mapFont.end())
|
|
104
|
+ {
|
|
105
|
+ boost::property_tree::ptree ptNode;
|
|
106
|
+ ptNode.put("", *iter);
|
|
107
|
+ ptRoot.push_back(std::make_pair("", ptNode));
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ FcFontSetDestroy(fs);
|
|
112
|
+
|
|
113
|
+ ptDoc.add_child("value", ptRoot);
|
|
114
|
+ std::stringstream stream;
|
|
115
|
+ write_json(stream, ptDoc);
|
|
116
|
+ std::string responseStr = stream.str();
|
|
117
|
+ context.response()->writeJson(responseStr);
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ void getImage(const DmpServerContext &context)
|
|
121
|
+ {
|
|
122
|
+ DmpServerParameters params = context.request()->serverParameters();
|
|
123
|
+ std::string name;
|
|
124
|
+ params.getValue("name", name);
|
|
125
|
+
|
|
126
|
+ std::string imagePath = getFilePath("../symbollib/" + name);
|
|
127
|
+ FILE *file = fopen(imagePath.c_str(), "rb");
|
|
128
|
+
|
|
129
|
+ try
|
|
130
|
+ {
|
|
131
|
+ char buff[1024];
|
|
132
|
+ if (file == 0)
|
|
133
|
+ {
|
|
134
|
+ context.response()->writeJson("{\"error\":\"open file failed\"}");
|
|
135
|
+ return;
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ context.response()->removeHeader("Content-Type");
|
|
139
|
+ context.response()->setHeader("Content-Type", "image/png");
|
|
140
|
+
|
|
141
|
+ for (int i = 0; i < 20000; i++)
|
|
142
|
+ {
|
|
143
|
+ size_t t = fread(buff, 1, sizeof(buff), file);
|
|
144
|
+ context.response()->writeContent(buff, t);
|
|
145
|
+
|
|
146
|
+ if (t != sizeof(buff))
|
|
147
|
+ break;
|
|
148
|
+ }
|
|
149
|
+ fclose(file);
|
|
150
|
+ return;
|
|
151
|
+ }
|
|
152
|
+ catch (...)
|
|
153
|
+ {
|
|
154
|
+ if (file)
|
|
155
|
+ fclose(file);
|
|
156
|
+ }
|
|
157
|
+ context.response()->writeJson("{\"error\":\"Parameter name is null\"}");
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ void getImageTypeList(const DmpServerContext &context)
|
|
161
|
+ {
|
|
162
|
+ DIR *dirp;
|
|
163
|
+ struct dirent *dp;
|
|
164
|
+ std::string imagePath = getFilePath("../symbollib");
|
|
165
|
+ dirp = opendir(imagePath.c_str());
|
|
166
|
+
|
|
167
|
+ boost::property_tree::ptree ptDoc;
|
|
168
|
+ boost::property_tree::ptree ptRoot;
|
|
169
|
+
|
|
170
|
+ if (dirp)
|
|
171
|
+ {
|
|
172
|
+ while ((dp = readdir(dirp)) != NULL)
|
|
173
|
+ {
|
|
174
|
+ if (dp->d_name == NULL || dp->d_name[0] == '.')
|
|
175
|
+ {
|
|
176
|
+ continue;
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ boost::property_tree::ptree ptDir;
|
|
180
|
+
|
|
181
|
+ ptDir.add("name", dp->d_name);
|
|
182
|
+ ptRoot.push_back(std::make_pair("", ptDir));
|
|
183
|
+ //ptRoot.add_child("imageType",ptDir);
|
|
184
|
+ }
|
|
185
|
+ closedir(dirp);
|
|
186
|
+ }
|
|
187
|
+ else
|
|
188
|
+ {
|
|
189
|
+ LOGGER_ERROR("not find dir " + imagePath);
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ ptDoc.add_child("value", ptRoot);
|
|
193
|
+ std::stringstream stream;
|
|
194
|
+ write_json(stream, ptDoc);
|
|
195
|
+ std::string responseStr = stream.str();
|
|
196
|
+ context.response()->writeJson(responseStr);
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ void getImageList(const DmpServerContext &context)
|
|
200
|
+ {
|
|
201
|
+ DmpServerParameters params = context.request()->serverParameters();
|
|
202
|
+ std::string name;
|
|
203
|
+ params.getValue("name", name);
|
|
204
|
+
|
|
205
|
+ if (name.empty())
|
|
206
|
+ {
|
|
207
|
+ context.response()->writeJson("{\"error\":\"Parameter name is null\"}");
|
|
208
|
+ return;
|
|
209
|
+ }
|
|
210
|
+
|
|
211
|
+ struct dirent *dp;
|
|
212
|
+ std::string imagePath = getFilePath("../symbollib/" + name);
|
|
213
|
+ DIR *dirp = opendir(imagePath.c_str());
|
|
214
|
+
|
|
215
|
+ boost::property_tree::ptree ptDoc;
|
|
216
|
+ boost::property_tree::ptree ptRoot;
|
|
217
|
+
|
|
218
|
+ if (dirp)
|
|
219
|
+ {
|
|
220
|
+ while ((dp = readdir(dirp)) != NULL)
|
|
221
|
+ {
|
|
222
|
+ boost::property_tree::ptree ptDir;
|
|
223
|
+ if (dp->d_name == NULL || dp->d_name[0] == '.')
|
|
224
|
+ {
|
|
225
|
+ continue;
|
|
226
|
+ }
|
|
227
|
+ ptDir.add("name", dp->d_name);
|
|
228
|
+ ptDir.add("path", ("../symbollib/" + name + "/" + dp->d_name));
|
|
229
|
+ ptRoot.push_back(std::make_pair("", ptDir));
|
|
230
|
+ }
|
|
231
|
+ closedir(dirp);
|
|
232
|
+ }
|
|
233
|
+ else
|
|
234
|
+ {
|
|
235
|
+ LOGGER_ERROR("not find dir " + imagePath);
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ ptDoc.add_child("value", ptRoot);
|
|
239
|
+ std::stringstream stream;
|
|
240
|
+ write_json(stream, ptDoc);
|
|
241
|
+ std::string responseStr = stream.str();
|
|
242
|
+ context.response()->writeJson(responseStr);
|
|
243
|
+ }
|
|
244
|
+
|
|
245
|
+ std::string getFilePath(const std::string &path)
|
|
246
|
+ {
|
|
247
|
+ char sz[300] = {0};
|
|
248
|
+ if (getCurrentFolderPath(sz, sizeof(sz)) > 0)
|
|
249
|
+ {
|
|
250
|
+ std::string s;
|
|
251
|
+ s = sz;
|
|
252
|
+ size_t t = s.rfind('/');
|
|
253
|
+ sz[t] = 0;
|
|
254
|
+ if (path[0] == '/')
|
|
255
|
+ {
|
|
256
|
+ return path;
|
|
257
|
+ }
|
|
258
|
+ int i = 0;
|
|
259
|
+ if (path[0] == '.')
|
|
260
|
+ {
|
|
261
|
+ if (path[1] == '.')
|
|
262
|
+ {
|
|
263
|
+ i++;
|
|
264
|
+ s = sz;
|
|
265
|
+ t = s.rfind('/');
|
|
266
|
+ sz[t] = 0;
|
|
267
|
+ }
|
|
268
|
+ i++;
|
|
269
|
+ }
|
|
270
|
+ if ((sz[t] == '/' || sz[t] == '\\') && (path[i] == '/' || path[i] == '\\'))
|
|
271
|
+ {
|
|
272
|
+ i++;
|
|
273
|
+ }
|
|
274
|
+
|
|
275
|
+ int j = t;
|
|
276
|
+ for (j = t; i < path.length(); i++, j++)
|
|
277
|
+ {
|
|
278
|
+ if (path[i] == '\\')
|
|
279
|
+ {
|
|
280
|
+ sz[j] = '/';
|
|
281
|
+ }
|
|
282
|
+ else
|
|
283
|
+ {
|
|
284
|
+ sz[j] = path[i];
|
|
285
|
+ }
|
|
286
|
+ }
|
|
287
|
+ sz[j] = 0;
|
|
288
|
+ //strcat(sz, path.c_str());
|
|
289
|
+ return sz;
|
|
290
|
+ }
|
|
291
|
+ else
|
|
292
|
+ return 0;
|
|
293
|
+ }
|
|
294
|
+
|
|
295
|
+ size_t getCurrentFolderPath(char *processdir, size_t len)
|
|
296
|
+ {
|
|
297
|
+ char *path_end;
|
|
298
|
+ if (readlink("/proc/self/exe", processdir, len) <= 0)
|
|
299
|
+ return -1;
|
|
300
|
+ path_end = strrchr(processdir, '/');
|
|
301
|
+ if (path_end == NULL)
|
|
302
|
+ return -1;
|
|
303
|
+ ++path_end;
|
|
304
|
+ //strcpy(processname, path_end);
|
|
305
|
+ *path_end = '\0';
|
|
306
|
+ return (size_t)(path_end - processdir);
|
|
307
|
+ }
|
|
308
|
+
|
|
309
|
+} |
|
|
\ No newline at end of file |
...
|
...
|
|