# HG changeset patch # User Alain Mazy # Date 1575737243 -3600 # Node ID 9ee6b28f53e804dd0227d2234214999ce5b2e994 # Parent f8bff27f1314ddb31180a8a2b5e79b25ccf1c061 RadiographyTextLayer: support multiple fonts diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographyScene.cpp --- 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(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 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()); diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographyScene.h --- 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); diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographySceneReader.cpp --- 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(jsonLayer["foreground"].asUInt()), &geometry); + scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["font"].asString(), jsonLayer["fontSize"].asUInt(), static_cast(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(jsonLayer["foreground"].asUInt()), &geometry); + scene_.LoadText(jsonLayer["text"].asString(), jsonLayer["font"].asString(), jsonLayer["fontSize"].asUInt(), static_cast(jsonLayer["foreground"].asUInt()), &geometry, false); } else if (jsonLayer["type"].asString() == "alpha") { diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographySceneWriter.cpp --- 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(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) diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographyTextLayer.cpp --- 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 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); } } diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographyTextLayer.h --- 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 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; } }; } diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographyWidget.cpp --- 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) diff -r f8bff27f1314 -r 9ee6b28f53e8 Framework/Radiography/RadiographyWidget.h --- 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 scene_; std::auto_ptr floatBuffer_; @@ -73,10 +77,7 @@ void Select(size_t layer); - void Unselect() - { - hasSelection_ = false; - } + void Unselect(); template bool SelectLayerByType(size_t index = 0);