Kaynağa Gözat

app: maintain correct emoji size when window_scale changes

darkfi 1 yıl önce
ebeveyn
işleme
66e10d754a
2 değiştirilmiş dosya ile 18 ekleme ve 7 silme
  1. 10 1
      bin/app/src/gfx/linalg.rs
  2. 8 6
      bin/app/src/text/mod.rs

+ 10 - 1
bin/app/src/gfx/linalg.rs

@@ -18,7 +18,7 @@
 
 use async_trait::async_trait;
 use darkfi_serial::{SerialDecodable, SerialEncodable};
-use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
+use std::ops::{Add, AddAssign, Div, DivAssign, Mul, Sub, SubAssign};
 
 #[derive(Clone, Copy, Debug, SerialEncodable, SerialDecodable)]
 pub struct Dimension {
@@ -278,6 +278,15 @@ impl Div<f32> for Rectangle {
     }
 }
 
+impl DivAssign<f32> for Rectangle {
+    fn div_assign(&mut self, scale: f32) {
+        self.x /= scale;
+        self.y /= scale;
+        self.w /= scale;
+        self.h /= scale;
+    }
+}
+
 impl std::fmt::Debug for Rectangle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(f, "({}, {}, {}, {})", self.x, self.y, self.w, self.h)

+ 8 - 6
bin/app/src/text/mod.rs

@@ -110,16 +110,19 @@ impl<'a> Iterator for GlyphPositionIter<'a> {
         let glyph = &self.glyphs[self.i];
         let sprite = &glyph.sprite;
 
+        // current_x/y is scaled real coords
+        // but the returned rect is unscaled
+
         let rect = if sprite.has_fixed_sizes {
             // Downscale by height
             let w = (sprite.bmp_width as f32 * EMOJI_SCALE_FACT * self.font_size) /
                 sprite.bmp_height as f32;
             let h = EMOJI_SCALE_FACT * self.font_size;
 
-            let x = self.current_x;
-            let y = self.current_y - EMOJI_PROP_ABOVE_BASELINE * h;
+            let x = self.current_x / self.window_scale;
+            let y = self.current_y / self.window_scale - (EMOJI_PROP_ABOVE_BASELINE * h);
 
-            self.current_x += w;
+            self.current_x += w * self.window_scale;
 
             Rectangle { x, y, w, h }
         } else {
@@ -136,11 +139,10 @@ impl<'a> Iterator for GlyphPositionIter<'a> {
             self.current_x += x_advance;
             self.current_y += y_advance;
 
-            Rectangle { x, y, w, h }
+            // Downscale back again
+            Rectangle { x, y, w, h } / self.window_scale
         };
 
-        let mut rect = rect / self.window_scale;
-
         self.i += 1;
         Some(rect)
     }