Skip to content

Commit

Permalink
Merge pull request #178 from mojavelinux/issue-9
Browse files Browse the repository at this point in the history
resolves #9 support inline images
  • Loading branch information
mojavelinux committed Jun 22, 2015
2 parents 0f46451 + df22c09 commit 2698943
Show file tree
Hide file tree
Showing 16 changed files with 575 additions and 294 deletions.
22 changes: 11 additions & 11 deletions data/themes/default-theme.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
font:
catalog:
NotoSerif:
Noto Serif:
normal: notoserif-regular-latin.ttf
bold: notoserif-bold-latin.ttf
italic: notoserif-italic-latin.ttf
bold_italic: notoserif-bolditalic-latin.ttf
Mplus1mn:
M+ 1mn:
normal: mplus1mn-regular-ascii-conums.ttf
bold: mplus1mn-bold-ascii.ttf
italic: mplus1mn-italic-ascii.ttf
bold_italic: mplus1mn-bolditalic-ascii.ttf
Mplus1pMultilingual:
M+ 1p Fallback:
normal: mplus1p-regular-multilingual.ttf
bold: mplus1p-regular-multilingual.ttf
italic: mplus1p-regular-multilingual.ttf
bold_italic: mplus1p-regular-multilingual.ttf
fallbacks:
# NOTE M+ 1p doesn't support all CJK characters, but it at least has some coverage
# NOTE M+ 1p provides the arrows for ->, <-, => and <=
- Mplus1pMultilingual
- M+ 1p Fallback
page:
background_color: ffffff
layout: portrait
Expand All @@ -35,7 +35,7 @@ base:
# color as CMYK array (approximated)
#font_color: [0, 0, 0, 0.92]
#font_color: [0, 0, 0, 92%]
font_family: NotoSerif
font_family: Noto Serif
# choose one of these font_size/line_height_length combinations
#font_size: 14
#line_height_length: 20
Expand All @@ -45,7 +45,7 @@ base:
#line_height_length: 16
font_size: 10.5
#line_height_length: 15
# correct line height for NotoSerif metrics
# correct line height for Noto Serif metrics
line_height_length: 12
#font_size: 11.25
#line_height_length: 18
Expand All @@ -60,15 +60,15 @@ base:
border_color: eeeeee
# FIXME vertical_rhythm is weird; we should think in terms of ems
#vertical_rhythm: $base_line_height_length * 2 / 3
# correct line height for NotoSerif metrics
# correct line height for Noto Serif metrics
vertical_rhythm: $base_line_height_length
horizontal_rhythm: $base_line_height_length
link:
font_color: 428bca
# literal is currently used for inline monospaced in prose and table cells
literal:
font_color: b12146
font_family: Mplus1mn
font_family: M+ 1mn
heading:
#font_color: 181818
font_color: $base_font_color
Expand All @@ -83,7 +83,7 @@ heading:
h6_font_size: $base_font_size_small
font_style: bold
#line_height: 1.4
# correct line height for NotoSerif metrics
# correct line height for Noto Serif metrics
line_height: 1.2
margin_top: $vertical_rhythm * 0.2
margin_bottom: $vertical_rhythm * 0.8
Expand Down Expand Up @@ -116,7 +116,7 @@ caption:
margin_outside: 0
code:
font_color: $base_font_color
#font_family: LiberationMono
#font_family: Liberation Mono
#font_size: floor($base_font_size * 0.9)
#font_size: 10
#padding: [9.5, 9.5, 9.5, 9.5]
Expand Down Expand Up @@ -158,7 +158,7 @@ admonition:
border_color: $base_border_color
border_width: $base_border_width
conum:
font_family: Mplus1mn
font_family: M+ 1mn
font_color: $literal_font_color
font_size: $base_font_size
line_height: 4 / 3
Expand Down
34 changes: 19 additions & 15 deletions docs/theming-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,14 @@ These fonts provide more characters than the built-in PDF fonts, but still only

The family name of the fonts bundled with Asciidoctor PDF are as follows:

http://www.google.com/get/noto/#/family/noto-serif[NotoSerif]::
http://www.google.com/get/noto/#/family/noto-serif[Noto Serif]::
A serif font that can be styled as normal, italic, bold or bold_italic.

http://mplus-fonts.osdn.jp/mplus-outline-fonts/design/index-en.html#mplus_1mn[Mplus1mn]::
http://mplus-fonts.osdn.jp/mplus-outline-fonts/design/index-en.html#mplus_1mn[M+ 1mn]::
A monospaced font that maps different thicknesses to the styles normal, italic, bold and bold_italic.
Also provides the circuled numbers used in callouts.

