# HG changeset patch # User Alain Mazy # Date 1571757877 -7200 # Node ID 63539e8263557ef1220ee56efec22f46c790699c # Parent 8493f5fb6165927541de5f474f9725275bfd6591 Added TextRenderer diff -r 8493f5fb6165 -r 63539e826355 Applications/Samples/CMakeLists.txt --- a/Applications/Samples/CMakeLists.txt Mon Oct 21 20:52:17 2019 +0200 +++ b/Applications/Samples/CMakeLists.txt Tue Oct 22 17:24:37 2019 +0200 @@ -10,6 +10,15 @@ set(ENABLE_STONE_DEPRECATED ON) # Need deprecated classes for these samples +include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake) +DownloadPackage( + "a24b8136b8f3bb93f166baf97d9328de" + "http://orthanc.osimis.io/ThirdPartyDownloads/ubuntu-font-family-0.83.zip" + "${CMAKE_BINARY_DIR}/ubuntu-font-family-0.83") + +set(ORTHANC_STONE_APPLICATION_RESOURCES + UBUNTU_FONT ${CMAKE_BINARY_DIR}/ubuntu-font-family-0.83/Ubuntu-R.ttf + ) if (OPENSSL_NO_CAPIENG) add_definitions(-DOPENSSL_NO_CAPIENG=1) diff -r 8493f5fb6165 -r 63539e826355 Applications/Samples/SingleFrameEditorApplication.h --- a/Applications/Samples/SingleFrameEditorApplication.h Mon Oct 21 20:52:17 2019 +0200 +++ b/Applications/Samples/SingleFrameEditorApplication.h Tue Oct 22 17:24:37 2019 +0200 @@ -36,10 +36,14 @@ #include "../../Framework/Radiography/RadiographySceneReader.h" #include "../../Framework/Radiography/RadiographyMaskLayer.h" +#include <../../Framework/Toolbox/TextRenderer.h> + #include #include #include #include +#include +#include // Export using PAM is faster than using PNG, but requires Orthanc @@ -117,12 +121,15 @@ if (tool_ == Tool_Windowing) { return new RadiographyWindowingTracker( - undoRedoStack_, widget.GetScene(), widget, ImageInterpolation_Nearest, - viewportX, viewportY, - RadiographyWindowingTracker::Action_DecreaseWidth, - RadiographyWindowingTracker::Action_IncreaseWidth, - RadiographyWindowingTracker::Action_DecreaseCenter, - RadiographyWindowingTracker::Action_IncreaseCenter); + undoRedoStack_, + widget.GetScene(), + widget, + OrthancStone::ImageInterpolation_Nearest, + viewportX, viewportY, + RadiographyWindowingTracker::Action_DecreaseWidth, + RadiographyWindowingTracker::Action_IncreaseWidth, + RadiographyWindowingTracker::Action_DecreaseCenter, + RadiographyWindowingTracker::Action_IncreaseCenter); } else if (!widget.LookupSelectedLayer(selected)) { @@ -511,6 +518,13 @@ interactor_.SetMaskLayer(maskLayer_); { + std::auto_ptr renderedTextAlpha(TextRenderer::RenderWhiteOnBlack(Orthanc::EmbeddedResources::UBUNTU_FONT, 100, + "%öÇaA&#")); + RadiographyLayer& layer = scene_->LoadAlphaBitmap(renderedTextAlpha.release(), NULL); + dynamic_cast(layer).SetForegroundValue(200); + } + + { RadiographyLayer& layer = scene_->LoadText(fontRegistry_.GetFont(0), "Hello\nworld", NULL); layer.SetResizeable(true); } diff -r 8493f5fb6165 -r 63539e826355 Framework/Toolbox/TextRenderer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Toolbox/TextRenderer.cpp Tue Oct 22 17:24:37 2019 +0200 @@ -0,0 +1,120 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2019 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **/ + + +#include "TextRenderer.h" + +#include <../../Framework/Scene2D/CairoCompositor.h> +#include <../../Framework/Scene2D/ColorTextureSceneLayer.h> +#include <../../Framework/Scene2D/FloatTextureSceneLayer.h> +#include <../../Framework/Scene2D/TextSceneLayer.h> +#include <../../Framework/Fonts/GlyphBitmapAlphabet.h> +#include <../../Framework/Fonts/FontRenderer.h> +#include + +#include "Core/Images/Image.h" +#include "Core/Images/ImageProcessing.h" + +namespace OrthancStone +{ + Orthanc::ImageAccessor* TextRenderer::Render(Orthanc::EmbeddedResources::FileResourceId font, + unsigned int fontSize, + const std::string& utf8String + ) + { + FontRenderer renderer; + renderer.LoadFont(font, fontSize); + + // add each char to be rendered to the alphabet + std::auto_ptr alphabet(new GlyphBitmapAlphabet); + + size_t posInString = 0; + uint32_t unicode; + size_t utf8CharLength; + + while (posInString < utf8String.size()) + { + Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, utf8CharLength, utf8String, posInString); + alphabet->AddUnicodeCharacter(renderer, unicode); + posInString += utf8CharLength; + } + + return alphabet->RenderText(utf8String); + } + + + Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource, + unsigned int fontSize, + const std::string& utf8String, + uint8_t foreground) + { + std::auto_ptr renderedText8(RenderWhiteOnBlack(resource, fontSize, utf8String)); + std::auto_ptr target(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, renderedText8->GetWidth(), renderedText8->GetHeight(), true)); + + Orthanc::ImageProcessing::Set(*target, foreground, foreground, foreground, *renderedText8); + return target.release(); + } + + + // currently disabled because the background is actually not transparent once we use the Cairo Compositor ! + // + // // renders text in color + a border with alpha in a RGBA32 image + // Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource, + // unsigned int fontSize, + // const std::string& utf8String, + // uint8_t foreground, + // uint8_t borderColor) + // { + // std::auto_ptr renderedBorderAlpha(RenderWithAlpha(resource, fontSize, utf8String, borderColor)); + // std::auto_ptr renderedTextAlpha(RenderWithAlpha(resource, fontSize, utf8String, foreground)); + + // unsigned int textWidth = renderedBorderAlpha->GetWidth(); + // unsigned int textHeight = renderedBorderAlpha->GetHeight(); + + // Scene2D targetScene; + // std::auto_ptr borderLayerLeft(new ColorTextureSceneLayer(*renderedBorderAlpha)); + // std::auto_ptr borderLayerRight(new ColorTextureSceneLayer(*renderedBorderAlpha)); + // std::auto_ptr borderLayerTop(new ColorTextureSceneLayer(*renderedBorderAlpha)); + // std::auto_ptr borderLayerBottom(new ColorTextureSceneLayer(*renderedBorderAlpha)); + // std::auto_ptr textLayerCenter(new ColorTextureSceneLayer(*renderedTextAlpha)); + + // borderLayerLeft->SetOrigin(0, 1); + // borderLayerRight->SetOrigin(2, 1); + // borderLayerTop->SetOrigin(1, 0); + // borderLayerBottom->SetOrigin(1, 2); + // textLayerCenter->SetOrigin(1, 1); + // targetScene.SetLayer(1, borderLayerLeft.release()); + // targetScene.SetLayer(2, borderLayerRight.release()); + // targetScene.SetLayer(3, borderLayerTop.release()); + // targetScene.SetLayer(4, borderLayerBottom.release()); + // targetScene.SetLayer(5, textLayerCenter.release()); + + // targetScene.FitContent(textWidth + 2, textHeight + 2); + // CairoCompositor compositor(targetScene, textWidth + 2, textHeight + 2); + // compositor.Refresh(); + + // Orthanc::ImageAccessor canvas; + // compositor.GetCanvas().GetReadOnlyAccessor(canvas); + + // std::auto_ptr output(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, canvas.GetWidth(), canvas.GetHeight(), false)); + // Orthanc::ImageProcessing::Convert(*output, canvas); + // return output.release(); + // } +} diff -r 8493f5fb6165 -r 63539e826355 Framework/Toolbox/TextRenderer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Toolbox/TextRenderer.h Tue Oct 22 17:24:37 2019 +0200 @@ -0,0 +1,53 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2019 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **/ + + +#pragma once + +#include "Core/Images/Image.h" +#include + +namespace OrthancStone +{ + // Helpers methods to render text in bitmaps. + // Compared to the GlyphBitmapAlphabet::RenderText, these methods do not need a + // code page. + class TextRenderer : public boost::noncopyable + { + public: + // simply renders text in GrayScale8 with a black background and a white text + static Orthanc::ImageAccessor* Render(Orthanc::EmbeddedResources::FileResourceId resource, + unsigned int fontSize, + const std::string& utf8String); + + // renders text in "color" with alpha in a RGBA32 image + static Orthanc::ImageAccessor* RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource, + unsigned int fontSize, + const std::string& utf8String, + uint8_t foreground); + + // // renders text in color + a border with alpha in a RGBA32 image + // static Orthanc::ImageAccessor* RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource, + // unsigned int fontSize, + // const std::string& utf8String, + // uint8_t foreground, + // uint8_t borderColor); + }; +} diff -r 8493f5fb6165 -r 63539e826355 Resources/CMake/OrthancStoneConfiguration.cmake --- a/Resources/CMake/OrthancStoneConfiguration.cmake Mon Oct 21 20:52:17 2019 +0200 +++ b/Resources/CMake/OrthancStoneConfiguration.cmake Tue Oct 22 17:24:37 2019 +0200 @@ -569,6 +569,8 @@ ${ORTHANC_STONE_ROOT}/Framework/Toolbox/SlicesSorter.h ${ORTHANC_STONE_ROOT}/Framework/Toolbox/SubpixelReader.h ${ORTHANC_STONE_ROOT}/Framework/Toolbox/SubvoxelReader.h + ${ORTHANC_STONE_ROOT}/Framework/Toolbox/TextRenderer.cpp + ${ORTHANC_STONE_ROOT}/Framework/Toolbox/TextRenderer.h ${ORTHANC_STONE_ROOT}/Framework/Toolbox/UndoRedoStack.cpp ${ORTHANC_STONE_ROOT}/Framework/Toolbox/UndoRedoStack.h ${ORTHANC_STONE_ROOT}/Framework/Toolbox/GenericToolbox.cpp