/*
 * Copyright 2006 Jeremias Maerki.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.krysalis.barcode4j.impl.datamatrix;

import org.krysalis.barcode4j.TwoDimBarcodeLogicHandler;

/**
 * Top-level class for the logic part of the DataMatrix implementation.
 * 
 * @version $Id$
 */
public class DataMatrixLogicImpl {

    /**
     * Convert a string of char codewords into a different string which lists each character 
     * using its decimal value.
     * @param codewords the codewords 
     * @return the visualized codewords
     */
    public static String visualize(String codewords) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < codewords.length(); i++) {
            if (i > 0) {
                sb.append(" ");
            }
            sb.append((int)codewords.charAt(i));
        }
        return sb.toString();
    }
    
    /**
     * Generates the barcode logic.
     * @param logic the logic handler to receive generated events
     * @param msg the message to encode
     * @param cols the number of columns
     */
    public void generateBarcodeLogic(TwoDimBarcodeLogicHandler logic, String msg) {

        //ECC 200
        //1. step: Data encodation
        String encoded = DataMatrixHighLevelEncoder.encodeHighLevel(msg);
        
        DataMatrixSymbolInfo symbolInfo = DataMatrixSymbolInfo.lookup(encoded.length());
        /*
        StringBuffer codewords = new StringBuffer(symbolInfo.getCodewordCount());
        codewords.append(encoded);
        if (codewords.length() < symbolInfo.dataCapacity) {
            codewords.append(DataMatrixConstants.PAD);
        }
        while (codewords.length() < symbolInfo.dataCapacity) {
            codewords.append(randomizedPad(DataMatrixConstants.PAD, codewords.length() + 1));
        }*/
        //TODO PADDING! Padding correct?
        
        //2. step: ECC generation
        StringBuffer codewords = new StringBuffer(symbolInfo.getCodewordCount());
        String ecc = DataMatrixErrorCorrection.encodeECC200(
                encoded, symbolInfo.errorCodewords);
        codewords.append(encoded);
        codewords.append(ecc);

        //3. step: Module placement in Matrix
        DefaultDataMatrixPlacement placement = new DefaultDataMatrixPlacement(
                    codewords.toString(), symbolInfo.matrixWidth, symbolInfo.matrixHeight);
        placement.place();
        
        //4. step: low-level encoding
        logic.startBarcode(msg, msg);
        encodeLowLevel(logic, placement, symbolInfo);
        logic.endBarcode();
    }

    private void encodeLowLevel(TwoDimBarcodeLogicHandler logic, 
            DataMatrixPlacement placement, DataMatrixSymbolInfo symbolInfo) {
        if (symbolInfo.dataRegions > 1) {
            throw new UnsupportedOperationException("Handling of multiple data regions NYI");
        }
        logic.startRow();
        for (int x = 0; x < symbolInfo.matrixWidth + 2; x++) {
            logic.addBar((x % 2) == 0, 1);
        }
        logic.endRow();
        for (int y = 0; y < symbolInfo.matrixHeight; y++) {
            logic.startRow();
            logic.addBar(true, 1); //left finder edge
            for (int x = 0; x < symbolInfo.matrixWidth; x++) {
                logic.addBar(placement.getBit(x, y), 1);
            }
            logic.addBar((y % 2) == 0, 1); //right finder edge
            logic.endRow();
        }
        logic.startRow();
        for (int x = 0; x < symbolInfo.matrixWidth + 2; x++) {
            logic.addBar(true, 1);
        }
        logic.endRow();
    }
    
}

@@ -855,556 +855,8 @@
  %7B%0A%0A
-    /**%0A     * Convert a string of char codewords into a different string which lists each character %0A     * using its decimal value.%0A     * @param codewords the codewords %0A     * @return the visualized codewords%0A     */%0A    public static String visualize(String codewords) %7B%0A        StringBuffer sb = new StringBuffer();%0A        for (int i = 0; i %3C codewords.length(); i++) %7B%0A            if (i %3E 0) %7B%0A                sb.append(%22 %22);%0A            %7D%0A            sb.append((int)codewords.charAt(i));%0A        %7D%0A        return sb.toString();%0A    %7D%0A    %0A
     
@@ -1364,471 +1364,8 @@
 ));%0A
-        /*%0A        StringBuffer codewords = new StringBuffer(symbolInfo.getCodewordCount());%0A        codewords.append(encoded);%0A        if (codewords.length() %3C symbolInfo.dataCapacity) %7B%0A            codewords.append(DataMatrixConstants.PAD);%0A        %7D%0A        while (codewords.length() %3C symbolInfo.dataCapacity) %7B%0A            codewords.append(randomizedPad(DataMatrixConstants.PAD, codewords.length() + 1));%0A        %7D*/%0A        //TODO PADDING! Padding correct?%0A
     

Remove obsolete code