obs-text.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. #define set_vis(var, val, show) \
  659. do { \
  660. p = obs_properties_get(props, val); \
  661. obs_property_set_visible(p, var == show); \
  662. } while (false)
  663. static bool use_file_changed(obs_properties_t *props, obs_property_t *p,
  664. obs_data_t *s)
  665. {
  666. bool use_file = obs_data_get_bool(s, S_USE_FILE);
  667. set_vis(use_file, S_TEXT, false);
  668. set_vis(use_file, S_FILE, true);
  669. return true;
  670. }
  671. static bool outline_changed(obs_properties_t *props, obs_property_t *p,
  672. obs_data_t *s)
  673. {
  674. bool outline = obs_data_get_bool(s, S_OUTLINE);
  675. set_vis(outline, S_OUTLINE_SIZE, true);
  676. set_vis(outline, S_OUTLINE_COLOR, true);
  677. set_vis(outline, S_OUTLINE_OPACITY, true);
  678. return true;
  679. }
  680. static bool chatlog_mode_changed(obs_properties_t *props, obs_property_t *p,
  681. obs_data_t *s)
  682. {
  683. bool chatlog_mode = obs_data_get_bool(s, S_CHATLOG_MODE);
  684. set_vis(chatlog_mode, S_CHATLOG_LINES, true);
  685. return true;
  686. }
  687. static bool gradient_changed(obs_properties_t *props, obs_property_t *p,
  688. obs_data_t *s)
  689. {
  690. bool gradient = obs_data_get_bool(s, S_GRADIENT);
  691. set_vis(gradient, S_GRADIENT_COLOR, true);
  692. set_vis(gradient, S_GRADIENT_OPACITY, true);
  693. set_vis(gradient, S_GRADIENT_DIR, true);
  694. return true;
  695. }
  696. static bool extents_modified(obs_properties_t *props, obs_property_t *p,
  697. obs_data_t *s)
  698. {
  699. bool use_extents = obs_data_get_bool(s, S_EXTENTS);
  700. set_vis(use_extents, S_EXTENTS_WRAP, true);
  701. set_vis(use_extents, S_EXTENTS_CX, true);
  702. set_vis(use_extents, S_EXTENTS_CY, true);
  703. return true;
  704. }
  705. #undef set_vis
  706. static obs_properties_t *get_properties(void *data)
  707. {
  708. TextSource *s = reinterpret_cast<TextSource*>(data);
  709. string path;
  710. obs_properties_t *props = obs_properties_create();
  711. obs_property_t *p;
  712. obs_properties_add_font(props, S_FONT, T_FONT);
  713. p = obs_properties_add_bool(props, S_USE_FILE, T_USE_FILE);
  714. obs_property_set_modified_callback(p, use_file_changed);
  715. string filter;
  716. filter += T_FILTER_TEXT_FILES;
  717. filter += " (*.txt);;";
  718. filter += T_FILTER_ALL_FILES;
  719. filter += " (*.*)";
  720. if (s && !s->file.empty()) {
  721. const char *slash;
  722. path = s->file;
  723. replace(path.begin(), path.end(), '\\', '/');
  724. slash = strrchr(path.c_str(), '/');
  725. if (slash)
  726. path.resize(slash - path.c_str() + 1);
  727. }
  728. obs_properties_add_text(props, S_TEXT, T_TEXT, OBS_TEXT_MULTILINE);
  729. obs_properties_add_path(props, S_FILE, T_FILE, OBS_PATH_FILE,
  730. filter.c_str(), path.c_str());
  731. obs_properties_add_bool(props, S_VERTICAL, T_VERTICAL);
  732. obs_properties_add_color(props, S_COLOR, T_COLOR);
  733. obs_properties_add_int_slider(props, S_OPACITY, T_OPACITY, 0, 100, 1);
  734. p = obs_properties_add_bool(props, S_GRADIENT, T_GRADIENT);
  735. obs_property_set_modified_callback(p, gradient_changed);
  736. obs_properties_add_color(props, S_GRADIENT_COLOR, T_GRADIENT_COLOR);
  737. obs_properties_add_int_slider(props, S_GRADIENT_OPACITY,
  738. T_GRADIENT_OPACITY, 0, 100, 1);
  739. obs_properties_add_float_slider(props, S_GRADIENT_DIR,
  740. T_GRADIENT_DIR, 0, 360, 0.1);
  741. obs_properties_add_color(props, S_BKCOLOR, T_BKCOLOR);
  742. obs_properties_add_int_slider(props, S_BKOPACITY, T_BKOPACITY,
  743. 0, 100, 1);
  744. p = obs_properties_add_list(props, S_ALIGN, T_ALIGN,
  745. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  746. obs_property_list_add_string(p, T_ALIGN_LEFT, S_ALIGN_LEFT);
  747. obs_property_list_add_string(p, T_ALIGN_CENTER, S_ALIGN_CENTER);
  748. obs_property_list_add_string(p, T_ALIGN_RIGHT, S_ALIGN_RIGHT);
  749. p = obs_properties_add_list(props, S_VALIGN, T_VALIGN,
  750. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  751. obs_property_list_add_string(p, T_VALIGN_TOP, S_VALIGN_TOP);
  752. obs_property_list_add_string(p, T_VALIGN_CENTER, S_VALIGN_CENTER);
  753. obs_property_list_add_string(p, T_VALIGN_BOTTOM, S_VALIGN_BOTTOM);
  754. p = obs_properties_add_bool(props, S_OUTLINE, T_OUTLINE);
  755. obs_property_set_modified_callback(p, outline_changed);
  756. obs_properties_add_int(props, S_OUTLINE_SIZE, T_OUTLINE_SIZE, 1, 20, 1);
  757. obs_properties_add_color(props, S_OUTLINE_COLOR, T_OUTLINE_COLOR);
  758. obs_properties_add_int_slider(props, S_OUTLINE_OPACITY,
  759. T_OUTLINE_OPACITY, 0, 100, 1);
  760. p = obs_properties_add_bool(props, S_CHATLOG_MODE, T_CHATLOG_MODE);
  761. obs_property_set_modified_callback(p, chatlog_mode_changed);
  762. obs_properties_add_int(props, S_CHATLOG_LINES, T_CHATLOG_LINES,
  763. 1, 1000, 1);
  764. p = obs_properties_add_bool(props, S_EXTENTS, T_EXTENTS);
  765. obs_property_set_modified_callback(p, extents_modified);
  766. obs_properties_add_int(props, S_EXTENTS_CX, T_EXTENTS_CX, 32, 8000, 1);
  767. obs_properties_add_int(props, S_EXTENTS_CY, T_EXTENTS_CY, 32, 8000, 1);
  768. obs_properties_add_bool(props, S_EXTENTS_WRAP, T_EXTENTS_WRAP);
  769. return props;
  770. }
  771. bool obs_module_load(void)
  772. {
  773. obs_source_info si = {};
  774. si.id = "text_gdiplus";
  775. si.type = OBS_SOURCE_TYPE_INPUT;
  776. si.output_flags = OBS_SOURCE_VIDEO;
  777. si.get_properties = get_properties;
  778. si.get_name = [] (void*)
  779. {
  780. return obs_module_text("TextGDIPlus");
  781. };
  782. si.create = [] (obs_data_t *settings, obs_source_t *source)
  783. {
  784. return (void*)new TextSource(source, settings);
  785. };
  786. si.destroy = [] (void *data)
  787. {
  788. delete reinterpret_cast<TextSource*>(data);
  789. };
  790. si.get_width = [] (void *data)
  791. {
  792. return reinterpret_cast<TextSource*>(data)->cx;
  793. };
  794. si.get_height = [] (void *data)
  795. {
  796. return reinterpret_cast<TextSource*>(data)->cy;
  797. };
  798. si.get_defaults = [] (obs_data_t *settings)
  799. {
  800. obs_data_t *font_obj = obs_data_create();
  801. obs_data_set_default_string(font_obj, "face", "Arial");
  802. obs_data_set_default_int(font_obj, "size", 36);
  803. obs_data_set_default_obj(settings, S_FONT, font_obj);
  804. obs_data_set_default_string(settings, S_ALIGN, S_ALIGN_LEFT);
  805. obs_data_set_default_string(settings, S_VALIGN, S_VALIGN_TOP);
  806. obs_data_set_default_int(settings, S_COLOR, 0xFFFFFF);
  807. obs_data_set_default_int(settings, S_OPACITY, 100);
  808. obs_data_set_default_int(settings, S_GRADIENT_COLOR, 0xFFFFFF);
  809. obs_data_set_default_int(settings, S_GRADIENT_OPACITY, 100);
  810. obs_data_set_default_double(settings, S_GRADIENT_DIR, 90.0);
  811. obs_data_set_default_int(settings, S_BKCOLOR, 0x000000);
  812. obs_data_set_default_int(settings, S_BKOPACITY, 0);
  813. obs_data_set_default_int(settings, S_OUTLINE_SIZE, 2);
  814. obs_data_set_default_int(settings, S_OUTLINE_COLOR, 0xFFFFFF);
  815. obs_data_set_default_int(settings, S_OUTLINE_OPACITY, 100);
  816. obs_data_set_default_int(settings, S_CHATLOG_LINES, 6);
  817. obs_data_set_default_bool(settings, S_EXTENTS_WRAP, true);
  818. obs_data_set_default_int(settings, S_EXTENTS_CX, 100);
  819. obs_data_set_default_int(settings, S_EXTENTS_CY, 100);
  820. obs_data_release(font_obj);
  821. };
  822. si.update = [] (void *data, obs_data_t *settings)
  823. {
  824. reinterpret_cast<TextSource*>(data)->Update(settings);
  825. };
  826. si.video_tick = [] (void *data, float seconds)
  827. {
  828. reinterpret_cast<TextSource*>(data)->Tick(seconds);
  829. };
  830. si.video_render = [] (void *data, gs_effect_t *effect)
  831. {
  832. reinterpret_cast<TextSource*>(data)->Render(effect);
  833. };
  834. obs_register_source(&si);
  835. const GdiplusStartupInput gdip_input;
  836. GdiplusStartup(&gdip_token, &gdip_input, nullptr);
  837. return true;
  838. }
  839. void obs_module_unload(void)
  840. {
  841. GdiplusShutdown(gdip_token);
  842. }