mirror of
https://github.com/csd4ni3l/browser.git
synced 2025-11-05 05:57:57 +01:00
fix css loading issues with weird urls that include parameters, fix font size issues with font sizes that are rem and fix text merging on Windows cause space width is 0, make clicking on link change search bar text.
This commit is contained in:
@@ -219,7 +219,7 @@ class HTTPClient():
|
||||
for css_link in css_links:
|
||||
self.content_response = ""
|
||||
|
||||
css_cache_filename = f"{self.scheme}_{self.host}_{self.port}_{self.path.replace('/', '_')}_{css_link.replace('/', '_')}.json" # we need to include the other variables so for example /styles.css wouldnt be cached for all websites
|
||||
css_cache_filename = f"{self.scheme}_{self.host}_{self.port}_{self.path.replace('/', '_')}_{css_link.replace('/', '_').replace('@', '_').replace('/', '_').replace(';', '_').replace('&', '_').replace('?', '_').replace(':', '')}.json" # we need to include the other variables so for example /styles.css wouldnt be cached for all websites
|
||||
|
||||
if css_cache_filename in os.listdir("css_cache"):
|
||||
with open(f"css_cache/{css_cache_filename}", "r") as file:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import arcade, pyglet
|
||||
import arcade, pyglet, platform
|
||||
|
||||
from utils.constants import BLOCK_ELEMENTS, token_pattern, emoji_pattern
|
||||
from utils.constants import BLOCK_ELEMENTS, token_pattern, emoji_pattern, INHERITED_PROPERTIES
|
||||
from utils.utils import get_color_from_name, hex_to_rgb
|
||||
|
||||
from http_client.connection import HTTPClient, resolve_url
|
||||
@@ -13,6 +13,8 @@ from functools import lru_cache
|
||||
HSTEP = 13
|
||||
VSTEP = 18
|
||||
|
||||
SPACE_MULTIPLIER = 0.25 if not platform.system() == "Windows" else 0.33
|
||||
|
||||
font_cache = {}
|
||||
|
||||
def ensure_font(font_family, size, weight, style, emoji):
|
||||
@@ -23,7 +25,7 @@ def ensure_font(font_family, size, weight, style, emoji):
|
||||
|
||||
@lru_cache
|
||||
def get_space_width(font: BaseFont):
|
||||
return font.get_text_size(" ")[0]
|
||||
return font.get_text_size("a")[0] * SPACE_MULTIPLIER # i have to do this because space width is 0 on Windows (of course, why wouldnt Windows ruin everything?)
|
||||
|
||||
class DrawText:
|
||||
def __init__(self, x1, y1, text, font, color):
|
||||
@@ -97,7 +99,10 @@ class TextLayout():
|
||||
style = self.node.style["font-style"]
|
||||
font_family = self.node.style["font-family"]
|
||||
style = "roman" if style == "normal" else style
|
||||
size = int(float(self.node.style["font-size"][:-2]))
|
||||
if not self.node.style["font-size"].endswith("em") and not self.node.style["font-size"].endswith("rem"):
|
||||
size = int(float(self.node.style["font-size"][:-2]))
|
||||
else:
|
||||
size = int(INHERITED_PROPERTIES["font-size"][:-2])
|
||||
self.font = ensure_font(font_family, size, weight, style, self.emoji)
|
||||
|
||||
self.width = self.font.get_text_size(self.word + (" " if not self.emoji else " "))[0]
|
||||
@@ -187,7 +192,10 @@ class BlockLayout:
|
||||
style = node.style["font-style"]
|
||||
font_family = node.style["font-family"]
|
||||
style = "roman" if style == "normal" else style
|
||||
size = int(float(node.style["font-size"][:-2]))
|
||||
if not node.style["font-size"].endswith("em") and not node.style["font-size"].endswith("rem"):
|
||||
size = int(float(node.style["font-size"][:-2]))
|
||||
else:
|
||||
size = int(INHERITED_PROPERTIES["font-size"][:-2])
|
||||
|
||||
font = ensure_font(font_family, size, weight, style, emoji)
|
||||
|
||||
@@ -237,10 +245,11 @@ def paint_tree(layout_object, display_list):
|
||||
paint_tree(child, display_list)
|
||||
|
||||
class Renderer():
|
||||
def __init__(self, http_client: HTTPClient, window: arcade.Window):
|
||||
def __init__(self, http_client: HTTPClient, view_class):
|
||||
self.content = ''
|
||||
self.request_scheme = 'http'
|
||||
self.window = window
|
||||
self.view_class = view_class
|
||||
self.window: arcade.Window = view_class.window
|
||||
self.http_client = http_client
|
||||
|
||||
self.scroll_y = 0
|
||||
@@ -310,6 +319,7 @@ class Renderer():
|
||||
elif elt.tag == "a" and "href" in elt.attributes:
|
||||
url = resolve_url(self.http_client.scheme, self.http_client.host, self.http_client.port, self.http_client.path, elt.attributes["href"])
|
||||
self.http_client.get_request(url, self.http_client.request_headers)
|
||||
self.view_class.search_bar.text = url
|
||||
|
||||
elt = elt.parent
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ class Main(arcade.gui.UIView):
|
||||
super().on_show_view()
|
||||
|
||||
self.search_bar = self.add_widget(arcade.gui.UIInputText(x=self.window.width / 4, y=self.window.height * 0.95, width=self.window.width / 2, height=self.window.height * 0.035, font_name="Roboto", font_size=14, text_color=arcade.color.BLACK, caret_color=arcade.color.BLACK, border_color=arcade.color.BLACK))
|
||||
self.renderer = Renderer(self.http_client, self.window)
|
||||
self.renderer = Renderer(self.http_client, self)
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
self.search_bar.text = self.search_bar.text.encode("ascii", "ignore").decode().strip("\n")
|
||||
|
||||
Reference in New Issue
Block a user