freetype-commit
[Top][All Lists]
Advanced

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

[freetype2-demos] master f374d2f 12/41: [ftinspect] Support embedded bit


From: Werner Lemberg
Subject: [freetype2-demos] master f374d2f 12/41: [ftinspect] Support embedded bitmap and bitmap-only fonts.
Date: Mon, 3 Oct 2022 11:27:01 -0400 (EDT)

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

    [ftinspect] Support embedded bitmap and bitmap-only fonts.
    
    Bitmap glyphs are already supported in the new rendering engine, so we make
    the option effective in the GUI. Also support bitmap-only fonts by coercing
    sizes to available ones and force embedded bitmaps.
    
    * src/ftinspect/widgets/fontsizeselector.cpp,
      src/ftinspect/widgets/fontsizeselector.hpp:
      Retrieve fixed sizes info from the engine and coerce to nearest available
      sizes when using bitmap-only fonts.
    
    * src/ftinspect/engine/engine.cpp, src/ftinspect/engine/engine.hpp:
      Add `embeddedBitmapEnabled` property and `currentFontHasEmbeddedBitmap`,
      `currentFontBitmapOnly` and `currentFontFixedSizes` getters.
    
    * src/ftinspect/panels/settingpanel.cpp: Uncomment functional code.
---
 src/ftinspect/engine/engine.cpp            | 44 +++++++++++++++++++----
 src/ftinspect/engine/engine.hpp            |  7 ++++
 src/ftinspect/panels/settingpanel.cpp      | 14 ++++----
 src/ftinspect/widgets/fontsizeselector.cpp | 57 ++++++++++++++++++++++++++++--
 src/ftinspect/widgets/fontsizeselector.hpp |  3 ++
 5 files changed, 110 insertions(+), 15 deletions(-)

diff --git a/src/ftinspect/engine/engine.cpp b/src/ftinspect/engine/engine.cpp
index 1b4deaf..349f930 100644
--- a/src/ftinspect/engine/engine.cpp
+++ b/src/ftinspect/engine/engine.cpp
@@ -386,6 +386,38 @@ Engine::removeFont(int fontIndex, bool closeFile)
 }
 
 
