freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2-demos] master 531d006 30/41: [ftinspect] Improve bitmap displ


From: Werner Lemberg
Subject: [freetype2-demos] master 531d006 30/41: [ftinspect] Improve bitmap display in the glyph details pane.
Date: Mon, 3 Oct 2022 11:27:03 -0400 (EDT)

branch: master
commit 531d00690b6eac7adfd4035a99cce5612c4874f3
Author: Charlie Jiang <w@chariri.moe>
Commit: Werner Lemberg <wl@gnu.org>

    [ftinspect] Improve bitmap display in the glyph details pane.
    
    Now the ppem square is displayed as light gray, ink box as blue,
    xy-axis as thin black lines.
    Adding ppem square improves displaying of non-spacing glyphs.
    
    * src/ftinspect/panels/glyphdetails.cpp:
      Pass the ppem box to the `GlyphBitmap` widget.
    
    * src/ftinspect/glyphcomponents/glyphbitmap.cpp,
      src/ftinspect/glyphcomponents/glyphbitmap.hpp:
      Add a `placeholderRect` to store the ppem square info. Layout the glyph
      bitmap on the basis of the ppem square.
      Draw auxiliary lines on the canvas.
---
 src/ftinspect/glyphcomponents/glyphbitmap.cpp | 66 ++++++++++++++++++++++-----
 src/ftinspect/glyphcomponents/glyphbitmap.hpp |  6 ++-
 src/ftinspect/panels/glyphdetails.cpp         |  7 ++-
 3 files changed, 63 insertions(+), 16 deletions(-)

diff --git a/src/ftinspect/glyphcomponents/glyphbitmap.cpp 
b/src/ftinspect/glyphcomponents/glyphbitmap.cpp
index 90a0b81..30f6552 100644
--- a/src/ftinspect/glyphcomponents/glyphbitmap.cpp
+++ b/src/ftinspect/glyphcomponents/glyphbitmap.cpp
@@ -107,14 +107,17 @@ GlyphBitmapWidget::~GlyphBitmapWidget()
 
 void
 GlyphBitmapWidget::updateImage(QImage* image,
-                               QRect rect)
+                               QRect rect,
+                               QRect placeholderRect)
 {
-  rect.moveTop(0);
-  rect.moveLeft(0);
-
   delete bitmapItem_;
   auto* copied = new QImage(image->copy());
-  bitmapItem_ = new GlyphBitmap(copied, rect);
+
+  rect_ = rect;
+  placeholderRect_ = placeholderRect;
+  auto zeroedRect = rect; // `GlyphBitmap` doesn't play well with offset
+  zeroedRect.moveTopLeft({ 0, 0 });
+  bitmapItem_ = new GlyphBitmap(copied, zeroedRect);
 
   repaint();
 }
@@ -135,24 +138,63 @@ GlyphBitmapWidget::paintEvent(QPaintEvent* event)
   if (!bitmapItem_)
     return;
   auto s = size();
-  auto br = bitmapItem_->boundingRect();
-  double xScale = s.width() / br.width();
-  double yScale = s.height() / br.height();
+
+  auto br = QRect(QPoint(std::min(rect_.left(), placeholderRect_.left()),
+                         std::min(rect_.top(), placeholderRect_.top())),
+                  QPoint(std::max(rect_.right(), placeholderRect_.right()),
+                         std::max(rect_.bottom(), placeholderRect_.bottom())));
+  
+  double xScale = 0.9 * s.width() / br.width();
+  double yScale = 0.9 * s.height() / br.height();
+  auto margin = br.width() * 0.05;
   auto scale = std::min(xScale, yScale);
 
   QPainter painter(this);
   painter.fillRect(rect(), Qt::white);
+  painter.save(); // push before scaling
   painter.scale(scale, scale);
+  painter.translate(-br.topLeft() + QPointF(margin, margin));
+
+  double scaledLineWidth = 4 / scale;
+  double scaledLineWidthHalf = scaledLineWidth / 2;
+  painter.setPen(QPen(Qt::blue, scaledLineWidth));
+  // Blue line: Ink box
+  painter.drawRect(QRectF(rect_).adjusted(-scaledLineWidthHalf,
+                                          -scaledLineWidthHalf,
+                                          scaledLineWidthHalf,
+                                          scaledLineWidthHalf));
+
+  painter.save(); // push before translating
+  painter.translate(rect_.topLeft());
 
   QStyleOptionGraphicsItem ogi;
   ogi.exposedRect = br;
   bitmapItem_->paint(&painter, &ogi, this);
 
