]> git.sesse.net Git - casparcg/blobdiff - core/producer/text/text_producer.cpp
[text_producer] Removed unused member function measure_string()
[casparcg] / core / producer / text / text_producer.cpp
index ce9dcd16c2c41b26beb7a6ef966d365d9337463f..dc7e0e0774ea8b479b9602e02231f0b5b2768643 100644 (file)
 #include <core/frame/draw_frame.h>
 #include <core/frame/frame_factory.h>
 #include <core/frame/pixel_format.h>
+#include <core/frame/audio_channel_layout.h>
 #include <core/monitor/monitor.h>
 #include <core/consumer/frame_consumer.h>
 #include <core/module_dependencies.h>
+#include <core/help/help_repository.h>
+#include <core/help/help_sink.h>
 
 #include <modules/image/consumer/image_consumer.h>
 
@@ -54,6 +57,7 @@
 
 #include "utils/texture_atlas.h"
 #include "utils/texture_font.h"
+#include "utils/freetype_library.h"
 
 class font_comparer {
        const std::wstring& lhs;
@@ -63,89 +67,91 @@ public:
 };
 
 
-namespace caspar { namespace core {
-       namespace text {
+namespace caspar { namespace core { namespace text {
 
-               using namespace boost::filesystem;
+using namespace boost::filesystem;
 
-               std::map<std::wstring, std::wstring> fonts;
+std::map<std::wstring, std::wstring> enumerate_fonts()
+{
+       std::map<std::wstring, std::wstring> result;
 
-               std::map<std::wstring, std::wstring> enumerate_fonts()
+       for(auto iter = directory_iterator(env::font_folder()), end = directory_iterator(); iter != end; ++iter)
+       {
+               try
                {
-                       std::map<std::wstring, std::wstring> result;
-
-                       FT_Library lib;
-                       FT_Error err = FT_Init_FreeType(&lib);
-                       if(err) 
-                               return result;
-
-                       auto fonts = directory_iterator(env::font_folder());
-                       auto end = directory_iterator();
-                       for(; fonts != end; ++fonts)
+                       auto file = (*iter);
+                       if (is_regular_file(file.path()))
                        {
-                               auto file = (*fonts);
-                               if(is_regular_file(file.path()))
+                               auto face = get_new_face(u8(file.path().native()));
+                               const char* fontname = FT_Get_Postscript_Name(face.get());      //this doesn't work for .fon fonts. Ignoring those for now
+                               if (fontname != nullptr)
                                {
-                                       FT_Face face;
-                                       err = FT_New_Face(lib, u8(file.path().native()).c_str(), 0, &face);
-                                       if(err) 
-                                               continue;
-
-                                       const char* fontname = FT_Get_Postscript_Name(face);    //this doesn't work for .fon fonts. Ignoring those for now
-                                       if(fontname != nullptr)
-                                       {
-                                               std::string fontname_str(fontname);
-                                               result.insert(std::make_pair(u16(fontname_str), u16(file.path().native())));
-                                       }
-
-                                       FT_Done_Face(face);
+                                       std::string fontname_str(fontname);
+                                       result.insert(std::make_pair(u16(fontname_str), u16(file.path().native())));
                                }
                        }
+               }
+               catch(...) { }
+       }
 
-                       FT_Done_FreeType(lib);
+       return result;
+}
 
-                       return result;
-               }
+std::vector<std::pair<std::wstring, std::wstring>> list_fonts()
+{
+       auto fonts = enumerate_fonts();
+       return std::vector<std::pair<std::wstring, std::wstring>>(fonts.begin(), fonts.end());
+}
 
-               void init(module_dependencies dependencies)
-               {
-                       fonts = enumerate_fonts();
-                       dependencies.producer_registry->register_producer_factory(create_text_producer);
-               }
+void describe_text_producer(help_sink&, const help_repository&);
+spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies&, const std::vector<std::wstring>&);
+
+void init(module_dependencies dependencies)
+{
+       dependencies.producer_registry->register_producer_factory(L"Text Producer", create_text_producer, describe_text_producer);
+}
+
+text_info& find_font_file(text_info& info)
+{
+       auto& font_name = info.font;
+       auto fonts = enumerate_fonts();
+       auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
+       info.font_file = (it != fonts.end()) ? (*it).second : L"";
+       return info;
+}
+
+} // namespace text
 
-               text_info& find_font_file(text_info& info)
-               {
-                       auto& font_name = info.font;
-                       auto it = std::find_if(fonts.begin(), fonts.end(), font_comparer(font_name));
-                       info.font_file = (it != fonts.end()) ? (*it).second : L"";
-                       return info;
-               }
-       }
-       
 
 struct text_producer::impl
 {
-       monitor::subject monitor_subject_;
-       spl::shared_ptr<core::frame_factory> frame_factory_;
-       constraints constraints_;
-       int x_, y_, parent_width_, parent_height_;
-       bool standalone_;
-       variable_impl<std::wstring> text_;
-       std::shared_ptr<void> text_subscription_;
-       variable_impl<double> tracking_;
-       std::shared_ptr<void> tracking_subscription_;
-       variable_impl<double> current_bearing_y_;
-       variable_impl<double> current_protrude_under_y_;
-       draw_frame frame_;
-       text::texture_atlas atlas_                                                              { 512, 512, 4 };
-       text::texture_font font_;
-       bool dirty_                                                                                             = false;
+       monitor::subject                                                monitor_subject_;
+       spl::shared_ptr<core::frame_factory>    frame_factory_;
+       int                                                                             x_;
+       int                                                                             y_;
+       int                                                                             parent_width_;
+       int                                                                             parent_height_;
+       bool                                                                    standalone_;
+       constraints                                                             constraints_                            { parent_width_, parent_height_ };
+       variable_impl<std::wstring>                             text_;
+       std::shared_ptr<void>                                   text_subscription_;
+       variable_impl<double>                                   tracking_;
+       variable_impl<double>                                   scale_x_;
+       variable_impl<double>                                   scale_y_;
+       variable_impl<double>                                   shear_;
+       std::shared_ptr<void>                                   tracking_subscription_;
+       variable_impl<double>                                   current_bearing_y_;
+       variable_impl<double>                                   current_protrude_under_y_;
+       draw_frame                                                              frame_;
+       text::texture_atlas                                             atlas_                                          { 1024, 512, 4 };
+       text::texture_font                                              font_;
+       const_frame                                                             atlas_frame_;
 
 public:
-       explicit impl(const spl::shared_ptr<frame_factory>& frame_factory, int x, int y, const std::wstring& str, text::text_info& text_info, long parent_width, long parent_height, bool standalone) 
+       explicit impl(const spl::shared_ptr<frame_factory>& frame_factory, int x, int y, const std::wstring& str, text::text_info& text_info, long parent_width, long parent_height, bool standalone)
                : frame_factory_(frame_factory)
-               , constraints_(parent_width, parent_height)
-               , x_(x), y_(y), parent_width_(parent_width), parent_height_(parent_height)
+               , x_(x), y_(y)
+               , parent_width_(parent_width), parent_height_(parent_height)
                , standalone_(standalone)
                , font_(atlas_, text::find_font_file(text_info), !standalone)
        {
@@ -154,14 +160,19 @@ public:
                font_.load_glyphs(text::unicode_block::Latin_1_Supplement, text_info.color);
                font_.load_glyphs(text::unicode_block::Latin_Extended_A, text_info.color);
 
+               atlas_frame_ = create_atlas_frame();
+
                tracking_.value().set(text_info.tracking);
+               scale_x_.value().set(text_info.scale_x);
+               scale_y_.value().set(text_info.scale_y);
+               shear_.value().set(text_info.shear);
                text_subscription_ = text_.value().on_change([this]()
                {
-                       dirty_ = true;
+                       generate_frame();
                });
                tracking_subscription_ = tracking_.value().on_change([this]()
                {
-                       dirty_ = true;
+                       generate_frame();
                });
 
                constraints_.height.depend_on(text());
@@ -175,40 +186,34 @@ public:
                CASPAR_LOG(info) << print() << L" Initialized";
        }
 
-       void generate_frame()
+       core::const_frame create_atlas_frame() const
        {
                core::pixel_format_desc pfd(core::pixel_format::bgra);
                pfd.planes.push_back(core::pixel_format_desc::plane(static_cast<int>(atlas_.width()), static_cast<int>(atlas_.height()), static_cast<int>(atlas_.depth())));
+               auto frame = frame_factory_->create_frame(this, pfd, core::audio_channel_layout::invalid());
+               memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
+               return std::move(frame);
+       }
 
+       void generate_frame()
+       {
                text::string_metrics metrics;
-               font_.set_tracking(static_cast<int>(tracking_.value().get()));
-               auto vertex_stream = font_.create_vertex_stream(text_.value().get(), x_, y_, parent_width_, parent_height_, &metrics);
-               auto frame = frame_factory_->create_frame(vertex_stream.data(), pfd);
-               memcpy(frame.image_data().data(), atlas_.data(), frame.image_data().size());
-               frame.set_geometry(frame_geometry(frame_geometry::geometry_type::quad_list, std::move(vertex_stream)));
+               font_.set_tracking(tracking_.value().get());
+
+               auto vertex_stream = font_.create_vertex_stream(text_.value().get(), x_, y_, parent_width_, parent_height_, &metrics, shear_.value().get());
+               auto frame = atlas_frame_.with_geometry(frame_geometry(frame_geometry::geometry_type::quad_list, std::move(vertex_stream)));
 
-               this->constraints_.width.set(metrics.width);
-               this->constraints_.height.set(metrics.height);
+               this->constraints_.width.set(metrics.width * this->scale_x_.value().get());
+               this->constraints_.height.set(metrics.height * this->scale_y_.value().get());
                current_bearing_y_.value().set(metrics.bearingY);
                current_protrude_under_y_.value().set(metrics.protrudeUnderY);
                frame_ = core::draw_frame(std::move(frame));
        }
 
-       text::string_metrics measure_string(const std::wstring& str)
-       {
-               return font_.measure_string(str);
-       }
-
        // frame_producer
-                       
+
        draw_frame receive_impl()
        {
-               if (dirty_)
-               {
-                       generate_frame();
-                       dirty_ = false;
-               }
-
                return frame_;
        }
 
@@ -231,7 +236,7 @@ public:
                else if (name == L"tracking")
                        return tracking_;
 
-               CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(L"text_producer does not have a variable called " + name));
+               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"text_producer does not have a variable called " + name));
        }
 
        const std::vector<std::wstring>& get_variables() const
