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/unix/text_renderer.h"
31
32 #include "base/coordinates.h"
33 #include "renderer/unix/font_spec.h"
34 #include "renderer/unix/pango_wrapper.h"
35
36 namespace mozc {
37 namespace renderer {
38 namespace gtk {
39
TextRenderer(FontSpecInterface * font_spec)40 TextRenderer::TextRenderer(FontSpecInterface *font_spec)
41 : font_spec_(font_spec),
42 pango_(nullptr) {
43 }
44
Initialize(GdkDrawable * drawable)45 void TextRenderer::Initialize(GdkDrawable *drawable) {
46 pango_.reset(new PangoWrapper(drawable));
47 }
48
SetUpPangoLayout(const string & str,FontSpecInterface::FONT_TYPE font_type,PangoLayoutWrapperInterface * layout)49 void TextRenderer::SetUpPangoLayout(const string &str,
50 FontSpecInterface::FONT_TYPE font_type,
51 PangoLayoutWrapperInterface *layout) {
52 PangoAttrList *attributes = pango_->CopyAttributes(
53 font_spec_->GetFontAttributes(font_type));
54 layout->SetText(str.c_str());
55 layout->SetAlignment(font_spec_->GetFontAlignment(font_type));
56 layout->SetAttributes(attributes);
57 layout->SetFontDescription(font_spec_->GetFontDescription(font_type));
58 pango_->AttributesUnref(attributes);
59 }
60
GetPixelSize(FontSpecInterface::FONT_TYPE font_type,const string & str)61 Size TextRenderer::GetPixelSize(FontSpecInterface::FONT_TYPE font_type,
62 const string &str) {
63 PangoLayoutWrapper layout(pango_->GetContext());
64 return GetPixelSizeInternal(font_type, str, &layout);
65 }
66
GetPixelSizeInternal(FontSpecInterface::FONT_TYPE font_type,const string & str,PangoLayoutWrapperInterface * layout)67 Size TextRenderer::GetPixelSizeInternal(FontSpecInterface::FONT_TYPE font_type,
68 const string &str,
69 PangoLayoutWrapperInterface *layout) {
70 SetUpPangoLayout(str, font_type, layout);
71 return layout->GetPixelSize();
72 }
73
GetMultiLinePixelSize(FontSpecInterface::FONT_TYPE font_type,const string & str,const int width)74 Size TextRenderer::GetMultiLinePixelSize(FontSpecInterface::FONT_TYPE font_type,
75 const string &str,
76 const int width) {
77 PangoLayoutWrapper layout(pango_->GetContext());
78 return GetMultiLinePixelSizeInternal(font_type, str, width, &layout);
79 }
80
GetMultiLinePixelSizeInternal(FontSpecInterface::FONT_TYPE font_type,const string & str,const int width,PangoLayoutWrapperInterface * layout)81 Size TextRenderer::GetMultiLinePixelSizeInternal(
82 FontSpecInterface::FONT_TYPE font_type,
83 const string &str,
84 const int width,
85 PangoLayoutWrapperInterface *layout) {
86 SetUpPangoLayout(str, font_type, layout);
87 layout->SetWidth(width * PANGO_SCALE);
88 return layout->GetPixelSize();
89 }
90
RenderText(const string & text,const Rect & rect,FontSpecInterface::FONT_TYPE font_type)91 void TextRenderer::RenderText(const string &text,
92 const Rect &rect,
93 FontSpecInterface::FONT_TYPE font_type) {
94 PangoLayoutWrapper layout(pango_->GetContext());
95 RenderTextInternal(text, rect, font_type, &layout);
96 }
97
RenderTextInternal(const string & text,const Rect & rect,FontSpecInterface::FONT_TYPE font_type,PangoLayoutWrapperInterface * layout)98 void TextRenderer::RenderTextInternal(const string& text,
99 const Rect &rect,
100 FontSpecInterface::FONT_TYPE font_type,
101 PangoLayoutWrapperInterface *layout) {
102 SetUpPangoLayout(text, font_type, layout);
103 layout->SetWidth(rect.size.width * PANGO_SCALE);
104 layout->SetHeight(rect.size.height * PANGO_SCALE);
105
106 // Vertical centering.
107 Size actual_size = layout->GetPixelSize();
108 const int delta_y = (rect.size.height - actual_size.height) / 2;
109
110 pango_->RendererDrawLayout(layout, rect.origin.x * PANGO_SCALE,
111 (rect.origin.y + delta_y) * PANGO_SCALE);
112 }
113
ReloadFontConfig(const string & font_description)114 void TextRenderer::ReloadFontConfig(const string &font_description) {
115 font_spec_->Reload(font_description);
116 }
117 } // namespace gtk
118 } // namespace renderer
119 } // namespace mozc
120