tb_style_edit_content.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_style_edit.h"
  6. #include "tb_style_edit_content.h"
  7. #include <assert.h>
  8. namespace tb {
  9. // == TBTextFragmentContentFactory ==========================================================================
  10. int TBTextFragmentContentFactory::GetContent(const char *text)
  11. {
  12. if (text[0] == '<')
  13. {
  14. int i = 0;
  15. while (text[i] != '>' && text[i] > 31)
  16. i++;
  17. if (text[i] == '>')
  18. {
  19. i++;
  20. return i;
  21. }
  22. }
  23. return 0;
  24. }
  25. TBTextFragmentContent *TBTextFragmentContentFactory::CreateFragmentContent(const char *text, int text_len)
  26. {
  27. if (strncmp(text, "<hr>", text_len) == 0)
  28. return new TBTextFragmentContentHR(100, 2);
  29. else if (strncmp(text, "<u>", text_len) == 0)
  30. return new TBTextFragmentContentUnderline();
  31. else if (strncmp(text, "<color ", MIN(text_len, 7)) == 0)
  32. {
  33. TBColor color;
  34. color.SetFromString(text + 7, text_len - 8);
  35. return new TBTextFragmentContentTextColor(color);
  36. }
  37. else if (strncmp(text, "</", MIN(text_len, 2)) == 0)
  38. return new TBTextFragmentContentStylePop();
  39. return nullptr;
  40. }
  41. // == PHorizontalLineContent ================================================================================
  42. TBTextFragmentContentHR::TBTextFragmentContentHR(int32 width_in_percent, int32 height)
  43. : width_in_percent(width_in_percent)
  44. , height(height)
  45. {
  46. }
  47. void TBTextFragmentContentHR::Paint(TBTextFragment *fragment, int32 translate_x, int32 translate_y, TBTextProps *props)
  48. {
  49. int x = translate_x + fragment->xpos;
  50. int y = translate_y + fragment->ypos;
  51. int w = fragment->block->styledit->layout_width * width_in_percent / 100;
  52. x += (fragment->block->styledit->layout_width - w) / 2;
  53. TBStyleEditListener *listener = fragment->block->styledit->listener;
  54. listener->DrawRectFill(TBRect(x, y, w, height), props->data->text_color);
  55. }
  56. int32 TBTextFragmentContentHR::GetWidth(TBFontFace *font, TBTextFragment *fragment) { return MAX(fragment->block->styledit->layout_width, 0); }
  57. int32 TBTextFragmentContentHR::GetHeight(TBFontFace *font, TBTextFragment *fragment) { return height; }
  58. // ============================================================================
  59. void TBTextFragmentContentUnderline::Paint(TBTextFragment *fragment, int32 translate_x, int32 translate_y, TBTextProps *props)
  60. {
  61. if (TBTextProps::Data *data = props->Push())
  62. data->underline = true;
  63. }
  64. void TBTextFragmentContentTextColor::Paint(TBTextFragment *fragment, int32 translate_x, int32 translate_y, TBTextProps *props)
  65. {
  66. if (TBTextProps::Data *data = props->Push())
  67. data->text_color = color;
  68. }
  69. void TBTextFragmentContentStylePop::Paint(TBTextFragment *fragment, int32 translate_x, int32 translate_y, TBTextProps *props)
  70. {
  71. props->Pop();
  72. }
  73. }; // namespace tb