annotate OrthancFramework/Sources/Images/Font.cpp @ 4224:38d446c9ee1d

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Sep 2020 17:59:09 +0200
parents bf7b9edf6b81
children 785a2713323e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1654
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * the License, or (at your option) any later version.
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
15 * Lesser General Public License for more details.
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
18 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * <http://www.gnu.org/licenses/>.
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "../PrecompiledHeaders.h"
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "Font.h"
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
2379
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
26 #if !defined(ORTHANC_ENABLE_LOCALE)
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
27 # error ORTHANC_ENABLE_LOCALE must be defined to use this file
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
28 #endif
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
29
2407
5edec967055e fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2379
diff changeset
30 #if ORTHANC_SANDBOXED == 0
5edec967055e fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2379
diff changeset
31 # include "../SystemToolbox.h"
5edec967055e fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2379
diff changeset
32 #endif
5edec967055e fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2379
diff changeset
33
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
34 #include "../OrthancException.h"
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "../Toolbox.h"
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
36 #include "Image.h"
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
37 #include "ImageProcessing.h"
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <stdio.h>
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include <memory>
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include <boost/lexical_cast.hpp>
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 namespace Orthanc
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 Font::~Font()
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 for (Characters::iterator it = characters_.begin();
1616
644c32c07306 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1612
diff changeset
48 it != characters_.end(); ++it)
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 delete it->second;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 void Font::LoadFromMemory(const std::string& font)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 Json::Value v;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 Json::Reader reader;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 if (!reader.parse(font, v) ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 v.type() != Json::objectValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 !v.isMember("Name") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 !v.isMember("Size") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 !v.isMember("Characters") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 v["Name"].type() != Json::stringValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 v["Size"].type() != Json::intValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 v["Characters"].type() != Json::objectValue)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 throw OrthancException(ErrorCode_BadFont);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 name_ = v["Name"].asString();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 size_ = v["Size"].asUInt();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 maxHeight_ = 0;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 Json::Value::Members characters = v["Characters"].getMemberNames();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 for (size_t i = 0; i < characters.size(); i++)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 const Json::Value& info = v["Characters"][characters[i]];
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 if (info.type() != Json::objectValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 !info.isMember("Advance") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 !info.isMember("Bitmap") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 !info.isMember("Height") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 !info.isMember("Top") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 !info.isMember("Width") ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 info["Advance"].type() != Json::intValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 info["Bitmap"].type() != Json::arrayValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 info["Height"].type() != Json::intValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 info["Top"].type() != Json::intValue ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 info["Width"].type() != Json::intValue)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 throw OrthancException(ErrorCode_BadFont);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
95 std::unique_ptr<Character> c(new Character);
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 c->advance_ = info["Advance"].asUInt();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 c->height_ = info["Height"].asUInt();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 c->top_ = info["Top"].asUInt();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 c->width_ = info["Width"].asUInt();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 c->bitmap_.resize(info["Bitmap"].size());
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 if (c->height_ > maxHeight_)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 maxHeight_ = c->height_;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 for (Json::Value::ArrayIndex j = 0; j < info["Bitmap"].size(); j++)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 if (info["Bitmap"][j].type() != Json::intValue)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 throw OrthancException(ErrorCode_BadFont);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 int value = info["Bitmap"][j].asInt();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 if (value < 0 || value > 255)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 throw OrthancException(ErrorCode_BadFont);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
1654
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
121 c->bitmap_[j] = static_cast<uint8_t>(value);
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 int index = boost::lexical_cast<int>(characters[i]);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if (index < 0 || index > 255)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 throw OrthancException(ErrorCode_BadFont);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 characters_[static_cast<char>(index)] = c.release();
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
2407
5edec967055e fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2379
diff changeset
135 #if ORTHANC_SANDBOXED == 0
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 void Font::LoadFromFile(const std::string& path)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 std::string font;
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
139 SystemToolbox::ReadFile(font, path);
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 LoadFromMemory(font);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
2407
5edec967055e fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2379
diff changeset
142 #endif
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 static unsigned int MyMin(unsigned int a,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 unsigned int b)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 return a < b ? a : b;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 void Font::DrawCharacter(ImageAccessor& target,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 const Character& character,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 int x,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 int y,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 const uint8_t color[4]) const
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 // Compute the bounds of the character
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 if (x >= static_cast<int>(target.GetWidth()) ||
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 y >= static_cast<int>(target.GetHeight()))
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 // The character is out of the image
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 return;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 unsigned int left = x < 0 ? -x : 0;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 unsigned int top = y < 0 ? -y : 0;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 unsigned int width = MyMin(character.width_, target.GetWidth() - x);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 unsigned int height = MyMin(character.height_, target.GetHeight() - y);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
1654
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
171 unsigned int bpp = target.GetBytesPerPixel();
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 // Blit the font bitmap OVER the target image
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 // https://en.wikipedia.org/wiki/Alpha_compositing
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 for (unsigned int cy = top; cy < height; cy++)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 uint8_t* p = reinterpret_cast<uint8_t*>(target.GetRow(y + cy)) + (x + left) * bpp;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 unsigned int pos = cy * character.width_ + left;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 switch (target.GetFormat())
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 case PixelFormat_Grayscale8:
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 assert(bpp == 1);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 for (unsigned int cx = left; cx < width; cx++, pos++, p++)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 uint16_t alpha = character.bitmap_[pos];
1654
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
189 uint16_t value = alpha * static_cast<uint16_t>(color[0]) + (255 - alpha) * static_cast<uint16_t>(*p);
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
190 *p = static_cast<uint8_t>(value >> 8);
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 break;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 case PixelFormat_RGB24:
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 assert(bpp == 3);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 for (unsigned int cx = left; cx < width; cx++, pos++, p += 3)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 uint16_t alpha = character.bitmap_[pos];
1654
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
202 for (uint8_t i = 0; i < 3; i++)
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
203 {
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
204 uint16_t value = alpha * static_cast<uint16_t>(color[i]) + (255 - alpha) * static_cast<uint16_t>(p[i]);
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
205 p[i] = static_cast<uint8_t>(value >> 8);
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1616
diff changeset
206 }
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 break;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 case PixelFormat_RGBA32:
2853
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
213 case PixelFormat_BGRA32:
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 assert(bpp == 4);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 for (unsigned int cx = left; cx < width; cx++, pos++, p += 4)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 float alpha = static_cast<float>(character.bitmap_[pos]) / 255.0f;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 float beta = (1.0f - alpha) * static_cast<float>(p[3]) / 255.0f;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 float denom = 1.0f / (alpha + beta);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 for (uint8_t i = 0; i < 3; i++)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 p[i] = static_cast<uint8_t>((alpha * static_cast<float>(color[i]) +
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 beta * static_cast<float>(p[i])) * denom);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 p[3] = static_cast<uint8_t>(255.0f * (alpha + beta));
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 break;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 default:
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 throw OrthancException(ErrorCode_NotImplemented);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 void Font::DrawInternal(ImageAccessor& target,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 const std::string& utf8,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 int x,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 int y,
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 const uint8_t color[4]) const
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 if (target.GetFormat() != PixelFormat_Grayscale8 &&
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 target.GetFormat() != PixelFormat_RGB24 &&
2853
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
251 target.GetFormat() != PixelFormat_RGBA32 &&
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
252 target.GetFormat() != PixelFormat_BGRA32)
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 throw OrthancException(ErrorCode_NotImplemented);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 int a = x;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
2379
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
259 #if ORTHANC_ENABLE_LOCALE == 1
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 std::string s = Toolbox::ConvertFromUtf8(utf8, Encoding_Latin1);
2379
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
261 #else
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
262 // If the locale support is disabled, simply drop non-ASCII
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
263 // characters from the source UTF-8 string
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
264 std::string s = Toolbox::ConvertToAscii(utf8);
4900688827a8 reorganization in CMake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
265 #endif
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 for (size_t i = 0; i < s.size(); i++)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 if (s[i] == '\n')
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 // Go to the next line
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 a = x;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 y += maxHeight_ + 1;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 else
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 Characters::const_iterator c = characters_.find(s[i]);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 if (c != characters_.end())
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 DrawCharacter(target, *c->second, a, y + static_cast<int>(c->second->top_), color);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 a += c->second->advance_;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287
1611
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
288 void Font::Draw(ImageAccessor& target,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
289 const std::string& utf8,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
290 int x,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
291 int y,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
292 uint8_t grayscale) const
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 uint8_t color[4] = { grayscale, grayscale, grayscale, 255 };
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 DrawInternal(target, utf8, x, y, color);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298
1611
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
299 void Font::Draw(ImageAccessor& target,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
300 const std::string& utf8,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
301 int x,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
302 int y,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
303 uint8_t r,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
304 uint8_t g,
5e9b2aac8b89 fix for visual studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1610
diff changeset
305 uint8_t b) const
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 {
2853
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
307 uint8_t color[4];
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
308
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
309 switch (target.GetFormat())
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
310 {
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
311 case PixelFormat_BGRA32:
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
312 color[0] = b;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
313 color[1] = g;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
314 color[2] = r;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
315 color[3] = 255;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
316 break;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
317
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
318 default:
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
319 color[0] = r;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
320 color[1] = g;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
321 color[2] = b;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
322 color[3] = 255;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
323 break;
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
324 }
52b017d22a4f New URI: "/studies/.../merge" to merge a study
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
325
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 DrawInternal(target, utf8, x, y, color);
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
329
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
330 void Font::ComputeTextExtent(unsigned int& width,
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
331 unsigned int& height,
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
332 const std::string& utf8) const
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
333 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
334 width = 0;
2914
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2913
diff changeset
335 height = 0;
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
336
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
337 #if ORTHANC_ENABLE_LOCALE == 1
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
338 std::string s = Toolbox::ConvertFromUtf8(utf8, Encoding_Latin1);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
339 #else
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
340 // If the locale support is disabled, simply drop non-ASCII
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
341 // characters from the source UTF-8 string
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
342 std::string s = Toolbox::ConvertToAscii(utf8);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
343 #endif
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
344
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
345 // Compute the text extent
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
346 unsigned int x = 0;
2901
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2900
diff changeset
347 unsigned int y = 0;
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
348
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
349 for (size_t i = 0; i < s.size(); i++)
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
350 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
351 if (s[i] == '\n')
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
352 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
353 // Go to the next line
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
354 x = 0;
2901
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2900
diff changeset
355 y += (maxHeight_ + 1);
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
356 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
357 else
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
358 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
359 Characters::const_iterator c = characters_.find(s[i]);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
360 if (c != characters_.end())
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
361 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
362 x += c->second->advance_;
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
363
2901
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2900
diff changeset
364 unsigned int bottom = y + c->second->top_ + c->second->height_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2900
diff changeset
365 if (bottom > height)
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
366 {
2901
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2900
diff changeset
367 height = bottom;
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
368 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
369
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
370 if (x > width)
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
371 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
372 width = x;
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
373 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
374 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
375 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
376 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
377 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
378
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
379
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
380 ImageAccessor* Font::Render(const std::string& utf8,
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
381 PixelFormat format,
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
382 uint8_t r,
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
383 uint8_t g,
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
384 uint8_t b) const
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
385 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
386 unsigned int width, height;
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
387 ComputeTextExtent(width, height, utf8);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
388
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
389 std::unique_ptr<ImageAccessor> target(new Image(format, width, height, false));
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
390 ImageProcessing::Set(*target, 0, 0, 0, 255);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
391 Draw(*target, utf8, 0, 0, r, g, b);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
392
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
393 return target.release();
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
394 }
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
395
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
396
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
397 ImageAccessor* Font::RenderAlpha(const std::string& utf8) const
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
398 {
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
399 unsigned int width, height;
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
400 ComputeTextExtent(width, height, utf8);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
401
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
402 std::unique_ptr<ImageAccessor> target(new Image(PixelFormat_Grayscale8, width, height, false));
2900
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
403 ImageProcessing::Set(*target, 0);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
404 Draw(*target, utf8, 0, 0, 255);
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
405
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
406 return target.release();
668d5ad73c74 Font::Render()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2853
diff changeset
407 }
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 }