void TexturePacker::process_resource(Canvas &canvas, Resource &item_resource, std::vector<Subtexture> &packed_sub_textures, std::map<Texture, std::string> &generated_texture_filenames, int &generated_texture_index, const std::string &image_pathname ) { // Found a sprite resource, lets modify its content! Resource resource = item_resource; // Iterate through all nodes, and remove all previous image tags DomElement &element = resource.get_element(); DomNode cur = element.get_first_child(); while (!cur.is_null()) { DomNode next = cur.get_next_sibling(); DomNode::NodeType nodeType = (DomNode::NodeType)cur.get_node_type(); // Only remove the <image> tag, as we want to keep the other sprite attributes if (cur.get_node_name() == "image") element.remove_child(cur); cur = next; } // Add new image tag to resource DOM std::vector<Subtexture>::size_type index, size; size = packed_sub_textures.size(); for(index = 0; index < size; ++index) { Subtexture subtexture = packed_sub_textures[index]; // Try to find out if we already have created a texture-on-disk for this subtexture std::string texture_filename; Texture2D texture = subtexture.get_texture(); std::map<Texture, std::string>::iterator it; it = generated_texture_filenames.find(texture); if(it == generated_texture_filenames.end()) { // Texture not found, generate a filename and dump texture to disk texture_filename = string_format("texture%1.png", ++generated_texture_index); PNGProvider::save(texture.get_pixeldata(canvas), image_pathname + texture_filename); generated_texture_filenames[texture] = texture_filename; } else { // Previously dumped textures, lets reuse the filename texture_filename = (*it).second; } // Add <grid> DOM element DomElement new_grid_element = element.get_owner_document().create_element("grid"); new_grid_element.set_attribute("pos", string_format("%1,%2", subtexture.get_geometry().left + last_border_size, subtexture.get_geometry().top + last_border_size)); new_grid_element.set_attribute("size", string_format("%1,%2", subtexture.get_geometry().get_width()- last_border_size*2, subtexture.get_geometry().get_height()- last_border_size*2)); // Add <image> DOM element DomElement new_image_element = element.get_owner_document().create_element("image"); new_image_element.set_attribute("file", texture_filename); new_image_element.append_child(new_grid_element); // Add <image> element under <sprite> element element.append_child(new_image_element); } }
DomNode DomNode::named_item(const DomString &name) const { DomNode node = get_first_child(); while (node.is_null() == false) { if (node.get_node_name() == name) return node; node = node.get_next_sibling(); } return DomNode(); }
DomNode DomNamedNodeMap::set_named_item(const DomNode &node) { if (!impl) return DomNode(); DomDocument_Impl *doc_impl = (DomDocument_Impl *) impl->owner_document.lock().get(); DomString name = node.get_node_name(); DomTreeNode *new_tree_node = (DomTreeNode *) node.impl->get_tree_node(); DomTreeNode *tree_node = impl->get_tree_node(); if (new_tree_node == tree_node) return node; unsigned int cur_index = tree_node->first_attribute; unsigned int last_index = cl_null_node_index; DomTreeNode *cur_attribute = tree_node->get_first_attribute(doc_impl); while (cur_attribute) { if (cur_attribute->get_node_name() == name) { new_tree_node->parent = cur_attribute->parent; new_tree_node->previous_sibling = cur_attribute->previous_sibling; new_tree_node->next_sibling = cur_attribute->next_sibling; if (cur_attribute->previous_sibling == cl_null_node_index) tree_node->first_attribute = node.impl->node_index; else cur_attribute->get_previous_sibling(doc_impl)->next_sibling = node.impl->node_index; if (cur_attribute->next_sibling != cl_null_node_index) cur_attribute->get_next_sibling(doc_impl)->previous_sibling = node.impl->node_index; cur_attribute->parent = cl_null_node_index; cur_attribute->previous_sibling = cl_null_node_index; cur_attribute->next_sibling = cl_null_node_index; return node; } last_index = cur_index; cur_index = cur_attribute->next_sibling; cur_attribute = cur_attribute->get_next_sibling(doc_impl); } if (last_index == cl_null_node_index) { tree_node->first_attribute = node.impl->node_index; new_tree_node->parent = impl->node_index; new_tree_node->previous_sibling = cl_null_node_index; new_tree_node->next_sibling = cl_null_node_index; } else { new_tree_node->parent = impl->node_index; new_tree_node->previous_sibling = last_index; new_tree_node->next_sibling = cl_null_node_index; doc_impl->nodes[last_index]->next_sibling = node.impl->node_index; } return node; }
DomNode DomDocument::import_node(const DomNode &node, bool deep) { DomNode imported_node; switch (node.get_node_type()) { case NULL_NODE: return imported_node; case ELEMENT_NODE: imported_node = create_element_ns(node.get_namespace_uri(), node.get_node_name()); break; case ATTRIBUTE_NODE: imported_node = create_attribute_ns(node.get_namespace_uri(), node.get_node_name()); imported_node.set_node_value(node.get_node_value()); break; case TEXT_NODE: imported_node = create_text_node(node.get_node_value()); break; case CDATA_SECTION_NODE: imported_node = create_cdata_section(node.get_node_value()); break; case ENTITY_REFERENCE_NODE: imported_node = create_entity_reference(node.get_node_name()); break; case ENTITY_NODE: return imported_node; case PROCESSING_INSTRUCTION_NODE: imported_node = create_processing_instruction(node.to_processing_instruction().get_target(), node.to_processing_instruction().get_data()); break; case COMMENT_NODE: imported_node = create_comment(node.get_node_value()); break; case DOCUMENT_NODE: imported_node = create_document_fragment(); break; case DOCUMENT_TYPE_NODE: return imported_node; case DOCUMENT_FRAGMENT_NODE: imported_node = create_document_fragment(); break; case NOTATION_NODE: return imported_node; } if (node.is_element()) { DomElement import_element = imported_node.to_element(); DomNamedNodeMap node_attributes = node.get_attributes(); int size = node_attributes.get_length(); for (int index = 0; index < size; index++) { DomNode attr = node_attributes.item(index); import_element.set_attribute_node_ns(import_node(attr, deep).to_attr()); } } if (deep) { DomNode cur = node.get_first_child(); while (!cur.is_null()) { imported_node.append_child(import_node(cur, true)); cur = cur.get_next_sibling(); } } return imported_node; }