http://mplus-fonts.osdn.jp/mplus-outline-fonts/design/index-en.html#mplus_1p[Mplus1pMultilingual]::
http://mplus-fonts.osdn.jp/mplus-outline-fonts/design/index-en.html#mplus_1p[M+ 1p Fallback]::
A sans-serif font that provides a very complete set of Unicode glyphs.
Cannot be styled as italic, bold or bold_italic.
Useful as a fallback font.
Expand Down Expand Up @@ -595,13 +595,17 @@ font:
italic: roboto-italic.ttf
bold: roboto-bold.ttf
bold_italic: roboto-bold_italic.ttf
RobotoLight:
Roboto Light:
normal: roboto-light-normal.ttf
italic: roboto-light-italic.ttf
bold: roboto-light-bold.ttf
bold_italic: roboto-light-bold_italic.ttf
----

TIP: Text in SVGs will use the font catalog from your theme.
We recommend that you match the font key to the name of the font seen by the operating system.
This will allow you to use the same font names (aka families) in both your graphics program and Asciidoctor PDF.

=== Fallback fonts

If one of your fonts is missing a character that is used in a document, such as special symbols, you can tell Asciidoctor PDF to retrieve the character from a fallback font.
Expand Down Expand Up @@ -700,7 +704,7 @@ TBW

|base_font_family
|<<fonts,font family name>>
|font_family: NotoSerif
|font_family: Noto Serif

|base_font_size
|<<values,number>>
Expand Down Expand Up @@ -801,7 +805,7 @@ The literal key is used for inline monospaced text in prose and table cells.

|literal_font_family
|<<fonts,font family name>>
|font_family: Mplus1mn
|font_family: M+ 1mn

|literal_font_size
|<<values,number>>
Expand All @@ -824,7 +828,7 @@ The literal key is used for inline monospaced text in prose and table cells.

|heading_font_family
|<<fonts,font family name>>
|font_family: NotoSerif
|font_family: Noto Serif

|heading_font_size
|<<values,number>>
Expand Down Expand Up @@ -981,7 +985,7 @@ Block styles are applied to the following block types:

|caption_font_family
|<<fonts,font family name>>
|font_family: Mplus1mn
|font_family: M+ 1mn

|caption_font_size
|<<values,number>>
Expand Down Expand Up @@ -1016,7 +1020,7 @@ Block styles are applied to the following block types:

|code_font_family
|<<fonts,font family name>>
|font_family: Mplus1mn
|font_family: M+ 1mn

|code_font_size
|<<values,number>>
Expand Down Expand Up @@ -1063,7 +1067,7 @@ Block styles are applied to the following block types:

|blockquote_font_family
|<<fonts,font family name>>
|font_family: Notoserif
|font_family: Noto Serif

|blockquote_font_size
|<<values,number>>
Expand Down Expand Up @@ -1091,7 +1095,7 @@ Block styles are applied to the following block types:

|blockquote_cite_font_family
|<<fonts,font family name>>
|cite_font_family: Notoserif
|cite_font_family: Noto Serif

|blockquote_cite_font_style
|normal, italic, bold, bold_italic
Expand Down Expand Up @@ -1127,7 +1131,7 @@ Block styles are applied to the following block types:

|sidebar_title_font_family
|<<fonts,font family name>>
|title_font_family: NotoSerif
|title_font_family: Noto Serif

|sidebar_title_font_size
|<<values,number>>
Expand Down Expand Up @@ -1344,7 +1348,7 @@ Block styles are applied to the following block types:

|toc_font_family
|<<fonts,font family name>>
|font_family: NotoSerif
|font_family: Noto Serif

|toc_font_size
|<<values,number>>
Expand Down Expand Up @@ -1396,7 +1400,7 @@ Block styles are applied to the following block types:

|header_font_family
|<<fonts,font family name>>
|font_family: NotoSerif
|font_family: Noto Serif

|header_font_size
|<<values,number>>
Expand Down Expand Up @@ -1449,7 +1453,7 @@ Block styles are applied to the following block types:

|footer_font_family
|<<fonts,font family name>>
|font_family: NotoSerif
|font_family: Noto Serif

