encodeHighLevel static method

String encodeHighLevel(
  1. String msg, [
  2. SymbolShapeHint shape = SymbolShapeHint.forceNone,
  3. Dimension? minSize,
  4. Dimension? maxSize,
  5. bool forceC40 = false,
])

Performs message encoding of a DataMatrix message using the algorithm described in annex P of ISO/IEC 16022:2000(E).

@param msg the message @param shape requested shape. May be {@code SymbolShapeHint.FORCE_NONE}, {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}. @param minSize the minimum symbol size constraint or null for no constraint @param maxSize the maximum symbol size constraint or null for no constraint @return the encoded message (the char values range from 0 to 255)

Implementation

static String encodeHighLevel(
  String msg, [
  SymbolShapeHint shape = SymbolShapeHint.forceNone,
  Dimension? minSize,
  Dimension? maxSize,
  bool forceC40 = false,
]) {
  //the codewords 0..255 are encoded as Unicode characters
  final c40Encoder = C40Encoder();
  final encoders = <Encoder>[
    ASCIIEncoder(),
    c40Encoder,
    TextEncoder(),
    X12Encoder(),
    EdifactEncoder(),
    Base256Encoder(),
  ];

  final context = EncoderContext(msg);
  context.setSymbolShape(shape);
  context.setSizeConstraints(minSize, maxSize);

  if (msg.startsWith(macro05Header) && msg.endsWith(macroTrailer)) {
    context.writeCodeword(_macro05);
    context.skipAtEnd = 2;
    context.pos += macro05Header.length;
  } else if (msg.startsWith(macro06Header) && msg.endsWith(macroTrailer)) {
    context.writeCodeword(_macro06);
    context.skipAtEnd = 2;
    context.pos += macro06Header.length;
  }

  //Default mode
  int encodingMode = asciiEncodation;

  if (forceC40) {
    c40Encoder.encodeMaximal(context);
    encodingMode = context.newEncoding;
    context.resetEncoderSignal();
  }

  while (context.hasMoreCharacters) {
    encoders[encodingMode].encode(context);
    if (context.newEncoding >= 0) {
      encodingMode = context.newEncoding;
      context.resetEncoderSignal();
    }
  }
  final len = context.codewordCount;
  context.updateSymbolInfo();
  final capacity = context.symbolInfo!.dataCapacity;
  if (len < capacity &&
      encodingMode != asciiEncodation &&
      encodingMode != base256Encodation &&
      encodingMode != edifactEncodation) {
    context.writeCodeword('\u00fe'); //Unlatch (254)
  }
  //Padding
  final codewords = context.codewords;
  if (codewords.length < capacity) {
    codewords.writeCharCode(_pad);
  }
  while (codewords.length < capacity) {
    codewords.writeCharCode(_randomize253State(codewords.length + 1));
  }

  return context.codewords.toString();
}