Skip to content

Commit 2674964

Browse files
maxmmitchellcopybara-github
authored andcommitted
Add C++ implementation of Stock Brushes
PiperOrigin-RevId: 843371642
1 parent 483b917 commit 2674964

8 files changed

Lines changed: 1194 additions & 1 deletion

File tree

ink/brush/BUILD.bazel

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,38 @@ cc_library(
335335
],
336336
)
337337

338+
cc_library(
339+
name = "stock_brushes",
340+
srcs = ["stock_brushes.cc"],
341+
hdrs = ["stock_brushes.h"],
342+
deps = [
343+
":brush_behavior",
344+
":brush_coat",
345+
":brush_family",
346+
":brush_paint",
347+
":brush_tip",
348+
":easing_function",
349+
"//ink/geometry:angle",
350+
"@com_google_absl//absl/log:absl_check",
351+
"@com_google_absl//absl/status",
352+
"@com_google_absl//absl/status:statusor",
353+
"@com_google_absl//absl/strings",
354+
"@com_google_absl//absl/strings:string_view",
355+
],
356+
)
357+
358+
cc_test(
359+
name = "stock_brushes_test",
360+
srcs = ["stock_brushes_test.cc"],
361+
deps = [
362+
":brush_behavior",
363+
":brush_tip",
364+
":stock_brushes",
365+
"@com_google_absl//absl/container:flat_hash_set",
366+
"@com_google_googletest//:gtest_main",
367+
],
368+
)
369+
338370
cc_test(
339371
name = "fuzz_domains_test",
340372
srcs = ["fuzz_domains_test.cc"],

ink/brush/brush_behavior.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace ink {
5353
// This behavior would be represented by the list {Source, Target}. An example
5454
// of such a behavior defined in C++ would be:
5555
//
56-
// BrushBeahvior behavior = {
56+
// BrushBehavior behavior = {
5757
// .nodes = {
5858
// BrushBehavior::SourceNode{
5959
// .source = BrushBehavior::Source::kPressure,

ink/brush/brush_paint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "absl/container/flat_hash_set.h"
2323
#include "absl/status/status.h"
24+
#include "absl/strings/string_view.h"
2425
#include "absl/time/time.h"
2526
#include "ink/brush/color_function.h"
2627
#include "ink/geometry/angle.h"

ink/brush/internal/jni/BUILD.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ cc_library(
2929
":brush_tip_jni",
3030
":color_function_jni",
3131
":easing_function_jni",
32+
":stock_brushes_jni",
3233
],
3334
alwayslink = 1,
3435
)
@@ -161,6 +162,35 @@ cc_library(
161162
alwayslink = 1,
162163
)
163164

165+
cc_library(
166+
name = "stock_brushes_jni",
167+
srcs = ["stock_brushes_jni.cc"],
168+
deps = [
169+
":brush_coat_jni",
170+
":brush_jni_helper",
171+
"//ink/brush",
172+
"//ink/brush:brush_coat",
173+
"//ink/brush:brush_family",
174+
"//ink/brush:brush_paint",
175+
"//ink/brush:brush_tip",
176+
"//ink/brush:stock_brushes",
177+
"//ink/jni/internal:jni_defines",
178+
"//ink/jni/internal:jni_string_util",
179+
"//ink/jni/internal:jni_throw_util",
180+
"//ink/types:duration",
181+
"@com_google_absl//absl/functional:overload",
182+
"@com_google_absl//absl/log:absl_check",
183+
"@com_google_absl//absl/status:statusor",
184+
"@com_google_absl//absl/types:span",
185+
] + select({
186+
"@platforms//os:android": [],
187+
"//conditions:default": [
188+
"@rules_jni//jni",
189+
],
190+
}),
191+
alwayslink = 1,
192+
)
193+
164194
cc_library(
165195
name = "brush_family_jni",
166196
srcs = ["brush_family_jni.cc"],
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <jni.h>
16+
17+
#include <utility>
18+
19+
#include "absl/status/statusor.h"
20+
#include "ink/brush/brush_family.h"
21+
#include "ink/brush/brush_paint.h"
22+
#include "ink/brush/internal/jni/brush_jni_helper.h"
23+
#include "ink/brush/stock_brushes.h"
24+
#include "ink/jni/internal/jni_defines.h"
25+
#include "ink/jni/internal/jni_string_util.h"
26+
#include "ink/jni/internal/jni_throw_util.h"
27+
28+
namespace {
29+
30+
using ::ink::BrushFamily;
31+
using ::ink::BrushPaint;
32+
using ::ink::DashedLineVersion;
33+
using ::ink::EmojiHighlighterVersion;
34+
using ::ink::HighlighterVersion;
35+
using ::ink::MarkerVersion;
36+
using ::ink::PressurePenVersion;
37+
using ::ink::jni::JStringToStdString;
38+
using ::ink::jni::NewNativeBrushBehavior;
39+
using ::ink::jni::NewNativeBrushFamily;
40+
using ::ink::jni::ThrowExceptionFromStatus;
41+
using ::ink::stock_brushes::dashedLine;
42+
using ::ink::stock_brushes::emojiHighlighter;
43+
using ::ink::stock_brushes::highlighter;
44+
using ::ink::stock_brushes::marker;
45+
using ::ink::stock_brushes::pencilUnstable;
46+
using ::ink::stock_brushes::predictionFadeOutBehavior;
47+
using ::ink::stock_brushes::pressurePen;
48+
49+
extern "C" {
50+
51+
JNI_METHOD(brush, StockBrushesNative, jlong, marker)
52+
(JNIEnv* env, jobject object, jstring version) {
53+
absl::StatusOr<BrushFamily> family =
54+
marker(MarkerVersion(JStringToStdString(env, version)));
55+
if (!family.ok()) {
56+
ThrowExceptionFromStatus(env, family.status());
57+
return 0;
58+
}
59+
return NewNativeBrushFamily(*std::move(family));
60+
}
61+
62+
JNI_METHOD(brush, StockBrushesNative, jlong, dashedLine)
63+
(JNIEnv* env, jobject object, jstring version) {
64+
absl::StatusOr<BrushFamily> family =
65+
dashedLine(DashedLineVersion(JStringToStdString(env, version)));
66+
if (!family.ok()) {
67+
ThrowExceptionFromStatus(env, family.status());
68+
return 0;
69+
}
70+
return NewNativeBrushFamily(*std::move(family));
71+
}
72+
73+
JNI_METHOD(brush, StockBrushesNative, jlong, pressurePen)
74+
(JNIEnv* env, jobject object, jstring version) {
75+
absl::StatusOr<BrushFamily> family =
76+
pressurePen(PressurePenVersion(JStringToStdString(env, version)));
77+
if (!family.ok()) {
78+
ThrowExceptionFromStatus(env, family.status());
79+
return 0;
80+
}
81+
return NewNativeBrushFamily(*std::move(family));
82+
}
83+
84+
JNI_METHOD(brush, StockBrushesNative, jlong, highlighter)
85+
(JNIEnv* env, jobject object, jint self_overlap, jstring version) {
86+
absl::StatusOr<BrushFamily> family =
87+
highlighter(static_cast<BrushPaint::SelfOverlap>(self_overlap),
88+
HighlighterVersion(JStringToStdString(env, version)));
89+
if (!family.ok()) {
90+
ThrowExceptionFromStatus(env, family.status());
91+
return 0;
92+
}
93+
return NewNativeBrushFamily(*std::move(family));
94+
}
95+
96+
JNI_METHOD(brush, StockBrushesNative, jlong, emojiHighlighter)
97+
(JNIEnv* env, jobject object, jstring client_texture_id,
98+
jboolean show_mini_emoji_trail, jint self_overlap, jstring version) {
99+
absl::StatusOr<BrushFamily> family = emojiHighlighter(
100+
JStringToStdString(env, client_texture_id), show_mini_emoji_trail,
101+
static_cast<BrushPaint::SelfOverlap>(self_overlap),
102+
EmojiHighlighterVersion(JStringToStdString(env, version)));
103+
if (!family.ok()) {
104+
ThrowExceptionFromStatus(env, family.status());
105+
return 0;
106+
}
107+
return NewNativeBrushFamily(*std::move(family));
108+
}
109+
110+
JNI_METHOD(brush, StockBrushesNative, jlong, pencilUnstable)
111+
(JNIEnv* env, jobject object) {
112+
absl::StatusOr<BrushFamily> family = pencilUnstable();
113+
if (!family.ok()) {
114+
ThrowExceptionFromStatus(env, family.status());
115+
return 0;
116+
}
117+
return NewNativeBrushFamily(*std::move(family));
118+
}
119+
120+
JNI_METHOD(brush, StockBrushesNative, jlong, predictionFadeOutBehavior)
121+
(JNIEnv* env, jobject object) {
122+
return NewNativeBrushBehavior(std::move(predictionFadeOutBehavior()));
123+
}
124+
} // extern "C"
125+
} // namespace

0 commit comments

Comments
 (0)