freetype-commit
[Top][All Lists]
Advanced

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

[freetype2-demos] gsoc-2022-chariri-3 8a4bbbe 25/36: [ftinspect] Improve


From: Werner Lemberg
Subject: [freetype2-demos] gsoc-2022-chariri-3 8a4bbbe 25/36: [ftinspect] Improve Waterfall mode.
Date: Wed, 27 Jul 2022 06:32:46 -0400 (EDT)

branch: gsoc-2022-chariri-3
commit 8a4bbbeedafbeb1896864080e915da5a5f383f68
Author: Charlie Jiang <w@chariri.moe>
Commit: Charlie Jiang <w@chariri.moe>

    [ftinspect] Improve Waterfall mode.
    
    * src/ftinspect/rendering/glyphcontinuous.cpp,
      src/ftinspect/rendering/glyphcontinuous.hpp:
      Don't paint the red square (placeholder for zero x-advance glyphs) when in
      Waterfall mode.
      Use the `StringRenderer`'s line begin callback to render a font point size
      prefix when in Waterfall mode. `sizeIndicatorOffset_` shifts the whole
      string right.
    
    * src/ftinspect/engine/stringrenderer.cpp: Fix `pointSize` return type.
      Add `isWaterfall` func.
      Add support to line begin hook callback.
    
    * src/ftinspect/engine/engine.hpp: `pointSize` func should return `double`.
---
 src/ftinspect/engine/engine.hpp             |  2 +-
 src/ftinspect/engine/stringrenderer.cpp     |  9 ++++++--
 src/ftinspect/engine/stringrenderer.hpp     | 26 +++++++++++++++++----
 src/ftinspect/rendering/glyphcontinuous.cpp | 36 +++++++++++++++++++++++++++--
 src/ftinspect/rendering/glyphcontinuous.hpp |  4 ++++
 5 files changed, 68 insertions(+), 9 deletions(-)

diff --git a/src/ftinspect/engine/engine.hpp b/src/ftinspect/engine/engine.hpp
index 8bd759a..2d89647 100644
--- a/src/ftinspect/engine/engine.hpp
+++ b/src/ftinspect/engine/engine.hpp
@@ -108,7 +108,7 @@ public:
   FT_Library ftLibrary() const { return library_; }
   FTC_Manager cacheManager() { return cacheManager_; }
   int dpi() { return dpi_; }
-  int pointSize() { return pointSize_; }
+  double pointSize() { return pointSize_; }
   int currentFontType() const { return fontType_; }
   const QString& currentFamilyName() { return curFamilyName_; }
   const QString& currentStyleName() { return curStyleName_; }