@@ -270,7 +275,7 @@ public:
        {
                return current_protrude_under_y_.value();
        }
-       
+
        std::wstring print() const
        {
                return L"text[" + text_.value().get() + L"]";
@@ -280,12 +285,14 @@ public:
        {
                return L"text";
        }
-       
+
        boost::property_tree::wptree info() const
        {
                boost::property_tree::wptree info;
                info.add(L"type", L"text");
                info.add(L"text", text_.value().get());
+               info.add(L"font", font_.get_name());
+               info.add(L"size", font_.get_size());
                return info;
        }
 };
@@ -298,7 +305,6 @@ draw_frame text_producer::receive_impl() { return impl_->receive_impl(); }
 std::future<std::wstring> text_producer::call(const std::vector<std::wstring>& param) { return impl_->call(param); }
 variable& text_producer::get_variable(const std::wstring& name) { return impl_->get_variable(name); }
 const std::vector<std::wstring>& text_producer::get_variables() const { return impl_->get_variables(); }
-text::string_metrics text_producer::measure_string(const std::wstring& str) { return impl_->measure_string(str); }
 
 constraints& text_producer::pixel_constraints() { return impl_->pixel_constraints(); }
 std::wstring text_producer::print() const { return impl_->print(); }
@@ -314,6 +320,28 @@ spl::shared_ptr<text_producer> text_producer::create(const spl::shared_ptr<frame
 {
        return spl::make_shared<text_producer>(frame_factory, x, y, str, text_info, parent_width, parent_height, standalone);
 }
+namespace text {
+
+void describe_text_producer(help_sink& sink, const help_repository& repo)
+{
+       sink.short_description(L"A producer for rendering dynamic text.");
+       sink.syntax(L"[TEXT] [text:string] {[x:int] [y:int]} {FONT [font:string]|verdana} {SIZE [size:float]|30.0} {COLOR [color:string]|#ffffffff} {STANDALONE [standalone:0,1]|0}");
+       sink.para()
+               ->text(L"Renders dynamic text using fonts found under the ")->code(L"fonts")->text(L" folder. ")
+               ->text(L"Parameters:");
+       sink.definitions()
+               ->item(L"text", L"The text to display. Can be changed later via CALL as well.")
+               ->item(L"x", L"The x position of the text.")
+               ->item(L"y", L"The y position of the text.")
+               ->item(L"font", L"The name of the font (not the actual filename, but the font name).")
+               ->item(L"size", L"The point size.")
+               ->item(L"color", L"The color as an ARGB hex value.")
+               ->item(L"standalone", L"Whether to normalize coordinates or not.");
+       sink.para()->text(L"Examples:");
+       sink.example(L">> PLAY 1-10 [TEXT] \"John Doe\" 0 0 FONT ArialMT SIZE 30 COLOR #1b698d STANDALONE 1");
+       sink.example(L">> CALL 1-10 \"Jane Doe\"", L"for modifying the text while playing.");
+       sink.para()->text(L"See ")->see(L"FLS")->text(L" for listing the available fonts.");
+}
 
 spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params)
 {
@@ -330,7 +358,7 @@ spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_depend
        text::text_info text_info;
        text_info.font = get_param(L"FONT", params, L"verdana");
        text_info.size = get_param(L"SIZE", params, 30.0); // 30.0f does not seem to work to get as float directly
-       
+
        std::wstring col_str = get_param(L"color", params, L"#ffffffff");
        uint32_t col_val = 0xffffffff;
        try_get_color(col_str, col_val);
@@ -347,4 +375,4 @@ spl::shared_ptr<frame_producer> create_text_producer(const frame_producer_depend
                        standalone);
 }
 
-}}
+}}}