def get_img_id(doc, image, bpc, nr_channels, cs, p=default_cfg()): spec = doc.image_definition() spec.dimensions(*p.img_dim) spec.bits_per_component(bpc) spec.dpi(*p.dpi) spec.color_space(cs) spec.format(jagpdf.IMAGE_FORMAT_NATIVE) img_data = g_img_cache.img_data(image, bpc, nr_channels, *p.img_dim) if p.from_file: handle, tmp_file = tempfile.mkstemp() img_data.tofile(open(tmp_file, 'wb')) os.close(handle) g_temp_files.add(tmp_file) if p.from_file: spec.file_name(tmp_file) else: spec.data(img_data) desc = "%d levels per channel (%d %s)" % (2**bpc, bpc, bpc > 1 and "bits" or "bit") if p.spec_fn: desc2 = p.spec_fn(spec) if desc2: desc = desc2 id_ = doc.image_load(spec) testlib.must_throw(doc.image_load, spec) # cannot load the same spec twice return id_, desc
def get_img_id(doc, image, bpc, nr_channels, cs, p = default_cfg()): spec = doc.image_definition() spec.dimensions(*p.img_dim) spec.bits_per_component(bpc) spec.dpi(*p.dpi) spec.color_space(cs) spec.format(jagpdf.IMAGE_FORMAT_NATIVE) img_data = g_img_cache.img_data(image, bpc, nr_channels, *p.img_dim) if p.from_file: handle, tmp_file = tempfile.mkstemp() img_data.tofile(open(tmp_file,'wb')) os.close(handle) g_temp_files.add(tmp_file) if p.from_file: spec.file_name(tmp_file) else: spec.data(img_data) desc = "%d levels per channel (%d %s)" % (2**bpc, bpc, bpc > 1 and "bits" or "bit") if p.spec_fn: desc2 = p.spec_fn(spec) if desc2: desc = desc2 id_ = doc.image_load(spec) testlib.must_throw(doc.image_load, spec) # cannot load the same spec twice return id_, desc
def test_faults(): doc = jagpdf.create_stream(testlib.NoopStreamOut()) doc.page_start(72, 72) canvas = doc.page().canvas() # cannot use simple text inside a text object canvas.text_start(0, 0) testlib.must_throw(canvas.text, 10, 10, "No!") canvas.text_end() canvas.text(10, 10, "Yes") doc.page_end() doc.finalize()
def test_main(argv=None): doc = jagpdf.create_stream(testlib.NoopStreamOut()) doc.page_start(597.6, 848.68) canvas = doc.page().canvas() # gradient = doc.function_2_load("domain=0, 1; c0=0; c1=1") p = doc.shading_pattern_load("axial; coords=0, 0, 400, 400", jagpdf.CS_DEVICE_GRAY, gradient) # cannot set pattern because the pattern color space has not been set testlib.must_throw(canvas.pattern, "f", p) # doc.page_end() doc.finalize()
def test_main(argv=None): doc = jagpdf.create_stream(testlib.NoopStreamOut(), jagpdf.create_profile()) doc.page_start(5.9*72, 3.5*72) writer = doc.page().canvas() # the following operations must fail as they require path construction # to be already started testlib.must_throw(writer.bezier_to, 1, 2, 3, 4, 5, 6) testlib.must_throw(writer.bezier_to_1st_ctrlpt, 0, 0, 0, 0) testlib.must_throw(writer.bezier_to_2nd_ctrlpt, 0, 0, 0, 0 ) testlib.must_throw(writer.line_to, 0, 0) testlib.must_throw(writer.path_close) doc.page_end() doc.finalize()
def test_main(argv=None): doc = jagpdf.create_stream(testlib.NoopStreamOut(), jagpdf.create_profile()) doc.page_start(5.9 * 72, 3.5 * 72) writer = doc.page().canvas() # the following operations must fail as they require path construction # to be already started testlib.must_throw(writer.bezier_to, 1, 2, 3, 4, 5, 6) testlib.must_throw(writer.bezier_to_1st_ctrlpt, 0, 0, 0, 0) testlib.must_throw(writer.bezier_to_2nd_ctrlpt, 0, 0, 0, 0) testlib.must_throw(writer.line_to, 0, 0) testlib.must_throw(writer.path_close) doc.page_end() doc.finalize()
def check_errors(): doc = jagpdf.create_stream(testlib.NoopStreamOut()) doc.page_start(1, 1) canvas = doc.page().canvas() colored = create_col_pattern(doc, 1) uncolored = create_uncol_pattern(doc, 1) # canvas.color_space_pattern('fs') testlib.must_throw(canvas.pattern, 'fs', colored, 1) canvas.pattern('fs', uncolored, 1) # should throw # canvas.color_space_pattern_uncolored('fs', jagpdf.CS_DEVICE_CMYK) testlib.must_throw(canvas.pattern, 'fs', uncolored) canvas.pattern('fs', colored) # should throw doc.page_end() doc.finalize()
def check_errors(): doc = jagpdf.create_stream(testlib.NoopStreamOut()) doc.page_start(1, 1) fn = function(doc, 'double_dot') sh = doc.shading_pattern_load("function; matrix_fun=100, 0, 0, 100, 50, 50", jagpdf.CS_DEVICE_GRAY, fn) canvas = doc.page().canvas() canvas.color_space_pattern('fs') testlib.must_throw(canvas.pattern, 'fs', sh, 1) canvas.color_space_pattern_uncolored('fs', jagpdf.CS_DEVICE_GRAY) canvas.pattern('fs', sh) # should throw # indexed not allowed pattern spaces indexed_cs = doc.color_space_load("adobe-rgb; palette=77, 179, 102") fn = doc.function_2_load("c0=1.0, 0.0, 0.0; c1=0.0, 0.0, 1.0") testlib.must_throw(doc.shading_pattern_load, "axial; coords=50, 50, 150, 150", indexed_cs, fn) doc.page_end() doc.finalize()
def test_main(argv=None): doc = jagpdf.create_stream(testlib.NoopStreamOut(), jagpdf.create_profile()) #doc.finalize() patt_canvas = doc.canvas_create() testlib.must_throw(doc.tiling_pattern_load, "step=0, 0", patt_canvas) # empty canvas patt_canvas.move_to(5, 5) patt_canvas.line_to(15, 15) patt_canvas.path_paint("fs") testlib.must_throw(doc.tiling_pattern_load, "matrix=1, 0, 0, 1, 0, 0", patt_canvas) # no step patt_id = doc.tiling_pattern_load("step=20, 20", patt_canvas) # use pattern doc.page_start(5.9*72, 3.5*72) writer = doc.page().canvas() writer.color_space_pattern_uncolored("f", jagpdf.CS_DEVICE_GRAY) writer.pattern("f", patt_id, .5) writer.rectangle(36, 36, 144, 144) writer.path_paint("sf") #try to write to pattern that is already registered patt_canvas.rectangle(7, 7, 6, 6) patt_canvas.path_paint("fs") writer.rectangle(36+144+4, 36, 144, 144) writer.path_paint("sf") doc.page_end() # !!!! suprising - try to write to pattern that is already outputted patt_canvas.circle(10, 10, 5) patt_canvas.path_paint("s") # !!!! this is wild -> this caused abort try: patt_canvas.paint("fs") except AttributeError: pass else: assert 0 doc.finalize()
def check_errors(doc): spec = doc.image_definition() testlib.must_throw(doc.image_load, spec) spec.data([10, 20, 30, 40]) testlib.must_throw(doc.image_load, spec) spec.dimensions(2, 2) testlib.must_throw(doc.image_load, spec) spec.bits_per_component(8) testlib.must_throw(doc.image_load, spec) spec.color_space(jagpdf.CS_DEVICE_GRAY) spec.format(jagpdf.IMAGE_FORMAT_NATIVE) doc.image_load(spec) spec1 = doc.image_definition() spec1.format(jagpdf.IMAGE_FORMAT_PNG) spec1.file_name("this_file_does_not_exist") testlib.must_throw(doc.image_load, spec1)
def check_errors(doc): spec = doc.image_definition() testlib.must_throw(doc.image_load, spec) spec.data([10,20,30,40]) testlib.must_throw(doc.image_load, spec) spec.dimensions(2, 2) testlib.must_throw(doc.image_load, spec) spec.bits_per_component(8) testlib.must_throw(doc.image_load, spec) spec.color_space(jagpdf.CS_DEVICE_GRAY) spec.format(jagpdf.IMAGE_FORMAT_NATIVE) doc.image_load(spec) spec1 = doc.image_definition() spec1.format(jagpdf.IMAGE_FORMAT_PNG) spec1.file_name("this_file_does_not_exist") testlib.must_throw(doc.image_load, spec1)
def test_main(argv=None): doc = jagpdf.create_stream(testlib.NoopStreamOut()) # function 2 doc.function_2_load("domain=0.0, 1.0; c0=1; c1=2") doc.function_2_load("domain=0.0, 1.0") f1 = doc.function_2_load("c0=11; c1=12") f2 = doc.function_2_load("exponent=2") doc.function_2_load("") doc.function_2_load("c1=12") testlib.must_throw(doc.function_2_load, "c0=11; c1=12,23") testlib.must_throw(doc.function_2_load, "c1=12,23") # function 3 doc.function_3_load("bounds=0.3; encode=0, 1, 0, 1", [f1, f2]) # function 4 doc.page_start(10, 10) doc.page_end() doc.finalize()
def test_main(argv=None): #raw_input('.......') if argv==None: argv=sys.argv out_file = os.path.abspath(os.path.join(argv[1], 'pattern-nonstd-op.pdf')) cfg = testlib.test_config() doc = jagpdf.create_stream(testlib.FileStreamOut(out_file), cfg) patt_canvas = doc.canvas_create() testlib.must_throw(doc.tiling_pattern_load, "step=0, 0", patt_canvas) # empty canvas patt_canvas.move_to(5, 5) patt_canvas.line_to(15, 15) patt_canvas.path_paint("fs") testlib.must_throw(doc.tiling_pattern_load, "matrix=1, 0, 0, 1, 0, 0", patt_canvas) # no step patt_id = doc.tiling_pattern_load("step=20, 20", patt_canvas) # use pattern doc.page_start(5.9*72, 3.5*72) writer = doc.page().canvas() writer.color_space_pattern_uncolored("f", jagpdf.CS_DEVICE_GRAY) writer.pattern("f", patt_id, .5) writer.rectangle(36, 36, 144, 144) writer.path_paint("sf") #try to write to pattern that is already registered patt_canvas.rectangle(7, 7, 6, 6) patt_canvas.path_paint("fs") writer.rectangle(36+144+4, 36, 144, 144) writer.path_paint("sf") doc.page_end() # !!!! suprising - try to write to pattern that is already outputted patt_canvas.circle(10, 10, 5) patt_canvas.path_paint("s") # !!!! this is wild -> causes abort #patt_canvas.paint("fs") doc.finalize()
def check_errors(): doc = jagpdf.create_stream(testlib.NoopStreamOut()) doc.page_start(1, 1) fn = function(doc, 'double_dot') sh = doc.shading_pattern_load( "function; matrix_fun=100, 0, 0, 100, 50, 50", jagpdf.CS_DEVICE_GRAY, fn) canvas = doc.page().canvas() canvas.color_space_pattern('fs') testlib.must_throw(canvas.pattern, 'fs', sh, 1) canvas.color_space_pattern_uncolored('fs', jagpdf.CS_DEVICE_GRAY) canvas.pattern('fs', sh) # should throw # indexed not allowed pattern spaces indexed_cs = doc.color_space_load("adobe-rgb; palette=77, 179, 102") fn = doc.function_2_load("c0=1.0, 0.0, 0.0; c1=0.0, 0.0, 1.0") testlib.must_throw(doc.shading_pattern_load, "axial; coords=50, 50, 150, 150", indexed_cs, fn) doc.page_end() doc.finalize()
def fault_injection(): doc = jagpdf.create_stream(testlib.NoopStreamOut()) outline = doc.outline() testlib.must_throw(outline.item, "Invalid bookmark") outline.state_save() doc.page_start(5.9 * 72, g_page_height) testlib.must_throw(outline.level_down) testlib.must_throw(outline.level_up) doc.page_end() outline.state_restore() testlib.must_throw(outline.state_restore) doc.finalize()
def test_main(argv=None): cfg = testlib.test_config() cfg.set('doc.version', "2") cfg.set('fonts.force_cid', "1") writer = testlib.create_test_doc(argv, "fonts_in_1_2.pdf", cfg) g_font.set_writer(writer) writer.page_start(72, 72) page = writer.page().canvas() page.text_font(g_font()) page.text(20, 20, "OK") page.text(20, 40, "1.2 - OK") fspec = os.path.expandvars('enc=ISO_8859-3:1988; size=10; file=${JAG_TEST_RESOURCES_DIR}/fonts/DejaVuSans.ttf') testlib.must_throw(writer.font_load, fspec) #no cid fonts embedding allowed in <1.3 writer.page_end() writer.finalize()
def fault_injection(): doc = jagpdf.create_stream(testlib.NoopStreamOut()) outline = doc.outline() testlib.must_throw(outline.item, "Invalid bookmark") outline.state_save() doc.page_start(5.9*72, g_page_height) testlib.must_throw(outline.level_down) testlib.must_throw(outline.level_up) doc.page_end() outline.state_restore() testlib.must_throw(outline.state_restore) doc.finalize()
def test_main(argv=None): #raw_input('.......') if argv == None: argv = sys.argv out_file = os.path.abspath(os.path.join(argv[1], 'pattern-nonstd-op.pdf')) cfg = testlib.test_config() doc = jagpdf.create_stream(testlib.FileStreamOut(out_file), cfg) patt_canvas = doc.canvas_create() testlib.must_throw(doc.tiling_pattern_load, "step=0, 0", patt_canvas) # empty canvas patt_canvas.move_to(5, 5) patt_canvas.line_to(15, 15) patt_canvas.path_paint("fs") testlib.must_throw(doc.tiling_pattern_load, "matrix=1, 0, 0, 1, 0, 0", patt_canvas) # no step patt_id = doc.tiling_pattern_load("step=20, 20", patt_canvas) # use pattern doc.page_start(5.9 * 72, 3.5 * 72) writer = doc.page().canvas() writer.color_space_pattern_uncolored("f", jagpdf.CS_DEVICE_GRAY) writer.pattern("f", patt_id, .5) writer.rectangle(36, 36, 144, 144) writer.path_paint("sf") #try to write to pattern that is already registered patt_canvas.rectangle(7, 7, 6, 6) patt_canvas.path_paint("fs") writer.rectangle(36 + 144 + 4, 36, 144, 144) writer.path_paint("sf") doc.page_end() # !!!! suprising - try to write to pattern that is already outputted patt_canvas.circle(10, 10, 5) patt_canvas.path_paint("s") # !!!! this is wild -> causes abort #patt_canvas.paint("fs") doc.finalize()
def test_main(argv=None): cfg = testlib.test_config() cfg.set('doc.version', "2") cfg.set('fonts.force_cid', "1") writer = testlib.create_test_doc(argv, "fonts_in_1_2.pdf", cfg) g_font.set_writer(writer) writer.page_start(72, 72) page = writer.page().canvas() page.text_font(g_font()) page.text(20, 20, "OK") page.text(20, 40, "1.2 - OK") fspec = os.path.expandvars( 'enc=ISO_8859-3:1988; size=10; file=${JAG_TEST_RESOURCES_DIR}/fonts/DejaVuSans.ttf' ) testlib.must_throw(writer.font_load, fspec) #no cid fonts embedding allowed in <1.3 writer.page_end() writer.finalize()
def do_invalid_destinations(argv,cfg,name): invalid_dests = [\ "zoom=1.2", "mode=nonsense", "mode=XYZ;zoom=onan", "mode=FitR;left=1;top=1;bottom=1" ] for d in invalid_dests: doc = testlib.create_test_doc(argv, name, cfg) doc.page_start(10.0*72, 10.0*72) doc.outline().item("~", d) doc.page_end() testlib.must_throw(doc.finalize) doc = None syntax_err_dests = ["oom=1.2"] doc = testlib.create_test_doc(argv, name, cfg) doc.page_start(10.0*72, 10.0*72) for d in syntax_err_dests: testlib.must_throw(doc.outline().item, "~", d) doc.page_end() doc.finalize()
def do_invalid_destinations(argv, cfg, name): invalid_dests = [\ "zoom=1.2", "mode=nonsense", "mode=XYZ;zoom=onan", "mode=FitR;left=1;top=1;bottom=1" ] for d in invalid_dests: doc = testlib.create_test_doc(argv, name, cfg) doc.page_start(10.0 * 72, 10.0 * 72) doc.outline().item("~", d) doc.page_end() testlib.must_throw(doc.finalize) doc = None syntax_err_dests = ["oom=1.2"] doc = testlib.create_test_doc(argv, name, cfg) doc.page_start(10.0 * 72, 10.0 * 72) for d in syntax_err_dests: testlib.must_throw(doc.outline().item, "~", d) doc.page_end() doc.finalize()
def test_main(argv=None): doc = jagpdf.create_stream(testlib.NoopStreamOut()) doc.page_start(*dim) canvas = doc.page().canvas() testlib.must_throw(canvas.line_to, 20, 20) testlib.must_throw(canvas.path_paint, 'f') canvas.text_start(0, 0) testlib.must_throw(canvas.text, 10, 10, "Not allowed here!") canvas.text("This is OK") canvas.text_end() doc.page_end() doc.finalize()
def test_main(argv=None): # # PageLayout # # TwoPageLeft and TwoPageRight not supported now for val in [ 'SinglePage', # single page 'OneColumn', # continuous 'TwoColumnLeft', # continuous facing 'TwoColumnRight' ]: # continuous facing (requires numbering) creator = DocFromFile(argv, 'doc.page_layout', val) do_document(creator) testlib.must_throw(do_document, DocNoopStream('doc.page_layout', 'InvalidValue')) # # PageMode # for val in ['UseNone', 'UseOutlines', 'UseThumbs', 'FullScreen']: creator = DocFromFile(argv, 'doc.page_mode', val) do_document(creator) testlib.must_throw(do_document, DocNoopStream('doc.page_mode', 'InvalidValue')) # # InitialDestination # creator = DocFromFile(argv, "doc.initial_destination", "mode=XYZ; zoom=1.5", "") do_document(creator) creator = DocFromFile(argv, "doc.initial_destination", "page=4; mode=XYZ", "_1") do_document(creator) testlib.must_throw( do_document, DocNoopStream("doc.initial_destination", "page=5; mode=XYZ")) # # ViewePreferences # for val in [ 'HideToolbar', 'HideMenubar', 'HideWindowUI', 'FitWindow', 'CenterWindow', 'DisplayDocTitle' ]: creator = DocFromFile(argv, "doc.viewer_preferences", val) do_document(creator) creator = DocFromFile(argv, "doc.viewer_preferences", "HideToolbar; CenterWindow; DisplayDocTitle", "_combined") do_document(creator)
def test_main(argv=None): # # PageLayout # # TwoPageLeft and TwoPageRight not supported now for val in ['SinglePage', # single page 'OneColumn', # continuous 'TwoColumnLeft', # continuous facing 'TwoColumnRight']: # continuous facing (requires numbering) creator = DocFromFile(argv, 'doc.page_layout', val) do_document(creator) testlib.must_throw(do_document, DocNoopStream('doc.page_layout', 'InvalidValue')) # # PageMode # for val in ['UseNone', 'UseOutlines', 'UseThumbs', 'FullScreen']: creator = DocFromFile(argv, 'doc.page_mode', val) do_document(creator) testlib.must_throw(do_document, DocNoopStream('doc.page_mode', 'InvalidValue')) # # InitialDestination # creator = DocFromFile(argv, "doc.initial_destination", "mode=XYZ; zoom=1.5", "") do_document(creator) creator = DocFromFile(argv, "doc.initial_destination", "page=4; mode=XYZ", "_1") do_document(creator) testlib.must_throw( do_document, DocNoopStream("doc.initial_destination", "page=5; mode=XYZ")) # # ViewePreferences # for val in ['HideToolbar', 'HideMenubar', 'HideWindowUI', 'FitWindow', 'CenterWindow', 'DisplayDocTitle']: creator = DocFromFile(argv, "doc.viewer_preferences", val) do_document(creator) creator = DocFromFile(argv, "doc.viewer_preferences", "HideToolbar; CenterWindow; DisplayDocTitle", "_combined") do_document(creator)
def test_errors(self): spec = self.doc.define_image_mask() testlib.must_throw(self.doc.register_image_mask, spec) spec.dimensions(*self.dim) testlib.must_throw(self.doc.register_image_mask, spec)
def test_main(argv=None): test_faults() # 'save state' and 'restore state' operators should not be used within # text objects. Regarding the graphics state, only 'General graphics # state' operators are allowed. doc = testlib.create_test_doc(argv, "textstate.pdf") #doc = testlib.create_test_doc("/mnt/win/c/Temp/textstate.pdf") fnt = testlib.EasyFont(doc) paper = testlib.paperA5 doc.page_start(*paper) canvas = doc.page().canvas() fid = fnt(32) canvas.text_font(fid) linespacing = fid.height() canvas.text_start(18, 36) def show_text_ex(canvas, text, action, *args): action(*args) canvas.text(text) canvas.text_translate_line(0, linespacing) #char spacing canvas.text("Character spacing normal") canvas.text_translate_line(0, linespacing) show_text_ex(canvas, "Character spacing: 1", canvas.text_character_spacing, 1) canvas.text_character_spacing(0) #horizontal scaling canvas.text("Horizontal scaling 100%") canvas.text_translate_line(0, linespacing) show_text_ex(canvas, "Horizontal scaling 50%", canvas.text_horizontal_scaling, 50) show_text_ex(canvas, "Horizontal scaling 110%", canvas.text_horizontal_scaling, 110) canvas.text_horizontal_scaling(100) # rise canvas.text_translate_line(0, linespacing) canvas.text("Rise ") canvas.text_rise(10) canvas.text("10") canvas.text_rise(-10) canvas.text("-10") canvas.text_end() doc.page_end() # text rendering modes doc.page_start(*paper) canvas = doc.page().canvas() canvas.color_space("s", jagpdf.CS_DEVICE_RGB) canvas.color("s", 0.0, 0.0, 1.0) canvas.translate(18, 36) canvas.text_font(fid) def clipped_obj(canvas): canvas.state_save() canvas.color_space("s", jagpdf.CS_DEVICE_RGB) canvas.color("s", 1.0, 0.0, 0.0) for i in range(1, int(paper[1]), 3): canvas.move_to(0, i) canvas.line_to(paper[0], i) canvas.path_paint("s") canvas.state_restore() modes = [("f", "fill", None), ("fs", "fill stroke", None), ("s", "stroke", None), ("i", "invisible", None), ("fc", "fill clip", clipped_obj), ("fsc", "fill stroke clip", clipped_obj), ("c", "clip", clipped_obj), ("sc", "stroke clip", clipped_obj)] curr_y = 0 for mode, text, action in modes: canvas.state_save() canvas.text_start(0, curr_y) canvas.text_rendering_mode(mode) canvas.text(text) canvas.text_end() canvas.translate(0, -curr_y) if action: clipped_obj(canvas) canvas.state_restore() curr_y += linespacing # other tests and fault injection testlib.must_throw(canvas.text_rendering_mode, "a") testlib.must_throw(canvas.text_rendering_mode, "ic") canvas.text_rendering_mode("fFsScC") canvas.text_rendering_mode("iI") doc.page_end() doc.finalize()
def bad_spec(str_): testlib.must_throw(writer.color_space_load, str_)
def test_corrupted_files(writer): for png in glob.glob(g_png_dir + 'X*.png'): testlib.must_throw(writer.image_load_file, png, jagpdf.IMAGE_FORMAT_PNG)