From 88d786e01fbb4a1d15e21ec5b2b940be313ee281 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Fri, 8 Dec 2023 00:50:21 -0400 Subject: [PATCH] Squashed commit of the following: commit ec98277b3ae368574032e3ab5f206feb76462b5c Author: Dylan Llewellyn <46717769+herefishyfish@users.noreply.github.com> Date: Fri Dec 8 03:47:51 2023 +0800 fix: add text baseline bindings (#106) * fix: add text baseline bindings * fix: add text baseline bindings * fix: add text baseline bindings commit b355ddc66cbbe1edff8832353e2dad2535f73de5 Author: Osei Fortune Date: Thu Nov 23 07:00:15 2023 -0400 chore: updates (#105) --- packages/canvas/Canvas/index.ios.ts | 1 + .../ios/CanvasNative.xcframework/Info.plist | 10 ++--- .../Headers/canvas_native.h | 5 +++ .../Headers/canvas_native.h | 5 +++ .../canvas2d/CanvasRenderingContext2DImpl.cpp | 29 +++++++++++++ .../canvas2d/CanvasRenderingContext2DImpl.h | 13 ++++-- .../src/main/cpp/include/canvas_native.h | 5 +++ .../CanvasNative/include/canvas_native.h | 5 +++ .../dist/CanvasNative.xcframework/Info.plist | 10 ++--- .../Headers/canvas_native.h | 5 +++ .../Headers/canvas_native.h | 5 +++ .../canvas-native/canvas-c/src/lib.rs | 41 ++++++++++++++++++ .../canvas-native/canvas-cxx/src/lib.rs | 43 +++++++++++++++++++ .../canvas-native/canvas-ios/src/lib.rs | 1 + .../src-native/canvas-native/my_header.h | 5 +++ tools/demo/canvas/index.ts | 23 ++++++++-- 16 files changed, 190 insertions(+), 16 deletions(-) diff --git a/packages/canvas/Canvas/index.ios.ts b/packages/canvas/Canvas/index.ios.ts index acefe2c31..a6a7e7a28 100644 --- a/packages/canvas/Canvas/index.ios.ts +++ b/packages/canvas/Canvas/index.ios.ts @@ -264,6 +264,7 @@ export class Canvas extends CanvasBase { const size = this._realSize; + // todo revisit const width = Utils.layout.toDevicePixels(size.width || 1); const height = Utils.layout.toDevicePixels(size.height || 1); diff --git a/packages/canvas/platforms/ios/CanvasNative.xcframework/Info.plist b/packages/canvas/platforms/ios/CanvasNative.xcframework/Info.plist index 803c127dd..2552f82e9 100644 --- a/packages/canvas/platforms/ios/CanvasNative.xcframework/Info.plist +++ b/packages/canvas/platforms/ios/CanvasNative.xcframework/Info.plist @@ -8,32 +8,32 @@ DebugSymbolsPath dSYMs LibraryIdentifier - ios-arm64 + ios-arm64_x86_64-simulator LibraryPath CanvasNative.framework SupportedArchitectures arm64 + x86_64 SupportedPlatform ios + SupportedPlatformVariant + simulator DebugSymbolsPath dSYMs LibraryIdentifier - ios-arm64_x86_64-simulator + ios-arm64 LibraryPath CanvasNative.framework SupportedArchitectures arm64 - x86_64 SupportedPlatform ios - SupportedPlatformVariant - simulator CFBundlePackageType diff --git a/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h b/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h index 6802f94aa..0a0dd3e42 100644 --- a/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h +++ b/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h @@ -354,6 +354,11 @@ const char *canvas_native_context_get_text_align(const struct CanvasRenderingCon void canvas_native_context_set_text_align(struct CanvasRenderingContext2D *context, const char *alignment); +const char *canvas_native_context_get_text_baseline(const struct CanvasRenderingContext2D *context); + +void canvas_native_context_set_text_baseline(struct CanvasRenderingContext2D *context, + const char *baseline); + const char *canvas_native_context_get_global_composition(const struct CanvasRenderingContext2D *context); void canvas_native_context_set_global_composition(struct CanvasRenderingContext2D *context, diff --git a/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h b/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h index 6802f94aa..0a0dd3e42 100644 --- a/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h +++ b/packages/canvas/platforms/ios/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h @@ -354,6 +354,11 @@ const char *canvas_native_context_get_text_align(const struct CanvasRenderingCon void canvas_native_context_set_text_align(struct CanvasRenderingContext2D *context, const char *alignment); +const char *canvas_native_context_get_text_baseline(const struct CanvasRenderingContext2D *context); + +void canvas_native_context_set_text_baseline(struct CanvasRenderingContext2D *context, + const char *baseline); + const char *canvas_native_context_get_global_composition(const struct CanvasRenderingContext2D *context); void canvas_native_context_set_global_composition(struct CanvasRenderingContext2D *context, diff --git a/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.cpp b/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.cpp index c7cac4824..6f03d8e97 100644 --- a/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.cpp +++ b/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.cpp @@ -98,6 +98,8 @@ v8::Local CanvasRenderingContext2DImpl::GetCtor(v8::Isolat tmpl->SetAccessor(ConvertToV8String(isolate, "shadowOffsetY"), GetShadowOffsetY, SetShadowOffsetY); tmpl->SetAccessor(ConvertToV8String(isolate, "textAlign"), GetTextAlign, SetTextAlign); + tmpl->SetAccessor(ConvertToV8String(isolate, "textBaseline"), GetTextBaseline, + SetTextBaseline); tmpl->SetAccessor(ConvertToV8String(isolate, "globalCompositeOperation"), GetGlobalCompositeOperation, SetGlobalCompositeOperation); tmpl->SetAccessor(ConvertToV8String(isolate, "fillStyle"), GetFillStyle, SetFillStyle); @@ -745,6 +747,33 @@ void CanvasRenderingContext2DImpl::SetTextAlign(v8::Local property, canvas_native_context_set_text_align(ptr->GetContext(), alignment.c_str()); } +void CanvasRenderingContext2DImpl::GetTextBaseline(v8::Local property, + const v8::PropertyCallbackInfo &info) { + CanvasRenderingContext2DImpl *ptr = GetPointer(info.This()); + auto isolate = info.GetIsolate(); + if (ptr == nullptr) { + info.GetReturnValue().Set(0); + return; + } + auto baseline = canvas_native_context_get_text_baseline(ptr->GetContext()); + info.GetReturnValue().Set( + ConvertToV8String(isolate, baseline)); + canvas_native_string_destroy((char *) baseline); +} + +void CanvasRenderingContext2DImpl::SetTextBaseline(v8::Local property, + v8::Local value, + const v8::PropertyCallbackInfo &info) { + CanvasRenderingContext2DImpl *ptr = GetPointer(info.This()); + if (ptr == nullptr) { + + return; + } + auto isolate = info.GetIsolate(); + auto baseline = ConvertFromV8String(isolate, value); + canvas_native_context_set_text_baseline(ptr->GetContext(), baseline.c_str()); +} + void CanvasRenderingContext2DImpl::GetGlobalCompositeOperation(v8::Local property, const v8::PropertyCallbackInfo &info) { diff --git a/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.h b/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.h index e3f286b9e..b51f547da 100644 --- a/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.h +++ b/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.h @@ -166,11 +166,18 @@ class CanvasRenderingContext2DImpl { const v8::PropertyCallbackInfo &info); static void GetTextAlign(v8::Local property, - const v8::PropertyCallbackInfo &info); + const v8::PropertyCallbackInfo &info); static void SetTextAlign(v8::Local property, - v8::Local value, - const v8::PropertyCallbackInfo &info); + v8::Local value, + const v8::PropertyCallbackInfo &info); + + static void GetTextBaseline(v8::Local property, + const v8::PropertyCallbackInfo &info); + + static void SetTextBaseline(v8::Local property, + v8::Local value, + const v8::PropertyCallbackInfo &info); static void GetGlobalCompositeOperation(v8::Local property, const v8::PropertyCallbackInfo &info); diff --git a/packages/canvas/src-native/canvas-android/canvas/src/main/cpp/include/canvas_native.h b/packages/canvas/src-native/canvas-android/canvas/src/main/cpp/include/canvas_native.h index 5b6e24dfe..d615bf1d7 100644 --- a/packages/canvas/src-native/canvas-android/canvas/src/main/cpp/include/canvas_native.h +++ b/packages/canvas/src-native/canvas-android/canvas/src/main/cpp/include/canvas_native.h @@ -364,6 +364,11 @@ const char *canvas_native_context_get_text_align(const struct CanvasRenderingCon void canvas_native_context_set_text_align(struct CanvasRenderingContext2D *context, const char *alignment); +const char *canvas_native_context_get_text_baseline(const struct CanvasRenderingContext2D *context); + +void canvas_native_context_set_text_baseline(struct CanvasRenderingContext2D *context, + const char *baseline); + const char *canvas_native_context_get_global_composition(const struct CanvasRenderingContext2D *context); void canvas_native_context_set_global_composition(struct CanvasRenderingContext2D *context, diff --git a/packages/canvas/src-native/canvas-ios/CanvasNative/include/canvas_native.h b/packages/canvas/src-native/canvas-ios/CanvasNative/include/canvas_native.h index 59c986a50..f799a4a91 100644 --- a/packages/canvas/src-native/canvas-ios/CanvasNative/include/canvas_native.h +++ b/packages/canvas/src-native/canvas-ios/CanvasNative/include/canvas_native.h @@ -364,6 +364,11 @@ const char *canvas_native_context_get_text_align(const struct CanvasRenderingCon void canvas_native_context_set_text_align(struct CanvasRenderingContext2D *context, const char *alignment); +const char *canvas_native_context_get_text_baseline(const struct CanvasRenderingContext2D *context); + +void canvas_native_context_set_text_baseline(struct CanvasRenderingContext2D *context, + const char *baseline); + const char *canvas_native_context_get_global_composition(const struct CanvasRenderingContext2D *context); void canvas_native_context_set_global_composition(struct CanvasRenderingContext2D *context, diff --git a/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/Info.plist b/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/Info.plist index 803c127dd..2552f82e9 100644 --- a/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/Info.plist +++ b/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/Info.plist @@ -8,32 +8,32 @@ DebugSymbolsPath dSYMs LibraryIdentifier - ios-arm64 + ios-arm64_x86_64-simulator LibraryPath CanvasNative.framework SupportedArchitectures arm64 + x86_64 SupportedPlatform ios + SupportedPlatformVariant + simulator DebugSymbolsPath dSYMs LibraryIdentifier - ios-arm64_x86_64-simulator + ios-arm64 LibraryPath CanvasNative.framework SupportedArchitectures arm64 - x86_64 SupportedPlatform ios - SupportedPlatformVariant - simulator CFBundlePackageType diff --git a/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h b/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h index 6802f94aa..0a0dd3e42 100644 --- a/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h +++ b/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64/CanvasNative.framework/Headers/canvas_native.h @@ -354,6 +354,11 @@ const char *canvas_native_context_get_text_align(const struct CanvasRenderingCon void canvas_native_context_set_text_align(struct CanvasRenderingContext2D *context, const char *alignment); +const char *canvas_native_context_get_text_baseline(const struct CanvasRenderingContext2D *context); + +void canvas_native_context_set_text_baseline(struct CanvasRenderingContext2D *context, + const char *baseline); + const char *canvas_native_context_get_global_composition(const struct CanvasRenderingContext2D *context); void canvas_native_context_set_global_composition(struct CanvasRenderingContext2D *context, diff --git a/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h b/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h index 6802f94aa..0a0dd3e42 100644 --- a/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h +++ b/packages/canvas/src-native/canvas-ios/dist/CanvasNative.xcframework/ios-arm64_x86_64-simulator/CanvasNative.framework/Headers/canvas_native.h @@ -354,6 +354,11 @@ const char *canvas_native_context_get_text_align(const struct CanvasRenderingCon void canvas_native_context_set_text_align(struct CanvasRenderingContext2D *context, const char *alignment); +const char *canvas_native_context_get_text_baseline(const struct CanvasRenderingContext2D *context); + +void canvas_native_context_set_text_baseline(struct CanvasRenderingContext2D *context, + const char *baseline); + const char *canvas_native_context_get_global_composition(const struct CanvasRenderingContext2D *context); void canvas_native_context_set_global_composition(struct CanvasRenderingContext2D *context, diff --git a/packages/canvas/src-native/canvas-native/canvas-c/src/lib.rs b/packages/canvas/src-native/canvas-native/canvas-c/src/lib.rs index cb447c519..4cff3ca46 100644 --- a/packages/canvas/src-native/canvas-native/canvas-c/src/lib.rs +++ b/packages/canvas/src-native/canvas-native/canvas-c/src/lib.rs @@ -1066,6 +1066,47 @@ pub extern "C" fn canvas_native_context_set_text_align( } } +#[no_mangle] +pub extern "C" fn canvas_native_context_get_text_baseline( + context: *const CanvasRenderingContext2D, +) -> *const c_char { + let context = unsafe { &*context }; + let ret = match context.get_context().text_baseline() { + TextBaseline::ALPHABETIC => "alphabetic", + TextBaseline::BOTTOM => "bottom", + TextBaseline::HANGING => "hanging", + TextBaseline::IDEOGRAPHIC => "ideographic", + TextBaseline::MIDDLE => "middle", + TextBaseline::TOP => "top", + }; + CString::new(ret).unwrap().into_raw() +} + +#[no_mangle] +pub extern "C" fn canvas_native_context_set_text_baseline( + context: *mut CanvasRenderingContext2D, + baseline: *const c_char, +) { + if baseline.is_null() { + return; + } + let context = unsafe { &mut *context }; + let baseline = unsafe { CStr::from_ptr(baseline) }; + match baseline.to_string_lossy().as_ref() { + "alphabetic" => context + .get_context_mut() + .set_text_baseline(TextBaseline::ALPHABETIC), + "bottom" => context.get_context_mut().set_text_baseline(TextBaseline::BOTTOM), + "hanging" => context.get_context_mut().set_text_baseline(TextBaseline::HANGING), + "ideographic" => context + .get_context_mut() + .set_text_baseline(TextBaseline::IDEOGRAPHIC), + "middle" => context.get_context_mut().set_text_baseline(TextBaseline::MIDDLE), + "top" => context.get_context_mut().set_text_baseline(TextBaseline::TOP), + _ => {} + } +} + #[no_mangle] pub extern "C" fn canvas_native_context_get_global_composition( context: *const CanvasRenderingContext2D, diff --git a/packages/canvas/src-native/canvas-native/canvas-cxx/src/lib.rs b/packages/canvas/src-native/canvas-native/canvas-cxx/src/lib.rs index 1c4bea0ca..dc29687f8 100644 --- a/packages/canvas/src-native/canvas-native/canvas-cxx/src/lib.rs +++ b/packages/canvas/src-native/canvas-native/canvas-cxx/src/lib.rs @@ -901,6 +901,13 @@ pub mod ffi { alignment: &str, ); + fn canvas_native_context_get_text_baseline(context: &CanvasRenderingContext2D) -> &str; + + fn canvas_native_context_set_text_baseline( + context: &mut CanvasRenderingContext2D, + baseline: &str, + ); + fn canvas_native_context_get_global_composition(context: &CanvasRenderingContext2D) -> &str; @@ -3621,6 +3628,42 @@ pub fn canvas_native_context_set_text_align( } } +pub fn canvas_native_context_get_text_baseline(context: &CanvasRenderingContext2D) -> &str { + match context.get_context().text_baseline() { + TextBaseline::ALPHABETIC => "alphabetic", + TextBaseline::TOP => "top", + TextBaseline::HANGING => "hanging", + TextBaseline::MIDDLE => "middle", + TextBaseline::IDEOGRAPHIC => "ideographic", + TextBaseline::BOTTOM => "bottom", + } +} + +pub fn canvas_native_context_set_text_baseline( + context: &mut CanvasRenderingContext2D, + baseline: &str, +) { + match baseline { + "alphabetic" => context + .get_context_mut() + .set_text_baseline(TextBaseline::ALPHABETIC), + "top" => context.get_context_mut().set_text_baseline(TextBaseline::TOP), + "hanging" => context + .get_context_mut() + .set_text_baseline(TextBaseline::HANGING), + "middle" => context + .get_context_mut() + .set_text_baseline(TextBaseline::MIDDLE), + "ideographic" => context + .get_context_mut() + .set_text_baseline(TextBaseline::IDEOGRAPHIC), + "bottom" => context + .get_context_mut() + .set_text_baseline(TextBaseline::BOTTOM), + _ => {} + } +} + pub fn canvas_native_context_get_global_composition(context: &CanvasRenderingContext2D) -> &str { context.get_context().global_composite_operation().to_str() } diff --git a/packages/canvas/src-native/canvas-native/canvas-ios/src/lib.rs b/packages/canvas/src-native/canvas-native/canvas-ios/src/lib.rs index 34df20b97..8870c7011 100644 --- a/packages/canvas/src-native/canvas-native/canvas-ios/src/lib.rs +++ b/packages/canvas/src-native/canvas-native/canvas-ios/src/lib.rs @@ -12,6 +12,7 @@ use std::ffi::{c_longlong, c_void, CStr, CString}; use std::ops::DerefMut; use std::os::raw::c_char; use std::ptr::NonNull; +use parking_lot::RwLock; #[allow(non_camel_case_types)] pub(crate) enum iOSView { diff --git a/packages/canvas/src-native/canvas-native/my_header.h b/packages/canvas/src-native/canvas-native/my_header.h index 59c986a50..f799a4a91 100644 --- a/packages/canvas/src-native/canvas-native/my_header.h +++ b/packages/canvas/src-native/canvas-native/my_header.h @@ -364,6 +364,11 @@ const char *canvas_native_context_get_text_align(const struct CanvasRenderingCon void canvas_native_context_set_text_align(struct CanvasRenderingContext2D *context, const char *alignment); +const char *canvas_native_context_get_text_baseline(const struct CanvasRenderingContext2D *context); + +void canvas_native_context_set_text_baseline(struct CanvasRenderingContext2D *context, + const char *baseline); + const char *canvas_native_context_get_global_composition(const struct CanvasRenderingContext2D *context); void canvas_native_context_set_global_composition(struct CanvasRenderingContext2D *context, diff --git a/tools/demo/canvas/index.ts b/tools/demo/canvas/index.ts index d792b3120..e013dff84 100644 --- a/tools/demo/canvas/index.ts +++ b/tools/demo/canvas/index.ts @@ -76,7 +76,26 @@ export class DemoSharedCanvas extends DemoSharedBase { canvasLoaded(args) { this.canvas = args.object; console.log('canvas ready'); - this.draw(); + this.textBaseLine(); + // this.draw(); + } + + textBaseLine() { + const ctx = this.canvas.getContext('2d'); + + const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom']; + ctx.font = '24px serif'; + ctx.strokeStyle = 'red'; + + baselines.forEach((baseline, index) => { + ctx.textBaseline = baseline; + const y = 75 + index * 75; + ctx.beginPath(); + ctx.moveTo(0, y + 0.5); + ctx.lineTo(550, y + 0.5); + ctx.stroke(); + ctx.fillText(`Abcdefghijklmnop (${baseline})`, 0, y); + }); } svgViewLoaded(args) { @@ -625,7 +644,6 @@ export class DemoSharedCanvas extends DemoSharedBase { // setTimeout(() => { //draw_instanced(this.canvas); //draw_image_space(this.canvas); - //fog(this.canvas); //environmentMap(this.canvas); //cubeRotationRotation(this.canvas); @@ -658,7 +676,6 @@ export class DemoSharedCanvas extends DemoSharedBase { //issue54(this.canvas); //this.decoder() //this.context2DTest(this.canvas); - //issue93(this.canvas); // const canvas = this.canvas;