175 if style_ == -1: |
175 if style_ == -1: |
176 styleNext = self.styleCurrent |
176 styleNext = self.styleCurrent |
177 |
177 |
178 buf = "" |
178 buf = "" |
179 if styleNext != self.styleCurrent or style_ == -1: |
179 if styleNext != self.styleCurrent or style_ == -1: |
180 if self.style[self.styleCurrent].font != \ |
180 if ( |
181 self.style[styleNext].font or style_ == -1: |
181 (self.style[self.styleCurrent].font != |
|
182 self.style[styleNext].font) or |
|
183 style_ == -1 |
|
184 ): |
182 buf += "/F{0:d} {1:d} Tf ".format( |
185 buf += "/F{0:d} {1:d} Tf ".format( |
183 self.style[styleNext].font + 1, self.fontSize) |
186 self.style[styleNext].font + 1, self.fontSize) |
184 if self.style[self.styleCurrent].fore != \ |
187 if ( |
185 self.style[styleNext].fore or style_ == -1: |
188 (self.style[self.styleCurrent].fore != |
|
189 self.style[styleNext].fore) or |
|
190 style_ == -1 |
|
191 ): |
186 buf += "{0}rg ".format(self.style[styleNext].fore) |
192 buf += "{0}rg ".format(self.style[styleNext].fore) |
187 return buf |
193 return buf |
188 |
194 |
189 def startPDF(self): |
195 def startPDF(self): |
190 """ |
196 """ |
195 |
201 |
196 # leading is the term for distance between lines |
202 # leading is the term for distance between lines |
197 self.leading = self.fontSize * PDF_SPACING_DEFAULT |
203 self.leading = self.fontSize * PDF_SPACING_DEFAULT |
198 |
204 |
199 # sanity check for page size and margins |
205 # sanity check for page size and margins |
200 pageWidthMin = int(self.leading) + \ |
206 pageWidthMin = ( |
201 self.pageMargins["left"] + self.pageMargins["right"] |
207 int(self.leading) + |
|
208 self.pageMargins["left"] + |
|
209 self.pageMargins["right"] |
|
210 ) |
202 if self.pageWidth < pageWidthMin: |
211 if self.pageWidth < pageWidthMin: |
203 self.pageWidth = pageWidthMin |
212 self.pageWidth = pageWidthMin |
204 pageHeightMin = int(self.leading) + \ |
213 pageHeightMin = ( |
205 self.pageMargins["top"] + self.pageMargins["bottom"] |
214 int(self.leading) + |
|
215 self.pageMargins["top"] + |
|
216 self.pageMargins["bottom"] |
|
217 ) |
206 if self.pageHeight < pageHeightMin: |
218 if self.pageHeight < pageHeightMin: |
207 self.pageHeight = pageHeightMin |
219 self.pageHeight = pageHeightMin |
208 |
220 |
209 # start to write PDF file here (PDF1.4Ref(p63)) |
221 # start to write PDF file here (PDF1.4Ref(p63)) |
210 # ASCII>127 characters to indicate binary-possible stream |
222 # ASCII>127 characters to indicate binary-possible stream |
213 |
225 |
214 # build objects for font resources; note that font objects are |
226 # build objects for font resources; note that font objects are |
215 # *expected* to start from index 1 since they are the first objects |
227 # *expected* to start from index 1 since they are the first objects |
216 # to be inserted (PDF1.4Ref(p317)) |
228 # to be inserted (PDF1.4Ref(p317)) |
217 for i in range(4): |
229 for i in range(4): |
218 buffer = \ |
230 buffer = ( |
219 "<</Type/Font/Subtype/Type1/Name/F{0:d}/BaseFont/{1}/" \ |
231 "<</Type/Font/Subtype/Type1/Name/F{0:d}/BaseFont/{1}/" |
220 "Encoding/{2}>>\n".format( |
232 "Encoding/{2}>>\n" |
221 i + 1, PDFfontNames[self.fontSet * 4 + i], PDF_ENCODING) |
233 ).format( |
|
234 i + 1, PDFfontNames[self.fontSet * 4 + i], PDF_ENCODING |
|
235 ) |
222 self.oT.add(buffer) |
236 self.oT.add(buffer) |
223 |
237 |
224 self.pageContentStart = self.oT.index |
238 self.pageContentStart = self.oT.index |
225 |
239 |
226 def endPDF(self): |
240 def endPDF(self): |
239 # create all the page objects (PDF1.4Ref(p88)) |
253 # create all the page objects (PDF1.4Ref(p88)) |
240 # forward reference pages object; calculate its object number |
254 # forward reference pages object; calculate its object number |
241 pageObjectStart = self.oT.index |
255 pageObjectStart = self.oT.index |
242 pagesRef = pageObjectStart + self.pageCount |
256 pagesRef = pageObjectStart + self.pageCount |
243 for i in range(self.pageCount): |
257 for i in range(self.pageCount): |
244 buffer = "<</Type/Page/Parent {0:d} 0 R\n" \ |
258 buffer = ( |
245 "/MediaBox[ 0 0 {1:d} {2:d}]\n" \ |
259 "<</Type/Page/Parent {0:d} 0 R\n" |
246 "/Contents {3:d} 0 R\n" \ |
260 "/MediaBox[ 0 0 {1:d} {2:d}]\n" |
247 "/Resources {4:d} 0 R\n>>\n".format( |
261 "/Contents {3:d} 0 R\n" |
248 pagesRef, self.pageWidth, self.pageHeight, |
262 "/Resources {4:d} 0 R\n>>\n" |
249 self.pageContentStart + i, resourceRef) |
263 ).format( |
|
264 pagesRef, self.pageWidth, self.pageHeight, |
|
265 self.pageContentStart + i, resourceRef |
|
266 ) |
250 self.oT.add(buffer) |
267 self.oT.add(buffer) |
251 |
268 |
252 # create page tree object (PDF1.4Ref(p86)) |
269 # create page tree object (PDF1.4Ref(p86)) |
253 self.pageData = "<</Type/Pages/Kids[\n" |
270 self.pageData = "<</Type/Pages/Kids[\n" |
254 for i in range(self.pageCount): |
271 for i in range(self.pageCount): |
262 |
279 |
263 # append the cross reference table (PDF1.4Ref(p64)) |
280 # append the cross reference table (PDF1.4Ref(p64)) |
264 xref = self.oT.xref() |
281 xref = self.oT.xref() |
265 |
282 |
266 # end the file with the trailer (PDF1.4Ref(p67)) |
283 # end the file with the trailer (PDF1.4Ref(p67)) |
267 buffer = \ |
284 buffer = ( |
268 "trailer\n<< /Size {0:d} /Root {1:d} 0 R\n>>\nstartxref\n{2:d}\n" \ |
285 "trailer\n<< /Size {0:d} /Root {1:d} 0 R\n>>\nstartxref\n{2:d}\n" |
269 "%%EOF\n".format(self.oT.index, catalogRef, xref) |
286 "%%EOF\n" |
|
287 ).format(self.oT.index, catalogRef, xref) |
270 self.oT.write(buffer) |
288 self.oT.write(buffer) |
271 |
289 |
272 def add(self, ch, style_): |
290 def add(self, ch, style_): |
273 """ |
291 """ |
274 Public method to add a character to the page. |
292 Public method to add a character to the page. |
481 if lex: |
499 if lex: |
482 istyle = 0 |
500 istyle = 0 |
483 while istyle <= QsciScintilla.STYLE_MAX: |
501 while istyle <= QsciScintilla.STYLE_MAX: |
484 if (istyle <= QsciScintilla.STYLE_DEFAULT or |
502 if (istyle <= QsciScintilla.STYLE_DEFAULT or |
485 istyle > QsciScintilla.STYLE_LASTPREDEFINED): |
503 istyle > QsciScintilla.STYLE_LASTPREDEFINED): |
486 if lex.description(istyle) or \ |
504 if ( |
487 istyle == QsciScintilla.STYLE_DEFAULT: |
505 lex.description(istyle) or |
|
506 istyle == QsciScintilla.STYLE_DEFAULT |
|
507 ): |
488 style = PDFStyle() |
508 style = PDFStyle() |
489 |
509 |
490 font = lex.font(istyle) |
510 font = lex.font(istyle) |
491 if font.italic(): |
511 if font.italic(): |
492 style.font |= 2 |
512 style.font |= 2 |
556 # expand tabs |
576 # expand tabs |
557 ts = tabSize - (column % tabSize) |
577 ts = tabSize - (column % tabSize) |
558 column += ts |
578 column += ts |
559 self.pr.add(' ' * ts, style) |
579 self.pr.add(' ' * ts, style) |
560 elif ch == b'\r' or ch == b'\n': |
580 elif ch == b'\r' or ch == b'\n': |
561 if ch == b'\r' and \ |
581 if ( |
562 self.editor.byteAt(pos + 1) == b'\n': |
582 ch == b'\r' and |
|
583 self.editor.byteAt(pos + 1) == b'\n' |
|
584 ): |
563 pos += 1 |
585 pos += 1 |
564 # close and begin a newline... |
586 # close and begin a newline... |
565 self.pr.nextLine() |
587 self.pr.nextLine() |
566 column = 0 |
588 column = 0 |
567 else: |
589 else: |