Example #1
0
 public Title SetOffset(float offset)
 {
     text.SetLocation(text.x + offset, text.y);
     return(this);
 }
Example #2
0
 public Title SetLocation(float x, float y)
 {
     prefix.SetLocation(x, y);
     textLine.SetPosition(x, y);
     return(this);
 }
Example #3
0
        public TextLine DrawTextLine(
            Page page, float x_text, float y_text, TextLine textLine, bool draw)
        {
            TextLine textLine2    = null;
            Font     font         = textLine.GetFont();
            Font     fallbackFont = textLine.GetFallbackFont();
            int      color        = textLine.GetColor();

            StringBuilder buf = new StringBuilder();

            String[] tokens           = Regex.Split(textLine.GetText(), @"\s+");
            bool     firstTextSegment = true;

            for (int i = 0; i < tokens.Length; i++)
            {
                String token = (i == 0) ? tokens[i] : (Single.space + tokens[i]);
                if (font.StringWidth(fallbackFont, token) < (this.w - (x_text - x)))
                {
                    buf.Append(token);
                    x_text += font.StringWidth(fallbackFont, token);
                }
                else
                {
                    if (draw)
                    {
                        new TextLine(font, buf.ToString())
                        .SetFallbackFont(textLine.GetFallbackFont())
                        .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()),
                                     y_text + textLine.GetVerticalOffset())
                        .SetColor(color)
                        .SetUnderline(textLine.GetUnderline())
                        .SetStrikeout(textLine.GetStrikeout())
                        .SetLanguage(textLine.GetLanguage())
                        .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                        .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                        .DrawOn(page);
                        firstTextSegment = false;
                    }
                    x_text     = x + font.StringWidth(fallbackFont, tokens[i]);
                    y_text    += leading;
                    buf.Length = 0;
                    buf.Append(tokens[i]);

                    if (y_text + font.GetDescent() > (y + h))
                    {
                        i++;
                        while (i < tokens.Length)
                        {
                            buf.Append(Single.space);
                            buf.Append(tokens[i]);
                            i++;
                        }
                        textLine2 = new TextLine(font, buf.ToString());
                        textLine2.SetLocation(x, y_text);
                        return(textLine2);
                    }
                }
            }
            if (draw)
            {
                new TextLine(font, buf.ToString())
                .SetFallbackFont(textLine.GetFallbackFont())
                .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()),
                             y_text + textLine.GetVerticalOffset())
                .SetColor(color)
                .SetUnderline(textLine.GetUnderline())
                .SetStrikeout(textLine.GetStrikeout())
                .SetLanguage(textLine.GetLanguage())
                .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                .DrawOn(page);
                firstTextSegment = false;
            }

            textLine2 = new TextLine(font, "");
            textLine2.SetLocation(x_text, y_text);
            return(textLine2);
        }
