[Bf-blender-cvs] [533ccfed037] greasepencil-object: GPencil: Include Styles

Antonio Vazquez noreply at git.blender.org
Sat Jul 25 16:33:29 CEST 2020


Commit: 533ccfed037017c50042ac04f18ad18cf2150606
Author: Antonio Vazquez
Date:   Sat Jul 25 16:17:56 2020 +0200
Branches: greasepencil-object
https://developer.blender.org/rB533ccfed037017c50042ac04f18ad18cf2150606

GPencil: Include Styles

===================================================================

M	source/blender/io/gpencil/intern/gpencil_io_base.cc
M	source/blender/io/gpencil/intern/gpencil_io_base.h
M	source/blender/io/gpencil/intern/gpencil_io_svg.cc
M	source/blender/io/gpencil/intern/gpencil_io_svg.h

===================================================================

diff --git a/source/blender/io/gpencil/intern/gpencil_io_base.cc b/source/blender/io/gpencil/intern/gpencil_io_base.cc
index f8427397338..26b801fa6aa 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_base.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_base.cc
@@ -17,6 +17,9 @@
 /** \file
  * \ingroup bgpencil
  */
+#include <algorithm>
+#include <cctype>
+
 #include <iostream>
 #include <string>
 
@@ -30,6 +33,8 @@
 #include "BLI_string.h"
 #include "BLI_utildefines.h"
 
+#include "DNA_object_types.h"
+
 #ifdef WIN32
 #  include "utfconv.h"
 #endif
@@ -38,8 +43,8 @@
 
 #include "ED_view3d.h"
 
+#include "gpencil_io_base.h"
 #include "gpencil_io_exporter.h"
-#include "gpencil_io_svg.h"
 
 #include "pugixml.hpp"
 