|footer_font_size
|<<values,number>>
Expand Down
2 changes: 2 additions & 0 deletions lib/asciidoctor-pdf/asciidoctor_ext.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# NOTE these are all candidates for inclusion in Asciidoctor core
require_relative 'asciidoctor_ext/section'
require_relative 'asciidoctor_ext/list'
require_relative 'asciidoctor_ext/list_item'
require_relative 'asciidoctor_ext/image'
51 changes: 32 additions & 19 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# encoding: UTF-8
# TODO cleanup imports...decide what belongs in asciidoctor-pdf.rb
require_relative 'core_ext'
require 'prawn'
require 'prawn-svg'
require 'prawn/table'
require 'prawn/templates'
require 'prawn/icon'
require_relative 'core_ext'
require_relative 'pdf_core_ext'
require_relative 'temporary_path'

This comment has been minimized.

Copy link
@msavy

msavy Jun 22, 2015

This require is broken when you build the project into a gem:

[msavy@mmbp pkg](master)$ asciidoctor-pdf --help
/Library/Ruby/Gems/2.0.0/gems/asciidoctor-pdf-1.5.0.dev/lib/asciidoctor-pdf/converter.rb:10:in `require_relative': cannot load such file -- /Library/Ruby/Gems/2.0.0/gems/asciidoctor-pdf-1.5.0.dev/lib/asciidoctor-pdf/temporary_path (LoadError)
    from /Library/Ruby/Gems/2.0.0/gems/asciidoctor-pdf-1.5.0.dev/lib/asciidoctor-pdf/converter.rb:10:in `<top (required)>'
    from /Library/Ruby/Gems/2.0.0/gems/asciidoctor-pdf-1.5.0.dev/lib/asciidoctor-pdf.rb:3:in `require_relative'
    from /Library/Ruby/Gems/2.0.0/gems/asciidoctor-pdf-1.5.0.dev/lib/asciidoctor-pdf.rb:3:in `<top (required)>'
    from /Library/Ruby/Gems/2.0.0/gems/asciidoctor-pdf-1.5.0.dev/bin/asciidoctor-pdf:3:in `require_relative'
    from /Library/Ruby/Gems/2.0.0/gems/asciidoctor-pdf-1.5.0.dev/bin/asciidoctor-pdf:3:in `<top (required)>'
    from /usr/bin/asciidoctor-pdf:23:in `load'
    from /usr/bin/asciidoctor-pdf:23:in `<main>'

Think this might have been debug code?

This comment has been minimized.

Copy link
@mojavelinux

mojavelinux via email Jun 22, 2015

Author Member

This comment has been minimized.

Copy link
@mojavelinux

mojavelinux Jun 22, 2015

Author Member

The missing files are now committed.

require_relative 'sanitizer'
require_relative 'prawn_ext'
require_relative 'formatted_text'
require_relative 'pdfmarks'
require_relative 'asciidoctor_ext'
require_relative 'theme_loader'
Expand Down Expand Up @@ -260,8 +262,7 @@ def build_pdf_options doc, theme

pdf_opts[:page_size] = (page_size || 'LETTER')

# FIXME change namespace of FormattedTextFormatter to ::Asciidoctor::Pdf (or deeper)
pdf_opts[:text_formatter] ||= ::Asciidoctor::Prawn::FormattedTextFormatter.new theme: theme
pdf_opts[:text_formatter] ||= FormattedText::Formatter.new theme: theme
pdf_opts
end

Expand Down Expand Up @@ -743,14 +744,13 @@ def convert_content_for_list_item node, opts = {}
end

def convert_image node
node.extend ::Asciidoctor::Image unless ::Asciidoctor::Image === node
valid_image = true
target = node.attr 'target'
# TODO file extension should be an attribute on an image node
image_type = (::File.extname target)[1..-1].downcase
target, image_type = node.target_with_image_type

