|
| 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