Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit ec98277
Author: Dylan Llewellyn <[email protected]>
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 b355ddc
Author: Osei Fortune <[email protected]>
Date:   Thu Nov 23 07:00:15 2023 -0400

    chore: updates (#105)
  • Loading branch information
triniwiz committed Dec 8, 2023
1 parent adfdb58 commit 88d786e
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/canvas/Canvas/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
<key>DebugSymbolsPath</key>
<string>dSYMs</string>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>CanvasNative.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>DebugSymbolsPath</key>
<string>dSYMs</string>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>CanvasNative.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
</array>
<key>CFBundlePackageType</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ v8::Local<v8::FunctionTemplate> 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);
Expand Down Expand Up @@ -745,6 +747,33 @@ void CanvasRenderingContext2DImpl::SetTextAlign(v8::Local<v8::String> property,
canvas_native_context_set_text_align(ptr->GetContext(), alignment.c_str());
}

void CanvasRenderingContext2DImpl::GetTextBaseline(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value> &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<v8::String> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void> &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<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value> &info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,18 @@ class CanvasRenderingContext2DImpl {
const v8::PropertyCallbackInfo<void> &info);

static void GetTextAlign(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value> &info);
const v8::PropertyCallbackInfo<v8::Value> &info);

static void SetTextAlign(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void> &info);
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void> &info);

static void GetTextBaseline(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value> &info);

static void SetTextBaseline(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void> &info);

static void GetGlobalCompositeOperation(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value> &info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
<key>DebugSymbolsPath</key>
<string>dSYMs</string>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>CanvasNative.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>DebugSymbolsPath</key>
<string>dSYMs</string>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>CanvasNative.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
</array>
<key>CFBundlePackageType</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions packages/canvas/src-native/canvas-native/canvas-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions packages/canvas/src-native/canvas-native/canvas-cxx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions packages/canvas/src-native/canvas-native/my_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 20 additions & 3 deletions tools/demo/canvas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 88d786e

Please sign in to comment.