changeset 1190:f417a0ae282b

wip: TextLayer with new fonts
author Alain Mazy <alain@mazy.be>
date Tue, 26 Nov 2019 15:27:48 +0100
parents 848dcba80d81
children c6a36ecd641d 91d86144fb79
files Applications/Samples/CMakeLists.txt Applications/Samples/build-wasm.sh Applications/Samples/build-web.sh Framework/Radiography/RadiographyScene.cpp Framework/Radiography/RadiographyScene.h Framework/Radiography/RadiographySceneReader.cpp Framework/Radiography/RadiographySceneReader.h Framework/Radiography/RadiographySceneWriter.cpp Framework/Radiography/RadiographyTextLayer.cpp Framework/Radiography/RadiographyTextLayer.h
diffstat 10 files changed, 76 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/Samples/CMakeLists.txt	Fri Nov 22 09:51:22 2019 +0100
+++ b/Applications/Samples/CMakeLists.txt	Tue Nov 26 15:27:48 2019 +0100
@@ -9,6 +9,7 @@
 include(../../Resources/CMake/OrthancStoneParameters.cmake)
 
 set(ENABLE_STONE_DEPRECATED ON)  # Need deprecated classes for these samples
+set(EMSCRIPTEN_SET_LLVM_WASM_BACKEND ON)
 
 include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake)
 DownloadPackage(
@@ -53,7 +54,7 @@
   set(WASM_FLAGS "${WASM_FLAGS} -s FILESYSTEM=1") # if we don't include it, gen_uuid.c fails to build because srand, getpid(), ... are not defined
   set(WASM_FLAGS "${WASM_FLAGS} -s DISABLE_EXCEPTION_CATCHING=0") # actually enable exception catching 
   set(WASM_FLAGS "${WASM_FLAGS} -s ERROR_ON_MISSING_LIBRARIES=1")
-
+  
   if (CMAKE_BUILD_TYPE MATCHES DEBUG)
     set(WASM_FLAGS "${WASM_FLAGS} -g4") # generate debug information
     set(WASM_FLAGS "${WASM_FLAGS} -s ASSERTIONS=2") # more runtime checks
@@ -238,6 +239,8 @@
       ${SIMPLE_VIEWER_APPLICATION_SOURCES}
       )
     target_link_libraries(OrthancStoneSimpleViewer OrthancStone)
+
+    BuildSingleFileSample(OrthancStoneSingleFrameEditor SingleFrameEditorApplication.h 9)
 endif()
 
 #####################################################################
--- a/Applications/Samples/build-wasm.sh	Fri Nov 22 09:51:22 2019 +0100
+++ b/Applications/Samples/build-wasm.sh	Tue Nov 26 15:27:48 2019 +0100
@@ -19,7 +19,7 @@
 cd $samplesRootDir/build-wasm
 
 source ~/apps/emsdk/emsdk_env.sh
-cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN}/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_BUILD_TYPE=$buildType -DSTONE_SOURCES_DIR=$currentDir/../../../orthanc-stone -DORTHANC_FRAMEWORK_SOURCE=path -DORTHANC_FRAMEWORK_ROOT=$currentDir/../../../orthanc -DALLOW_DOWNLOADS=ON .. -DENABLE_WASM=ON
+cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=~/apps/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_BUILD_TYPE=$buildType -DSTONE_SOURCES_DIR=$currentDir/../../../orthanc-stone -DORTHANC_FRAMEWORK_SOURCE=path -DORTHANC_FRAMEWORK_ROOT=$currentDir/../../../orthanc -DALLOW_DOWNLOADS=ON .. -DENABLE_WASM=ON
 ninja $target
 
 echo "-- building the web application -- "
--- a/Applications/Samples/build-web.sh	Fri Nov 22 09:51:22 2019 +0100
+++ b/Applications/Samples/build-web.sh	Tue Nov 26 15:27:48 2019 +0100
@@ -19,29 +19,29 @@
 cp "$samplesRootDir/Web/index.html" "$outputDir"
 cp "$samplesRootDir/Web/samples-styles.css" "$outputDir"
 