-  double scaledLineWidth = 4 / scale;
-  painter.setPen(QPen(Qt::black, scaledLineWidth));
+  painter.restore(); // undo translating.
+
   scaledLineWidth /= 2;
-  painter.drawRect(br.adjusted(scaledLineWidth, scaledLineWidth,
-                               -scaledLineWidth, -scaledLineWidth));
+  scaledLineWidthHalf /= 2;
+  // Light gray line: EM box
+  painter.setPen(QPen(Qt::lightGray, scaledLineWidth));
+  painter.drawRect(QRectF(placeholderRect_).adjusted(-scaledLineWidthHalf, 
+                                                     -scaledLineWidthHalf,
+                                                     scaledLineWidthHalf,
+                                                     scaledLineWidthHalf));
+
+  auto tfForAxis = painter.transform().inverted();
+  painter.setPen(QPen(Qt::black, scaledLineWidth / 2));
+  // Thin black line: xy-axis
+  painter.drawLine(QPointF(tfForAxis.map(QPointF(0, 0)).x(), 0),
+                   QPointF(tfForAxis.map(QPointF(s.width(), 0)).x(), 0));
+  painter.drawLine(QPointF(0, tfForAxis.map(QPointF(0, 0)).y()),
+                   QPointF(0, tfForAxis.map(QPointF(0, s.height())).y()));
+
+  painter.restore(); // undo scaling.
+
+  // main border
+  painter.setPen(QPen(Qt::black, 4));
+  painter.drawRect(rect().adjusted(2, 2, -2, -2));
 }
 
 
diff --git a/src/ftinspect/glyphcomponents/glyphbitmap.hpp 
b/src/ftinspect/glyphcomponents/glyphbitmap.hpp
index 52cfd22..11d5f3f 100644
--- a/src/ftinspect/glyphcomponents/glyphbitmap.hpp
+++ b/src/ftinspect/glyphcomponents/glyphbitmap.hpp
@@ -48,8 +48,8 @@ class GlyphBitmapWidget
 public:
   GlyphBitmapWidget(QWidget* parent);
   ~GlyphBitmapWidget() override;
-
-  void updateImage(QImage* image, QRect rect);
+  
+  void updateImage(QImage* image, QRect rect, QRect placeholderRect = {});
   void releaseImage();
 
 signals:
@@ -62,6 +62,8 @@ protected:
 
 private:
   GlyphBitmap* bitmapItem_ = NULL;
+  QRect rect_ = {};
+  QRect placeholderRect_ = {};
 };
 
 
diff --git a/src/ftinspect/panels/glyphdetails.cpp 
b/src/ftinspect/panels/glyphdetails.cpp
index c412029..fa595ee 100644
--- a/src/ftinspect/panels/glyphdetails.cpp
+++ b/src/ftinspect/panels/glyphdetails.cpp
@@ -28,6 +28,7 @@ GlyphDetails::~GlyphDetails()
 void
 GlyphDetails::updateGlyph(GlyphCacheEntry& ctxt, int charMapIndex)
 {
+  auto metrics = engine_->currentFontMetrics();
   auto& cMaps = engine_->currentFontCharMaps();
 
   glyphIndex_ = ctxt.glyphIndex;
@@ -49,10 +50,12 @@ GlyphDetails::updateGlyph(GlyphCacheEntry& ctxt, int 
charMapIndex)
   if (glyphName.isEmpty())
     glyphName = "(none)";
   glyphNameLabel_->setText(glyphName);
-
+  
   auto rect = ctxt.basePosition.translated(-(ctxt.penPos.x()),
                                            -(ctxt.penPos.y()));
-  bitmapWidget_->updateImage(ctxt.image, rect);
+  bitmapWidget_->updateImage(
+      ctxt.image, rect,
+      QRect(0, -metrics.y_ppem, metrics.y_ppem, metrics.y_ppem));
 
   // load glyphs in all units
   dpi_ = engine_->dpi();



reply via email to

[Prev in Thread] Current Thread [Next in Thread]