Skip to content

Commit 67cede2

Browse files
Use renderer_style.textproto file to specify the design of candidate window.
* Support the --as_string_view flag to embed_file.py * Embed the textproto data from renderer_style.textproto instead of hard-coding. #codehealth PiperOrigin-RevId: 923207099
1 parent 1f17c85 commit 67cede2

5 files changed

Lines changed: 160 additions & 65 deletions

File tree

src/build_tools/embed_file.bzl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@
3131

3232
load("//bazel:run_build_tool.bzl", "mozc_run_build_tool")
3333

34-
def mozc_embed_file(name, srcs, out, var_name, **kwargs):
34+
def mozc_embed_file(name, srcs, out, var_name, as_string_view = False, **kwargs):
35+
args = ["--name=" + var_name]
36+
if as_string_view:
37+
args.append("--as_string_view")
38+
3539
mozc_run_build_tool(
3640
name = name,
3741
srcs = {
3842
"--input": srcs,
3943
},
40-
args = [
41-
"--name=" + var_name,
42-
],
44+
args = args,
4345
outs = {"--output": out},
4446
tool = "//build_tools:embed_file",
4547
**kwargs

src/build_tools/embed_file.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"""Generate a C++ array definition for file embedding."""
3232
import argparse
3333
import os
34+
import sys
3435

3536

3637
def _ParseArgs():
@@ -41,9 +42,54 @@ def _ParseArgs():
4142
parser.add_argument(
4243
'--output', type=argparse.FileType(mode='w', encoding='utf-8')
4344
)
45+
parser.add_argument('--as_string_view', action='store_true')
4446
return parser.parse_args()
4547

4648

49+
def _EmbedAsStringView(input_path, name, output_file):
50+
"""Embed file as absl::string_view using Raw String Literal."""
51+
# Load input file as binary to validate UTF-8 and Null bytes
52+
with open(input_path, 'rb') as infile:
53+
data = infile.read()
54+
55+
# 1. Validate invalid UTF-8 byte sequence
56+
try:
57+
content = data.decode('utf-8', errors='strict')
58+
except UnicodeDecodeError as e:
59+
sys.stderr.write(
60+
f'Error: {input_path} contains invalid UTF-8 byte sequence: {e}\n'
61+
)
62+
sys.exit(1)
63+
64+
# 2. Validate Null byte
65+
if '\0' in content:
66+
sys.stderr.write(
67+
f'Error: {input_path} contains a null byte (\\0), which is not'
68+
' allowed in as_string_view mode.\n'
69+
)
70+
sys.exit(1)
71+
72+
# 3. Validate delimiter conflict
73+
delimiter_conflict = f'){name}'
74+
if delimiter_conflict in content:
75+
sys.stderr.write(
76+
f'Error: {input_path} contains the delimiter string'
77+
f" '{delimiter_conflict}', which conflicts with the C++ Raw String"
78+
' delimiter.\n'
79+
)
80+
sys.exit(1)
81+
82+
output_file.write("""\
83+
#ifdef MOZC_EMBEDDED_FILE_%(name)s
84+
#error "%(name)s was already included or defined elsewhere"
85+
#else
86+
#define MOZC_EMBEDDED_FILE_%(name)s
87+
constexpr absl::string_view %(name)s = R"%(name)s(
88+
%(content)s)%(name)s";
89+
#endif // MOZC_EMBEDDED_FILE_%(name)s
90+
""" % {'name': name, 'content': content})
91+
92+
4793
def _EmbedAsUint64Array(input_path, name, output_file):
4894
"""Embed file as uint64_t array using EmbeddedFile struct."""
4995

@@ -81,7 +127,10 @@ def _EmbedAsUint64Array(input_path, name, output_file):
81127

82128
def main():
83129
args = _ParseArgs()
84-
_EmbedAsUint64Array(args.input, args.name, args.output)
130+
if args.as_string_view:
131+
_EmbedAsStringView(args.input, args.name, args.output)
132+
else:
133+
_EmbedAsUint64Array(args.input, args.name, args.output)
85134

86135

87136
if __name__ == '__main__':

src/renderer/BUILD.bazel

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ load(
4040
"BRANDING",
4141
"MACOS_BUNDLE_ID_PREFIX",
4242
)
43+
load(
44+
"//build_tools:embed_file.bzl",
45+
"mozc_embed_file",
46+
)
4347

4448
package(default_visibility = ["//visibility:private"])
4549

@@ -229,9 +233,20 @@ mozc_cc_test(
229233
],
230234
)
231235