Example #4
0
        private float[] DrawCode39(Page page, float x1, float y1)
        {
            text = "*" + text + "*";

            float x = x1;
            float y = y1;
            float w = m1 * barHeightFactor; // Barcode width when drawn vertically
            float h = m1 * barHeightFactor; // Barcode height when drawn horizontally

            float[] xy = new float[] { 0f, 0f };

            if (direction == LEFT_TO_RIGHT)
            {
                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    if (code == null)
                    {
                        throw new Exception("The input string '" + text +
                                            "' contains characters that are invalid in a Code39 barcode.");
                    }

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w')
                        {
                            x += m1;
                        }
                        else if (ch == 'W')
                        {
                            x += m1 * 3;
                        }
                        else if (ch == 'b')
                        {
                            DrawVertBar(page, x, y, m1, h);
                            x += m1;
                        }
                        else if (ch == 'B')
                        {
                            DrawVertBar(page, x, y, m1 * 3, h);
                            x += m1 * 3;
                        }
                    }

                    x += m1;
                }

                if (font != null)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x1 + ((x - x1) - font.StringWidth(text)) / 2,
                        y1 + h + font.bodyHeight);
                    xy    = textLine.DrawOn(page);
                    xy[0] = Math.Max(x, xy[0]);
                }
            }
            else if (direction == TOP_TO_BOTTOM)
            {
                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    if (code == null)
                    {
                        throw new Exception("The input string '" + text +
                                            "' contains characters that are invalid in a Code39 barcode.");
                    }

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w')
                        {
                            y += m1;
                        }
                        else if (ch == 'W')
                        {
                            y += 3 * m1;
                        }
                        else if (ch == 'b')
                        {
                            DrawHorzBar(page, x, y, m1, h);
                            y += m1;
                        }
                        else if (ch == 'B')
                        {
                            DrawHorzBar(page, x, y, 3 * m1, h);
                            y += 3 * m1;
                        }
                    }

                    y += m1;
                }

                if (font != null)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x - font.bodyHeight,
                        y1 + ((y - y1) - font.StringWidth(text)) / 2);
                    textLine.SetTextDirection(270);
                    xy    = textLine.DrawOn(page);
                    xy[0] = Math.Max(x, xy[0]) + w;
                    xy[1] = Math.Max(y, xy[1]);
                }
            }
            else if (direction == BOTTOM_TO_TOP)
            {
                float height = 0.0f;

                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    if (code == null)
                    {
                        throw new Exception("The input string '" + text +
                                            "' contains characters that are invalid in a Code39 barcode.");
                    }

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w' || ch == 'b')
                        {
                            height += m1;
                        }
                        else if (ch == 'W' || ch == 'B')
                        {
                            height += 3 * m1;
                        }
                    }

                    height += m1;
                }

                y += height - m1;

                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w')
                        {
                            y -= m1;
                        }
                        else if (ch == 'W')
                        {
                            y -= 3 * m1;
                        }
                        else if (ch == 'b')
                        {
                            DrawHorzBar2(page, x, y, m1, h);
                            y -= m1;
                        }
                        else if (ch == 'B')
                        {
                            DrawHorzBar2(page, x, y, 3 * m1, h);
                            y -= 3 * m1;
                        }
                    }

                    y -= m1;
                }

                if (font != null)
                {
                    y = y1 + (height - m1);

                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x + w + font.bodyHeight,
                        y - ((y - y1) - font.StringWidth(text)) / 2);
                    textLine.SetTextDirection(90);
                    xy    = textLine.DrawOn(page);
                    xy[1] = Math.Max(y, xy[1]);
                    return(new float[] { xy[0], xy[1] + font.descent });
                }
            }

            return(new float[] { xy[0], xy[1] });
        }
Example #5
0
        private float[] DrawCode128(Page page, float x1, float y1)
        {
            float x = x1;
            float y = y1;
            float w = m1;
            float h = m1;

            if (direction == TOP_TO_BOTTOM)
            {
                w *= barHeightFactor;
            }
            else if (direction == LEFT_TO_RIGHT)
            {
                h *= barHeightFactor;
            }

            List <Int32> list = new List <Int32>();

            for (int i = 0; i < text.Length; i++)
            {
                char symchar = text[i];
                if (symchar < 32)
                {
                    list.Add(GS1_128.SHIFT);
                    list.Add(symchar + 64);
                }
                else if (symchar < 128)
                {
                    list.Add(symchar - 32);
                }
                else if (symchar < 256)
                {
                    list.Add(GS1_128.FNC_4);
                    list.Add(symchar - 160); // 128 + 32
                }
                else
                {
                    // list.Add(31);            // '?'
                    list.Add(256);          // This will generate an exception.
                }
                if (list.Count == 48)
                {
                    // Maximum number of data characters is 48
                    break;
                }
            }

            StringBuilder buf        = new StringBuilder();
            int           checkDigit = GS1_128.START_B;

            buf.Append((char)checkDigit);
            for (int i = 0; i < list.Count; i++)
            {
                int codeword = list[i];
                buf.Append((char)codeword);
                checkDigit += codeword * (i + 1);
            }
            checkDigit %= GS1_128.START_A;
            buf.Append((char)checkDigit);
            buf.Append((char)GS1_128.STOP);

            for (int i = 0; i < buf.Length; i++)
            {
                int    si     = buf[i];
                String symbol = GS1_128.TABLE[si].ToString();
                for (int j = 0; j < symbol.Length; j++)
                {
                    int n = symbol[j] - 0x30;
                    if (j % 2 == 0)
                    {
                        if (direction == LEFT_TO_RIGHT)
                        {
                            DrawVertBar(page, x, y, m1 * n, h);
                        }
                        else if (direction == TOP_TO_BOTTOM)
                        {
                            DrawHorzBar(page, x, y, m1 * n, w);
                        }
                    }
                    if (direction == LEFT_TO_RIGHT)
                    {
                        x += n * m1;
                    }
                    else if (direction == TOP_TO_BOTTOM)
                    {
                        y += n * m1;
                    }
                }
            }

            float[] xy = new float[] { x, y };
            if (font != null)
            {
                if (direction == LEFT_TO_RIGHT)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x1 + ((x - x1) - font.StringWidth(text)) / 2,
                        y1 + h + font.bodyHeight);
                    xy    = textLine.DrawOn(page);
                    xy[0] = Math.Max(x, xy[0]);
                    return(new float[] { xy[0], xy[1] + font.descent });
                }
                else if (direction == TOP_TO_BOTTOM)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x + w + font.bodyHeight,
                        y - ((y - y1) - font.StringWidth(text)) / 2);
                    textLine.SetTextDirection(90);
                    xy    = textLine.DrawOn(page);
                    xy[1] = Math.Max(y, xy[1]);
                }
            }

            return(xy);
        }