-# build simple-viewer-single-file (obsolete project)
-if [[ $target == "all" || $target == "OrthancStoneSimpleViewerSingleFile" ]]; then
-  cp $samplesRootDir/Web/simple-viewer-single-file.html $outputDir
-  tsc --project $samplesRootDir/Web/simple-viewer-single-file.tsconfig.json --outDir "$outputDir"
-  browserify \
-      "$outputDir/Platforms/Wasm/wasm-application-runner.js" \
-      "$outputDir/Applications/Samples/Web/simple-viewer-single-file.js" \
-      -o "$outputDir/app-simple-viewer-single-file.js"
-  cp "$currentDir/build-wasm/OrthancStoneSimpleViewerSingleFile.js"  $outputDir
-  cp "$currentDir/build-wasm/OrthancStoneSimpleViewerSingleFile.wasm"  $outputDir
-fi
+# # build simple-viewer-single-file (obsolete project)
+# if [[ $target == "all" || $target == "OrthancStoneSimpleViewerSingleFile" ]]; then
+#   cp $samplesRootDir/Web/simple-viewer-single-file.html $outputDir
+#   tsc --project $samplesRootDir/Web/simple-viewer-single-file.tsconfig.json --outDir "$outputDir"
+#   browserify \
+#       "$outputDir/Platforms/Wasm/wasm-application-runner.js" \
+#       "$outputDir/Applications/Samples/Web/simple-viewer-single-file.js" \
+#       -o "$outputDir/app-simple-viewer-single-file.js"
+#   cp "$currentDir/build-wasm/OrthancStoneSimpleViewerSingleFile.js"  $outputDir
+#   cp "$currentDir/build-wasm/OrthancStoneSimpleViewerSingleFile.wasm"  $outputDir
+# fi
 
-# build single-frame
-if [[ $target == "all" || $target == "OrthancStoneSingleFrame" ]]; then
-  cp $samplesRootDir/Web/single-frame.html $outputDir
-  tsc --project $samplesRootDir/Web/single-frame.tsconfig.json --outDir "$outputDir"
-  browserify \
-      "$outputDir/Platforms/Wasm/wasm-application-runner.js" \
-      "$outputDir/Applications/Samples/Web/single-frame.js" \
-      -o "$outputDir/app-single-frame.js"
-  cp "$currentDir/build-wasm/OrthancStoneSingleFrame.js"  $outputDir
-  cp "$currentDir/build-wasm/OrthancStoneSingleFrame.wasm"  $outputDir
-fi
+# # build single-frame
+# if [[ $target == "all" || $target == "OrthancStoneSingleFrame" ]]; then
+#   cp $samplesRootDir/Web/single-frame.html $outputDir
+#   tsc --project $samplesRootDir/Web/single-frame.tsconfig.json --outDir "$outputDir"
+#   browserify \
+#       "$outputDir/Platforms/Wasm/wasm-application-runner.js" \
+#       "$outputDir/Applications/Samples/Web/single-frame.js" \
+#       -o "$outputDir/app-single-frame.js"
+#   cp "$currentDir/build-wasm/OrthancStoneSingleFrame.js"  $outputDir
+#   cp "$currentDir/build-wasm/OrthancStoneSingleFrame.wasm"  $outputDir
+# fi
 
 # build single-frame-editor
 if [[ $target == "all" || $target == "OrthancStoneSingleFrameEditor" ]]; then
--- a/Framework/Radiography/RadiographyScene.cpp	Fri Nov 22 09:51:22 2019 +0100
+++ b/Framework/Radiography/RadiographyScene.cpp	Tue Nov 26 15:27:48 2019 +0100
@@ -282,12 +282,13 @@
   }
 
 
