<commit_before># -*- coding: utf-8 -*-

""" Sahana Optical Character Recognision Utility (s3ocr)

    @author: <NAME> <<EMAIL>[at]gmail.com>
    @author: <NAME> <idlecool[at]gmail.com>

    @copyright: 2009-2011 (c) Sahana Software Foundation
    @license: MIT

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

"""

#========================== import section ================================

__all__ = ["s3ocr_generate_pdf", "s3ocr_get_languages"]

#Generic stuff
import os
import sys
import uuid
from StringIO import StringIO


#Importing the xml stuff
from xml.sax.handler import ContentHandler
from xml.sax import make_parser
from xml.dom.minidom import Document


#Importing reportlab stuff
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4
from reportlab.graphics.barcode import code128


# Fonts
Courier = "Courier"
Helvetica = "Helvetica"
Helvetica_Bold = "Helvetica-Bold"
Helvetica_Bold_Oblique = "Helvetica-BoldOblique"
Helvetica_Oblique = "Helvetica-Oblique"

#==========================================================================
#=============== internal Class Definations and functions =================
#==========================================================================

#======================== pdf layout from xform ===========================

class Form:
    """ Form class to use reportlab to generate pdf """

    def __init__(self, pdfname="ocrform.pdf", margintop=50, marginsides=50, **kw):
	""" Form initialization """
        self.pdfpath = kw.get("pdfpath", pdfname)
        self.verbose = kw.get("verbose", 0)
        self.font = kw.get("typeface", Courier)
        self.fontsize = kw.get("fontsize", 13)
        self.IObuffer = StringIO()
        self.canvas = Canvas(self.IObuffer, pagesize = A4)
	self.width, self.height = A4
	self.x = marginsides
	self.lastx = marginsides
	self.marginsides = marginsides
	self.margintop = margintop
	self.y = self.height - margintop
        self.lasty = self.height - margintop
        self.num = 1

    def barcode(self, uuid):
        """ Generate barcode of uuid """
        barcode=code128.Code128(str(uuid), barWidth=1, barHeight=20)
        barcode.drawOn(self.canvas, self.lastx, self.lasty)
        self.lasty = self.lasty - 20
        self.y = self.lasty

    def decorate(self):
        """ Decorates the the form with the markers needed to align the form later """
        c = self.canvas
        c.rect(20, 20, 20, 20, fill = 1)
        c.rect(self.width - 40, 20, 20, 20, fill = 1)
        c.rect(20, self.height - 40, 20, 20, fill = 1)
        c.rect(self.width/2 - 10, 20, 20, 20, fill = 1)
        c.rect(20, self.height/2 - 10, 20, 20, fill = 1)
        c.rect(self.width - 40, self.height - 40, 20, 20, fill = 1)
        c.rect(self.width - 40, self.height/2 - 10, 20, 20, fill = 1)

    def print_text(self, lines, fontsize=12, gray=0, seek=0, continuetext=0, style="default"):
	""" Give the lines to be printed as a list, set the font and grey level """
        c = self.canvas
        self.fontsize = fontsize
        if style == "center":
            self.x = self.width/2
	if seek > (self.width-(self.marginsides+self.fontsize)):
            seek = 0
	if seek != 0:
            self.x = self.x + seek
        if continuetext == 1:
            self.x = self.lastx + seek
            if seek == 0:
                self.y = self.y + fontsize
        for line in lines:
            if style == "center":
                self.x = self.x - (len(line))*self.fontsize/2
            if style == "right":
                self.x = self.width - (self.marginsides + len(line)*self.fontsize)
            if (self.width - self.marginsides - self.lastx) < 200:
                self.x = self.marginsides
                if continuetext == 1:
                    self.y = self.y - 2*fontsize
            if (self.y - self.fontsize) < 50:
                self.set_new_page()
            t = c.beginText(self.x, self.y)
            t.setFont(Helvetica, fontsize)
            t.setFillGray(gray)
            t.textOut(line)
            c.drawText(t)
	    self.y = self.y - fontsize
	    self.lastx = t.getX()
            self.lasty = self.y
        self.x = self.marginsides

    def draw_check_boxes(self, boxes=1, completeline=0, lines=0, seek=0, continuetext=0, fontsize=0, gray=0, style="", isdate=0):
	""" Function to draw check boxes default no of boxes = 1 """
        c = self.canvas
        c.setLineWidth(0.90)
	c.setStrokeGray(gray)
        if style == "center":
            self.x = self.width/2
	elif style == "right":
            self.x = self.width - self.marginsides - self.fontsize
	if seek > (self.width - (self.marginsides + self.fontsize)):
            seek = 0
        if (self.y - self.fontsize) < 40:
            self.set_new_page()
        if continuetext == 1:
            self.y = self.y + self.fontsize
            self.x = self.lastx
        else:
            self.x = self.marginsides
	if seek != 0:
            self.x = self.x + seek
        if fontsize == 0:
            fontsize = self.fontsize
	else:
            self.fontsize = fontsize
        if completeline == 1:
            boxes = int(self.width/self.fontsize)
	for i in range(boxes):
            c.rect(self.x, self.y, self.fontsize, self.fontsize)
            self.x = self.x + self.fontsize
            if self.x > (self.width-(self.marginsides+self.fontsize)):
                break
        self.lastx = self.x
        self.x = self.marginsides
	self.y = self.y - self.fontsize
        if isdate:
            t = c.beginText(self.x, self.y)
            t.setFont(Helvetica, 13)
            t.setFillGray(gray)
            t.textOut("   D  D  M  M  Y  Y  Y  Y")
            c.drawText(t)
	    self.y = self.y - fontsize
	    self.lastx = t.getX()
            self.lasty = self.y
        self.lastx = self.x
        self.x = self.marginsides<commit_msg>make date helper more visible<commit_after># -*- coding: utf-8 -*-