Example #6
0
        private float[] DrawCodeUPC(Page page, float x1, float y1)
        {
            float x = x1;
            float y = y1;
            float h = m1 * barHeightFactor; // Barcode height when drawn horizontally

            // Calculate the check digit:
            // 1. Add the digits in the odd-numbered positions (first, third, fifth, etc.)
            // together and multiply by three.
            // 2. Add the digits in the even-numbered positions (second, fourth, sixth, etc.)
            // to the result.
            // 3. Subtract the result modulo 10 from ten.
            // 4. The answer modulo 10 is the check digit.
            int sum = 0;

            for (int i = 0; i < 11; i += 2)
            {
                sum += text[i] - 48;
            }
            sum *= 3;
            for (int i = 1; i < 11; i += 2)
            {
                sum += text[i] - 48;
            }
            int reminder   = sum % 10;
            int checkDigit = (10 - reminder) % 10;

            text += checkDigit.ToString();

            x = DrawEGuard(page, x, y, m1, h + 8);
            for (int i = 0; i < 6; i++)
            {
                int digit = text[i] - 0x30;
                // page.DrawString(digit.ToString(), x + 1, y + h + 12);
                String symbol = tableA[digit].ToString();
                for (int j = 0; j < symbol.Length; j++)
                {
                    int n = symbol[j] - 0x30;
                    if (j % 2 != 0)
                    {
                        DrawVertBar(page, x, y, n * m1, h);
                    }
                    x += n * m1;
                }
            }
            x = DrawMGuard(page, x, y, m1, h + 8);
            for (int i = 6; i < 12; i++)
            {
                int digit = text[i] - 0x30;
                // page.DrawString(digit.ToString(), x + 1, y + h + 12);
                String symbol = tableA[digit].ToString();
                for (int j = 0; j < symbol.Length; j++)
                {
                    int n = symbol[j] - 0x30;
                    if (j % 2 == 0)
                    {
                        DrawVertBar(page, x, y, n * m1, h);
                    }
                    x += n * m1;
                }
            }
            x = DrawEGuard(page, x, y, m1, h + 8);

            float[] xy = new float[] { x, y };
            if (font != null)
            {
                String label =
                    text[0] +
                    "  " +
                    text[1] +
                    text[2] +
                    text[3] +
                    text[4] +
                    text[5] +
                    "   " +
                    text[6] +
                    text[7] +
                    text[8] +
                    text[9] +
                    text[10] +
                    "  " +
                    text[11];
                float fontSize = font.GetSize();
                font.SetSize(10f);

                TextLine textLine = new TextLine(font, label);
                textLine.SetLocation(
                    x1 + ((x - x1) - font.StringWidth(label)) / 2,
                    y1 + h + font.bodyHeight);
                xy    = textLine.DrawOn(page);
                xy[0] = Math.Max(x, xy[0]);
                xy[1] = Math.Max(y, xy[1]);

                font.SetSize(fontSize);
                return(new float[] { xy[0], xy[1] + font.descent });
            }

            return(new float[] { xy[0], xy[1] });
        }