if image_type == 'gif'
valid_image = false
warn %(asciidoctor: WARNING: GIF image format not supported. Please convert the image #{target} to PNG.)
warn %(asciidoctor: WARNING: GIF image format not supported. Please convert #{target} to PNG.)
#elsif image_type == 'pdf'
# import_page image_path
# return
Expand Down Expand Up @@ -1256,7 +1256,7 @@ def convert_inline_break node
end

def convert_inline_button node
%(<b>[#{NarrowNoBreakSpace}#{node.text}#{NarrowNoBreakSpace}]</b>)
%(<strong>[#{NarrowNoBreakSpace}#{node.text}#{NarrowNoBreakSpace}]</strong>)
end

def convert_inline_callout node
Expand Down Expand Up @@ -1305,7 +1305,23 @@ def convert_inline_image node
%([#{node.attr 'alt'}])
end
else
warn %(asciidoctor: WARNING: conversion missing in backend #{@backend} for inline_image)
node.extend ::Asciidoctor::Image unless ::Asciidoctor::Image === node
target, image_type = node.target_with_image_type
valid = true
if image_type == 'gif'
warn %(asciidoctor: WARNING: GIF image format not supported. Please convert #{target} to PNG.) unless scratch?
valid = false
end
unless (image_path = resolve_image_path node, target) && (::File.readable? image_path)
warn %(asciidoctor: WARNING: image to embed not found or not readable: #{image_path || target}) unless scratch?
valid = false
end
if valid
width_attr = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : nil
%(<img src="#{image_path}" type="#{image_type}" alt="#{node.attr 'alt'}"#{width_attr} tmp="#{TemporaryPath === image_path}">)
else
node.attr 'alt'
end
end
end

Expand Down Expand Up @@ -1498,7 +1514,7 @@ def layout_cover_page position, doc
end
# QUESTION should we go to page 1 when position == :front?
go_to_page page_count if position == :back
if (::File.extname cover_image) == '.pdf'
if cover_image.downcase.end_with? '.pdf'
import_page cover_image
else
image_page cover_image, canvas: true
Expand Down Expand Up @@ -2156,12 +2172,11 @@ def resolve_imagesdir doc
# the temporary file. If the target is a URI and the allow-uri-read attribute
# is not set, or the URI cannot be read, this method returns a nil value.
#
# When a temporary file is used, the file descriptor is assigned to the
# @tmp_file instance variable of the return string.
# When a temporary file is used, the TemporaryPath type is mixed into the path string.
def resolve_image_path node, image_path = nil, image_type = nil
imagesdir = resolve_imagesdir(doc = node.document)
image_path ||= (node.attr 'target', nil, false)
image_type ||= (::File.extname image_path)[1..-1]
image_type ||= ::Asciidoctor::Image.image_type image_path
# handle case when image is a URI
if (node.is_uri? image_path) || (imagesdir && (node.is_uri? imagesdir) &&
(image_path = (node.normalize_web_path image_path, image_base_uri, false)))
Expand All @@ -2179,7 +2194,7 @@ def resolve_image_path node, image_path = nil, image_type = nil
begin
open(image_path, (binary ? 'rb' : 'r')) {|fd| tmp_image.write(fd.read) }
tmp_image_path = tmp_image.path
tmp_image_path.instance_variable_set :@tmp_file, tmp_image
tmp_image_path.extend TemporaryPath
rescue
tmp_image_path = nil
ensure
Expand All @@ -2194,11 +2209,9 @@ def resolve_image_path node, image_path = nil, image_type = nil

# QUESTION is there a better way to do this?
# I suppose we could have @tmp_files as an instance variable on converter instead
def unlink_tmp_file holder
if (tmp_file = (holder.instance_variable_get :@tmp_file))
tmp_file.unlink
holder.remove_instance_variable :@tmp_file
end
# It might be sufficient to delete temporary files once per conversion
def unlink_tmp_file path
path.unlink if TemporaryPath === path
end

# QUESTION move to prawn/extensions.rb?
Expand Down
8 changes: 8 additions & 0 deletions lib/asciidoctor-pdf/formatted_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'treetop'
require 'set' unless defined? Set
require_relative 'formatted_text/parser'
require_relative 'formatted_text/transform'
require_relative 'formatted_text/formatter'
require_relative 'formatted_text/inline_destination_marker'
require_relative 'formatted_text/inline_image_arranger'
require_relative 'formatted_text/inline_image_renderer'
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
require 'treetop'
require 'set'
require_relative 'parser'
require_relative 'transform'

module Asciidoctor
module Prawn
class FormattedTextFormatter
module Pdf
module FormattedText
class Formatter
FormattingSnifferPattern = /[<&]/

def initialize options = {}
@parser = FormattedTextParser.new
@transform = FormattedTextTransform.new merge_adjacent_text_nodes: true, theme: options[:theme]
@parser = MarkupParser.new
@transform = Transform.new merge_adjacent_text_nodes: true, theme: options[:theme]
end

def format string, *args
Expand All @@ -27,3 +23,4 @@ def format string, *args
end
end
end
end
Loading

0 comments on commit 2698943

Please sign in to comment.