obs-text.cpp 28 KB

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