Copying GState

Good day to you all. I am trying to back up an element's graphics state (in my case it's a text object) to another GState object for some specific treatment, but the moment the iterator parses the next element my backed up GState changes dynamically. I know this problem is probably due to shallow copying but the same doesn't happen if I back up the fill color of the text. Is there a way to avoid copying the gState by reference and do a deep copy of it? Here is a snippet of what I'm basically doing:

GState backupGState = null;
while ((element = != null) {
   switch (element.getType()) {
      case Element.e_text: {
         GState gs = element.getGState();
         backupGState = gs;

So after going into the while loop for the second time, the backupGState doesn't have the graphics state of the first element, but it changes dynamically to the state that the second element is in. This happens immediately after element = is computed.

I guess you could copy all GState properties into a temp copy and implement an assignment operator that copies the properties back into a GState. To limit the amount of work, you could only copy a subset of GSTate related to text. For example:

class GSCopy
GSCopy(GState gs) {
// call gs getters to initialize the state…
fill_color = gs.GetFillColor();
fill_cs = gs.GetFillColorSpace();


void Assign(GState gs) {
// GSCopy to GState



// the state …
ColorPt fill_color;
ColorSpace fill_color_space;

Please let me know if this would work for you.