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/renderer_style_handler.h"
31 
32 #if defined(OS_WIN)
33 #include <windows.h>
34 #define _ATL_NO_AUTOMATIC_NAMESPACE
35 #define _WTL_NO_AUTOMATIC_NAMESPACE
36 #include <atlbase.h>
37 #include <atlapp.h>
38 #include <atlgdi.h>
39 #endif  // OS_WIN
40 
41 #include "base/singleton.h"
42 #include "protocol/renderer_style.pb.h"
43 
44 namespace mozc {
45 namespace renderer {
46 namespace {
47 #if defined(OS_WIN)
48 const int kDefaultDPI = 96;
49 #endif  // OS_WIN
50 
51 class RendererStyleHandlerImpl {
52  public:
53   RendererStyleHandlerImpl();
~RendererStyleHandlerImpl()54   ~RendererStyleHandlerImpl() {}
55   bool GetRendererStyle(RendererStyle *style);
56   bool SetRendererStyle(const RendererStyle &style);
57   void GetDefaultRendererStyle(RendererStyle *style);
58  private:
59   RendererStyle style_;
60 };
61 
GetRendererStyleHandlerImpl()62 RendererStyleHandlerImpl *GetRendererStyleHandlerImpl() {
63   return Singleton<RendererStyleHandlerImpl>::get();
64 }
65 
RendererStyleHandlerImpl()66 RendererStyleHandlerImpl::RendererStyleHandlerImpl() {
67   RendererStyle style;
68   GetDefaultRendererStyle(&style);
69   SetRendererStyle(style);
70 }
71 
GetRendererStyle(RendererStyle * style)72 bool RendererStyleHandlerImpl::GetRendererStyle(RendererStyle *style) {
73   style->CopyFrom(this->style_);
74   return true;
75 }
SetRendererStyle(const RendererStyle & style)76 bool RendererStyleHandlerImpl::SetRendererStyle(const RendererStyle &style) {
77   style_.CopyFrom(style);
78   return true;
79 }
GetDefaultRendererStyle(RendererStyle * style)80 void RendererStyleHandlerImpl::GetDefaultRendererStyle(
81     RendererStyle *style) {
82   double scale_factor_x = 1.0;
83   double scale_factor_y = 1.0;
84   RendererStyleHandler::GetDPIScalingFactor(&scale_factor_x,
85                                             &scale_factor_y);
86 
87   // TODO(horo): Change to read from human-readable ASCII format protobuf.
88   style->Clear();
89   style->set_window_border(1);  // non-scalable
90   style->set_scrollbar_width(4 * scale_factor_x);
91   style->set_row_rect_padding(0 * scale_factor_x);
92   style->mutable_border_color()->set_r(0x96);
93   style->mutable_border_color()->set_g(0x96);
94   style->mutable_border_color()->set_b(0x96);
95 
96   RendererStyle::TextStyle *shortcutStyle = style->add_text_styles();
97   shortcutStyle->set_font_size(14 * scale_factor_y);
98   shortcutStyle->mutable_foreground_color()->set_r(0x77);
99   shortcutStyle->mutable_foreground_color()->set_g(0x77);
100   shortcutStyle->mutable_foreground_color()->set_b(0x77);
101   shortcutStyle->mutable_background_color()->set_r(0xf3);
102   shortcutStyle->mutable_background_color()->set_g(0xf4);
103   shortcutStyle->mutable_background_color()->set_b(0xff);
104   shortcutStyle->set_left_padding(8 * scale_factor_x);
105   shortcutStyle->set_right_padding(8 * scale_factor_x);
106 
107   RendererStyle::TextStyle *gap1Style = style->add_text_styles();
108   gap1Style->set_font_size(14 * scale_factor_y);
109 
110   RendererStyle::TextStyle *candidateStyle = style->add_text_styles();
111   candidateStyle->set_font_size(14 * scale_factor_y);
112 
113   RendererStyle::TextStyle *descriptionStyle = style->add_text_styles();
114   descriptionStyle->set_font_size(12 * scale_factor_y);
115   descriptionStyle->mutable_foreground_color()->set_r(0x88);
116   descriptionStyle->mutable_foreground_color()->set_g(0x88);
117   descriptionStyle->mutable_foreground_color()->set_b(0x88);
118   descriptionStyle->set_right_padding(8 * scale_factor_x);
119 
120   // We want to ensure that the candidate window is at least wide
121   // enough to render "そのほかの文字種  " as a candidate.
122   style->set_column_minimum_width_string("そのほかの文字種  ");
123 
124   style->mutable_footer_style()->set_font_size(14 * scale_factor_y);
125   style->mutable_footer_style()->set_left_padding(4 * scale_factor_x);
126   style->mutable_footer_style()->set_right_padding(4 * scale_factor_x);
127 
128   RendererStyle::TextStyle *footer_sub_label_style =
129     style->mutable_footer_sub_label_style();
130   footer_sub_label_style->set_font_size(10 * scale_factor_y);
131   footer_sub_label_style->mutable_foreground_color()->set_r(167);
132   footer_sub_label_style->mutable_foreground_color()->set_g(167);
133   footer_sub_label_style->mutable_foreground_color()->set_b(167);
134   footer_sub_label_style->set_left_padding(4 * scale_factor_x);
135   footer_sub_label_style->set_right_padding(4 * scale_factor_x);
136 
137   RendererStyle::RGBAColor *color = style->add_footer_border_colors();
138   color->set_r(96);
139   color->set_r(96);
140   color->set_r(96);
141 
142   style->mutable_footer_top_color()->set_r(0xff);
143   style->mutable_footer_top_color()->set_g(0xff);
144   style->mutable_footer_top_color()->set_b(0xff);
145 
146   style->mutable_footer_bottom_color()->set_r(0xee);
147   style->mutable_footer_bottom_color()->set_g(0xee);
148   style->mutable_footer_bottom_color()->set_b(0xee);
149 
150   style->set_logo_file_name("candidate_window_logo.tiff");
151 
152   style->mutable_focused_background_color()->set_r(0xd1);
153   style->mutable_focused_background_color()->set_g(0xea);
154   style->mutable_focused_background_color()->set_b(0xff);
155 
156   style->mutable_focused_border_color()->set_r(0x7f);
157   style->mutable_focused_border_color()->set_g(0xac);
158   style->mutable_focused_border_color()->set_b(0xdd);
159 
160   style->mutable_scrollbar_background_color()->set_r(0xe0);
161   style->mutable_scrollbar_background_color()->set_g(0xe0);
162   style->mutable_scrollbar_background_color()->set_b(0xe0);
163 
164   style->mutable_scrollbar_indicator_color()->set_r(0x75);
165   style->mutable_scrollbar_indicator_color()->set_g(0x90);
166   style->mutable_scrollbar_indicator_color()->set_b(0xb8);
167 
168   RendererStyle::InfolistStyle *infostyle = style->mutable_infolist_style();
169   infostyle->set_caption_string("用例");
170   infostyle->set_caption_height(20 * scale_factor_y);
171   infostyle->set_caption_padding(1);
172   infostyle->mutable_caption_style()->set_font_size(12 * scale_factor_y);
173   infostyle->mutable_caption_style()->set_left_padding(2 * scale_factor_x);
174   infostyle->mutable_caption_background_color()->set_r(0xec);
175   infostyle->mutable_caption_background_color()->set_g(0xf0);
176   infostyle->mutable_caption_background_color()->set_b(0xfa);
177 
178   infostyle->set_window_border(1);  // non-scalable
179   infostyle->set_row_rect_padding(2 * scale_factor_x);
180   infostyle->set_window_width(300 * scale_factor_x);
181   infostyle->mutable_title_style()->set_font_size(15 * scale_factor_y);
182   infostyle->mutable_title_style()->set_left_padding(5 * scale_factor_x);
183   infostyle->mutable_description_style()->set_font_size(12 * scale_factor_y);
184   infostyle->mutable_description_style()->set_left_padding(
185       15 * scale_factor_x);
186   infostyle->mutable_border_color()->set_r(0x96);
187   infostyle->mutable_border_color()->set_g(0x96);
188   infostyle->mutable_border_color()->set_b(0x96);
189   infostyle->mutable_focused_background_color()->set_r(0xd1);
190   infostyle->mutable_focused_background_color()->set_g(0xea);
191   infostyle->mutable_focused_background_color()->set_b(0xff);
192   infostyle->mutable_focused_border_color()->set_r(0x7f);
193   infostyle->mutable_focused_border_color()->set_g(0xac);
194   infostyle->mutable_focused_border_color()->set_b(0xdd);
195 }
196 }  // namespace
197 
GetRendererStyle(RendererStyle * style)198 bool RendererStyleHandler::GetRendererStyle(RendererStyle *style) {
199   return GetRendererStyleHandlerImpl()->GetRendererStyle(style);
200 }
SetRendererStyle(const RendererStyle & style)201 bool RendererStyleHandler::SetRendererStyle(const RendererStyle &style) {
202   return GetRendererStyleHandlerImpl()->SetRendererStyle(style);
203 }
GetDefaultRendererStyle(RendererStyle * style)204 void RendererStyleHandler::GetDefaultRendererStyle(RendererStyle *style) {
205   return GetRendererStyleHandlerImpl()->GetDefaultRendererStyle(style);
206 }
207 
GetDPIScalingFactor(double * x,double * y)208 void RendererStyleHandler::GetDPIScalingFactor(double *x, double *y) {
209 #if defined OS_WIN
210   WTL::CDC desktop_dc(::GetDC(nullptr));
211   const int dpi_x = desktop_dc.GetDeviceCaps(LOGPIXELSX);
212   const int dpi_y = desktop_dc.GetDeviceCaps(LOGPIXELSY);
213   if (x != nullptr) {
214     *x = static_cast<double>(dpi_x) / kDefaultDPI;
215   }
216   if (y != nullptr) {
217     *y = static_cast<double>(dpi_y) / kDefaultDPI;
218   }
219 #else
220   if (x != nullptr) {
221     *x = 1.0;
222   }
223   if (y != nullptr) {
224     *y = 1.0;
225   }
226 #endif
227 }
228 
229 }  // namespace renderer
230 }  // namespace mozc
231