Skip to content

Commit

Permalink
fix: add text baseline bindings (#106)
Browse files Browse the repository at this point in the history
* fix: add text baseline bindings

* fix: add text baseline bindings

* fix: add text baseline bindings
  • Loading branch information
herefishyfish authored Dec 7, 2023
1 parent b355ddc commit ec98277
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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 @@ -685,6 +687,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 @@ -151,11 +151,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 @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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,
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
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 @@ -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,
Expand Down
25 changes: 20 additions & 5 deletions tools/demo/canvas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ec98277

Please sign in to comment.