freetype-commit
[Top][All Lists]
Advanced

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

[freetype2-demos] master 422b6b6 3/3: [ftinspect] Add file watching; re


From: Werner LEMBERG
Subject: [freetype2-demos] master 422b6b6 3/3: [ftinspect] Add file watching; remove watch button.
Date: Wed, 11 May 2016 10:43:48 +0000 (UTC)

branch: master
commit 422b6b63e6b0dd9d30b3da7741ebfd20a14a2f2c
Author: Werner Lemberg <address@hidden>
Commit: Werner Lemberg <address@hidden>

    [ftinspect] Add file watching; remove watch button.
    
    * src/ftinspect.cpp (Engine::loadFont): Initialize file info.
    (Engine::watchCurrentFont): New method.
    (Engine::update): Updated.
    (MainGUI::MainGUI): Set up timer.
    (MainGUI::showFont): Add parameter to control updating of current
    indices.
    (MainGUI::watchCurrentFont): New method.
    (MainGUI::createLayout): Remove watch button code.
    (MainGUI::createConnections): Handle timer.
    
    * src/ftinspect.h (Engine): Add `currentFontFileInfo',
    `currentFontDateTime', `maxRetries', and `currentRetry'.
    Updated.
    (MainGUI): Updated.
---
 ChangeLog         |   19 ++++++++
 src/ftinspect.cpp |  125 +++++++++++++++++++++++++++++++++++++++++++++++------
 src/ftinspect.h   |   17 ++++++--
 3 files changed, 145 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index aebe712..e2d3acd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2016-05-11  Werner Lemberg  <address@hidden>
+
+       [ftinspect] Add file watching; remove watch button.
+
+       * src/ftinspect.cpp (Engine::loadFont): Initialize file info.
+       (Engine::watchCurrentFont): New method.
+       (Engine::update): Updated.
+       (MainGUI::MainGUI): Set up timer.
+       (MainGUI::showFont): Add parameter to control updating of current
+       indices.
+       (MainGUI::watchCurrentFont): New method.
+       (MainGUI::createLayout): Remove watch button code.
+       (MainGUI::createConnections): Handle timer.
+
+       * src/ftinspect.h (Engine): Add `currentFontFileInfo',
+       `currentFontDateTime', `maxRetries', and `currentRetry'.
+       Updated.
+       (MainGUI): Updated.
+
 2016-05-10  Werner Lemberg  <address@hidden>
 
        * src/ftinspect.cpp, src/ftinspect.h (MainGUI): Provide `aboutQt'.
diff --git a/src/ftinspect.cpp b/src/ftinspect.cpp
index 0d1e8de..8fc72ca 100644
--- a/src/ftinspect.cpp
+++ b/src/ftinspect.cpp
@@ -332,6 +332,12 @@ Engine::loadFont(int fontIndex,
     return -1;
   }
 
+  currentFontFileInfo.setFile(gui->fonts[fontIndex].filePathname);
+  currentFontFileInfo.setCaching(false);
+  currentFontDateTime = currentFontFileInfo.lastModified();
+  if (currentFontFileInfo.exists())
+    currentRetry = 0;
+
   FT_Module module = &ftSize->face->driver->root;
 
   // XXX cover all available modules
@@ -344,6 +350,83 @@ Engine::loadFont(int fontIndex,
 }
 
 
+// this function must be followed by `showFont'
+// to reload the font if necessary
+
+bool
+Engine::watchCurrentFont()
+{
+  int index = gui->currentFontIndex;
+
+  if (index < 0)
+    return false;
+
+  Font& font = gui->fonts[index];
+
+  if (currentFontFileInfo.exists()
+      && currentFontFileInfo.isReadable())
+  {
+    QDateTime modified = currentFontFileInfo.lastModified();
+    if (modified > currentFontDateTime)
+    {
+      // the font has changed on disk; check whether we can load it,
+      // otherwise we increase our retry counter
+      FT_Face face;
+      if (FT_New_Face(library,
+                      qPrintable(font.filePathname),
+                      -1,
+                      &face))
+        goto Retry;
+
+      FT_Done_Face(face);
+
+      // remove all entries
+      for (int i = 0; i < font.numInstancesList.size(); i++)
+        for (int j = 0; j < font.numInstancesList[i]; j++)
+        {
+          removeFont(index, i, j);
+          gui->faceIDHash.remove(FaceID(index, i, j));
+        }
+      font.numInstancesList.clear();
+
+      currentRetry = 0;
+
+      // current face and instance indices should be preserved
+      return true;
+    }
+  }
+  else
+  {
+  Retry:
+    if (currentRetry < maxRetries)
+    {
+      currentRetry++;
+      return false;
+    }
+
+    // font is no longer available, thus replace all entries...
+    for (int i = 0; i < font.numInstancesList.size(); i++)
+      for (int j = 0; j < font.numInstancesList[i]; j++)
+      {
+        removeFont(index, i, j);
+        gui->faceIDHash.remove(FaceID(index, i, j));
+      }
+    font.numInstancesList.clear();
+
+    // ...with an invalid one
+    font.numInstancesList.append(0);
+
+    // XXX move this to MainGUI::watchCurrentFont
+    gui->currentFaceIndex = -1;
+    gui->currentInstanceIndex = -1;
+
+    // XXX emit a warning message
+  }
+
+  return false;
+}
+
+
 void
 Engine::removeFont(int fontIndex,
                    int faceIndex,
@@ -514,6 +597,9 @@ Engine::update()
     scaler.x_res = dpi;
     scaler.y_res = dpi;
   }