-  RadiographyLayer& RadiographyScene::LoadText(const Orthanc::Font& font,
-                                               const std::string& utf8,
+  RadiographyLayer& RadiographyScene::LoadText(const std::string& utf8,
+                                               size_t fontSize,
+                                               uint8_t foreground,
                                                RadiographyLayer::Geometry* geometry)
   {
     std::auto_ptr<RadiographyTextLayer>  alpha(new RadiographyTextLayer(IObservable::GetBroker(), *this));
-    alpha->LoadText(font, utf8);
+    alpha->LoadText(utf8, fontSize, foreground);
     if (geometry != NULL)
     {
       alpha->SetGeometry(*geometry);
--- a/Framework/Radiography/RadiographyScene.h	Fri Nov 22 09:51:22 2019 +0100
+++ b/Framework/Radiography/RadiographyScene.h	Tue Nov 26 15:27:48 2019 +0100
@@ -197,8 +197,9 @@
 
     RadiographyPhotometricDisplayMode GetPreferredPhotomotricDisplayMode() const;
 
-    RadiographyLayer& LoadText(const Orthanc::Font& font,
-                               const std::string& utf8,
+    RadiographyLayer& LoadText(const std::string& utf8,
+                               size_t fontSize,
+                               uint8_t foreground,
                                RadiographyLayer::Geometry* geometry);
     
     RadiographyLayer& LoadTestBlock(unsigned int width,
--- a/Framework/Radiography/RadiographySceneReader.cpp	Fri Nov 22 09:51:22 2019 +0100
+++ b/Framework/Radiography/RadiographySceneReader.cpp	Tue Nov 26 15:27:48 2019 +0100
@@ -97,18 +97,8 @@
       }
       else if (jsonLayer["type"].asString() == "text")
       {
-        if (fontRegistry_ == NULL || fontRegistry_->GetSize() == 0)
-        {
-          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); // you must provide a FontRegistry if you need to re-create text layers.
-        }
-
         ReadLayerGeometry(geometry, jsonLayer);
-        const Orthanc::Font* font = fontRegistry_->FindFont(jsonLayer["fontName"].asString());
-        if (font == NULL) // if not found, take the first font in the registry
-        {
-          font = &(fontRegistry_->GetFont(0));
-        }
-        scene_.LoadText(*font, jsonLayer["text"].asString(), &geometry);
+        scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["fontSize"].asUInt(), static_cast<uint8_t>(jsonLayer["foreground"].asUInt()), &geometry);
       }
       else if (jsonLayer["type"].asString() == "alpha")
       {
@@ -184,18 +174,8 @@
       }
       else if (jsonLayer["type"].asString() == "text")
       {
-        if (fontRegistry_ == NULL || fontRegistry_->GetSize() == 0)
-        {
-          throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); // you must provide a FontRegistry if you need to re-create text layers.
-        }
-
         ReadLayerGeometry(geometry, jsonLayer);
-        const Orthanc::Font* font = fontRegistry_->FindFont(jsonLayer["fontName"].asString());
-        if (font == NULL) // if not found, take the first font in the registry
-        {
-          font = &(fontRegistry_->GetFont(0));
-        }
-        scene_.LoadText(*font, jsonLayer["text"].asString(), &geometry);
+        scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["fontSize"].asUInt(), static_cast<uint8_t>(jsonLayer["foreground"].asUInt()), &geometry);
       }
       else if (jsonLayer["type"].asString() == "alpha")
       {
--- a/Framework/Radiography/RadiographySceneReader.h	Fri Nov 22 09:51:22 2019 +0100
+++ b/Framework/Radiography/RadiographySceneReader.h	Tue Nov 26 15:27:48 2019 +0100
@@ -40,15 +40,13 @@
   {
   protected:
     RadiographyScene&                               scene_;
-    const Orthanc::FontRegistry*                    fontRegistry_;
     std::auto_ptr<Orthanc::ImageAccessor>           dicomImage_;
     std::auto_ptr<Deprecated::DicomFrameConverter>  dicomFrameConverter_;
     RadiographyPhotometricDisplayMode               preferredPhotometricDisplayMode_;
 
   public:
     RadiographySceneBuilder(RadiographyScene& scene) :
-      scene_(scene),
-      fontRegistry_(NULL)
+      scene_(scene)
     {
     }
 
@@ -59,11 +57,6 @@
               RadiographyPhotometricDisplayMode preferredPhotometricDisplayMode
               );
 
-    void SetFontRegistry(const Orthanc::FontRegistry& fontRegistry)
-    {
-      fontRegistry_ = &fontRegistry;
-    }
-
     static void ReadLayerGeometry(RadiographyLayer::Geometry& geometry, const Json::Value& input);
     static void ReadDicomLayerGeometry(RadiographyLayer::Geometry& geometry, const Json::Value& input);
 
--- a/Framework/Radiography/RadiographySceneWriter.cpp	Fri Nov 22 09:51:22 2019 +0100
+++ b/Framework/Radiography/RadiographySceneWriter.cpp	Tue Nov 26 15:27:48 2019 +0100
@@ -62,7 +62,8 @@
   {
     output["type"] = "text";
     output["text"] = layer.GetText();
-    output["fontName"] = layer.GetFontName();
+    output["fontSize"] = layer.GetFontSize();
+    output["foreground"] = layer.GetForeground();
   }
 
   void RadiographySceneWriter::WriteLayer(Json::Value& output, const RadiographyMaskLayer& layer)
--- a/Framework/Radiography/RadiographyTextLayer.cpp	Fri Nov 22 09:51:22 2019 +0100
+++ b/Framework/Radiography/RadiographyTextLayer.cpp	Tue Nov 26 15:27:48 2019 +0100
@@ -18,19 +18,32 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  **/
 
-
 #include "RadiographyTextLayer.h"
 
+#include "Core/OrthancException.h"
 #include "RadiographyScene.h"
+#include "Framework/Toolbox/TextRenderer.h"
 
 namespace OrthancStone
 {
-  void RadiographyTextLayer::LoadText(const Orthanc::Font& font,
-                                      const std::string& utf8)
+  bool RadiographyTextLayer::fontHasBeenConfigured_ = false;
+  Orthanc::EmbeddedResources::FileResourceId RadiographyTextLayer::fontResourceId_;
+
+  void RadiographyTextLayer::LoadText(const std::string& utf8,
+                                      size_t fontSize,
+                                      uint8_t foreground)
   {
+    if (!fontHasBeenConfigured_)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, "No font has been loaded");
+    }
+
     text_ = utf8;
-    fontName_ = font.GetName();
+    fontSize_ = fontSize;
+    foreground_ = foreground;
 
-    SetAlpha(font.RenderAlpha(utf8));
+    SetAlpha(TextRenderer::Render(fontResourceId_,
+                                  fontSize_,
+                                  text_));
   }
 }
