changeset 1220:9ee6b28f53e8

RadiographyTextLayer: support multiple fonts
author Alain Mazy <alain@mazy.be>
date Sat, 07 Dec 2019 17:47:23 +0100
parents f8bff27f1314
children e2435a524029
files Framework/Radiography/RadiographyScene.cpp Framework/Radiography/RadiographyScene.h Framework/Radiography/RadiographySceneReader.cpp Framework/Radiography/RadiographySceneWriter.cpp Framework/Radiography/RadiographyTextLayer.cpp Framework/Radiography/RadiographyTextLayer.h Framework/Radiography/RadiographyWidget.cpp Framework/Radiography/RadiographyWidget.h
diffstat 8 files changed, 66 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Radiography/RadiographyScene.cpp	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographyScene.cpp	Sat Dec 07 17:47:23 2019 +0100
@@ -298,11 +298,12 @@
 
   RadiographyLayer& RadiographyScene::UpdateText(size_t layerIndex,
                                                  const std::string& utf8,
+                                                 const std::string& font,
                                                  unsigned int fontSize,
                                                  uint8_t foreground)
   {
     RadiographyTextLayer& textLayer = dynamic_cast<RadiographyTextLayer&>(GetLayer(layerIndex));
-    textLayer.SetText(utf8, fontSize, foreground);
+    textLayer.SetText(utf8, font, fontSize, foreground);
 
     BroadcastMessage(RadiographyScene::ContentChangedMessage(*this, textLayer));
     BroadcastMessage(RadiographyScene::LayerEditedMessage(*this, textLayer));
@@ -311,15 +312,27 @@
 
 
   RadiographyLayer& RadiographyScene::LoadText(const std::string& utf8,
+                                               const std::string& font,
                                                unsigned int fontSize,
                                                uint8_t foreground,
-                                               RadiographyLayer::Geometry* geometry)
+                                               RadiographyLayer::Geometry* centerGeometry,
+                                               bool isCenterGeometry)
   {
     std::auto_ptr<RadiographyTextLayer>  alpha(new RadiographyTextLayer(IObservable::GetBroker(), *this));
-    alpha->SetText(utf8, fontSize, foreground);
-    if (geometry != NULL)
+    alpha->SetText(utf8, font, fontSize, foreground);
+    if (centerGeometry != NULL)
     {
-      alpha->SetGeometry(*geometry);
+      if (isCenterGeometry)
+      {
+        // modify geometry to reference the top left corner
+        double tlx = centerGeometry->GetPanX();
+        double tly = centerGeometry->GetPanY();
+        Extent2D textExtent = alpha->GetExtent();
+        tlx = tlx - (textExtent.GetWidth() / 2) * centerGeometry->GetPixelSpacingX();
+        tly = tly - (textExtent.GetHeight() / 2) * centerGeometry->GetPixelSpacingY();
+        centerGeometry->SetPan(tlx, tly);
+      }
+      alpha->SetGeometry(*centerGeometry);
     }
 
     RadiographyLayer& registeredLayer = RegisterLayer(alpha.release());
--- a/Framework/Radiography/RadiographyScene.h	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographyScene.h	Sat Dec 07 17:47:23 2019 +0100
@@ -199,11 +199,14 @@
     RadiographyPhotometricDisplayMode GetPreferredPhotomotricDisplayMode() const;
 
     RadiographyLayer& LoadText(const std::string& utf8,
+                               const std::string& font,
                                unsigned int fontSize,
                                uint8_t foreground,
-                               RadiographyLayer::Geometry* geometry);
+                               RadiographyLayer::Geometry* geometry,
+                               bool isCenterGeometry);
 
     RadiographyLayer& UpdateText(size_t layerIndex,
+                                 const std::string& font,
                                  const std::string& utf8,
                                  unsigned int fontSize,
                                  uint8_t foreground);
--- a/Framework/Radiography/RadiographySceneReader.cpp	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographySceneReader.cpp	Sat Dec 07 17:47:23 2019 +0100
@@ -98,7 +98,7 @@
       else if (jsonLayer["type"].asString() == "text")
       {
         ReadLayerGeometry(geometry, jsonLayer);
-        scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["fontSize"].asUInt(), static_cast<uint8_t>(jsonLayer["foreground"].asUInt()), &geometry);
+        scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["font"].asString(), jsonLayer["fontSize"].asUInt(), static_cast<uint8_t>(jsonLayer["foreground"].asUInt()), &geometry, false);
       }
       else if (jsonLayer["type"].asString() == "alpha")
       {
@@ -175,7 +175,7 @@
       else if (jsonLayer["type"].asString() == "text")
       {
         ReadLayerGeometry(geometry, jsonLayer);
-        scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["fontSize"].asUInt(), static_cast<uint8_t>(jsonLayer["foreground"].asUInt()), &geometry);
+        scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["font"].asString(), jsonLayer["fontSize"].asUInt(), static_cast<uint8_t>(jsonLayer["foreground"].asUInt()), &geometry, false);
       }
       else if (jsonLayer["type"].asString() == "alpha")
       {
--- a/Framework/Radiography/RadiographySceneWriter.cpp	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographySceneWriter.cpp	Sat Dec 07 17:47:23 2019 +0100
@@ -62,8 +62,9 @@
   {
     output["type"] = "text";
     output["text"] = layer.GetText();
-    output["fontSize"] = static_cast<unsigned int>(layer.GetFontSize());
-    output["foreground"] = layer.GetForeground();
+    output["font"] = layer.GetFont();
+    output["fontSize"] = layer.GetFontSize();
+    output["foreground"] = layer.GetForegroundGreyLevel();
   }
 
   void RadiographySceneWriter::WriteLayer(Json::Value& output, const RadiographyMaskLayer& layer)
--- a/Framework/Radiography/RadiographyTextLayer.cpp	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographyTextLayer.cpp	Sat Dec 07 17:47:23 2019 +0100
@@ -26,27 +26,28 @@
 
 namespace OrthancStone
 {
-  bool RadiographyTextLayer::fontHasBeenConfigured_ = false;
-  Orthanc::EmbeddedResources::FileResourceId RadiographyTextLayer::fontResourceId_;
+  std::map<std::string, Orthanc::EmbeddedResources::FileResourceId> RadiographyTextLayer::fonts_;
 
   void RadiographyTextLayer::SetText(const std::string& utf8,
-                                      unsigned int fontSize,
-                                      uint8_t foreground)
+                                     const std::string& font,
+                                     unsigned int fontSize,
+                                     uint8_t foregroundGreyLevel)
   {
-    if (!fontHasBeenConfigured_)
+    if (fonts_.find(font) == fonts_.end())
     {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, "No font has been loaded");
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, "The font has not been registered");
     }
 
     text_ = utf8;
