1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "renderer/win32/win32_font_util.h"
31
32 #include "base/util.h"
33 #include "protocol/renderer_command.pb.h"
34
35 namespace mozc {
36 namespace win32 {
37
ToWinLogFont(const LOGFONTW & log_font,mozc::commands::RendererCommand_WinLogFont * win_log_font)38 bool FontUtil::ToWinLogFont(
39 const LOGFONTW &log_font,
40 mozc::commands::RendererCommand_WinLogFont *win_log_font) {
41 if (win_log_font == nullptr) {
42 return false;
43 }
44
45 win_log_font->Clear();
46 win_log_font->set_height(log_font.lfHeight);
47 win_log_font->set_width(log_font.lfWidth);
48 win_log_font->set_escapement(log_font.lfEscapement);
49 win_log_font->set_orientation(log_font.lfOrientation);
50 win_log_font->set_weight(log_font.lfWeight);
51 win_log_font->set_italic(log_font.lfItalic != FALSE);
52 win_log_font->set_underline(log_font.lfUnderline != FALSE);
53 win_log_font->set_strike_out(log_font.lfStrikeOut != FALSE);
54 win_log_font->set_char_set(log_font.lfCharSet);
55 win_log_font->set_out_precision(log_font.lfOutPrecision);
56 win_log_font->set_clip_precision(log_font.lfClipPrecision);
57 win_log_font->set_quality(log_font.lfQuality);
58 win_log_font->set_pitch_and_family(log_font.lfPitchAndFamily);
59
60 // make sure |log_font.lfFaceName| is properly null-terminated.
61 bool null_terminated = false;
62 for (size_t i = 0; i < arraysize(log_font.lfFaceName); ++i) {
63 if (log_font.lfFaceName[i] == L'\0') {
64 null_terminated = true;
65 break;
66 }
67 }
68
69 if (!null_terminated) {
70 return false;
71 }
72
73 string name;
74 mozc::Util::WideToUTF8(log_font.lfFaceName, &name);
75 win_log_font->set_face_name(name);
76
77 return true;
78 }
79
ToLOGFONT(const mozc::commands::RendererCommand_WinLogFont & win_log_font,LOGFONTW * log_font)80 bool FontUtil::ToLOGFONT(
81 const mozc::commands::RendererCommand_WinLogFont &win_log_font,
82 LOGFONTW *log_font) {
83 if (log_font == nullptr) {
84 return false;
85 }
86 if (!win_log_font.IsInitialized()) {
87 return false;
88 }
89
90 ::ZeroMemory(log_font, sizeof(log_font));
91
92 log_font->lfHeight = win_log_font.height();
93 log_font->lfWidth = win_log_font.width();
94 log_font->lfEscapement = win_log_font.escapement();
95 log_font->lfOrientation = win_log_font.orientation();
96 log_font->lfWeight = win_log_font.weight();
97 log_font->lfItalic = win_log_font.italic() ? TRUE : FALSE;
98 log_font->lfUnderline = win_log_font.underline() ? TRUE : FALSE;
99 log_font->lfStrikeOut = win_log_font.strike_out() ? TRUE : FALSE;
100 log_font->lfCharSet = win_log_font.char_set();
101 log_font->lfOutPrecision = win_log_font.out_precision();
102 log_font->lfClipPrecision = win_log_font.clip_precision();
103 log_font->lfQuality = win_log_font.quality();
104 log_font->lfPitchAndFamily = win_log_font.pitch_and_family();
105
106 std::wstring face_name;
107 mozc::Util::UTF8ToWide(win_log_font.face_name(), &face_name);
108
109 // Although the result of mozc::Util::UTF8ToWide never contains any null
110 // character, make sure it again just for the safety.
111 face_name += L'\0';
112 face_name = face_name.substr(0, face_name.find(L'\0') + 1);
113
114 // +1 is for nullptr.
115 if (face_name.size() + 1 > arraysize(log_font->lfFaceName)) {
116 return false;
117 }
118
119 for (size_t i = 0; i < arraysize(log_font->lfFaceName); ++i) {
120 if (i < face_name.size()) {
121 log_font->lfFaceName[i] = face_name[i];
122 } else {
123 log_font->lfFaceName[i] = L'\0';
124 }
125 }
126
127 return true;
128 }
129
130 } // namespace win32
131 } // namespace mozc
132