@@ -47,6 +52,11 @@ namespace blender {
 namespace io {
 namespace gpencil {
 
+/**
+ * Set output file input_text full path.
+ * \param C: Context.
+ * \param filename: Path of the file provided by save dialog.
+ */
 void GpencilExporter::set_out_filename(struct bContext *C, char *filename)
 {
   Main *bmain = CTX_data_main(C);
@@ -80,6 +90,11 @@ bool GpencilExporter::gpencil_3d_point_to_screen_space(struct ARegion *region,
   return false;
 }
 
+/**
+ * Convert a color to Hex value (#FFFFFF)
+ * \param color: Original RGB color
+ * \return String with the conversion
+ */
 std::string GpencilExporter::rgb_to_hex(float color[3])
 {
   int r = color[0] * 255.0f;
@@ -93,6 +108,19 @@ std::string GpencilExporter::rgb_to_hex(float color[3])
   return hexstr;
 }
 
+/**
+ * Convert a full string to lowercase
+ * \param input_text: Input input_text
+ * \return Lower case string
+ */
+std::string GpencilExporter::to_lower_string(char *input_text)
+{
+  ::std::string text = input_text;
+  std::transform(
+      text.begin(), text.end(), text.begin(), [](unsigned char c) { return std::tolower(c); });
+  return text;
+}
+
 }  // namespace gpencil
 }  // namespace io
 }  // namespace blender
diff --git a/source/blender/io/gpencil/intern/gpencil_io_base.h b/source/blender/io/gpencil/intern/gpencil_io_base.h
index bac751b7121..f0137eacafb 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_base.h
+++ b/source/blender/io/gpencil/intern/gpencil_io_base.h
@@ -18,12 +18,17 @@
 /** \file
  * \ingroup bgpencil
  */
+#include <map>
+#include <string>
+#include <vector>
+
 #include "BLI_path_util.h"
 
-#include "pugixml.hpp"
+#include "DNA_defs.h"
+
+#include "gpencil_io_exporter.h"
 
 struct Main;
-struct GpencilExportParams;
 struct ARegion;
 
 namespace blender {
@@ -43,6 +48,7 @@ class GpencilExporter {
                                         int r_co[2]);
 
   std::string rgb_to_hex(float color[3]);
+  std::string to_lower_string(char *input_text);
 
  protected:
   GpencilExportParams params;
diff --git a/source/blender/io/gpencil/intern/gpencil_io_svg.cc b/source/blender/io/gpencil/intern/gpencil_io_svg.cc
index dd125ae7e99..b6864b4123f 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_svg.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_svg.cc
@@ -23,6 +23,7 @@
 #include "BKE_context.h"
 #include "BKE_gpencil.h"
 #include "BKE_main.h"
+#include "BKE_material.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
@@ -30,6 +31,7 @@
 #include "BLI_utildefines.h"
 
 #include "DNA_gpencil_types.h"
+#include "DNA_material_types.h"
 #include "DNA_object_types.h"
 #include "DNA_screen_types.h"
 
@@ -69,7 +71,9 @@ GpencilExporterSVG::GpencilExporterSVG(const struct GpencilExportParams *params)
 bool GpencilExporterSVG::write(void)
 {
   create_document_header();
-  layers_loop();
+
+  export_style_list();
+  export_layers();
 
   doc.save_file(out_filename);
 
@@ -82,7 +86,7 @@ void GpencilExporterSVG::create_document_header(void)
   int x = params.region->winx;
   int y = params.region->winy;
 
-  /* Add a custom document declaration node */
+  /* Add a custom document declaration node. */
   pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
   decl.append_attribute("version") = "1.0";
   decl.append_attribute("encoding") = "UTF-8";
@@ -109,7 +113,7 @@ void GpencilExporterSVG::create_document_header(void)
 }
 
 /* Main layer loop. */
-void GpencilExporterSVG::layers_loop(void)
+void GpencilExporterSVG::export_layers(void)
 {
   float color[3] = {1.0f, 0.5f, 0.01f};
   std::string hex = rgb_to_hex(color);
@@ -131,34 +135,91 @@ void GpencilExporterSVG::layers_loop(void)
       continue;
     }
 
+    float diff_mat[4][4];
+    BKE_gpencil_parent_matrix_get(depsgraph, ob, gpl, diff_mat);
+
     LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
-      float diff_mat[4][4];
-      BKE_gpencil_parent_matrix_get(depsgraph, ob, gpl, diff_mat);
-
-      pugi::xml_node gps_node = gpl_node.append_child("path");
-      // gps_node.append_attribute("fill").set_value("#000000");
-      gps_node.append_attribute("stroke").set_value("#000000");
-      gps_node.append_attribute("stroke-width").set_value("1.0");
-      std::string txt = "M";
-      for (int i = 0; i < gps->totpoints; i++) {
-        if (i > 0) {
-          txt.append("L");
-        }
-        bGPDspoint *pt = &gps->points[i];
-        int screen_co[2];
-        gpencil_3d_point_to_screen_space(params.region, diff_mat, &pt->x, screen_co);
-        /* Invert Y axis. */
-        int y = params.region->winy - screen_co[1];
-        txt.append(std::to_string(screen_co[0]) + "," + std::to_string(y));
-      }
-      /* Close patch (cyclic)*/
-      if (gps->flag & GP_STROKE_CYCLIC) {
-        txt.append("z");
-      }
-
-      gps_node.append_attribute("d").set_value(txt.c_str());
+      Material *ma = BKE_object_material_get(ob, gps->mat_nr + 1);
+
+      export_stroke(gpl_node, gps, ma, diff_mat);
+    }
+  }
+}
+
+/**
+ * Export a stroke
+ * \param gpl_node: Node of the layer.
+ * \param gps: Stroke to export.
+ * \param ma: Material of the stroke.
+ * \param diff_mat: Transformation matrix.
+ */
+void GpencilExporterSVG::export_stroke(pugi::xml_node gpl_node,
+                                       struct bGPDstroke *gps,
+                                       struct Material *ma,
+                                       float diff_mat[4][4])
+{
+
+  pugi::xml_node gps_node = gpl_node.append_child("path");
+  // gps_node.append_attribute("fill").set_value("#000000");
+  // gps_node.append_attribute("stroke").set_value("#000000");
+
+  gps_node.append_attribute("class").set_value(to_lower_string(ma->id.name + 2).c_str());
+
+  gps_node.append_attribute("stroke-width").set_value("1.0");
+
+  std::string txt = "M";
+  for (int i = 0; i < gps->totpoints; i++) {
+    if (i > 0) {
+      txt.append("L");
+    }
+    bGPDspoint *pt = &gps->points[i];
+    int screen_co[2];
+    gpencil_3d_point_to_screen_space(params.region, diff_mat, &pt->x, screen_co);
+    /* Invert Y axis. */
+    int y = params.region->winy - screen_co[1];
+    txt.append(std::to_string(screen_co[0]) + "," + std::to_string(y));
+  }
+  /* Close patch (cyclic)*/
+  if (gps->flag & GP_STROKE_CYCLIC) {
+    txt.append("z");
+  }
+
+  gps_node.append_attribute("d").set_value(txt.c_str());
+}
+
+/**
+ * Create Style (materials) list.
+ */
+void GpencilExporterSVG::export_style_list(void)
+{
+  Object *ob = this->params.ob;
+  int mat_len = max_ii(1, ob->totcol);
+  pugi::xml_node style_node = main_node.append_child("style");
+  style_node.append_attribute("type").set_value("text/css");
+
+  std::string txt;
+  float col[3];
+
+  for (int i = 0; i < mat_len; i++) {
+    Material *ma = BKE_object_material_get(ob, i + 1);
+    MaterialGPencilStyle *gp_style = ma->gp_style;
+    txt.append("\n\t.");
+    txt.append(to_lower_string(ma->id.name + 2).c_str());
+
+    txt.append("{");
+    if (gp_style->flag & GP_MATERIAL_FILL_SHOW) {
+      copy_v3_v3(col, gp_style->fill_rgba);
+      txt.append("fill:" + rgb_to_hex(col) + ";");
+    }
+
+    if (gp_style->flag & GP_MATERIAL_STROKE_SHOW) {
+      copy_v3_v3(col, gp_style->stroke_rgba);
+      txt.append("stroke:" + rgb_to_hex(col) + ";");
     }
+    txt.append("}");
   }
+  txt.append("\n\t");
+  style_node.text().set(txt.c_str());
 }
 
 }  // namespace gpencil
