Skip to content

Commit

Permalink
fix(image): Use the image size as the default width/ height if none s…
Browse files Browse the repository at this point in the history
…pecified (#995)

__Issue:__ If no width or height are specified (ie, `<Image src="some-image.png />`), the image won't render.

__Fix:__ When no width / height is set, use the image dimensions as the default.

This also wires up the `Skia.Image.width` / `sk_image_get_width` and `Skia.Image.height`/ `sk_image_get_height` APIs
  • Loading branch information
bryphe authored Sep 1, 2020
1 parent 836351d commit f5732e7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
2 changes: 0 additions & 2 deletions examples/Hello.re
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ module Logo = {
<Image
src={`File("outrun-logo.png")}
style=Style.[
width(512),
height(256),
transform([
Transform.RotateY(Angle.from_radians(rotationY)),
Transform.RotateX(Angle.from_radians(rotationX)),
Expand Down
8 changes: 8 additions & 0 deletions packages/reason-skia/examples/skia-cli/SkiaCli.re
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ let draw = canvas => {
switch (maybeImg) {
| None => failwith("Unable to load image: uv.png")
| Some(img) =>
print_endline(
Printf.sprintf(
"%s Image dimensions: %dx%d",
imgPath,
Skia.Image.width(img),
Skia.Image.height(img),
),
);
let imgFill = Paint.make();
Paint.setAlpha(imgFill, 0.0);
Canvas.drawImage(canvas, img, 250., 250., Some(imgFill));
Expand Down
3 changes: 3 additions & 0 deletions packages/reason-skia/src/Skia.re
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@ module Image = {
Gc.finalise(SkiaWrapped.Data.delete, data);
data;
};

let width = SkiaWrapped.Image.width;
let height = SkiaWrapped.Image.height;
};

type pixelGeometry = SkiaWrapped.pixelGeometry;
Expand Down
3 changes: 3 additions & 0 deletions packages/reason-skia/src/Skia.rei
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ module Image: {

let makeFromEncoded: (Data.t, option(IRect.t)) => option(t);
let encodeToData: t => Data.t;

let width: t => int;
let height: t => int;
};

type pixelGeometry = SkiaWrapped.pixelGeometry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ module M = (F: FOREIGN) => {
let delete = foreign("sk_image_unref", t @-> returning(void));

let encode = foreign("sk_image_encode", t @-> returning(Data.t));

let width = foreign("sk_image_get_width", t @-> returning(int));
let height = foreign("sk_image_get_height", t @-> returning(int));
};

type pixelGeometry = SkiaTypes.pixelGeometry;
Expand Down
34 changes: 32 additions & 2 deletions src/UI/ImageNode.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class imageNode (data: option(Skia.Image.t)) = {
inherit (class node)() as _super;
val mutable _opacity = 1.0;
val mutable _resizeMode = ImageResizeMode.Stretch;
val mutable _maybeWidth = None;
val mutable _maybeHeight = None;
val _paint = Skia.Paint.make();
pub! draw = (parentContext: NodeDrawContext.t) => {
/* Draw background first */
Expand Down Expand Up @@ -53,7 +55,35 @@ class imageNode (data: option(Skia.Image.t)) = {
pub setQuality = quality => {
Skia.Paint.setFilterQuality(_paint, quality);
};
pub setData = newData => {
data = newData;
pub! setStyle = style => {
// If neither the height and width are defined, then
// use the image size as the default height and width.

let adjustedStyle =
Layout.Encoding.(
{
let noDimensionsSet =
style.width == cssUndefined && style.height == cssUndefined;

Style.{
...style,
width:
noDimensionsSet
? _maybeWidth |> Option.value(~default=cssUndefined)
: style.width,
height:
noDimensionsSet
? _maybeHeight |> Option.value(~default=cssUndefined)
: style.height,
};
}
);
_super#setStyle(adjustedStyle);
};
pub setData = maybeImg => {
data = maybeImg;
_maybeWidth = maybeImg |> Option.map(Skia.Image.width);
_maybeHeight = maybeImg |> Option.map(Skia.Image.height);
_this#setStyle(_super#getStyle());
};
};

0 comments on commit f5732e7

Please sign in to comment.