obs-text.cpp 28 KB

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