private void encode(ScilabString var, VectorOfDouble vec) { // header encodeHeader(var, vec, ScilabTypeEnum.sci_strings); // add the offset table which contains the offset of each UTF-8 encoded strings int offsetTableStart = vec.size(); vec.resize(offsetTableStart + var.getHeight() * var.getWidth()); int offsetTableAccumulated = 0; // encode the strings as UTF-8 and store the associated offset Charset utf8 = Charset.forName("UTF-8"); for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { String str = var.getData()[i][j]; byte[] bytes = str.getBytes(utf8); // append the terminal '\0' final int requiredBytes = bytes.length + 1; final int doubleLen = (requiredBytes + Double.BYTES - 1) / Double.BYTES; // set the offset offsetTableAccumulated += doubleLen; vec.set(offsetTableStart++, offsetTableAccumulated); // push the data through a temporary byte buffer int index = vec.size(); vec.resize(index + doubleLen); vec.asByteBuffer(index, doubleLen).put(bytes).put((byte) 0); } } }
/** * Allocate a port according to the explicit/implicit values. * * @return a new typed port */ private InputPort allocatePort() { InputPort ret; /* * backward compatibility, use explicit as default. */ if (graphics.size() <= GRAPHICS_INIMPL_INDEX) { return new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } ScilabType inImpl = graphics.get(GRAPHICS_INIMPL_INDEX); /* * backward compatibility, use explicit as default. */ if (isEmptyField(inImpl)) { return new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } final ScilabString inImplicit = (ScilabString) inImpl; /* * backward compatibility, use explicit as default. */ if (isEmptyField(inImplicit)) { return new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } final boolean isColumnDominant = inImplicit.getHeight() >= inImplicit.getWidth(); final int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant); final String[][] inimpl = inImplicit.getData(); // can we safely access the indexed data ? final boolean isSet = canGet(inImplicit, indexes); /* * when the type is set, create a new port instance; create an explicit typed port otherwise. */ if (isSet && inimpl[indexes[0]][indexes[1]].equals(EXPLICIT)) { ret = new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } else if (isSet && inimpl[indexes[0]][indexes[1]].equals(IMPLICIT)) { ret = new ImplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } else { // when not specified, use explicit ret = new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } return ret; }
/** @return the fontSize */ private int getFontSize() { final ScilabString exprs = getLocalExprs(); int number; final boolean isColumnDominant = exprs.getHeight() >= exprs.getWidth(); final int[] indexes = getIndexes(2, isColumnDominant); if (canGet(exprs, indexes)) { number = Integer.parseInt(exprs.getData()[indexes[0]][indexes[1]]); } else { number = 0; } return Font.getSize(number); }
/** * Fill the port with the parameters from the graphics structure. * * @param port the target instance */ private void decodeGraphics(InputPort port) { // protection against previously stored blocks if (graphics.size() > GRAPHICS_INSTYLE_INDEX && !isEmptyField(graphics.get(GRAPHICS_INSTYLE_INDEX))) { final ScilabString styles = (ScilabString) graphics.get(GRAPHICS_INSTYLE_INDEX); boolean isColumnDominant = styles.getHeight() >= styles.getWidth(); int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant); if (canGet(styles, indexes)) { final String style; style = styles.getData()[indexes[0]][indexes[1]]; port.setStyle(new StyleMap(port.getStyle()).putAll(style).toString()); } } // protection against previously stored blocks if (graphics.size() > GRAPHICS_INLABEL_INDEX && !isEmptyField(graphics.get(GRAPHICS_INLABEL_INDEX))) { final ScilabString labels = (ScilabString) graphics.get(GRAPHICS_INLABEL_INDEX); boolean isColumnDominant = labels.getHeight() >= labels.getWidth(); int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant); if (canGet(labels, indexes)) { final String label = labels.getData()[indexes[0]][indexes[1]]; if (label != null) { port.setValue(label); } else { port.setValue(""); } } } }
private ScilabType decode(VectorOfDouble vec, ScilabString var) { final String[][] data = var.getData(); decodeString(var.getHeight(), var.getWidth(), vec, (i, j, str) -> data[i][j] = str); return var; }