Skip to content

Commit

Permalink
chore: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jan 11, 2024
1 parent 5a9f452 commit 15bd146
Show file tree
Hide file tree
Showing 40 changed files with 208 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::collections::HashMap;

use once_cell::sync::Lazy;
use parking_lot::Mutex;
use skia_safe::{FontArguments, FontMgr, Typeface};
use skia_safe::font_arguments::variation_position::Coordinate;
use skia_safe::font_arguments::VariationPosition;
use skia_safe::font_style::Slant;
use skia_safe::textlayout::{FontCollection, TextStyle, TypefaceFontProvider};
use skia_safe::{FontArguments, FontMgr, Typeface};

#[derive(PartialEq, Eq, Hash)]
struct CollectionKey {
Expand Down Expand Up @@ -169,7 +169,7 @@ impl FontLibrary {
Ok(())
}

pub fn reset(){
pub fn reset() {
let mut library = FONT_LIBRARY.lock();
library.fonts.clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::os::raw::c_float;

use skia_safe::{Canvas, Paint};

use crate::context::Context;
use crate::context::drawing_text::global_fonts::FONT_LIBRARY;
use crate::context::drawing_text::text_metrics::TextMetrics;
use crate::context::text_styles::text_align::TextAlign;
use crate::context::text_styles::text_baseline::TextBaseLine;
use crate::context::text_styles::text_direction::TextDirection;
use crate::context::Context;

pub(crate) const MAX_TEXT_WIDTH: f32 = 100_000.0;

Expand Down Expand Up @@ -138,10 +138,12 @@ impl Context {

let font = paragraph.get_font_at(0);
let (_, font_metrics) = font.metrics();
if font_metrics.has_bounds() { }
if font_metrics.has_bounds() {}
let glyphs = font.str_to_glyphs_vec(text);
let glyphs_size = glyphs.len();
if glyphs_size == 0 { return; }
if glyphs_size == 0 {
return;
}
let mut bounds = vec![skia_safe::Rect::default(); glyphs_size];
font.get_bounds(glyphs.as_slice(), bounds.as_mut_slice(), None);
let range: Range<usize> = 0_usize..glyphs_size;
Expand All @@ -158,8 +160,9 @@ impl Context {
line_width += tbox.rect.width();
}

if line_width == 0. { return; }

if line_width == 0. {
return;
}

let first_char_bounds = bounds[0];
let mut descent = first_char_bounds.bottom;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::f32::consts::PI;

use skia_safe::{color_filters, image_filters, Point};
use skia_safe::wrapper::NativeTransmutableWrapper;
use skia_safe::{color_filters, image_filters, Point};

use crate::context::Context;
use crate::utils::color::parse_color;
Expand Down Expand Up @@ -62,7 +62,8 @@ impl Into<skia_safe::ColorChannel> for ColorChannel {
ColorChannel::G => skia_safe::ColorChannel::G,
ColorChannel::B => skia_safe::ColorChannel::B,
ColorChannel::A => skia_safe::ColorChannel::A,
}.into()
}
.into()
}
}

Expand All @@ -83,14 +84,14 @@ impl Into<Option<skia_safe::ImageFilter>> for ImageFilter {
fn into(self) -> Option<skia_safe::ImageFilter> {
match self {
ImageFilter::Blur(value) => Some(value),
ImageFilter::Brightness(value) => Some(value),
ImageFilter::ColorMatrix(value) => Some(value),
ImageFilter::Contrast(value) => Some(value),
ImageFilter::DisplacementMap(value) => Some(value),
ImageFilter::Morphology(value) => Some(value),
ImageFilter::Offset(value) => Some(value),
ImageFilter::Shadow(value) => Some(value),
ImageFilter::None => None
ImageFilter::Brightness(value) => Some(value),
ImageFilter::ColorMatrix(value) => Some(value),
ImageFilter::Contrast(value) => Some(value),
ImageFilter::DisplacementMap(value) => Some(value),
ImageFilter::Morphology(value) => Some(value),
ImageFilter::Offset(value) => Some(value),
ImageFilter::Shadow(value) => Some(value),
ImageFilter::None => None,
}
}
}
Expand All @@ -99,14 +100,14 @@ impl Into<Option<skia_safe::ImageFilter>> for &ImageFilter {
fn into(self) -> Option<skia_safe::ImageFilter> {
match self {
ImageFilter::Blur(value) => Some(value.clone()),
ImageFilter::Brightness(value) => Some(value.clone()),
ImageFilter::ColorMatrix(value) => Some(value.clone()),
ImageFilter::Contrast(value) => Some(value.clone()),
ImageFilter::DisplacementMap(value) => Some(value.clone()),
ImageFilter::Morphology(value) => Some(value.clone()),
ImageFilter::Offset(value) => Some(value.clone()),
ImageFilter::Shadow(value) => Some(value.clone()),
ImageFilter::None => None
ImageFilter::Brightness(value) => Some(value.clone()),
ImageFilter::ColorMatrix(value) => Some(value.clone()),
ImageFilter::Contrast(value) => Some(value.clone()),
ImageFilter::DisplacementMap(value) => Some(value.clone()),
ImageFilter::Morphology(value) => Some(value.clone()),
ImageFilter::Offset(value) => Some(value.clone()),
ImageFilter::Shadow(value) => Some(value.clone()),
ImageFilter::None => None,
}
}
}
Expand Down Expand Up @@ -156,22 +157,27 @@ impl ImageFilter {
.map_or(Self::None, |v| Self::Contrast(v))
}

pub fn displacement_map(x: ColorChannel, y: ColorChannel, scale: f32, color: &ImageFilter, input: Option<&ImageFilter>) -> Self {

pub fn displacement_map(
x: ColorChannel,
y: ColorChannel,
scale: f32,
color: &ImageFilter,
input: Option<&ImageFilter>,
) -> Self {
let input: Option<skia_safe::ImageFilter> = input.map(|i| i.clone().into()).flatten();

let color: Option<skia_safe::ImageFilter> = color.into();
image_filters::displacement_map((x.into(), y.into()),scale,input, color.unwrap(),None)
.map_or(Self::None, |v| Self::Contrast(v))
image_filters::displacement_map((x.into(), y.into()), scale, input, color.unwrap(), None)
.map_or(Self::None, |v| Self::Contrast(v))
}


pub fn shadow(dx: f32, dy: f32, blur: f32, color: &str, input: Option<&ImageFilter>) -> Self {
if blur > 0.0 {
if let Some(color) = parse_color(color) {
let sigma = blur / 2.0;

let input: Option<skia_safe::ImageFilter> = input.map(|i| i.clone().into()).flatten();
let input: Option<skia_safe::ImageFilter> =
input.map(|i| i.clone().into()).flatten();

return image_filters::drop_shadow(
Point::new(dx, dy),
Expand Down Expand Up @@ -204,17 +210,17 @@ impl Context {
}
}

self.state.paint.fill_paint_mut().set_image_filter(filter.clone());

self.state
.paint
.stroke_paint_mut()
.fill_paint_mut()
.set_image_filter(filter.clone());

self.state
.paint
.image_paint_mut()
.set_image_filter(filter);
.stroke_paint_mut()
.set_image_filter(filter.clone());

self.state.paint.image_paint_mut().set_image_filter(filter);
}

pub fn clear_image_filter(&mut self) {
Expand Down Expand Up @@ -371,7 +377,8 @@ impl Context {
*val = (orig * (1.0 - amt) + inv * amt) as u8;
}
let table = Some(&ramp);
let color_table = color_filters::table_argb(None, table, table, table).unwrap();
let color_table =
color_filters::table_argb(None, table, table, table).unwrap();
image_filters::color_filter(color_table, chain, None)
} else {
chain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ impl Context {
}
}

pub fn create_conic_gradient(
&self,
start_angle: c_float,
x: c_float,
y: c_float,
) -> Gradient {
pub fn create_conic_gradient(&self, start_angle: c_float, x: c_float, y: c_float) -> Gradient {
let angle = crate::utils::geometry::to_degrees(start_angle) - 90.0;
Gradient::Conic {
center: Point::new(x, y),
Expand All @@ -60,7 +55,6 @@ impl Context {
}
}


pub fn create_conic_gradient_with_matrix(
&self,
start_angle: c_float,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use log::log;
use parking_lot::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
use std::os::raw::c_float;
use std::sync::Arc;
use log::log;

use skia_safe::gpu::gl::TextureInfo;
use skia_safe::{images, Color, Data, Image, Point, Surface};
Expand Down Expand Up @@ -164,7 +164,7 @@ impl State {
word_spacing_value: "0px".to_string(),
word_spacing: 0.,
letter_spacing_value: "0px".to_string(),
letter_spacing: 0.
letter_spacing: 0.,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::context::Context;
pub mod path;

impl Context {

pub fn begin_path(&mut self) {
self.path.begin_path();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use encoding_rs::UTF_8;
use std::borrow::Cow;

#[derive(Clone)]
pub struct TextEncoder {
Expand All @@ -19,7 +19,7 @@ impl TextEncoder {
Vec::from(result.0)
}

pub fn encode_to_cow<'a>(&mut self, text: &'a str) -> Cow<'a ,[u8]> {
pub fn encode_to_cow<'a>(&mut self, text: &'a str) -> Cow<'a, [u8]> {
let result = self.inner.encode(text);
result.0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ impl From<skia_safe::textlayout::TextDirection> for TextDirection {
}
}



impl From<u32> for TextDirection {
fn from(value: u32) -> Self {
match value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use core::convert::{From, Into};

use skia_safe::{EncodedImageFormat, surfaces};
use skia_safe::{surfaces, EncodedImageFormat};

use canvas_core::image_asset::ImageAsset;

Expand Down Expand Up @@ -240,11 +240,7 @@ pub(crate) fn create_image_bitmap_internal(
ImageBitmapPremultiplyAlpha::from(premultiply_alpha).into(),
ImageBitmapColorSpaceConversion::from(color_space_conversion).to_color_space(),
);
match surfaces::raster(
&image_info,
Some((source_rect.width() * 4.) as usize),
None,
) {
match surfaces::raster(&image_info, Some((source_rect.width() * 4.) as usize), None) {
None => {}
Some(mut surface) => {
let canvas = surface.canvas();
Expand Down Expand Up @@ -278,22 +274,16 @@ pub(crate) fn create_image_bitmap_internal(
None,
);


let data = pixel_map.encode(EncodedImageFormat::PNG, 100);

if let Some(data) = data {
output.load_from_bytes(data.as_slice());
};

}

} else {


let size = image_info.height() as usize * image_info.min_row_bytes();
let mut buf = vec![0_u8; size];


let mut info = skia_safe::ImageInfo::new(
skia_safe::ISize::new(image.width(), image.height()),
skia_safe::ColorType::RGBA8888,
Expand All @@ -309,8 +299,6 @@ pub(crate) fn create_image_bitmap_internal(
skia_safe::image::CachingHint::Allow,
);



let encoded = image.encode(&mut ctx, EncodedImageFormat::PNG, 100);

if let Some(encoded) = encoded {
Expand Down
20 changes: 11 additions & 9 deletions packages/canvas/src-native/canvas-native/canvas-2d/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ impl Context {
};
let mut context = skia_safe::gpu::DirectContext::new_metal(backend, None).unwrap();
let surface_props = SurfaceProps::new(SurfacePropsFlags::default(), PixelGeometry::Unknown);
let surface_holder = unsafe { gpu::surfaces::wrap_mtk_view(
&mut context,
view as skia_safe::gpu::mtl::Handle,
gpu::SurfaceOrigin::TopLeft,
Some(samples),
ColorType::BGRA8888,
None,
Some(&surface_props),
)};
let surface_holder = unsafe {
gpu::surfaces::wrap_mtk_view(
&mut context,
view as skia_safe::gpu::mtl::Handle,
gpu::SurfaceOrigin::TopLeft,
Some(samples),
ColorType::BGRA8888,
None,
Some(&surface_props),
)
};

Context {
surface: surface_holder.unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ impl ScaleUtils for Rect {
}
}


#[repr(transparent)]
#[derive(Copy, Clone, Default)]
pub struct ByteBufInner {
Expand Down Expand Up @@ -82,4 +81,4 @@ impl Drop for ByteBufMut {
}
}

unsafe impl Send for ByteBufMut {}
unsafe impl Send for ByteBufMut {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::{Read, Seek, SeekFrom};
use skia_safe::FontMgr;
use std::io::{Read, Seek, SeekFrom};

use skia_safe::svg::Dom;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const SIZE_PX: f32 = 1.0 / 96.0 * SIZE_INCH;
const SIZE_POINT: f32 = 1.0 / 72.0 * SIZE_INCH;
const SIZE_PICAS: f32 = SIZE_POINT * 12.0;


/// The default font size.
pub const FONT_MEDIUM_PX: f32 = 16.0;

Expand Down
Loading

0 comments on commit 15bd146

Please sign in to comment.