--- a/Framework/Radiography/RadiographyTextLayer.h	Fri Nov 22 09:51:22 2019 +0100
+++ b/Framework/Radiography/RadiographyTextLayer.h	Tue Nov 26 15:27:48 2019 +0100
@@ -30,26 +30,39 @@
   class RadiographyTextLayer : public RadiographyAlphaLayer
   {
   private:
-    std::string                text_;
-    std::string                fontName_;
+    std::string                 text_;
+    size_t                      fontSize_;
+    uint8_t                     foreground_;
 
+    static bool                                       fontHasBeenConfigured_;
+    static Orthanc::EmbeddedResources::FileResourceId fontResourceId_;
   public:
     RadiographyTextLayer(MessageBroker& broker, const RadiographyScene& scene) :
       RadiographyAlphaLayer(broker, scene)
     {
     }
 
-    void LoadText(const Orthanc::Font& font,
-                  const std::string& utf8);
+    void LoadText(const std::string& utf8, size_t fontSize, uint8_t foreground);
 
     const std::string& GetText() const
     {
       return text_;
     }
 
-    const std::string& GetFontName() const
+    const size_t& GetFontSize() const
+    {
+      return fontSize_;
+    }
+
+    const size_t& GetForeground() const
     {
-      return fontName_;
+      return foreground_;
+    }
+
+    static void SetFont(Orthanc::EmbeddedResources::FileResourceId fontResourceId)
+    {
+      fontResourceId_ = fontResourceId;
+      fontHasBeenConfigured_ = true;
     }
   };
 }