Skip to content

Commit

Permalink
Reverse gradients to make them easier/faster.
Browse files Browse the repository at this point in the history
Make update rectangles more square for performance.
  • Loading branch information
Erik Corry committed Nov 30, 2023
1 parent 2a44e30 commit 9edd5d8
Show file tree
Hide file tree
Showing 42 changed files with 136 additions and 102 deletions.
32 changes: 13 additions & 19 deletions src/bar_code.toit
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,11 @@ class BarCodeEanElement extends CustomElement:

// Make a white background behind the bar code and draw the digits along the bottom.
draw_background_ canvas/Canvas:
if not (x and y): return
Background.draw background_ canvas x_ y_ w h

// Bar code coordinates.
text_x := x + EAN_13_QUIET_ZONE_WIDTH + EAN_13_START_WIDTH
text_y := y + EAN_13_HEIGHT + number_height_ - EAN_13_BOTTOM_SPACE + 1
text_x := EAN_13_QUIET_ZONE_WIDTH + EAN_13_START_WIDTH
text_y := EAN_13_HEIGHT + number_height_ - EAN_13_BOTTOM_SPACE + 1

canvas.text (x + 1) text_y --text=code_[..1] --color=color_ --font=sans10_
canvas.text 1 text_y --text=code_[..1] --color=color_ --font=sans10_

code_[1..7].split "":
if it != "":
Expand All @@ -97,18 +94,15 @@ class BarCodeEanElement extends CustomElement:
canvas.text text_x text_y --text=">" --color=color_ --font=sans10_

// Redraw routine.
draw canvas/Canvas:
if not (x and y): return
if (canvas.bounds_analysis x y w h) == Canvas.ALL_OUTSIDE: return
custom_draw canvas/Canvas:
draw_background_ canvas

x := x_ + EAN_13_QUIET_ZONE_WIDTH
top := y_
x := EAN_13_QUIET_ZONE_WIDTH
long_height := EAN_13_HEIGHT
short_height := EAN_13_HEIGHT - EAN_13_BOTTOM_SPACE
// Start bars: 101.
canvas.rectangle x top --w=1 --h=long_height --color=color_
canvas.rectangle x + 2 top --w=1 --h=long_height --color=color_
canvas.rectangle x 0 --w=1 --h=long_height --color=color_
canvas.rectangle x + 2 0 --w=1 --h=long_height --color=color_
x += 3
first_code := EAN_13_FIRST_CODES_[code_[0] & 0xf]
// Left digits using the L or G mapping.
Expand All @@ -117,20 +111,20 @@ class BarCodeEanElement extends CustomElement:
code := ((first_code >> (6 - i)) & 1) == 0 ? (l_ digit) : (g_ digit)
for b := 6; b >= 0; b--:
if ((1 << b) & code) != 0:
canvas.rectangle x top --w=1 --h=short_height --color=color_
canvas.rectangle x 0 --w=1 --h=short_height --color=color_
x++
// Middle bars: 01010
canvas.rectangle x + 1 top --w=1 --h=long_height --color=color_
canvas.rectangle x + 3 top --w=1 --h=long_height --color=color_
canvas.rectangle x + 1 0 --w=1 --h=long_height --color=color_
canvas.rectangle x + 3 0 --w=1 --h=long_height --color=color_
x += 5
// Left digits using the R mapping.
for i := 7; i < 13; i++:
digit := code_[i]
code := r_ digit
for b := 6; b >= 0; b--:
if ((1 << b) & code) != 0:
canvas.rectangle x top --w=1 --h=short_height --color=color_
canvas.rectangle x 0 --w=1 --h=short_height --color=color_
x++
// End bars: 101.
canvas.rectangle x top --w=1 --h=long_height --color=color_
canvas.rectangle x + 2 top --w=1 --h=long_height --color=color_
canvas.rectangle x 0 --w=1 --h=long_height --color=color_
canvas.rectangle x + 2 0 --w=1 --h=long_height --color=color_
26 changes: 19 additions & 7 deletions src/common.toit
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,30 @@ abstract class Canvas:
abstract make_alpha_map -> Canvas
abstract make_alpha_map --padding/int -> Canvas

static ALL_OUTSIDE ::= 0
static ALL_INSIDE ::= 1
static MIXED_BOUNDS ::= 2
/*
A, C disjoint or one of them is empty 0
A subset of C, A not empty 1
C subset of A, C not empty 2
A identical to C and non-empty 3
*/

static DISJOINT ::= 0 // The area and the canvas are disjoint.
static AREA_IN_CANVAS ::= 1 // The area is a subset of the canvas.
static CANVAS_IN_AREA ::= 2 // The canvas is a subset of the area.
static COINCIDENT ::= 3 // The area and the canvas are identical.
static OVERLAP ::= 4 // The areas overlap, but neither is a subset of the other.
bounds_analysis x/int y/int w/int h/int -> int:
if h == 0 or w == 0: return ALL_OUTSIDE
if h == 0 or w == 0 or width_ == 0 or height_ == 0: return DISJOINT
transform.xywh x y w h: | x2 y2 w2 h2 |
right := x2 + w2
bottom := y2 + h2
if right < 0 or bottom < 0 or x2 >= width_ or y2 >= height_: return ALL_OUTSIDE
if x2 >= 0 and y2 >= 0 and right <= width_ and bottom <= height_: return ALL_INSIDE
return MIXED_BOUNDS
if right < 0 or bottom < 0 or x2 >= width_ or y2 >= height_: return DISJOINT
if x2 >= 0 and y2 >= 0 and right <= width_ and bottom <= height_:
if x2 == 0 and y2 == 0 and right == width_ and bottom == height_: return COINCIDENT
return AREA_IN_CANVAS
if x2 <= 0 and y2 <= 0 and right >= width_ and bottom >= height_: return CANVAS_IN_AREA
return OVERLAP