+    font_ = font;
     fontSize_ = fontSize;
-    foreground_ = foreground;
+    foregroundGreyLevel_ = foregroundGreyLevel;
 
-    SetAlpha(TextRenderer::Render(fontResourceId_,
+    SetAlpha(TextRenderer::Render(fonts_[font_],
                                   fontSize_,
                                   text_));
 
-    SetForegroundValue(foreground * 256.0f);
+    SetForegroundValue(foregroundGreyLevel * 256.0f);
   }
 
 }
--- a/Framework/Radiography/RadiographyTextLayer.h	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographyTextLayer.h	Sat Dec 07 17:47:23 2019 +0100
@@ -31,38 +31,42 @@
   {
   private:
     std::string                 text_;
+    std::string                 font_;
     unsigned int                fontSize_;
-    uint8_t                     foreground_;
+    uint8_t                     foregroundGreyLevel_;
 
-    static bool                                       fontHasBeenConfigured_;
-    static Orthanc::EmbeddedResources::FileResourceId fontResourceId_;
+    static std::map<std::string, Orthanc::EmbeddedResources::FileResourceId>  fonts_;
   public:
     RadiographyTextLayer(MessageBroker& broker, const RadiographyScene& scene) :
       RadiographyAlphaLayer(broker, scene)
     {
     }
 
-    void SetText(const std::string& utf8, unsigned int fontSize, uint8_t foreground);
+    void SetText(const std::string& utf8, const std::string& font, unsigned int fontSize, uint8_t foregroundGreyLevel);
 
     const std::string& GetText() const
     {
       return text_;
     }
 
+    const std::string& GetFont() const
+    {
+      return font_;
+    }
+
     unsigned int GetFontSize() const
     {
       return fontSize_;
     }
 
-    uint8_t GetForeground() const
+    uint8_t GetForegroundGreyLevel() const
     {
-      return foreground_;
+      return foregroundGreyLevel_;
     }
 
-    static void SetFont(Orthanc::EmbeddedResources::FileResourceId fontResourceId)
+    static void RegisterFont(const std::string& name, Orthanc::EmbeddedResources::FileResourceId fontResourceId)
     {
-      fontResourceId_ = fontResourceId;
-      fontHasBeenConfigured_ = true;
+      fonts_[name] = fontResourceId;
     }
   };
 }
--- a/Framework/Radiography/RadiographyWidget.cpp	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographyWidget.cpp	Sat Dec 07 17:47:23 2019 +0100
@@ -169,6 +169,7 @@
                                        const std::string& name) :
     WorldSceneWidget(name),
     IObserver(broker),
+    IObservable(broker),
     invert_(false),
     interpolation_(ImageInterpolation_Nearest),
     hasSelection_(false),
@@ -182,6 +183,15 @@
   {
     hasSelection_ = true;
     selectedLayer_ = layer;
+
+    NotifyContentChanged();
+    BroadcastMessage(SelectionChangedMessage(*this));
+  }
+
+  void RadiographyWidget::Unselect()
+  {
+    hasSelection_ = false;
+    BroadcastMessage(SelectionChangedMessage(*this));
   }
 
   bool RadiographyWidget::LookupSelectedLayer(size_t& layer)
--- a/Framework/Radiography/RadiographyWidget.h	Sat Dec 07 17:46:25 2019 +0100
+++ b/Framework/Radiography/RadiographyWidget.h	Sat Dec 07 17:47:23 2019 +0100
@@ -31,8 +31,12 @@
 
   class RadiographyWidget :
     public Deprecated::WorldSceneWidget,
-    public IObserver
+    public IObserver,
+    public IObservable
   {
+  public:
+    ORTHANC_STONE_DEFINE_ORIGIN_MESSAGE(__FILE__, __LINE__, SelectionChangedMessage, RadiographyWidget);
+
   private:
     boost::shared_ptr<RadiographyScene>    scene_;
     std::auto_ptr<Orthanc::ImageAccessor>  floatBuffer_;
@@ -73,10 +77,7 @@
 
     void Select(size_t layer);
 
-    void Unselect()
-    {
-      hasSelection_ = false;
-    }
+    void Unselect();
 
     template<typename LayerType> bool SelectLayerByType(size_t index = 0);