236+
mozc_embed_file(
237+
name = "renderer_style_data",
238+
srcs = ["renderer_style.textproto"],
239+
out = "renderer_style.inc",
240+
as_string_view = True,
241+
var_name = "kStyleTextProto",
242+
)
243+
232244
mozc_cc_library(
233245
name = "renderer_style_handler",
234-
srcs = ["renderer_style_handler.cc"],
246+
srcs = [
247+
"renderer_style_handler.cc",
248+
":renderer_style_data",
249+
],
235250
hdrs = ["renderer_style_handler.h"],
236251
visibility = ["//renderer:__subpackages__"],
237252
deps = [
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright 2010-2021, 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+
# proto-file: third_party/mozc/src/protocol/renderer_style.proto
31+
# proto-message: RendererStyle
32+
window_border: 1
33+
scrollbar_width: 4
34+
row_rect_padding: 0
35+
border_color { r: 150 g: 150 b: 150 }
36+
# text_style of shortcut
37+
text_styles {
38+
font_size: 14
39+
foreground_color { r: 119 g: 119 b: 119 }
40+
background_color { r: 243 g: 244 b: 255 }
41+
left_padding: 8
42+
right_padding: 8
43+
}
44+
# text_style of gap1
45+
text_styles { font_size: 14 }
46+
# text_style of candidate
47+
text_styles { font_size: 14 }
48+
# text_style of description
49+
text_styles {
50+
font_size: 12
51+
foreground_color { r: 136 g: 136 b: 136 }
52+
right_padding: 8
53+
}
54+
# We want to ensure that the candidate window is at least wide
55+
# enough to render "そのほかの文字種 " as a candidate.
56+
column_minimum_width_string: "そのほかの文字種 "
57+
footer_style { font_size: 14 left_padding: 4 right_padding: 4 }
58+
footer_sub_label_style {
59+
font_size: 10
60+
foreground_color { r: 167 g: 167 b: 167 }
61+
left_padding: 4
62+
right_padding: 4
63+
}
64+
footer_border_colors { r: 96 g: 96 b: 96 }
65+
footer_top_color { r: 255 g: 255 b: 255 }
66+
footer_bottom_color { r: 238 g: 238 b: 238 }
67+
logo_file_name: "candidate_window_logo.tiff"
68+
focused_background_color { r: 209 g: 234 b: 255 }
69+
focused_border_color { r: 127 g: 172 b: 221 }
70+
scrollbar_background_color { r: 224 g: 224 b: 224 }
71+
scrollbar_indicator_color { r: 117 g: 144 b: 184 }
72+
infolist_style {
73+
caption_string: "用例"
74+
caption_height: 20
75+
caption_padding: 1
76+
caption_style { font_size: 12 left_padding: 2 }
77+
caption_background_color { r: 236 g: 240 b: 250 }
78+
window_border: 1
79+
row_rect_padding: 2
80+
window_width: 300
81+
title_style { font_size: 15 left_padding: 5 }
82+
description_style { font_size: 12 left_padding: 15 }
83+
border_color { r: 150 g: 150 b: 150 }
84+
focused_background_color { r: 209 g: 234 b: 255 }
85+
focused_border_color { r: 127 g: 172 b: 221 }
86+
}

src/renderer/renderer_style_handler.cc

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,65 +38,8 @@ namespace mozc {
3838
namespace renderer {
3939

4040
namespace {
41-
constexpr absl::string_view kStyleTextProto = R"pb(
42-
# proto-file: protocol/renderer_style.proto
43-
# proto-message: RendererStyle
44-
window_border: 1
45-
scrollbar_width: 4
46-
row_rect_padding: 0
47-
border_color { r: 150 g: 150 b: 150 }
48-
# text_style of shortcut
49-
text_styles {
50-
font_size: 14
51-
foreground_color { r: 119 g: 119 b: 119 }
52-
background_color { r: 243 g: 244 b: 255 }
53-
left_padding: 8
54-
right_padding: 8
55-
}
56-
# text_style of gap1
57-
text_styles { font_size: 14 }
58-
# text_style of candidate
59-
text_styles { font_size: 14 }
60-
# text_style of description
61-
text_styles {
62-
font_size: 12
63-
foreground_color { r: 136 g: 136 b: 136 }
64-
right_padding: 8
65-
}
66-
# We want to ensure that the candidate window is at least wide
67-
# enough to render "そのほかの文字種 " as a candidate.
68-
column_minimum_width_string: "そのほかの文字種 "
69-
footer_style { font_size: 14 left_padding: 4 right_padding: 4 }
70-
footer_sub_label_style {
71-
font_size: 10
72-
foreground_color { r: 167 g: 167 b: 167 }
73-
left_padding: 4
74-
right_padding: 4
75-
}
76-
footer_border_colors { r: 96 g: 96 b: 96 }
77-
footer_top_color { r: 255 g: 255 b: 255 }
78-
footer_bottom_color { r: 238 g: 238 b: 238 }
79-
logo_file_name: "candidate_window_logo.tiff"
80-
focused_background_color { r: 209 g: 234 b: 255 }
81-
focused_border_color { r: 127 g: 172 b: 221 }
82-
scrollbar_background_color { r: 224 g: 224 b: 224 }
83-
scrollbar_indicator_color { r: 117 g: 144 b: 184 }
84-
infolist_style {
85-
caption_string: "用例"
86-
caption_height: 20
87-
caption_padding: 1
88-
caption_style { font_size: 12 left_padding: 2 }
89-
caption_background_color { r: 236 g: 240 b: 250 }
90-
window_border: 1
91-
row_rect_padding: 2
92-
window_width: 300
93-
title_style { font_size: 15 left_padding: 5 }
94-
description_style { font_size: 12 left_padding: 15 }
95-
border_color { r: 150 g: 150 b: 150 }
96-
focused_background_color { r: 209 g: 234 b: 255 }
97-
focused_border_color { r: 127 g: 172 b: 221 }
98-
}
99-
)pb";
41+
// absl::string_view kStyleTextProto is defined in renderer_style.inc.
42+
#include "renderer/renderer_style.inc"
10043
} // namespace
10144

10245
void RendererStyleHandler::GetRendererStyle(RendererStyle* style) {

0 commit comments

Comments
 (0)