abstract composit frame_opacity frame_canvas/Canvas painting_opacity painting_canvas/Canvas

Expand Down
30 changes: 16 additions & 14 deletions src/element.toit
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class Div extends Element:
draw canvas/Canvas -> none:
old_transform := canvas.transform
canvas.transform = old_transform.translate x_ y_
Background.draw background_ canvas 0 0 w h
Background.draw background_ canvas 0 0 w h --no-autocropped
custom_draw canvas
if border_: border_.draw canvas 0 0 w h
canvas.transform = old_transform
Expand Down Expand Up @@ -423,10 +423,12 @@ abstract class CustomElement extends ClippingDiv:

draw canvas/Canvas -> none:
if not (x and y): return
if (canvas.bounds_analysis x y w h) == Canvas.ALL_OUTSIDE: return
analysis := canvas.bounds_analysis x y w h
if analysis == Canvas.DISJOINT: return
autocropped := analysis == Canvas.CANVAS_IN_AREA or analysis == Canvas.COINCIDENT
old_transform := canvas.transform
canvas.transform = old_transform.translate x_ y_
Background.draw background_ canvas 0 0 w h
Background.draw background_ canvas 0 0 w h --autocropped=autocropped
custom_draw canvas
if border_: border_.draw canvas 0 0 w h
canvas.transform = old_transform
Expand Down Expand Up @@ -494,7 +496,7 @@ class ClippingDiv extends Div:
draw canvas/Canvas -> none:
// If we are outside the window and the decorations, there is nothing to do.
extent: | x2 y2 w2 h2 |
if (canvas.bounds_analysis x2 y2 w2 h2) == Canvas.ALL_OUTSIDE: return
if (canvas.bounds_analysis x2 y2 w2 h2) == Canvas.DISJOINT: return

old_transform := canvas.transform
canvas.transform = old_transform.translate x_ y_
Expand All @@ -521,7 +523,7 @@ class ClippingDiv extends Div:
if border_: border_.draw border_canvas 0 0 w h

painting_canvas := canvas.create_similar
Background.draw background_ painting_canvas 0 0 w h
Background.draw background_ painting_canvas 0 0 w h --autocropped
custom_draw painting_canvas

canvas.composit frame_opacity border_canvas content_opacity painting_canvas
Expand Down Expand Up @@ -584,7 +586,7 @@ class PngElement extends CustomElement:
// Redraw routine.
custom_draw canvas/Canvas:
y2 := 0
while y2 < h and (canvas.bounds_analysis 0 y2 w (h - y2)) != Canvas.ALL_OUTSIDE:
while y2 < h and (canvas.bounds_analysis 0 y2 w (h - y2)) != Canvas.DISJOINT:
png_.get_indexed_image_data y2 h
--accept_8_bit=canvas.supports_8_bit
--need_gray_palette=canvas.gray_scale: | y_from/int y_to/int bits_per_pixel/int pixels/ByteArray line_stride/int palette/ByteArray alpha_palette/ByteArray |
Expand Down Expand Up @@ -689,9 +691,9 @@ class Slider extends CustomElement:
analysis = canvas.bounds_analysis 0 0 (w - boundary_) h
else:
analysis = canvas.bounds_analysis 0 0 w (h - boundary_)
if analysis != Canvas.ALL_OUTSIDE:
if analysis == Canvas.ALL_INSIDE:
background_lo_.draw canvas 0 0 w h
if analysis != Canvas.DISJOINT:
if analysis == Canvas.CANVAS_IN_AREA or analysis == Canvas.COINCIDENT:
background_lo_.draw canvas 0 0 w h --autocropped
else:
blend = true
if background_hi_ and boundary_ < thumb_max:
Expand All @@ -700,9 +702,9 @@ class Slider extends CustomElement:
analysis = canvas.bounds_analysis (w - boundary_) 0 w h
else:
analysis = canvas.bounds_analysis 0 (h - boundary_) w h
if analysis != Canvas.ALL_OUTSIDE:
if analysis == Canvas.ALL_INSIDE:
background_hi_.draw canvas 0 0 w h
if analysis != Canvas.DISJOINT:
if analysis == Canvas.CANVAS_IN_AREA or analysis == Canvas.COINCIDENT:
background_hi_.draw canvas 0 0 w h --autocropped
else:
blend = true
if not blend: return
Expand All @@ -717,13 +719,13 @@ class Slider extends CustomElement:
lo_alpha.rectangle 0 0 --w=(w - boundary_) --h=h --color=0xff
else:
lo_alpha.rectangle 0 0 --w=w --h=(h - boundary_) --color=0xff
background_lo_.draw lo 0 0 w h
Background.draw background_lo_ lo 0 0 w h --autocropped
if background_hi_:
if horizontal_:
hi_alpha.rectangle (w - boundary_) 0 --w=boundary_ --h=h --color=0xff
else:
hi_alpha.rectangle 0 (h - boundary_) --w=w --h=boundary_ --color=0xff
background_hi_.draw hi 0 0 w h
Background.draw background_hi_ hi 0 0 w h --autocropped

canvas.composit hi_alpha hi lo_alpha lo

Expand Down
Loading

0 comments on commit 9edd5d8

Please sign in to comment.