obs-text.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. #include <graphics/math-defs.h>
  2. #include <util/platform.h>
  3. #include <util/util.hpp>
  4. #include <obs-module.h>
  5. #include <sys/stat.h>
  6. #include <windows.h>
  7. #include <gdiplus.h>
  8. #include <algorithm>
  9. #include <string>
  10. #include <memory>
  11. using namespace std;
  12. using namespace Gdiplus;
  13. #define warning(format, ...) blog(LOG_WARNING, "[%s] " format, \
  14. obs_source_get_name(source), ##__VA_ARGS__)
  15. #define warn_stat(call) \
  16. do { \
  17. if (stat != Ok) \
  18. warning("%s: %s failed (%d)", __FUNCTION__, call, \
  19. (int)stat); \
  20. } while (false)
  21. #ifndef clamp
  22. #define clamp(val, min_val, max_val) \
  23. if (val < min_val) val = min_val; \
  24. else if (val > max_val) val = max_val;
  25. #endif
  26. #define MIN_SIZE_CX 2
  27. #define MIN_SIZE_CY 2
  28. #define MAX_SIZE_CX 16384
  29. #define MAX_SIZE_CY 16384
  30. #define MAX_AREA (4096LL * 4096LL)
  31. /* ------------------------------------------------------------------------- */
  32. #define S_FONT "font"
  33. #define S_USE_FILE "read_from_file"
  34. #define S_FILE "file"
  35. #define S_TEXT "text"
  36. #define S_COLOR "color"
  37. #define S_ALIGN "align"
  38. #define S_VALIGN "valign"
  39. #define S_OPACITY "opacity"
  40. #define S_BKCOLOR "bk_color"
  41. #define S_BKOPACITY "bk_opacity"
  42. #define S_VERTICAL "vertical"
  43. #define S_OUTLINE "outline"
  44. #define S_OUTLINE_SIZE "outline_size"
  45. #define S_OUTLINE_COLOR "outline_color"
  46. #define S_OUTLINE_OPACITY "outline_opacity"
  47. #define S_CHATLOG_MODE "chatlog"
  48. #define S_CHATLOG_LINES "chatlog_lines"
  49. #define S_EXTENTS "extents"
  50. #define S_EXTENTS_WRAP "extents_wrap"
  51. #define S_EXTENTS_CX "extents_cx"
  52. #define S_EXTENTS_CY "extents_cy"
  53. #define S_ALIGN_LEFT "left"
  54. #define S_ALIGN_CENTER "center"
  55. #define S_ALIGN_RIGHT "right"
  56. #define S_VALIGN_TOP "top"
  57. #define S_VALIGN_CENTER S_ALIGN_CENTER
  58. #define S_VALIGN_BOTTOM "bottom"
  59. #define T_(v) obs_module_text(v)
  60. #define T_FONT T_("Font")
  61. #define T_USE_FILE T_("ReadFromFile")
  62. #define T_FILE T_("TextFile")
  63. #define T_TEXT T_("Text")
  64. #define T_COLOR T_("Color")
  65. #define T_ALIGN T_("Alignment")
  66. #define T_VALIGN T_("VerticalAlignment")
  67. #define T_OPACITY T_("Opacity")
  68. #define T_BKCOLOR T_("BkColor")
  69. #define T_BKOPACITY T_("BkOpacity")
  70. #define T_VERTICAL T_("Vertical")
  71. #define T_OUTLINE T_("Outline")
  72. #define T_OUTLINE_SIZE T_("Outline.Size")
  73. #define T_OUTLINE_COLOR T_("Outline.Color")
  74. #define T_OUTLINE_OPACITY T_("Outline.Opacity")
  75. #define T_CHATLOG_MODE T_("ChatlogMode")
  76. #define T_CHATLOG_LINES T_("ChatlogMode.Lines")
  77. #define T_EXTENTS T_("UseCustomExtents")
  78. #define T_EXTENTS_WRAP T_("UseCustomExtents.Wrap")
  79. #define T_EXTENTS_CX T_("Width")
  80. #define T_EXTENTS_CY T_("Height")
  81. #define T_FILTER_TEXT_FILES T_("Filter.TextFiles")
  82. #define T_FILTER_ALL_FILES T_("Filter.AllFiles")
  83. #define T_ALIGN_LEFT T_("Alignment.Left")
  84. #define T_ALIGN_CENTER T_("Alignment.Center")
  85. #define T_ALIGN_RIGHT T_("Alignment.Right")
  86. #define T_VALIGN_TOP T_("VerticalAlignment.Top")
  87. #define T_VALIGN_CENTER T_ALIGN_CENTER
  88. #define T_VALIGN_BOTTOM T_("VerticalAlignment.Bottom")
  89. /* ------------------------------------------------------------------------- */
  90. static inline DWORD get_alpha_val(uint32_t opacity)
  91. {
  92. return ((opacity * 255 / 100) & 0xFF) << 24;
  93. }
  94. static inline DWORD calc_color(uint32_t color, uint32_t opacity)
  95. {
  96. return color & 0xFFFFFF | get_alpha_val(opacity);
  97. }
  98. static inline wstring to_wide(const char *utf8)
  99. {
  100. wstring text;
  101. size_t len = os_utf8_to_wcs(utf8, 0, nullptr, 0);
  102. text.resize(len);
  103. if (len)
  104. os_utf8_to_wcs(utf8, 0, &text[0], len + 1);
  105. return text;
  106. }
  107. static inline uint32_t rgb_to_bgr(uint32_t rgb)
  108. {
  109. return ((rgb & 0xFF) << 16) | (rgb & 0xFF00) | ((rgb & 0xFF0000) >> 16);
  110. }
  111. /* ------------------------------------------------------------------------- */
  112. template<typename T, typename T2, BOOL WINAPI deleter(T2)> class GDIObj {
  113. T obj = nullptr;
  114. inline GDIObj &Replace(T obj_)
  115. {
  116. if (obj) deleter(obj);
  117. obj = obj_;
  118. return *this;
  119. }
  120. public:
  121. inline GDIObj() {}
  122. inline GDIObj(T obj_) : obj(obj_) {}
  123. inline ~GDIObj() {deleter(obj);}
  124. inline T operator=(T obj_) {Replace(obj_); return obj;}
  125. inline operator T() const {return obj;}
  126. inline bool operator==(T obj_) const {return obj == obj_;}
  127. inline bool operator!=(T obj_) const {return obj != obj_;}
  128. };
  129. using HDCObj = GDIObj<HDC, HDC, DeleteDC>;
  130. using HFONTObj = GDIObj<HFONT, HGDIOBJ, DeleteObject>;
  131. using HBITMAPObj = GDIObj<HBITMAP, HGDIOBJ, DeleteObject>;
  132. /* ------------------------------------------------------------------------- */
  133. enum class Align {
  134. Left,
  135. Center,
  136. Right
  137. };
  138. enum class VAlign {
  139. Top,
  140. Center,
  141. Bottom
  142. };
  143. struct TextSource {
  144. obs_source_t *source = nullptr;
  145. gs_texture_t *tex = nullptr;
  146. uint32_t cx = 0;
  147. uint32_t cy = 0;
  148. HDCObj hdc;
  149. Graphics graphics;
  150. HFONTObj hfont;
  151. unique_ptr<Font> font;
  152. bool read_from_file = false;
  153. string file;
  154. time_t file_timestamp = 0;
  155. float update_time_elapsed = 0.0f;
  156. wstring text;
  157. wstring face;
  158. int face_size = 0;
  159. uint32_t color = 0xFFFFFF;
  160. uint32_t opacity = 100;
  161. uint32_t bk_color = 0;
  162. uint32_t bk_opacity = 0;
  163. Align align = Align::Left;
  164. VAlign valign = VAlign::Top;
  165. bool bold = false;
  166. bool italic = false;
  167. bool underline = false;
  168. bool strikeout = false;
  169. bool vertical = false;
  170. bool use_outline = false;
  171. float outline_size = 0.0f;
  172. uint32_t outline_color = 0;
  173. uint32_t outline_opacity = 100;
  174. bool use_extents = false;
  175. bool wrap = false;
  176. uint32_t extents_cx = 0;
  177. uint32_t extents_cy = 0;
  178. bool chatlog_mode = false;
  179. int chatlog_lines = 6;
  180. /* --------------------------- */
  181. inline TextSource(obs_source_t *source_, obs_data_t *settings) :
  182. source (source_),
  183. hdc (CreateCompatibleDC(nullptr)),
  184. graphics (hdc)
  185. {
  186. obs_source_update(source, settings);
  187. }
  188. inline ~TextSource()
  189. {
  190. if (tex) {
  191. obs_enter_graphics();
  192. gs_texture_destroy(tex);
  193. obs_leave_graphics();
  194. }
  195. }
  196. void UpdateFont();
  197. void GetStringFormat(StringFormat &format);
  198. void RemoveNewlinePadding(const StringFormat &format, RectF &box);
  199. void CalculateTextSizes(const StringFormat &format,
  200. RectF &bounding_box, SIZE &text_size);
  201. void RenderOutlineText(Graphics &graphics,
  202. const GraphicsPath &path,
  203. const Brush &brush);
  204. void RenderText();
  205. void LoadFileText();
  206. const char *GetMainString(const char *str);
  207. inline void Update(obs_data_t *settings);
  208. inline void Tick(float seconds);
  209. inline void Render(gs_effect_t *effect);
  210. };
  211. static time_t get_modified_timestamp(const char *filename)
  212. {
  213. struct stat stats;
  214. if (os_stat(filename, &stats) != 0)
  215. return -1;
  216. return stats.st_mtime;
  217. }
  218. void TextSource::UpdateFont()
  219. {
  220. hfont = nullptr;
  221. font.reset(nullptr);
  222. LOGFONT lf = {};
  223. lf.lfHeight = face_size;
  224. lf.lfWeight = bold ? FW_BOLD : FW_DONTCARE;
  225. lf.lfItalic = italic;
  226. lf.lfUnderline = underline;
  227. lf.lfStrikeOut = strikeout;
  228. lf.lfQuality = ANTIALIASED_QUALITY;
  229. if (!face.empty()) {
  230. wcscpy(lf.lfFaceName, face.c_str());
  231. hfont = CreateFontIndirect(&lf);
  232. }
  233. if (!hfont) {
  234. wcscpy(lf.lfFaceName, L"Arial");
  235. hfont = CreateFontIndirect(&lf);
  236. }
  237. if (hfont)
  238. font.reset(new Font(hdc, hfont));
  239. }
  240. void TextSource::GetStringFormat(StringFormat &format)
  241. {
  242. UINT flags = StringFormatFlagsNoFitBlackBox |
  243. StringFormatFlagsMeasureTrailingSpaces;
  244. if (vertical)
  245. flags |= StringFormatFlagsDirectionVertical |
  246. StringFormatFlagsDirectionRightToLeft;
  247. format.SetFormatFlags(flags);
  248. format.SetTrimming(StringTrimmingWord);
  249. switch (align) {
  250. case Align::Left:
  251. if (vertical)
  252. format.SetLineAlignment(StringAlignmentFar);
  253. else
  254. format.SetAlignment(StringAlignmentNear);
  255. break;
  256. case Align::Center:
  257. if (vertical)
  258. format.SetLineAlignment(StringAlignmentCenter);
  259. else
  260. format.SetAlignment(StringAlignmentCenter);
  261. break;
  262. case Align::Right:
  263. if (vertical)
  264. format.SetLineAlignment(StringAlignmentNear);
  265. else
  266. format.SetAlignment(StringAlignmentFar);
  267. }
  268. switch (valign) {
  269. case VAlign::Top:
  270. if (vertical)
  271. format.SetAlignment(StringAlignmentNear);
  272. else
  273. format.SetLineAlignment(StringAlignmentNear);
  274. break;
  275. case VAlign::Center:
  276. if (vertical)
  277. format.SetAlignment(StringAlignmentCenter);
  278. else
  279. format.SetLineAlignment(StringAlignmentCenter);
  280. break;
  281. case VAlign::Bottom:
  282. if (vertical)
  283. format.SetAlignment(StringAlignmentFar);
  284. else
  285. format.SetLineAlignment(StringAlignmentFar);
  286. }
  287. }
  288. /* GDI+ treats '\n' as an extra character with an actual render size when
  289. * calculating the texture size, so we have to calculate the size of '\n' to
  290. * remove the padding. Because we always add a newline to the string, we
  291. * also remove the extra unused newline. */
  292. void TextSource::RemoveNewlinePadding(const StringFormat &format, RectF &box)
  293. {
  294. RectF before;
  295. RectF after;
  296. Status stat;
  297. stat = graphics.MeasureString(L"W", 2, font.get(), PointF(0.0f, 0.0f),
  298. &format, &before);
  299. warn_stat("MeasureString (without newline)");
  300. stat = graphics.MeasureString(L"W\n", 3, font.get(), PointF(0.0f, 0.0f),
  301. &format, &after);
  302. warn_stat("MeasureString (with newline)");
  303. float offset_cx = after.Width - before.Width;
  304. float offset_cy = after.Height - before.Height;
  305. if (!vertical) {
  306. if (offset_cx >= 1.0f)
  307. offset_cx -= 1.0f;
  308. if (valign == VAlign::Center)
  309. box.Y -= offset_cy * 0.5f;
  310. else if (valign == VAlign::Bottom)
  311. box.Y -= offset_cy;
  312. } else {
  313. if (offset_cy >= 1.0f)
  314. offset_cy -= 1.0f;
  315. if (align == Align::Center)
  316. box.X -= offset_cx * 0.5f;
  317. else if (align == Align::Right)
  318. box.X -= offset_cx;
  319. }
  320. box.Width -= offset_cx;
  321. box.Height -= offset_cy;
  322. }
  323. void TextSource::CalculateTextSizes(const StringFormat &format,
  324. RectF &bounding_box, SIZE &text_size)
  325. {
  326. RectF layout_box;
  327. RectF temp_box;
  328. Status stat;
  329. if (!text.empty()) {
  330. if (use_extents && wrap) {
  331. layout_box.X = layout_box.Y = 0;
  332. layout_box.Width = float(extents_cx);
  333. layout_box.Height = float(extents_cy);
  334. if (use_outline) {
  335. layout_box.Width -= outline_size;
  336. layout_box.Height -= outline_size;
  337. }
  338. stat = graphics.MeasureString(text.c_str(),
  339. (int)text.size() + 1, font.get(),
  340. layout_box, &format,
  341. &bounding_box);
  342. warn_stat("MeasureString (wrapped)");
  343. temp_box = bounding_box;
  344. } else {
  345. stat = graphics.MeasureString(text.c_str(),
  346. (int)text.size() + 1, font.get(),
  347. PointF(0.0f, 0.0f), &format,
  348. &bounding_box);
  349. warn_stat("MeasureString (non-wrapped)");
  350. temp_box = bounding_box;
  351. bounding_box.X = 0.0f;
  352. bounding_box.Y = 0.0f;
  353. RemoveNewlinePadding(format, bounding_box);
  354. if (use_outline) {
  355. bounding_box.Width += outline_size;
  356. bounding_box.Height += outline_size;
  357. }
  358. }
  359. }
  360. if (vertical) {
  361. if (bounding_box.Width < face_size) {
  362. text_size.cx = face_size;
  363. bounding_box.Width = float(face_size);
  364. } else {
  365. text_size.cx = LONG(bounding_box.Width + EPSILON);
  366. }
  367. text_size.cy = LONG(bounding_box.Height + EPSILON);
  368. } else {
  369. if (bounding_box.Height < face_size) {
  370. text_size.cy = face_size;
  371. bounding_box.Height = float(face_size);
  372. } else {
  373. text_size.cy = LONG(bounding_box.Height + EPSILON);
  374. }
  375. text_size.cx = LONG(bounding_box.Width + EPSILON);
  376. }
  377. if (use_extents) {
  378. text_size.cx = extents_cx;
  379. text_size.cy = extents_cy;
  380. }
  381. text_size.cx += text_size.cx % 2;
  382. text_size.cy += text_size.cy % 2;
  383. int64_t total_size = int64_t(text_size.cx) * int64_t(text_size.cy);
  384. /* GPUs typically have texture size limitations */
  385. clamp(text_size.cx, MIN_SIZE_CX, MAX_SIZE_CX);
  386. clamp(text_size.cy, MIN_SIZE_CY, MAX_SIZE_CY);
  387. /* avoid taking up too much VRAM */
  388. if (total_size > MAX_AREA) {
  389. if (text_size.cx > text_size.cy)
  390. text_size.cx = (LONG)MAX_AREA / text_size.cy;
  391. else
  392. text_size.cy = (LONG)MAX_AREA / text_size.cx;
  393. }
  394. /* the internal text-rendering bounding box for is reset to
  395. * its internal value in case the texture gets cut off */
  396. bounding_box.Width = temp_box.Width;
  397. bounding_box.Height = temp_box.Height;
  398. }
  399. void TextSource::RenderOutlineText(Graphics &graphics,
  400. const GraphicsPath &path,
  401. const Brush &brush)
  402. {
  403. DWORD outline_rgba = calc_color(outline_color, outline_opacity);
  404. Status stat;
  405. Pen pen(Color(outline_rgba), outline_size);
  406. stat = pen.SetLineJoin(LineJoinRound);
  407. warn_stat("pen.SetLineJoin");
  408. stat = graphics.DrawPath(&pen, &path);
  409. warn_stat("graphics.DrawPath");
  410. stat = graphics.FillPath(&brush, &path);
  411. warn_stat("graphics.FillPath");
  412. }
  413. void TextSource::RenderText()
  414. {
  415. StringFormat format(StringFormat::GenericTypographic());
  416. Status stat;
  417. RectF box;
  418. SIZE size;
  419. GetStringFormat(format);
  420. CalculateTextSizes(format, box, size);
  421. unique_ptr<uint8_t> bits(new uint8_t[size.cx * size.cy * 4]);
  422. Bitmap bitmap(size.cx, size.cy, 4 * size.cx, PixelFormat32bppARGB,
  423. bits.get());
  424. Graphics graphics_bitmap(&bitmap);
  425. SolidBrush brush(Color(get_alpha_val(opacity) | (color & 0xFFFFFF)));
  426. DWORD full_bk_color = bk_color & 0xFFFFFF;
  427. if (!text.empty() || use_extents)
  428. full_bk_color |= get_alpha_val(bk_opacity);
  429. if ((size.cx > box.Width || size.cy > box.Height) && !use_extents) {
  430. stat = graphics_bitmap.Clear(Color(0));
  431. warn_stat("graphics_bitmap.Clear");
  432. SolidBrush bk_brush = Color(full_bk_color);
  433. stat = graphics_bitmap.FillRectangle(&bk_brush, box);
  434. warn_stat("graphics_bitmap.FillRectangle");
  435. } else {
  436. stat = graphics_bitmap.Clear(Color(full_bk_color));
  437. warn_stat("graphics_bitmap.Clear");
  438. }
  439. graphics_bitmap.SetTextRenderingHint(TextRenderingHintAntiAlias);
  440. graphics_bitmap.SetCompositingMode(CompositingModeSourceOver);
  441. graphics_bitmap.SetSmoothingMode(SmoothingModeAntiAlias);
  442. if (!text.empty()) {
  443. if (use_outline) {
  444. box.Offset(outline_size / 2, outline_size / 2);
  445. FontFamily family;
  446. GraphicsPath path;
  447. font->GetFamily(&family);
  448. stat = path.AddString(text.c_str(), (int)text.size(),
  449. &family, font->GetStyle(),
  450. font->GetSize(), box, &format);
  451. warn_stat("path.AddString");
  452. RenderOutlineText(graphics_bitmap, path, brush);
  453. } else {
  454. stat = graphics_bitmap.DrawString(text.c_str(),
  455. (int)text.size(), font.get(),
  456. box, &format, &brush);
  457. warn_stat("graphics_bitmap.DrawString");
  458. }
  459. }
  460. if (!tex || (LONG)cx != size.cx || (LONG)cy != size.cy) {
  461. obs_enter_graphics();
  462. if (tex)
  463. gs_texture_destroy(tex);
  464. const uint8_t *data = (uint8_t*)bits.get();
  465. tex = gs_texture_create(size.cx, size.cy, GS_BGRA, 1, &data,
  466. GS_DYNAMIC);
  467. obs_leave_graphics();
  468. cx = (uint32_t)size.cx;
  469. cy = (uint32_t)size.cy;
  470. } else if (tex) {
  471. obs_enter_graphics();
  472. gs_texture_set_image(tex, bits.get(), size.cx * 4, false);
  473. obs_leave_graphics();
  474. }
  475. }
  476. const char *TextSource::GetMainString(const char *str)
  477. {
  478. if (!str)
  479. return "";
  480. if (!chatlog_mode || !chatlog_lines)
  481. return str;
  482. int lines = chatlog_lines;
  483. size_t len = strlen(str);
  484. if (!len)
  485. return str;
  486. const char *temp = str + len;
  487. while(temp != str) {
  488. temp--;
  489. if (temp[0] == '\n' && temp[1] != 0) {
  490. if (!--lines)
  491. break;
  492. }
  493. }
  494. return *temp == '\n' ? temp + 1 : temp;
  495. }
  496. void TextSource::LoadFileText()
  497. {
  498. BPtr<char> file_text = os_quick_read_utf8_file(file.c_str());
  499. text = to_wide(GetMainString(file_text));
  500. if (!text.empty() && text.back() != '\n')
  501. text.push_back('\n');
  502. }
  503. #define obs_data_get_uint32 (uint32_t)obs_data_get_int
  504. inline void TextSource::Update(obs_data_t *s)
  505. {
  506. const char *new_text = obs_data_get_string(s, S_TEXT);
  507. obs_data_t *font_obj = obs_data_get_obj(s, S_FONT);
  508. const char *align_str = obs_data_get_string(s, S_ALIGN);
  509. const char *valign_str = obs_data_get_string(s, S_VALIGN);
  510. uint32_t new_color = obs_data_get_uint32(s, S_COLOR);
  511. uint32_t new_opacity = obs_data_get_uint32(s, S_OPACITY);
  512. bool new_vertical = obs_data_get_bool(s, S_VERTICAL);
  513. bool new_outline = obs_data_get_bool(s, S_OUTLINE);
  514. uint32_t new_o_color = obs_data_get_uint32(s, S_OUTLINE_COLOR);
  515. uint32_t new_o_opacity = obs_data_get_uint32(s, S_OUTLINE_OPACITY);
  516. uint32_t new_o_size = obs_data_get_uint32(s, S_OUTLINE_SIZE);
  517. bool new_use_file = obs_data_get_bool(s, S_USE_FILE);
  518. const char *new_file = obs_data_get_string(s, S_FILE);
  519. bool new_chat_mode = obs_data_get_bool(s, S_CHATLOG_MODE);
  520. int new_chat_lines = (int)obs_data_get_int(s, S_CHATLOG_LINES);
  521. bool new_extents = obs_data_get_bool(s, S_EXTENTS);
  522. bool new_extents_wrap = obs_data_get_bool(s, S_EXTENTS_WRAP);
  523. uint32_t n_extents_cx = obs_data_get_uint32(s, S_EXTENTS_CX);
  524. uint32_t n_extents_cy = obs_data_get_uint32(s, S_EXTENTS_CY);
  525. const char *font_face = obs_data_get_string(font_obj, "face");
  526. int font_size = (int)obs_data_get_int(font_obj, "size");
  527. int64_t font_flags = obs_data_get_int(font_obj, "flags");
  528. bool new_bold = (font_flags & OBS_FONT_BOLD) != 0;
  529. bool new_italic = (font_flags & OBS_FONT_ITALIC) != 0;
  530. bool new_underline = (font_flags & OBS_FONT_UNDERLINE) != 0;
  531. bool new_strikeout = (font_flags & OBS_FONT_STRIKEOUT) != 0;
  532. uint32_t new_bk_color = obs_data_get_uint32(s, S_BKCOLOR);
  533. uint32_t new_bk_opacity = obs_data_get_uint32(s, S_BKOPACITY);
  534. /* ----------------------------- */
  535. wstring new_face = to_wide(font_face);
  536. if (new_face != face ||
  537. face_size != font_size ||
  538. new_bold != bold ||
  539. new_italic != italic ||
  540. new_underline != underline ||
  541. new_strikeout != strikeout) {
  542. face = new_face;
  543. face_size = font_size;
  544. bold = new_bold;
  545. italic = new_italic;
  546. underline = new_underline;
  547. strikeout = new_strikeout;
  548. UpdateFont();
  549. }
  550. /* ----------------------------- */
  551. new_color = rgb_to_bgr(new_color);
  552. new_o_color = rgb_to_bgr(new_o_color);
  553. new_bk_color = rgb_to_bgr(new_bk_color);
  554. color = new_color;
  555. opacity = new_opacity;
  556. vertical = new_vertical;
  557. bk_color = new_bk_color;
  558. bk_opacity = new_bk_opacity;
  559. use_extents = new_extents;
  560. wrap = new_extents_wrap;
  561. extents_cx = n_extents_cx;
  562. extents_cy = n_extents_cy;
  563. read_from_file = new_use_file;
  564. chatlog_mode = new_chat_mode;
  565. chatlog_lines = new_chat_lines;
  566. if (read_from_file) {
  567. file = new_file;
  568. file_timestamp = get_modified_timestamp(new_file);
  569. LoadFileText();
  570. } else {
  571. text = to_wide(GetMainString(new_text));
  572. /* all text should end with newlines due to the fact that GDI+
  573. * treats strings without newlines differently in terms of
  574. * render size */
  575. if (!text.empty())
  576. text.push_back('\n');
  577. }
  578. use_outline = new_outline;
  579. outline_color = new_o_color;
  580. outline_opacity = new_o_opacity;
  581. outline_size = roundf(float(new_o_size));
  582. if (strcmp(align_str, S_ALIGN_CENTER) == 0)
  583. align = Align::Center;
  584. else if (strcmp(align_str, S_ALIGN_RIGHT) == 0)
  585. align = Align::Right;
  586. else
  587. align = Align::Left;
  588. if (strcmp(valign_str, S_VALIGN_CENTER) == 0)
  589. valign = VAlign::Center;
  590. else if (strcmp(valign_str, S_VALIGN_BOTTOM) == 0)
  591. valign = VAlign::Bottom;
  592. else
  593. valign = VAlign::Top;
  594. RenderText();
  595. update_time_elapsed = 0.0f;
  596. /* ----------------------------- */
  597. obs_data_release(font_obj);
  598. }
  599. inline void TextSource::Tick(float seconds)
  600. {
  601. if (!read_from_file)
  602. return;
  603. update_time_elapsed += seconds;
  604. if (update_time_elapsed >= 2.0f) {
  605. time_t t = get_modified_timestamp(file.c_str());
  606. update_time_elapsed = 0.0f;
  607. if (file_timestamp != t) {
  608. LoadFileText();
  609. RenderText();
  610. file_timestamp = t;
  611. }
  612. }
  613. }
  614. inline void TextSource::Render(gs_effect_t *effect)
  615. {
  616. if (!tex)
  617. return;
  618. gs_reset_blend_state();
  619. gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"), tex);
  620. gs_draw_sprite(tex, 0, cx, cy);
  621. }
  622. /* ------------------------------------------------------------------------- */
  623. static ULONG_PTR gdip_token = 0;
  624. OBS_DECLARE_MODULE()
  625. OBS_MODULE_USE_DEFAULT_LOCALE("obs-text", "en-US")
  626. #define set_vis(var, val, show) \
  627. do { \
  628. p = obs_properties_get(props, val); \
  629. obs_property_set_visible(p, var == show); \
  630. } while (false)
  631. static bool use_file_changed(obs_properties_t *props, obs_property_t *p,
  632. obs_data_t *s)
  633. {
  634. bool use_file = obs_data_get_bool(s, S_USE_FILE);
  635. set_vis(use_file, S_TEXT, false);
  636. set_vis(use_file, S_FILE, true);
  637. return true;
  638. }
  639. static bool outline_changed(obs_properties_t *props, obs_property_t *p,
  640. obs_data_t *s)
  641. {
  642. bool outline = obs_data_get_bool(s, S_OUTLINE);
  643. set_vis(outline, S_OUTLINE_SIZE, true);
  644. set_vis(outline, S_OUTLINE_COLOR, true);
  645. set_vis(outline, S_OUTLINE_OPACITY, true);
  646. return true;
  647. }
  648. static bool chatlog_mode_changed(obs_properties_t *props, obs_property_t *p,
  649. obs_data_t *s)
  650. {
  651. bool chatlog_mode = obs_data_get_bool(s, S_CHATLOG_MODE);
  652. set_vis(chatlog_mode, S_CHATLOG_LINES, true);
  653. return true;
  654. }
  655. static bool extents_modified(obs_properties_t *props, obs_property_t *p,
  656. obs_data_t *s)
  657. {
  658. bool use_extents = obs_data_get_bool(s, S_EXTENTS);
  659. set_vis(use_extents, S_EXTENTS_WRAP, true);
  660. set_vis(use_extents, S_EXTENTS_CX, true);
  661. set_vis(use_extents, S_EXTENTS_CY, true);
  662. return true;
  663. }
  664. #undef set_vis
  665. static obs_properties_t *get_properties(void *data)
  666. {
  667. TextSource *s = reinterpret_cast<TextSource*>(data);
  668. string path;
  669. obs_properties_t *props = obs_properties_create();
  670. obs_property_t *p;
  671. obs_properties_add_font(props, S_FONT, T_FONT);
  672. p = obs_properties_add_bool(props, S_USE_FILE, T_USE_FILE);
  673. obs_property_set_modified_callback(p, use_file_changed);
  674. string filter;
  675. filter += T_FILTER_TEXT_FILES;
  676. filter += " (*.txt);;";
  677. filter += T_FILTER_ALL_FILES;
  678. filter += " (*.*)";
  679. if (s && !s->file.empty()) {
  680. const char *slash;
  681. path = s->file;
  682. replace(path.begin(), path.end(), '\\', '/');
  683. slash = strrchr(path.c_str(), '/');
  684. if (slash)
  685. path.resize(slash - path.c_str() + 1);
  686. }
  687. obs_properties_add_text(props, S_TEXT, T_TEXT, OBS_TEXT_MULTILINE);
  688. obs_properties_add_path(props, S_FILE, T_FILE, OBS_PATH_FILE,
  689. filter.c_str(), path.c_str());
  690. obs_properties_add_bool(props, S_VERTICAL, T_VERTICAL);
  691. obs_properties_add_color(props, S_COLOR, T_COLOR);
  692. obs_properties_add_int_slider(props, S_OPACITY, T_OPACITY, 0, 100, 1);
  693. obs_properties_add_color(props, S_BKCOLOR, T_BKCOLOR);
  694. obs_properties_add_int_slider(props, S_BKOPACITY, T_BKOPACITY,
  695. 0, 100, 1);
  696. p = obs_properties_add_list(props, S_ALIGN, T_ALIGN,
  697. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  698. obs_property_list_add_string(p, T_ALIGN_LEFT, S_ALIGN_LEFT);
  699. obs_property_list_add_string(p, T_ALIGN_CENTER, S_ALIGN_CENTER);
  700. obs_property_list_add_string(p, T_ALIGN_RIGHT, S_ALIGN_RIGHT);
  701. p = obs_properties_add_list(props, S_VALIGN, T_VALIGN,
  702. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  703. obs_property_list_add_string(p, T_VALIGN_TOP, S_VALIGN_TOP);
  704. obs_property_list_add_string(p, T_VALIGN_CENTER, S_VALIGN_CENTER);
  705. obs_property_list_add_string(p, T_VALIGN_BOTTOM, S_VALIGN_BOTTOM);
  706. p = obs_properties_add_bool(props, S_OUTLINE, T_OUTLINE);
  707. obs_property_set_modified_callback(p, outline_changed);
  708. obs_properties_add_int(props, S_OUTLINE_SIZE, T_OUTLINE_SIZE, 1, 20, 1);
  709. obs_properties_add_color(props, S_OUTLINE_COLOR, T_OUTLINE_COLOR);
  710. obs_properties_add_int_slider(props, S_OUTLINE_OPACITY,
  711. T_OUTLINE_OPACITY, 0, 100, 1);
  712. p = obs_properties_add_bool(props, S_CHATLOG_MODE, T_CHATLOG_MODE);
  713. obs_property_set_modified_callback(p, chatlog_mode_changed);
  714. obs_properties_add_int(props, S_CHATLOG_LINES, T_CHATLOG_LINES,
  715. 1, 1000, 1);
  716. p = obs_properties_add_bool(props, S_EXTENTS, T_EXTENTS);
  717. obs_property_set_modified_callback(p, extents_modified);
  718. obs_properties_add_int(props, S_EXTENTS_CX, T_EXTENTS_CX, 32, 8000, 1);
  719. obs_properties_add_int(props, S_EXTENTS_CY, T_EXTENTS_CY, 32, 8000, 1);
  720. obs_properties_add_bool(props, S_EXTENTS_WRAP, T_EXTENTS_WRAP);
  721. return props;
  722. }
  723. bool obs_module_load(void)
  724. {
  725. obs_source_info si = {};
  726. si.id = "text_gdiplus";
  727. si.type = OBS_SOURCE_TYPE_INPUT;
  728. si.output_flags = OBS_SOURCE_VIDEO;
  729. si.get_properties = get_properties;
  730. si.get_name = [] (void*)
  731. {
  732. return obs_module_text("TextGDIPlus");
  733. };
  734. si.create = [] (obs_data_t *settings, obs_source_t *source)
  735. {
  736. return (void*)new TextSource(source, settings);
  737. };
  738. si.destroy = [] (void *data)
  739. {
  740. delete reinterpret_cast<TextSource*>(data);
  741. };
  742. si.get_width = [] (void *data)
  743. {
  744. return reinterpret_cast<TextSource*>(data)->cx;
  745. };
  746. si.get_height = [] (void *data)
  747. {
  748. return reinterpret_cast<TextSource*>(data)->cy;
  749. };
  750. si.get_defaults = [] (obs_data_t *settings)
  751. {
  752. obs_data_t *font_obj = obs_data_create();
  753. obs_data_set_default_string(font_obj, "face", "Arial");
  754. obs_data_set_default_int(font_obj, "size", 22);
  755. obs_data_set_default_obj(settings, S_FONT, font_obj);
  756. obs_data_set_default_string(settings, S_ALIGN, S_ALIGN_LEFT);
  757. obs_data_set_default_string(settings, S_VALIGN, S_VALIGN_TOP);
  758. obs_data_set_default_int(settings, S_COLOR, 0xFFFFFF);
  759. obs_data_set_default_int(settings, S_OPACITY, 100);
  760. obs_data_set_default_int(settings, S_BKCOLOR, 0x000000);
  761. obs_data_set_default_int(settings, S_BKOPACITY, 0);
  762. obs_data_set_default_int(settings, S_OUTLINE_SIZE, 2);
  763. obs_data_set_default_int(settings, S_OUTLINE_COLOR, 0xFFFFFF);
  764. obs_data_set_default_int(settings, S_OUTLINE_OPACITY, 100);
  765. obs_data_set_default_int(settings, S_CHATLOG_LINES, 6);
  766. obs_data_set_default_bool(settings, S_EXTENTS_WRAP, true);
  767. obs_data_set_default_int(settings, S_EXTENTS_CX, 100);
  768. obs_data_set_default_int(settings, S_EXTENTS_CY, 100);
  769. obs_data_release(font_obj);
  770. };
  771. si.update = [] (void *data, obs_data_t *settings)
  772. {
  773. reinterpret_cast<TextSource*>(data)->Update(settings);
  774. };
  775. si.video_tick = [] (void *data, float seconds)
  776. {
  777. reinterpret_cast<TextSource*>(data)->Tick(seconds);
  778. };
  779. si.video_render = [] (void *data, gs_effect_t *effect)
  780. {
  781. reinterpret_cast<TextSource*>(data)->Render(effect);
  782. };
  783. obs_register_source(&si);
  784. const GdiplusStartupInput gdip_input;
  785. GdiplusStartup(&gdip_token, &gdip_input, nullptr);
  786. return true;
  787. }
  788. void obs_module_unload(void)
  789. {
  790. GdiplusShutdown(gdip_token);
  791. }