""" Sahana Optical Character Recognision Utility (s3ocr)

    @author: <NAME> <<EMAIL>[at]gmail.com>
    @author: <NAME> <<EMAIL>cool[at]gmail.<EMAIL>>

    @copyright: 2009-2011 (c) Sahana Software Foundation
    @license: MIT

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

"""

#========================== import section ================================

__all__ = ["s3ocr_generate_pdf", "s3ocr_get_languages"]

#Generic stuff
import os
import sys
import uuid
from StringIO import StringIO


#Importing the xml stuff
from xml.sax.handler import ContentHandler
from xml.sax import make_parser
from xml.dom.minidom import Document


#Importing reportlab stuff
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4
from reportlab.graphics.barcode import code128


# Fonts
Courier = "Courier"
Helvetica = "Helvetica"
Helvetica_Bold = "Helvetica-Bold"
Helvetica_Bold_Oblique = "Helvetica-BoldOblique"
Helvetica_Oblique = "Helvetica-Oblique"

#==========================================================================
#=============== internal Class Definations and functions =================
#==========================================================================

#======================== pdf layout from xform ===========================

class Form:
    """ Form class to use reportlab to generate pdf """

    def __init__(self, pdfname="ocrform.pdf", margintop=50, marginsides=50, **kw):
	""" Form initialization """
        self.pdfpath = kw.get("pdfpath", pdfname)
        self.verbose = kw.get("verbose", 0)
        self.font = kw.get("typeface", Courier)
        self.fontsize = kw.get("fontsize", 13)
        self.IObuffer = StringIO()
        self.canvas = Canvas(self.IObuffer, pagesize = A4)
	self.width, self.height = A4
	self.x = marginsides
	self.lastx = marginsides
	self.marginsides = marginsides
	self.margintop = margintop
	self.y = self.height - margintop
        self.lasty = self.height - margintop
        self.num = 1

    def barcode(self, uuid):
        """ Generate barcode of uuid """
        barcode=code128.Code128(str(uuid), barWidth=1, barHeight=20)
        barcode.drawOn(self.canvas, self.lastx, self.lasty)
        self.lasty = self.lasty - 20
        self.y = self.lasty

    def decorate(self):
        """ Decorates the the form with the markers needed to align the form later """
        c = self.canvas
        c.rect(20, 20, 20, 20, fill = 1)
        c.rect(self.width - 40, 20, 20, 20, fill = 1)
        c.rect(20, self.height - 40, 20, 20, fill = 1)
        c.rect(self.width/2 - 10, 20, 20, 20, fill = 1)
        c.rect(20, self.height/2 - 10, 20, 20, fill = 1)
        c.rect(self.width - 40, self.height - 40, 20, 20, fill = 1)
        c.rect(self.width - 40, self.height/2 - 10, 20, 20, fill = 1)

    def print_text(self, lines, fontsize=12, gray=0, seek=0, continuetext=0, style="default"):
	""" Give the lines to be printed as a list, set the font and grey level """
        c = self.canvas
        self.fontsize = fontsize
        if style == "center":
            self.x = self.width/2
	if seek > (self.width-(self.marginsides+self.fontsize)):
            seek = 0
	if seek != 0:
            self.x = self.x + seek
        if continuetext == 1:
            self.x = self.lastx + seek
            if seek == 0:
                self.y = self.y + fontsize
        for line in lines:
            if style == "center":
                self.x = self.x - (len(line))*self.fontsize/2
            if style == "right":
                self.x = self.width - (self.marginsides + len(line)*self.fontsize)
            if (self.width - self.marginsides - self.lastx) < 200:
                self.x = self.marginsides
                if continuetext == 1:
                    self.y = self.y - 2*fontsize
            if (self.y - self.fontsize) < 50:
                self.set_new_page()
            t = c.beginText(self.x, self.y)
            t.setFont(Helvetica, fontsize)
            t.setFillGray(gray)
            t.textOut(line)
            c.drawText(t)
	    self.y = self.y - fontsize
	    self.lastx = t.getX()
            self.lasty = self.y
        self.x = self.marginsides

    def draw_check_boxes(self, boxes=1, completeline=0, lines=0, seek=0, continuetext=0, fontsize=0, gray=0, style="", isdate=0):
	""" Function to draw check boxes default no of boxes = 1 """
        c = self.canvas
        c.setLineWidth(0.90)
	c.setStrokeGray(gray)
        if style == "center":
            self.x = self.width/2
	elif style == "right":
            self.x = self.width - self.marginsides - self.fontsize
	if seek > (self.width - (self.marginsides + self.fontsize)):
            seek = 0
        if (self.y - self.fontsize) < 40:
            self.set_new_page()
        if continuetext == 1:
            self.y = self.y + self.fontsize
            self.x = self.lastx
        else:
            self.x = self.marginsides
	if seek != 0:
            self.x = self.x + seek
        if fontsize == 0:
            fontsize = self.fontsize
	else:
            self.fontsize = fontsize
        if completeline == 1:
            boxes = int(self.width/self.fontsize)
	for i in range(boxes):
            c.rect(self.x, self.y, self.fontsize, self.fontsize)
            self.x = self.x + self.fontsize
            if self.x > (self.width-(self.marginsides+self.fontsize)):
                break
        self.lastx = self.x
        self.x = self.marginsides
	self.y = self.y - self.fontsize
        if isdate:
            t = c.beginText(self.x, self.y)
            t.setFont(Helvetica, 13)
            t.setFillGray(0)
            t.textOut("   D  D  M  M  Y  Y  Y  Y")
            c.drawText(t)
	    self.y = self.y - fontsize
	    self.lastx = t.getX()
            self.lasty = self.y
        self.lastx = self.x
        self.x = self.marginsides
{'fraction_non_alphanumeric': 0.14488143435511858, 'fraction_numerical': 0.020748987854251014, 'mean_word_length': 2.363238512035011, 'pattern_counts': {'":': 10, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4583261', 'n_tokens_mistral': 4552, 'n_tokens_neox': 4214, 'n_words': 1263}