Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local fonts and background local image #295

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions examples/backdround_with_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Shoes.app do
stack width:200, height:400 do
background "docs/static/avatar.png"
end
end
4 changes: 4 additions & 0 deletions examples/local_fonts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Scarpe.app do
font "fonts/Pacifico.ttf"
para "Hello yayyy"
end
Binary file added fonts/Pacifico.ttf
Binary file not shown.
28 changes: 28 additions & 0 deletions lib/scarpe/base64.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "base64"
require "uri"

class Scarpe
module Base64
def valid_url?(string)
uri = URI.parse(string)
uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
rescue URI::InvalidURIError, URI::BadURIError
false
end

def encode_file_to_base64(image_filename)
directory_path = File.dirname(__FILE__, 3)

image_path = File.join(directory_path, image_filename)
puts "directory_path: #{directory_path}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably remove this puts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah hm ha sorry abt tht


image_data = File.binread(image_path)

encoded_data = ::Base64.strict_encode64(image_data)

encoded_data
end
end
end
14 changes: 14 additions & 0 deletions lib/scarpe/font.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class Scarpe
class Font < Scarpe::Widget
display_properties :font

def initialize(font)
@font = font
super

create_display_widget
end
end
end
1 change: 1 addition & 0 deletions lib/scarpe/widgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
require_relative "text_widget"
require_relative "link"
require_relative "line"
require_relative "font"
1 change: 1 addition & 0 deletions lib/scarpe/wv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
require_relative "wv/text_widget"
require_relative "wv/link"
require_relative "wv/line"
require_relative "wv/font"
5 changes: 5 additions & 0 deletions lib/scarpe/wv/background.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# frozen_string_literal: true

require "scarpe/base64"
class Scarpe
module WebviewBackground
include Base64
def style
styles = (super if defined?(super)) || {}
return styles unless @background_color

color = if @background_color.is_a?(Range)
"linear-gradient(45deg, #{@background_color.first}, #{@background_color.last})"
elsif File.exist?(@background_color)
# @background_color is a valid file path
"url(data:image/png;base64,#{encode_file_to_base64(@background_color)})"
else
@background_color
end
Expand Down
33 changes: 33 additions & 0 deletions lib/scarpe/wv/font.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require "scarpe/base64"

class Scarpe
class WebviewFont < WebviewWidget
include Base64
attr_accessor :font

def initialize(properties)
@font = properties[:font]
super
end

def element
puts @font
HTML.render do |h|
h.link(href: @font, rel: "stylesheet")
h.style do
<<~CSS
@font-face {
font-family: Pacifico;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. Can we change this so it's not always Pacifico?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah oh we can its just a custom name but like get name from filepath?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good way to do it, yeah.

I mean, if we can't customise then we can't, but it seems worth a try.

src: url("data:font/truetype;base64,#{encode_file_to_base64(@font)}") format('truetype');
}
* {
font-family: Pacifico;
}
CSS
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/scarpe/wv/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

class Scarpe
class HTML
CONTENT_TAGS = [:div, :p, :button, :ul, :li, :textarea, :a, :strong, :em, :code, :u, :line, :span, :svg].freeze
VOID_TAGS = [:input, :img, :polygon, :path].freeze
CONTENT_TAGS = [:div, :p, :button, :ul, :li, :textarea, :a, :strong, :style, :em, :code, :u, :line, :span, :svg].freeze
VOID_TAGS = [:input, :img, :polygon, :link, :path].freeze
TAGS = (CONTENT_TAGS + VOID_TAGS).freeze

class << self
Expand Down
25 changes: 3 additions & 22 deletions lib/scarpe/wv/image.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

require "base64"
require "uri"
require "scarpe/base64"

class Scarpe
class WebviewImage < WebviewWidget
include Base64
def initialize(properties)
super

@url = valid_url?(@url) ? @url : "data:image/png;base64,#{encode_image_to_base64(@url)}"
@url = valid_url?(@url) ? @url : "data:image/png;base64,#{encode_file_to_base64(@url)}"
end

def element
Expand All @@ -25,25 +25,6 @@ def element

private

def valid_url?(string)
uri = URI.parse(string)
uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
rescue URI::InvalidURIError, URI::BadURIError
false
end

def encode_image_to_base64(image_filename)
directory_path = File.dirname(__FILE__, 4)

image_path = File.join(directory_path, image_filename)

image_data = File.binread(image_path)

encoded_data = Base64.strict_encode64(image_data)

encoded_data
end

def style
styles = {}

Expand Down