+bool
+Engine::currentFontBitmapOnly()
+{
+  if (!ftFallbackFace_)
+    return false;
+  return !FT_IS_SCALABLE(ftFallbackFace_);
+}
+
+
+bool
+Engine::currentFontHasEmbeddedBitmap()
+{
+  if (!ftFallbackFace_)
+    return false;
+  return FT_HAS_FIXED_SIZES(ftFallbackFace_);
+}
+
+
+std::vector<int>
+Engine::currentFontFixedSizes()
+{
+  if (!ftFallbackFace_ || !FT_HAS_FIXED_SIZES(ftFallbackFace_)
+      || !ftFallbackFace_->available_sizes)
+    return {};
+  std::vector<int> result;
+  result.resize(ftFallbackFace_->num_fixed_sizes);
+  for (int i = 0; i < ftFallbackFace_->num_fixed_sizes; i++)
+    result[i] = ftFallbackFace_->available_sizes[i].x_ppem >> 6; // XXX: ????
+  return result;
+}
+
+
 QString
 Engine::glyphName(int index)
 {
@@ -557,15 +589,15 @@ void
 Engine::update()
 {
   loadFlags_ = FT_LOAD_DEFAULT;
-  if (doAutoHinting_)
-    loadFlags_ |= FT_LOAD_FORCE_AUTOHINT;
-  loadFlags_ |= FT_LOAD_NO_BITMAP; // XXX handle bitmap fonts also
+
+  if (!embeddedBitmap_)
+    loadFlags_ |= FT_LOAD_NO_BITMAP;
 
   if (doHinting_)
   {
-    // TODO Differentiate RGB/BGR here?
-    unsigned long target = antiAliasingTarget_;
-    loadFlags_ |= target;
+    loadFlags_ |= antiAliasingTarget_;
+    if (doAutoHinting_)
+      loadFlags_ |= FT_LOAD_FORCE_AUTOHINT;
   }
   else
   {
diff --git a/src/ftinspect/engine/engine.hpp b/src/ftinspect/engine/engine.hpp
index 8698a70..efcc847 100644
--- a/src/ftinspect/engine/engine.hpp
+++ b/src/ftinspect/engine/engine.hpp
@@ -115,9 +115,14 @@ public:
                              long faceIndex);
   QString namedInstanceName(int fontIndex, long faceIndex, int index);
 
+  bool currentFontBitmapOnly();
+  bool currentFontHasEmbeddedBitmap();
+  std::vector<int> currentFontFixedSizes();
+
   // (settings)
   int dpi() { return dpi_; }
   bool antiAliasingEnabled() { return antiAliasingEnabled_; }
+  bool embeddedBitmapEnabled() { return embeddedBitmap_; }
   FT_Render_Mode
   renderMode()
   {
@@ -147,6 +152,7 @@ public:
   void setAntiAliasingTarget(int target) { antiAliasingTarget_ = target; }
   void setRenderMode(int mode) { renderMode_ = mode; }
   void setAntiAliasingEnabled(bool enabled) { antiAliasingEnabled_ = enabled; }
+  void setEmbeddedBitmapEnabled(bool enabled) { embeddedBitmap_ = enabled; }
 
   // Note: These 3 functions now takes actual mode/version from FreeType,
   // instead of values from enum in MainGUI!
@@ -202,6 +208,7 @@ private:
   bool doVerticalHinting_;
   bool doBlueZoneHinting_;
   bool showSegments_;
+  bool embeddedBitmap_;
   int antiAliasingTarget_;
   int renderMode_;
 
diff --git a/src/ftinspect/panels/settingpanel.cpp 
b/src/ftinspect/panels/settingpanel.cpp
index 8b29509..ecf0f14 100644
--- a/src/ftinspect/panels/settingpanel.cpp
+++ b/src/ftinspect/panels/settingpanel.cpp
@@ -286,7 +286,7 @@ SettingPanel::onFontChanged()
 
   checkHinting();
 
-  //engine_->reloadFont();
+  engine_->reloadFont();
   //auto hasColor = engine_->currentFontHasColorLayers();
   //colorLayerCheckBox_->setEnabled(hasColor);
   //if (!hasColor)
@@ -296,11 +296,11 @@ SettingPanel::onFontChanged()
   blockSignals(blockState);
 
   // Place this after `blockSignals` to let the signals emitted normally
-  //auto bmapOnly = engine_->currentFontBitmapOnly();
-  //embeddedBitmapCheckBox_->setEnabled(
-  //  !bmapOnly && engine_->currentFontHasEmbeddedBitmap());
-  //if (bmapOnly)
-  //  embeddedBitmapCheckBox_->setChecked(true);
+  auto bmapOnly = engine_->currentFontBitmapOnly();
+  embeddedBitmapCheckBox_->setEnabled(
+    !bmapOnly && engine_->currentFontHasEmbeddedBitmap());
+  if (bmapOnly)
+    embeddedBitmapCheckBox_->setChecked(true);
 }
 
 
@@ -331,7 +331,7 @@ SettingPanel::applySettings()
 
   engine_->renderingEngine()->setGamma(gammaSlider_->value() / 10.0);
 
-  //engine_->setEmbeddedBitmap(embeddedBitmapCheckBox_->isChecked());
+  engine_->setEmbeddedBitmapEnabled(embeddedBitmapCheckBox_->isChecked());
   //engine_->setPaletteIndex(paletteComboBox_->currentIndex());
 
   //engine_->setUseColorLayer(colorLayerCheckBox_->isChecked());
diff --git a/src/ftinspect/widgets/fontsizeselector.cpp 
b/src/ftinspect/widgets/fontsizeselector.cpp
index 58daa2e..575fe12 100644
--- a/src/ftinspect/widgets/fontsizeselector.cpp
+++ b/src/ftinspect/widgets/fontsizeselector.cpp
@@ -70,7 +70,21 @@ FontSizeSelector::setZoomFactor(double zoomFactor)
 void
 FontSizeSelector::reloadFromFont(Engine* engine)
 {
-  // TODO: update available sizes.
+  engine->reloadFont();
+  bitmapOnly_ = engine->currentFontBitmapOnly();
+  fixedSizes_ = engine->currentFontFixedSizes();
+  std::sort(fixedSizes_.begin(), fixedSizes_.end());
+  if (fixedSizes_.empty())
+    bitmapOnly_ = false; // Well this won't work...
+
+  unitsComboBox_->setEnabled(!bitmapOnly_);
+  sizeDoubleSpinBox_->setKeyboardTracking(!bitmapOnly_);
+
+  if (bitmapOnly_) 
+  {
+    QSignalBlocker blocker(this);
+    unitsComboBox_->setCurrentIndex(Units_px);
+  }
   checkFixedSizeAndEmit();
 }
 
@@ -291,7 +305,46 @@ FontSizeSelector::setDefaults(bool sizeOnly)
 void
 FontSizeSelector::checkFixedSizeAndEmit()
 {
-  // TODO: check fixed sizes, coerce to available sizes.
+  if (bitmapOnly_ && !fixedSizes_.empty())
+  {
+    auto newValue = sizeDoubleSpinBox_->value();
+    auto intNewValue = static_cast<int>(newValue);
+    if (newValue != static_cast<double>(intNewValue))
+    {
+      sizeDoubleSpinBox_->setValue(intNewValue);
+      return; // Don't emit.
+    }
+
+    if (!std::binary_search(fixedSizes_.begin(), fixedSizes_.end(), newValue))
+    {
+      // Value not available, find next value.
+      if (intNewValue > lastValue_)
+      {
+        // find next larger value...
+        auto it = std::upper_bound(fixedSizes_.begin(), fixedSizes_.end(),
+                                   lastValue_);
+        if (it == fixedSizes_.end())
+          sizeDoubleSpinBox_->setValue(lastValue_);
+        else
+          sizeDoubleSpinBox_->setValue(*it);
+      }
+      else
+      {
+        // find next smaller value...
+        auto it = std::lower_bound(fixedSizes_.begin(), fixedSizes_.end(),
+                                   lastValue_);
+
+        // there's no element >= lastValue => all elements < last value
+        if (it == fixedSizes_.begin())
+          sizeDoubleSpinBox_->setValue(fixedSizes_.front());
+        else
+          sizeDoubleSpinBox_->setValue(*(it - 1));
+      }
+      return;
+    }
+  }
+
+  lastValue_ = sizeDoubleSpinBox_->value();
   emit valueChanged();
 }
 
diff --git a/src/ftinspect/widgets/fontsizeselector.hpp 
b/src/ftinspect/widgets/fontsizeselector.hpp
index 9694110..4f0dc46 100644
--- a/src/ftinspect/widgets/fontsizeselector.hpp
+++ b/src/ftinspect/widgets/fontsizeselector.hpp
@@ -6,6 +6,7 @@
 
 #include "customwidgets.hpp"
 
+#include <vector>
 #include <QComboBox>
 #include <QDoubleSpinBox>
 #include <QLabel>
@@ -69,6 +70,8 @@ private:
 
   bool continuousView_;
   double lastValue_;
+  bool bitmapOnly_ = false;
+  std::vector<int> fixedSizes_;
 
   void createLayout(bool zoomNewLine);
   void createConnections();



reply via email to

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