diff --git a/src/ftinspect/engine/stringrenderer.cpp 
b/src/ftinspect/engine/stringrenderer.cpp
index e2dd74c..6c05422 100644
--- a/src/ftinspect/engine/stringrenderer.cpp
+++ b/src/ftinspect/engine/stringrenderer.cpp
@@ -350,7 +350,7 @@ StringRenderer::render(int width,
     // Waterfall
 
     vertical_ = false;
-    auto originalSize = engine_->pointSize() * 64;
+    auto originalSize = static_cast<int>(engine_->pointSize() * 64);
     auto ptSize = originalSize;
     auto ptHeight = 64 * 72 * height / engine_->dpi();
     auto step = (ptSize * ptSize / ptHeight + 64) & ~63;
@@ -431,7 +431,7 @@ StringRenderer::renderLine(int x,
 {
   if (x < 0 || y < 0 || x > width || y > height)
     return 0;
-  
+
   y = height - y; // change to Cartesian coordinates
 
   FT_Vector pen = { 0, 0 };
@@ -462,6 +462,11 @@ StringRenderer::renderLine(int x,
   pen.x = (x << 6) - pen.x;
   pen.y = (y << 6) - pen.y;
 
+  // Need to transform the coord back to normal coord system
+  lineBeginCallback_({ (pen.x >> 6), 
+                       height - (pen.y >> 6) },
+                     engine_->pointSize());
+
   for (int i = offset; i < totalCount + offset; i++)
   {
     auto& ctx = activeGlyphs_[i % activeGlyphs_.size()];
diff --git a/src/ftinspect/engine/stringrenderer.hpp 
b/src/ftinspect/engine/stringrenderer.hpp
index 9d61da1..f4f62cd 100644
--- a/src/ftinspect/engine/stringrenderer.hpp
+++ b/src/ftinspect/engine/stringrenderer.hpp
@@ -57,7 +57,8 @@ public:
   };
 
   using RenderCallback = std::function<void(FT_Glyph)>;
-  /* The glyph pointer may be replaced. In that case, ownership is transfered
+  /*
+   * The glyph pointer may be replaced. In that case, ownership is transfered
    * to the renderer, and the new glyph will be eventually freed by
    * the renderer. The callback is responsible to free the old glyph.
    * This allows you to do the following:
@@ -69,16 +70,32 @@ public:
    * }
    */
   using PreprocessCallback = std::function<void(FT_Glyph*)>;
+  /*
+   * Called when a new line begins.
+   * The 1st parameter is the initial pen position;
+   * The 2nd parameter is the current size in points.
+   */
+  using LineBeginCallback = std::function<void(FT_Vector, double)>;
 
-  void setCharMapIndex(int charMapIndex, int limitIndex);
-  void setCallback(RenderCallback cb)
+  bool isWaterfall() { return waterfall_; }
+
+  void
+  setCallback(RenderCallback cb)
   {
     renderCallback_ = std::move(cb);
   }
-  void setPreprocessCallback(PreprocessCallback cb)
+  void
+  setPreprocessCallback(PreprocessCallback cb)
   {
     glyphPreprocessCallback_ = std::move(cb);
   }
+  void
+  setLineBeginCallback(LineBeginCallback cb)
+  {
+    lineBeginCallback_ = std::move(cb);
+  }
+
+  void setCharMapIndex(int charMapIndex, int limitIndex);
   void setRepeated(bool repeated) { repeated_ = repeated; }
   void setVertical(bool vertical) { vertical_ = vertical; }
   void setRotation(double rotation);
@@ -151,6 +168,7 @@ private:
 
   RenderCallback renderCallback_;
   PreprocessCallback glyphPreprocessCallback_;
+  LineBeginCallback lineBeginCallback_;
 
   void reloadGlyphIndices();
   void prepareRendering();
diff --git a/src/ftinspect/rendering/glyphcontinuous.cpp 
b/src/ftinspect/rendering/glyphcontinuous.cpp
index f30ea51..c93f764 100644
--- a/src/ftinspect/rendering/glyphcontinuous.cpp
+++ b/src/ftinspect/rendering/glyphcontinuous.cpp
@@ -106,6 +106,11 @@ GlyphContinuous::paintByRenderer(QPainter* painter)
     {
       preprocessGlyph(ptr);
     });
+  stringRenderer_.setLineBeginCallback(
+    [&](FT_Vector pos, double size)
+    {
+      beginLine(painter, pos, size);
+    });
   displayingCount_ = stringRenderer_.render(width(), height(), beginIndex_);
 }
 
@@ -229,6 +234,32 @@ GlyphContinuous::preprocessGlyph(FT_Glyph* glyphPtr)
 }
 
 
+void
+GlyphContinuous::beginLine(QPainter* painter,
+                           FT_Vector pos,
+                           double sizePoint)
+{
+  // Now only used by waterfall mode to draw a size indicator.
+  if (!stringRenderer_.isWaterfall())
+  {
+    sizeIndicatorOffset_ = 0;
+    return;
+  }
+
+  auto oldFont = painter->font();
+  oldFont.setPointSizeF(sizePoint);
+  painter->setFont(oldFont);
+  auto metrics = painter->fontMetrics();
+
+  auto sizePrefix = QString("%1: ").arg(sizePoint);
+  QPoint posQ = { static_cast<int>(pos.x),
+                  static_cast<int>(pos.y) };
+  painter->drawText(posQ, sizePrefix);
+
+  sizeIndicatorOffset_ = metrics.horizontalAdvance(sizePrefix);
+}
+
+
 void
 GlyphContinuous::drawSingleGlyph(QPainter* painter, FT_Glyph glyph)
 {
@@ -236,7 +267,7 @@ GlyphContinuous::drawSingleGlyph(QPainter* painter, 
FT_Glyph glyph)
   int width = glyph->advance.x ? glyph->advance.x >> 16
                                 : metrics_.y_ppem / 2;
   
-  if (glyph->advance.x == 0)
+  if (glyph->advance.x == 0 && !stringRenderer_.isWaterfall())
   {
     // Draw a red square to indicate
       painter->fillRect(x_, y_ - width, width, width,
@@ -245,7 +276,8 @@ GlyphContinuous::drawSingleGlyph(QPainter* painter, 
FT_Glyph glyph)
 
   QRect rect;
   QImage* image = engine_->convertGlyphToQImage(glyph, &rect, false);
-  rect.setTop(height() - rect.top());
+  rect.setTop(height() - rect.top()); // TODO Don't place this here...
+  rect.setLeft(rect.left() + sizeIndicatorOffset_);
 
   painter->drawImage(rect.topLeft(), *image);
   delete image;
diff --git a/src/ftinspect/rendering/glyphcontinuous.hpp 
b/src/ftinspect/rendering/glyphcontinuous.hpp
index 284a6dd..7ead399 100644
--- a/src/ftinspect/rendering/glyphcontinuous.hpp
+++ b/src/ftinspect/rendering/glyphcontinuous.hpp
@@ -75,6 +75,7 @@ private:
   double boldX_, boldY_, slant_;
   double strokeRadius_;
   QString text_;
+  int sizeIndicatorOffset_; // For Waterfall Rendering...
 
   int displayingCount_ = 0;
   FT_Size_Metrics metrics_;
@@ -96,6 +97,9 @@ private:
   void prePaint();
   void updateRendererText();
   void preprocessGlyph(FT_Glyph* glyphPtr);
+  void beginLine(QPainter* painter,
+                 FT_Vector pos, 
+                 double sizePoint);
   void drawSingleGlyph(QPainter* painter,
                        FT_Glyph glyph);
 };



reply via email to

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