Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/canvas-2d/src/context/drawing_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ mod parity {
let sk_font_style =
skia_safe::FontStyle::new(weight, font_style.stretch.into(), font_style.style.into());

let families: Vec<&str> = font_style.family.iter().map(|s| s.as_str()).collect();
let families = font_style.skia_families();

let mut text_style = skia_safe::textlayout::TextStyle::new();
text_style.set_font_families(families.as_slice());
Expand Down
4 changes: 2 additions & 2 deletions crates/canvas-2d/src/context/drawing_text/shaping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ fn layout(
font_style.style.into(),
);

let families: Vec<&str> = font_style.family.iter().map(|s| s.as_str()).collect();
let families = font_style.skia_families();

let mut text_style = skia_safe::textlayout::TextStyle::new();
text_style.set_font_families(families.as_slice());
Expand Down Expand Up @@ -351,7 +351,7 @@ mod tests {
let weight = skia_safe::font_style::Weight::from(font.weight as i32);
let sk_font_style =
skia_safe::FontStyle::new(weight, font.stretch.into(), font.style.into());
let families: Vec<&str> = font.family.iter().map(|s| s.as_str()).collect();
let families = font.skia_families();
let mut ts = skia_safe::textlayout::TextStyle::new();
ts.set_font_families(families.as_slice());
ts.set_font_size(font.size);
Expand Down
35 changes: 35 additions & 0 deletions crates/canvas-2d/src/context/drawing_text/typography.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,42 @@ static FONT_CACHE: OnceLock<parking_lot::RwLock<HashMap<String, Font>>> = OnceLo
/// Once full it stops accepting entries rather than evicting.
const FONT_CACHE_CAPACITY: usize = 256;

/// Chrome's Windows defaults for the CSS generic families, which DirectWrite does not know.
#[cfg(target_os = "windows")]
fn windows_generic_family(name: &str) -> Option<&'static str> {
Some(match name.to_ascii_lowercase().as_str() {
"serif" | "ui-serif" => "Times New Roman",
"sans-serif" | "ui-sans-serif" => "Arial",
"monospace" | "ui-monospace" => "Consolas",
"cursive" => "Comic Sans MS",
"fantasy" => "Impact",
"system-ui" => "Segoe UI",
_ => return None,
})
}

impl Font {
/// The families to hand Skia. On Windows Chrome's standard font ends the list, or SkParagraph
/// falls back to the last registered web font when DirectWrite matches nothing.
pub(crate) fn skia_families(&self) -> Vec<&str> {
#[cfg(target_os = "windows")]
{
let mut families: Vec<&str> = self
.family
.iter()
.map(|f| windows_generic_family(f.as_str()).unwrap_or(f.as_str()))
.collect();
if !families.contains(&"Times New Roman") {
families.push("Times New Roman");
}
families
}
#[cfg(not(target_os = "windows"))]
{
self.family.iter().map(|s| s.as_str()).collect()
}
}

pub fn new(font_rules: &str) -> Result<Font, String> {
let cache = FONT_CACHE.get_or_init(|| parking_lot::RwLock::new(HashMap::new()));

Expand Down
13 changes: 10 additions & 3 deletions packages/canvas/WebGL/WebGLRenderingContext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ import { imageAssetFor } from '../../platform/index';
import { isCanvasView, isOffscreenCanvas } from '../../Canvas/utils';
import { ElementImage, elementAsset, resolveElementSource } from '../../Canvas/drawable/common';

/** Node-API bindings take a WebGPU canvas as a canvas source, other contexts as they are. */
function canvasTextureSource(canvas: any) {
const native = canvas.native;
const webgpu = canvas._contextId === 'webgpu' || (!!canvas._gpuContext && canvas._gpuContext.native === native);
return NAPI_HOST && native && webgpu ? { __native__context: native } : native;
}

/** An image, canvas, video, bitmap or ImageData: not pixels in a buffer, an offset or null. */
function isTexImageSource(value: unknown): boolean {
return !!value && typeof value === 'object' && !ArrayBuffer.isView(value) && !(value instanceof ArrayBuffer);
Expand Down Expand Up @@ -941,7 +948,7 @@ export class WebGLRenderingContextBase extends WebGLRenderingCommon {
} else if (pixels instanceof ImageBitmap) {
this.native.texImage2D(target, level, internalformat, width, height, border, format, type ?? internalformat, pixels.native);
} else if (isCanvasView(pixels) || isOffscreenCanvas(pixels)) {
this.native.texImage2D(target, level, internalformat, width, height, border, format, type ?? internalformat, pixels.native);
this.native.texImage2D(target, level, internalformat, width, height, border, format, type ?? internalformat, canvasTextureSource(pixels));
} else if (__ANDROID__ && pixels instanceof android.graphics.Bitmap) {
(<any>org).nativescript.canvas.NSCWebGLRenderingContext.texImage2D(java.lang.Long.valueOf(this._contextPtr), target, level, internalformat, width, height, pixels, this.native.__flipY);
//this.native.texImage2D(target, level, internalformat, width, height, pixels);
Expand Down Expand Up @@ -997,7 +1004,7 @@ export class WebGLRenderingContextBase extends WebGLRenderingCommon {
} else if (border instanceof ImageBitmap) {
this.native.texImage2D(target, level, internalformat, width, height, border.native);
} else if (isCanvasView(border) || isOffscreenCanvas(border)) {
this.native.texImage2D(target, level, internalformat, width, height, border.native);
this.native.texImage2D(target, level, internalformat, width, height, canvasTextureSource(border));
} else if (__ANDROID__ && border instanceof android.graphics.Bitmap) {
(<any>org).nativescript.canvas.NSCWebGLRenderingContext.texImage2D(java.lang.Long.valueOf(this._contextPtr), target, level, internalformat, width, height, border, this.native.__flipY);
this.native.texImage2D(target, level, internalformat, width, height, border);
Expand Down Expand Up @@ -1093,7 +1100,7 @@ export class WebGLRenderingContextBase extends WebGLRenderingCommon {
} else if (format instanceof ImageAsset) {
this.native.texSubImage2D(target, level, xoffset, yoffset, width, height, format.native);
} else if (isCanvasView(format) || isOffscreenCanvas(format)) {
this.native.texSubImage2D(target, level, xoffset, yoffset, width, height, format.native);
this.native.texSubImage2D(target, level, xoffset, yoffset, width, height, canvasTextureSource(format));
} else if (format instanceof ImageBitmap) {
this.native.texSubImage2D(target, level, xoffset, yoffset, width, height, format.native);
} else if (format && typeof format.tagName === 'string' && (format.tagName === 'IMG' || format.tagName === 'IMAGE')) {
Expand Down
3 changes: 2 additions & 1 deletion tools/demo/canvas/spec/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export function registerAudioSpec() {
oscillator.start();
const buffer = await context.startRendering();
equal(buffer.length, 4800);
closeTo(rms(buffer.getChannelData(0).subarray(480)), 0.5, 0.02, 'square wave rms');
// The square is the spec's normalized PeriodicWave (peak 1, RMS about 0.83), as in Chrome.
closeTo(rms(buffer.getChannelData(0).subarray(480)), 0.415, 0.01, 'square wave rms');
equal(context.state, 'closed');
});

Expand Down
10 changes: 7 additions & 3 deletions tools/demo/canvas/spec/webgpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,15 @@ export function registerWebGPUSpec() {
const drawn = performance.now();
// The first two frames are left out: the display link is still starting.
if (frame >= 2) {
// Timed from the first poll: NativeScript Windows runs timers once a frame, so the first poll can
// itself land a frame after drawing. A present at the next vsync still shows on a later poll.
let first = 0;
const poll = () => {
const waited = performance.now() - drawn;
const now = performance.now();
first ||= now;
if (ctx.native.hasCurrentTexture === false) {
delays.push(waited);
} else if (waited < 100) {
delays.push(now - first);
} else if (now - drawn < 100) {
setTimeout(poll, 0);
} else {
delays.push(Infinity);
Expand Down
Loading