diff --git a/source/blender/io/gpencil/intern/gpencil_io_svg.h b/source/blender/io/gpencil/intern/gpencil_io_svg.h
index ac729720e15..0f7f6f91535 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_svg.h
+++ b/source/blender/io/gpencil/intern/gpencil_io_svg.h
@@ -20,35 +20,22 @@
  */
 #include "BLI_path_util.h"
 
+#include "DNA_material_types.h"
+
+#include "gpencil_io_base.h"
 #include "pugixml.hpp"
 
 struct Main;
+struct Material;
 struct GpencilExportParams;
 struct ARegion;
 
+struct bGPDstroke;
+
 namespace blender {
 namespace io {
 namespace gpencil {
 
-class GpencilExporter {
-
- public:
-  virtual bool write(void) = 0;
-  void set_out_filename(struct bContext *C, char *filename);
-
-  /* Geometry functions. */
-  bool gpencil_3d_point_to_screen_space(struct ARegion *region,
-                                        const float diff_mat[4][4],
-                                        const float co[3],
-                                        int r_co[2]);
-
-  std::string rgb_to_hex(float color[3]);
-
- protected:
-  GpencilExportParams params;
-  char out_filename[FILE_MAX];
-};
-
 class GpencilExporterSVG : public GpencilExporter {
 
  public:
@@ -62,7 +49,12 @@ class GpencilExporterSVG : public GpencilExporter {
   pugi::xml_node main_node;
 
   void create_document_header(void);
-  void layers_loop(void);
+  void export_layers(void);
+  void export_style_list(void);
+  void export_stroke(pugi::xml_node gpl_node,
+                     struct bGPDstroke *gps,
+                     struct Material *ma,
+                     float diff_mat[4][4]);
 };
 
 }  // namespace gpencil



More information about the Bf-blender-cvs mailing list