+
+  // XXX make this configurable
+  maxRetries = 10;
 }
 
 
@@ -1067,6 +1153,11 @@ MainGUI::MainGUI()
 {
   engine = NULL;
 
+  // we are going to watch the current font file once in a second
+  timer = new QTimer();
+  timer->setInterval(1000);
+  timer->start();
+
   setGraphicsDefaults();
   createLayout();
   createConnections();
@@ -1196,7 +1287,7 @@ MainGUI::closeFont()
 
 
 void
-MainGUI::showFont()
+MainGUI::showFont(bool preserveIndices)
 {
   if (currentFontIndex >= 0)
   {
@@ -1216,8 +1307,11 @@ MainGUI::showFont()
         for (int i = 0; i < numFaces; i++)
           font.numInstancesList.append(-1);
 
-        currentFaceIndex = 0;
-        currentInstanceIndex = 0;
+        if (!preserveIndices)
+        {
+          currentFaceIndex = 0;
+          currentInstanceIndex = 0;
+        }
       }
       else
       {
@@ -1252,7 +1346,8 @@ MainGUI::showFont()
       // instance index 0 represents a face without an instance;
       // consequently, `n' instances are enumerated from 1 to `n'
       // (instead of having indices 0 to `n-1')
-      currentInstanceIndex = 0;
+      if (!preserveIndices)
+        currentInstanceIndex = 0;
     }
 
     if (currentFontIndex >= 0
@@ -1685,6 +1780,16 @@ MainGUI::zoom()
 
 
 void
+MainGUI::watchCurrentFont()
+{
+  if (engine->watchCurrentFont())
+    showFont(true);
+  else
+    showFont(false);
+}
+
+
+void
 MainGUI::setGraphicsDefaults()
 {
   // color tables (with suitable opacity values) for converting
@@ -1893,8 +1998,6 @@ MainGUI::createLayout()
   showPointNumbersCheckBox = new QCheckBox(tr("Show Point Numbers"));
   showOutlinesCheckBox = new QCheckBox(tr("Show Outlines"));
 
-  watchButton = new QPushButton(tr("Watch"));
-
   hintingModeLayout = new QHBoxLayout;
   hintingModeLayout->addWidget(hintingModeLabel);
   hintingModeLayout->addWidget(hintingModeComboBoxx);
@@ -1963,19 +2066,12 @@ MainGUI::createLayout()
 
   mmgxTabWidget = new QWidget;
 
-  watchLayout = new QHBoxLayout;
-  watchLayout->addStretch(1);
-  watchLayout->addWidget(watchButton);
-  watchLayout->addStretch(1);
-
   tabWidget = new QTabWidget;
   tabWidget->addTab(generalTabWidget, tr("General"));
   tabWidget->addTab(mmgxTabWidget, tr("MM/GX"));
 
   leftLayout = new QVBoxLayout;
   leftLayout->addWidget(tabWidget);
-  leftLayout->addSpacing(10); // XXX px
-  leftLayout->addLayout(watchLayout);
 
   // we don't want to expand the left side horizontally;
   // to change the policy we have to use a widget wrapper
@@ -2199,6 +2295,9 @@ MainGUI::createConnections()
   glyphNavigationMapper->setMapping(toP100Buttonx, 100);
   glyphNavigationMapper->setMapping(toP1000Buttonx, 1000);
   glyphNavigationMapper->setMapping(toEndButtonx, 0x10000);
+
+  connect(timer, SIGNAL(timeout()),
+          SLOT(watchCurrentFont()));
 }
 
 
diff --git a/src/ftinspect.h b/src/ftinspect.h
index 87e0371..7a453cd 100644
--- a/src/ftinspect.h
+++ b/src/ftinspect.h
@@ -23,10 +23,12 @@
 #include <QCloseEvent>
 #include <QColor>
 #include <QComboBox>
+#include <QDateTime>
 #include <QDesktopWidget>
 #include <QDir>
 #include <QDoubleSpinBox>
 #include <QFileDialog>
+#include <QFileInfo>
 #include <QGraphicsItem>
 #include <QGraphicsScene>
 #include <QGraphicsView>
@@ -51,6 +53,7 @@
 #include <QStandardItemModel>
 #include <QStatusBar>
 #include <QTabWidget>
+#include <QTimer>
 #include <QTransform>
 #include <QVariant>
 #include <QVector2D>
@@ -117,6 +120,7 @@ public:
   void setCFFHintingMode(int);
   void setTTInterpreterVersion(int);
   void update();
+  bool watchCurrentFont(); // returns `true' if we have to preserve indices
 
   friend class MainGUI;
 
@@ -131,6 +135,12 @@ public:
 private:
   MainGUI* gui;
 
+  QFileInfo currentFontFileInfo;
+  QDateTime currentFontDateTime;
+  int maxRetries; // how often we try to reload a font
+                  // if it suddenly disappears (because it is
+                  // in the process of being regenerated)
+  int currentRetry;
   FT_Library library;
   FTC_Manager cacheManager;
   FTC_ImageCache imageCache;
@@ -336,6 +346,7 @@ private slots:
   void previousFace();
   void previousFont();
   void previousInstance();
+  void watchCurrentFont();
   void zoom();
 
 private:
@@ -408,7 +419,6 @@ private:
   QHBoxLayout *sizeLayout;
   QHBoxLayout *verticalHintingLayout;
   QHBoxLayout *warpingLayout;
-  QHBoxLayout *watchLayout;
 
   QLabel *antiAliasingLabel;
   QLabel *dpiLabel;
@@ -439,7 +449,6 @@ private:
   QPushButton *previousFaceButton;
   QPushButton *previousFontButton;
   QPushButton *previousInstanceButton;
-  QPushButton *watchButton;
 
   QPushButtonx *toEndButtonx;
   QPushButtonx *toM1000Buttonx;
@@ -461,6 +470,8 @@ private:
 
   QTabWidget *tabWidget;
 
+  QTimer *timer;
+
   QVBoxLayout *generalTabLayout;
   QVBoxLayout *leftLayout;
   QVBoxLayout *rightLayout;
@@ -513,7 +524,7 @@ private:
   void createStatusBar();
   void readSettings();
   void setGraphicsDefaults();
-  void showFont();
+  void showFont(bool = false);
   void writeSettings();
 };
 



reply via email to

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