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 6ef1c0c51..7e86d752f 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 @@ -350,6 +350,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 6ef1c0c51..7e86d752f 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 @@ -350,6 +350,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 4385802c7..ddac93dd7 100644 --- a/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.cpp +++ b/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.cpp @@ -93,6 +93,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); @@ -685,6 +687,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 a16dad963..0c9123531 100644 --- a/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.h +++ b/packages/canvas/platforms/ios/src/cpp/canvas2d/CanvasRenderingContext2DImpl.h @@ -151,11 +151,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 6ef1c0c51..7e86d752f 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 @@ -350,6 +350,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 6ef1c0c51..7e86d752f 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 @@ -350,6 +350,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/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 6ef1c0c51..7e86d752f 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 @@ -350,6 +350,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 6ef1c0c51..7e86d752f 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 @@ -350,6 +350,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 8a95af86d..1d323017c 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 @@ -985,6 +985,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/my_header.h b/packages/canvas/src-native/canvas-native/my_header.h index 4fd192046..9e4ae93ba 100644 --- a/packages/canvas/src-native/canvas-native/my_header.h +++ b/packages/canvas/src-native/canvas-native/my_header.h @@ -348,6 +348,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 767444552..0baf7d59c 100644 --- a/tools/demo/canvas/index.ts +++ b/tools/demo/canvas/index.ts @@ -75,7 +75,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) { @@ -624,7 +643,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); @@ -653,11 +671,8 @@ export class DemoSharedCanvas extends DemoSharedBase { //issue54(this.canvas); //this.decoder() //this.context2DTest(this.canvas); - //issue93(this.canvas); - // const canvas = this.canvas; - // console.time('getBoundingClientRect'); // for(let i = 0; i < 100000;i++){ // canvas.getBoundingClientRect();