Blender V5.3
wm_window_ime_draw.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#ifdef WITH_INPUT_IME
14
15# include <algorithm>
16# include <optional>
17
18# include "BLI_assert.hh"
19# include "BLI_index_range.hh"
20# include "BLI_string_utf8.hh"
21
22# include "DNA_screen_types.h"
23# include "DNA_vec_types.h"
24
25# include "BKE_screen.hh"
26
27# include "BLF_api.hh"
28# include "GPU_immediate.hh"
29# include "GPU_state.hh"
30
31# include "ED_ime.hh"
32# include "UI_resources.hh"
33
34# include "WM_api.hh"
35# include "WM_types.hh"
36
37namespace blender::ed::ime {
38
44static void cursor_overlay_draw(const wmIMEData &ime_data,
45 const rcti &cursor_rect,
46 const int font_size,
47 const int region_size[2],
48 const float pixelsize)
49{
50 BLI_assert(!ime_data.composite.empty());
51 const char *str = ime_data.composite.c_str();
52 const int str_len = int(ime_data.composite.size());
53
54 const int line_height = cursor_rect.ymax - cursor_rect.ymin;
55
56 /* NOTE(@ideasman42): the font is currently hard-coded & mono,
57 * which matches the text editor & console but 3D text or text strips
58 * could use custom fonts. Variable width support would be a little more involved here. */
59 const int font_id = blf_mono_font;
60 BLF_size(font_id, font_size);
61 const int char_width = std::max(int(BLF_fixed_width(font_id)), 1);
62
63 auto column_from_byte_offset_fn = [&](const int64_t byte_offset) {
64 return BLI_str_utf8_offset_to_column(str, size_t(str_len), int(byte_offset));
65 };
66 const int columns = column_from_byte_offset_fn(str_len);
67
68 /* Snap the line width to whole pixels, every edge below is an integer so the rectangles
69 * rasterize to exact pixel bounds, see #wmOrtho2_region_pixelspace. */
70 const int border = std::max(int(pixelsize), 1);
71
72 /* Place a one-line box at the cursor, where the text lands once the composition is accepted,
73 * so accepting it doesn't shift the text sideways. Clamped inside the region. */
74 const int box_width = columns * char_width;
75 const int box_x = std::clamp(cursor_rect.xmin, 0, std::max(region_size[0] - box_width, 0));
76 const int box_y = std::clamp(cursor_rect.ymin, 0, std::max(region_size[1] - line_height, 0));
77 const rcti box = {
78 .xmin = box_x,
79 .xmax = box_x + box_width,
80 .ymin = box_y,
81 .ymax = box_y + line_height,
82 };
83
84 /* Lift the baseline by the descender so glyphs fit the box, the underlines fill the gap
85 * below. */
86 const int baseline_y = box.ymin - BLF_descender(font_id);
87 const int underline_y = box.ymin;
88
89 GPU_blend(GPU_BLEND_ALPHA);
90
91 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
92 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
93 /* Draw the background & outline, so the preview covers whatever is behind it.
94 * The outline doesn't extend left, that would overlap the cursor and the text before it. */
95 immUniformThemeColor(TH_SHADE1);
96 immRectf(pos, box.xmin, box.ymin - border, box.xmax + border, box.ymax + border);
97 immUniformThemeColor(TH_BACK);
98 immRectf(pos, box.xmin, box.ymin, box.xmax, box.ymax);
100
101 ui::theme::font_theme_color_set(font_id, TH_TEXT);
102 BLF_position(font_id, box.xmin, baseline_y, 0);
103 BLF_draw_mono(font_id, str, size_t(str_len), char_width, 1);
104
105 pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
106 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
107
108 /* Draw a thin underline across the whole composite. */
109 immUniformThemeColor(TH_TEXT);
110 immRectf(pos, box.xmin, underline_y, box.xmax, underline_y + border);
111
112 /* Draw the thick line under the selection. */
113 if (const std::optional<IndexRange> sel_range = WM_window_IME_composite_select_range(&ime_data))
114 {
115 /* Byte offsets convert to columns in order, so the start can't come after the end. */
116 const IndexRange sel_cols = IndexRange::from_begin_end(
117 column_from_byte_offset_fn(sel_range->start()),
118 column_from_byte_offset_fn(sel_range->one_after_last()))
119 .intersect(IndexRange(columns));
120 if (!sel_cols.is_empty()) {
121 immRectf(pos,
122 box.xmin + int(sel_cols.start()) * char_width,
123 underline_y,
124 box.xmin + int(sel_cols.one_after_last()) * char_width,
125 underline_y + (2 * border));
126 }
127 }
128
129 /* Draw the cursor within the composite. */
130 if (!ime_data.composite.empty() && (ime_data.cursor_pos != -1)) {
131 const int cursor_width = 2 * border;
132 const int cursor_x = box.xmin +
133 std::min(column_from_byte_offset_fn(ime_data.cursor_pos) * char_width,
134 std::max(box_width - cursor_width, 0));
135 immUniformThemeColor(TH_WIDGET_TEXT_CURSOR);
136 immRectf(pos, cursor_x, box.ymin, cursor_x + cursor_width, box.ymax);
137 }
139
140 GPU_blend(GPU_BLEND_NONE);
141}
142
143void region_overlay_draw(wmWindow *win,
144 const ARegion *region,
145 const float pixelsize,
146 const ARegionIMECursor &cursor)
147{
148 const wmIMEData *ime_data = WM_window_IME_data_get(win, region);
149 if (ime_data == nullptr) {
150 return;
151 }
152 const int region_size[2] = {region->winx, region->winy};
153 cursor_overlay_draw(*ime_data, cursor.rect, cursor.font_size, region_size, pixelsize);
154}
155
156} // namespace blender::ed::ime
157
158#endif /* WITH_INPUT_IME */
#define BLI_assert(a)
long long int int64_t
#define str(s)
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:390
unsigned int uint
Definition BKE_text.h:55
GPUVertFormat * immVertexFormat()
void immUnbindProgram()
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
int BLI_str_utf8_offset_to_column(const char *str, size_t str_len, int offset_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void immUniformThemeColor(int color_id)
void BLF_size(int fontid, float size)
Definition blf.cc:445
float BLF_fixed_width(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:848
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
int BLF_draw_mono(int fontid, const char *str, size_t str_len, int char_width, int tab_columns) ATTR_NONNULL(2)
Definition blf.cc:609
int BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:905
int blf_mono_font
Definition blf.cc:50
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void immRectf(uint pos, float x1, float y1, float x2, float y2)