| index 83d76c5..a1d2562 100644 |
| --- a/internal/docx/read.go |
| +++ b/internal/docx/read.go |
| @@ -11,7 +11,6 @@ import ( |
| "strconv" |
| "strings" |
| "unicode" |
| - "unicode/utf8" |
| |
| "github.com/carlosframework/docs/internal/export" |
| "github.com/carlosframework/docs/internal/fonts" |
| @@ -99,10 +98,6 @@ const ( |
| // maxLocations is how many paragraph numbers one report item keeps; |
| // its Count is still exact. |
| maxLocations = 20 |
| - // maxFontNames bounds the face names the report quotes, and |
| - // maxFontName each name's length in bytes. |
| - maxFontNames = 20 |
| - maxFontName = 64 |
| ) |
| |
| // Import's refusals. Each is a whole answer: none of them says anything |
| @@ -134,12 +129,12 @@ type Report struct { |
| |
| // Item is one kind of loss: how many times it happened and, where it |
| // has one, the first maxLocations paragraphs (1-based, in the imported |
| -// document) it happened at. |
| +// document) it happened at. Closed metadata only: nothing from the file |
| +// is ever quoted, not even a face name, which is as arbitrary as text. |
| type Item struct { |
| - Code Code `json:"code"` |
| - Count int `json:"count"` |
| - Paragraphs []int `json:"paragraphs,omitempty"` |
| - Names []string `json:"names,omitempty"` |
| + Code Code `json:"code"` |
| + Count int `json:"count"` |
| + Paragraphs []int `json:"paragraphs,omitempty"` |
| } |
| |
| // Code names one kind of loss. The set is closed: Codes lists it. |
| @@ -198,11 +193,10 @@ func readPart(f *zip.File, budget *int) ([]byte, error) { |
| |
| type reporter struct { |
| items map[Code]*Item |
| - fonts map[string]bool |
| } |
| |
| func newReporter() *reporter { |
| - return &reporter{items: map[Code]*Item{}, fonts: map[string]bool{}} |
| + return &reporter{items: map[Code]*Item{}} |
| } |
| |
| // add counts one occurrence of code at paragraph (1-based; 0 for none). |
| @@ -218,26 +212,10 @@ func (r *reporter) add(code Code, paragraph int) { |
| } |
| } |
| |
| -func (r *reporter) font(name string, paragraph int) { |
| - r.add(CodeFontSubstituted, paragraph) |
| - if len(r.fonts) < maxFontNames { |
| - if len(name) > maxFontName { |
| - name = truncate(name, maxFontName) |
| - } |
| - r.fonts[name] = true |
| - } |
| -} |
| - |
| func (r *reporter) report() Report { |
| var out Report |
| for _, c := range Codes { |
| if it := r.items[c]; it != nil { |
| - if c == CodeFontSubstituted { |
| - for n := range r.fonts { |
| - it.Names = append(it.Names, n) |
| - } |
| - sort.Strings(it.Names) |
| - } |
| sort.Ints(it.Paragraphs) |
| out.Items = append(out.Items, *it) |
| } |
| @@ -245,13 +223,6 @@ func (r *reporter) report() Report { |
| return out |
| } |
| |
| -func truncate(s string, n int) string { |
| - for n > 0 && !utf8.RuneStart(s[n]) { |
| - n-- |
| - } |
| - return s[:n] |
| -} |
| - |
| // ---- styles |
| |
| const ( |
| @@ -261,9 +232,9 @@ const ( |
| |
| type style struct { |
| name, basedOn string |
| - font string |
| - size int // half-points, 0 for unset |
| + rp props // the style's own run properties |
| numbered bool |
| + aligned bool // centred or right-aligned |
| } |
| |
| // styleKind is what a paragraph style makes of a block. |
| @@ -280,21 +251,60 @@ type block struct { |
| runs []export.Node |
| } |
| |
| +// tri is an OOXML toggle as stated somewhere in the cascade: unset, |
| +// on, or explicitly off. An explicit off stops inheritance. |
| +type tri int8 |
| + |
| +const ( |
| + unset tri = iota |
| + on |
| + off |
| +) |
| + |
| +func triOf(b bool) tri { |
| + if b { |
| + return on |
| + } |
| + return off |
| +} |
| + |
| +// props are run properties at one level of the cascade: a run's own |
| +// w:rPr, a character style, a paragraph style, or the document default. |
| type props struct { |
| - bold, italic bool |
| - font string // a Word face name, "" for inherited |
| - size int // half-points, 0 for inherited |
| + bold, italic tri |
| + font string // a Word face name, "" for unset |
| + size int // half-points, 0 for unset |
| lost bool // underline, strike, colour, highlight, raised, caps |
| + rstyle string // a run's character style |
| +} |
| + |
| +// over is p with every property p leaves unset taken from base. Lost |
| +// formatting accumulates: a style's colour is as lost as a run's. |
| +func (p props) over(base props) props { |
| + if p.bold == unset { |
| + p.bold = base.bold |
| + } |
| + if p.italic == unset { |
| + p.italic = base.italic |
| + } |
| + if p.font == "" { |
| + p.font = base.font |
| + } |
| + if p.size == 0 { |
| + p.size = base.size |
| + } |
| + p.lost = p.lost || base.lost |
| + return p |
| } |
| |
| type reader struct { |
| dec *xml.Decoder |
| depth int |
| |
| - styles map[string]*style |
| + styles map[string]*style // paragraph styles |
| + charStyles map[string]*style |
| defaultStyle string |
| - baseFont string |
| - baseSize int |
| + base props // w:docDefaults' run properties |
| blocks []block |
| runs, text int |
| rep *reporter |
| @@ -414,6 +424,7 @@ func (r *reader) readStyles(b []byte) error { |
| r.dec = newDecoder(b) |
| r.depth = 0 |
| r.styles = map[string]*style{} |
| + r.charStyles = map[string]*style{} |
| if err := r.root("styles"); err != nil { |
| return err |
| } |
| @@ -428,12 +439,7 @@ func (r *reader) readStyles(b []byte) error { |
| if !isW(e, "rPr") { |
| return r.skip() |
| } |
| - p := props{} |
| - if err := r.runProps(&p); err != nil { |
| - return err |
| - } |
| - r.baseFont, r.baseSize = p.font, p.size |
| - return nil |
| + return r.runProps(&r.base) |
| }, nil) |
| }, nil) |
| case isW(e, "style"): |
| @@ -448,16 +454,14 @@ func (r *reader) readStyles(b []byte) error { |
| case isW(c, "basedOn"): |
| s.basedOn, _ = attr(c, "val") |
| case isW(c, "rPr"): |
| - p := props{} |
| - if err := r.runProps(&p); err != nil { |
| - return err |
| - } |
| - s.font, s.size = p.font, p.size |
| - return nil |
| + return r.runProps(&s.rp) |
| case isW(c, "pPr"): |
| return r.children(func(p xml.StartElement) error { |
| - if isW(p, "numPr") { |
| + switch { |
| + case isW(p, "numPr"): |
| s.numbered = true |
| + case isW(p, "jc"): |
| + s.aligned = alignedValue(p) |
| } |
| return r.skip() |
| }, nil) |
| @@ -467,11 +471,15 @@ func (r *reader) readStyles(b []byte) error { |
| if err != nil { |
| return err |
| } |
| - if kind == "paragraph" && id != "" { |
| + switch { |
| + case id == "": |
| + case kind == "paragraph": |
| r.styles[id] = s |
| if toggle1(def) { |
| r.defaultStyle = id |
| } |
| + case kind == "character": |
| + r.charStyles[id] = s |
| } |
| return nil |
| } |
| @@ -487,11 +495,15 @@ func toggle1(v string) bool { |
| // never revisiting one (a basedOn loop is a damaged file, read as far as |
| // it makes sense). |
| func (r *reader) chain(id string) []*style { |
| + return chainIn(r.styles, id) |
| +} |
| + |
| +func chainIn(styles map[string]*style, id string) []*style { |
| var out []*style |
| seen := map[string]bool{} |
| for id != "" && !seen[id] && len(out) < 10 { |
| seen[id] = true |
| - s := r.styles[id] |
| + s := styles[id] |
| if s == nil { |
| break |
| } |
| @@ -523,27 +535,50 @@ func (r *reader) kind(id string) styleKind { |
| return styleKind{} |
| } |
| |
| -// inherited is the face and size a run in a paragraph of this style |
| -// inherits: the style chain's, then the document default's. |
| -func (r *reader) inherited(id string) (font string, size int) { |
| +// inherited is what a run in a paragraph of this style inherits: the |
| +// style chain's run properties, nearest first, then the document |
| +// default's. |
| +func (r *reader) inherited(id string) props { |
| if id == "" { |
| id = r.defaultStyle |
| } |
| + var p props |
| for _, s := range r.chain(id) { |
| - if font == "" { |
| - font = s.font |
| - } |
| - if size == 0 { |
| - size = s.size |
| - } |
| + p = p.over(s.rp) |
| } |
| - if font == "" { |
| - font = r.baseFont |
| + return p.over(r.base) |
| +} |
| + |
| +// charInherited is a character style chain's run properties. |
| +func (r *reader) charInherited(id string) props { |
| + var p props |
| + for _, s := range chainIn(r.charStyles, id) { |
| + p = p.over(s.rp) |
| } |
| - if size == 0 { |
| - size = r.baseSize |
| + return p |
| +} |
| + |
| +func (r *reader) alignedStyle(id string) bool { |
| + if id == "" { |
| + id = r.defaultStyle |
| } |
| - return font, size |
| + for _, s := range r.chain(id) { |
| + if s.aligned { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// alignedValue is whether a w:jc is an alignment Docs does not import: |
| +// centred, right, or anything else that is not the start edge or |
| +// justification. |
| +func alignedValue(e xml.StartElement) bool { |
| + switch v, _ := attr(e, "val"); v { |
| + case "", "left", "start", "both", "distribute": |
| + return false |
| + } |
| + return true |
| } |
| |
| func (r *reader) numberedStyle(id string) bool { |
| @@ -661,9 +696,9 @@ func (r *reader) table() error { |
| type para struct { |
| style string |
| kind styleKind |
| - font string |
| - size int |
| + inh props // what its runs inherit |
| listMarked bool |
| + jcSet bool // the paragraph states its own alignment |
| } |
| |
| func (r *reader) newBlock(p *para) error { |
| @@ -711,11 +746,15 @@ func (r *reader) paragraph() error { |
| // settle applies the paragraph's style once pPr has been read. |
| func (r *reader) settle(p *para) { |
| p.kind = r.kind(p.style) |
| - p.font, p.size = r.inherited(p.style) |
| + p.inh = r.inherited(p.style) |
| if p.kind.heading > 0 { |
| - // A heading's size is its level's in Docs; only a size its runs |
| - // state themselves is carried. |
| - p.size = 0 |
| + // A heading looks like its level in Docs: the heading style's |
| + // own size, weight, slope and colour are the level's here, and |
| + // the import page says so. Only its face, and what its runs |
| + // state themselves, are carried. |
| + p.inh = props{font: p.inh.font} |
| + } else if !p.jcSet && r.alignedStyle(p.style) { |
| + r.rep.add(CodeAlignmentLost, r.here()) |
| } |
| r.blocks[len(r.blocks)-1].heading = p.kind.heading |
| switch { |
| @@ -753,9 +792,8 @@ func (r *reader) paragraphProps(p *para) error { |
| } |
| return nil |
| case isW(e, "jc"): |
| - switch v, _ := attr(e, "val"); v { |
| - case "", "left", "start", "both", "distribute": |
| - default: |
| + p.jcSet = true |
| + if alignedValue(e) { |
| r.rep.add(CodeAlignmentLost, r.here()) |
| } |
| case isW(e, "sectPr"): |
| @@ -846,9 +884,11 @@ func (r *reader) runProps(p *props) error { |
| return r.children(func(e xml.StartElement) error { |
| switch { |
| case isW(e, "b"): |
| - p.bold = toggle(e) |
| + p.bold = triOf(toggle(e)) |
| case isW(e, "i"): |
| - p.italic = toggle(e) |
| + p.italic = triOf(toggle(e)) |
| + case isW(e, "rStyle"): |
| + p.rstyle, _ = attr(e, "val") |
| case isW(e, "rFonts"): |
| if v, ok := attr(e, "ascii"); ok && v != "" { |
| p.font = v |
| @@ -921,23 +961,20 @@ func (r *reader) emit(p *para, rp props, s string) error { |
| } |
| run := export.Node{Type: "text", Text: s} |
| |
| + // The cascade: the run's own properties, its character style, its |
| + // paragraph's style, the document default. |
| + rp = rp.over(r.charInherited(rp.rstyle)).over(p.inh) |
| face := rp.font |
| - if face == "" { |
| - face = p.font |
| - } |
| if face != "" { |
| if f, ok := fonts.ByDocxName(face); ok { |
| if f != fonts.Default { |
| run.Marks = append(run.Marks, export.Mark{Type: f.Mark()}) |
| } |
| } else { |
| - r.rep.font(face, r.here()) |
| + r.rep.add(CodeFontSubstituted, r.here()) |
| } |
| } |
| size := rp.size |
| - if size == 0 { |
| - size = p.size |
| - } |
| if size != 0 && size != export.BodySizePt*2 { |
| if mark, ok := sizeMark(size); ok { |
| run.Marks = append(run.Marks, export.Mark{Type: mark}) |
| @@ -945,10 +982,10 @@ func (r *reader) emit(p *para, rp props, s string) error { |
| r.rep.add(CodeSizeDropped, r.here()) |
| } |
| } |
| - if rp.bold { |
| + if rp.bold == on { |
| run.Marks = append(run.Marks, export.Mark{Type: "strong"}) |
| } |
| - if rp.italic { |
| + if rp.italic == on { |
| run.Marks = append(run.Marks, export.Mark{Type: "em"}) |
| } |
| if rp.lost { |