Skip to content

Conversation

Flamenco
Copy link
Contributor

see documentation: https://github.com/Flamenco/jsPDF/wiki/c2d-Plugin
see test page at: http://htmlpreview.github.io/?https://github.com/Flamenco/jsPDF/blob/c2d/test/test_context2d.html

Notable new features include arcs, stroked text, text baselines, and context stacks. Also css color names.

I feel that by standardizing the API via this interface, more users will be able to understand the usage, convert existing HTML5 canvas code to vector based PDFs, and save time by not requiring the developers to write documentation :)

This is far from complete, but still quite useful.

jspdf.js Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? I mean, perhaps strokeOption = '' is enough?

Well, if it's to clear a previously set stroke state, i'd use an internal flag and only set 0 tr when flags.stroke is no longer set (?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't reset the Tr after setting it to 1, it persists for the rest of the page. So we would have to track it per page.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking on something like this:

var stroke_state = false;
API.text = function(...) {
  ...
  var strokeOption = '';
  if(!stroke_state && flags.stroke) strokeOption = '1 tr';
  else if(stroke_state && !flags.stroke) strokeOption = '0 tr';
  stroke_state = !!flags.stroke;
  ...
}

That way it'd persist as long flags.stroke is set only, avoiding the need to define the stroke with every .text() call. (that will save some bytes, specially if the function is used tons of times)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works for me; Should we store the state in a page/path object instead of main pdf object?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the stroke can persist across pages, then my code should be fine, otherwise we will need to store it per-page indeed.

@Flamenco
Copy link
Contributor Author

goto http://htmlpreview.github.io/?https://github.com/Flamenco/jsPDF/blob/c2d/test/test_context2d.html?src=true
and search for 1 Tr. You will see 2 stroked text blocks, then reset to 0 Tr. No wasted bytes...

suggested changes
annotation plugin should use current page (not last page)
move c2d context object into plugin scope
added missing comma in object definition
added c2d test cases
@diegocr
Copy link
Contributor

diegocr commented Nov 28, 2014

Thanks a lot!!

Btw, please note lastTextWasStroke !== this.lastTextWasStroke (should be used without this.)

diegocr added a commit that referenced this pull request Nov 28, 2014
HTML5 CanvasRenderingContext2D interface
@diegocr diegocr merged commit eafb2e4 into parallax:master Nov 28, 2014
@Flamenco
Copy link
Contributor Author

lastTextWasStroke !== this.lastTextWasStroke

I am referring to the variable declared in the pdf object on line 202, not global or function scope. Will lastTextWasStroke without this refer to that variable? What am I missing?

Ideally this should be something like pageContext.lastTextWasStroke. A page context is not yet available, but is on my todo list.

@Flamenco Flamenco deleted the c2d branch November 28, 2014 17:35
@diegocr
Copy link
Contributor

diegocr commented Nov 29, 2014

In 202 you're declaring a private variable within the jsPDF constructor scope.

For API/PlugIns functions, this refers to the API object declared at line 200, this should illustrate it:

function jsPDF()
{
    var something = "priv";
    var API = {
        publicv : "Doh"
    }
    API.text = function()
    {
        console.log(typeof this.something); // undefined
        console.log(typeof this.publicv);  // string
    };
}

For that usecase your code wouldn't cause any problem, but i wanted to let you be aware of this.

I'd keep that as-is and remove the this. since that also means our build script will convert that to a one-letter variable :)

@Flamenco
Copy link
Contributor Author

Thanks for the example. I see what you mean. I did not realize that the variable without the object scope would refer to the function var (not the global var).

The current version uses a page context variable, so the case is no longer relevant.

Here is an expanded example for future reference.

Thanks!

BTW, The insert, delete, and move page functions have removed my current need to change the rendering processing, as I can insert my TOC and title page. I would like to combine pages[], pagedim{}, and pagesContext{} into a single object now. pageInfo{pageNumber, out[], dim{}, context{}, objId}. Are you ok with that? It will make the mentioned page arrange methods more extensible for plugins (such as the annotation plugin that creates its own pagenumber indexed variable.

<html>
<head>
<script>

var something = 'global';

function jsPDF()
{
    var something = "priv";
    var API = {
        publicv : "Doh"
    }
    this.API = API;
    API.text = function()
    {
        console.log(typeof this.something); // undefined
        console.log(typeof this.publicv);  // string

        console.log(typeof something); // string ('priv')
        console.log(typeof publicv);  // undefined
    };
}

var pdf = new jsPDF();
pdf.API.text();
</script>
</head>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants