Module: Uniword::Docx::Reconciler::Parts

Included in:
Uniword::Docx::Reconciler
Defined in:
lib/uniword/docx/reconciler/parts.rb

Overview

Profile-dependent support parts reconciliation.

Populates settings, font table, styles, numbering, web settings, and document/app/core properties with profile-appropriate defaults. Only runs when a profile is provided.

Instance Method Summary collapse

Instance Method Details

#reconcile_app_propertiesObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/uniword/docx/reconciler/parts.rb', line 183

def reconcile_app_properties
  return unless profile

  app = package.app_properties
  app ||= begin
    package.app_properties = Ooxml::AppProperties.new
    package.app_properties
  end

  app.template = "Normal.dotm"
  app.application = profile.application_name
  app.app_version = profile.app_version
  if profile.user_company && !profile.user_company.empty?
    app.company = profile.user_company
  end

  unless app.pages && !app.pages.to_s.empty?
    stats = calculate_document_statistics
    app.pages = stats[:pages].to_s
    app.words = stats[:words].to_s
    app.characters = stats[:characters].to_s
    app.characters_with_spaces = stats[:characters_with_spaces].to_s
    app.paragraphs = stats[:paragraphs].to_s
    app.lines = stats[:lines].to_s
  end

  app.total_time = app.total_time || "0"
  app.scale_crop = app.scale_crop || "false"
  app.doc_security = app.doc_security || "0"
  app.links_up_to_date = app.links_up_to_date || "false"
  app.shared_doc = app.shared_doc || "false"
  app.hyperlinks_changed = app.hyperlinks_changed || "false"

  app.heading_pairs = nil
  app.titles_of_parts = nil

  record_fix(FixCodes::APP_PROPERTIES_ENSURED, "Ensured app properties with statistics")
end

#reconcile_core_propertiesObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/uniword/docx/reconciler/parts.rb', line 222

def reconcile_core_properties
  return unless profile

  old_cp = package.core_properties
  package.core_properties = if old_cp
                             Ooxml::CoreProperties.new(
                               title: old_cp.title,
                               subject: old_cp.subject,
                               creator: old_cp.creator,
                               keywords: old_cp.keywords,
                               description: old_cp.description,
                               last_modified_by: old_cp.last_modified_by,
                               revision: old_cp.revision,
                               created: old_cp.created,
                               modified: old_cp.modified,
                             )
                           else
                             Ooxml::CoreProperties.new
                           end
  cp = package.core_properties

  if profile.user_name && !profile.user_name.empty?
    cp.last_modified_by = profile.user_name
    cp.creator ||= profile.user_name
  end

  cp.last_modified_by ||= profile.application_name

  now = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
  cp.modified = Ooxml::Types::DctermsModifiedType.new(
    value: now, type: "dcterms:W3CDTF",
  )
  cp.created ||= Ooxml::Types::DctermsCreatedType.new(
    value: now, type: "dcterms:W3CDTF",
  )

  cp.revision = "1" unless cp.revision
  record_fix(FixCodes::CORE_PROPERTIES_REBUILT, "Rebuilt core properties with namespace declarations")
end

#reconcile_font_tableObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/uniword/docx/reconciler/parts.rb', line 72

def reconcile_font_table
  return unless profile

  font_table = package.font_table
  font_table ||= begin
    package.font_table = Wordprocessingml::FontTable.new
    record_fix(FixCodes::FONT_TABLE_CREATED, "Created font table")
    package.font_table
  end

  set_mc_ignorable(font_table)

  return unless font_table.fonts.empty?

   = 
  return unless 

  font_names = font_names_for_profile
  font_names.each do |name|
    meta = [name]
    next unless meta

    sig_data = meta["sig"] || {}
    font = Wordprocessingml::Font.new(
      name: name,
      panose1: Wordprocessingml::Panose1.new(val: meta["panose1"]),
      charset: Wordprocessingml::Charset.new(val: meta["charset"]),
      family: Wordprocessingml::Family.new(val: meta["family"]),
      pitch: Wordprocessingml::Pitch.new(val: meta["pitch"]),
      sig: Wordprocessingml::Sig.new(
        usb0: sig_data["usb0"], usb1: sig_data["usb1"],
        usb2: sig_data["usb2"], usb3: sig_data["usb3"],
        csb0: sig_data["csb0"], csb1: sig_data["csb1"]
      ),
    )

    font.alt_name = Wordprocessingml::AltName.new(val: meta["alt_name"]) if meta["alt_name"]
    font_table.fonts << font
  end

  set_mc_ignorable(font_table)
  record_fix(FixCodes::FONT_TABLE_CREATED,
             "Populated font table with profile fonts and signatures")
end

#reconcile_numberingObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/uniword/docx/reconciler/parts.rb', line 136

def reconcile_numbering
  return unless profile
  return unless package.numbering

  set_mc_ignorable(package.numbering, prefixes: FULL_IGNORABLE)

  package.numbering.instances.each_with_index do |inst, idx|
    next if inst.durable_id

    raw = hex_derive("durableId:#{inst.num_id}:#{idx}", 4).to_i(16)
    raw = raw - 0x100000000 if raw >= 0x80000000
    inst.durable_id = raw.to_s
    record_fix(FixCodes::NUMBERING_REFERENCED,
               "Generated w16cid:durableId for numId=#{inst.num_id}")
  end

  package.numbering.instances.each do |inst|
    next unless inst.abstract_num_id

    abs_id = if inst.abstract_num_id.is_a?(Uniword::Wordprocessingml::AbstractNumId)
               inst.abstract_num_id.val
             else
               inst.abstract_num_id
             end
    defn = package.numbering.definitions.find do |d|
      d.abstract_num_id == abs_id
    end
    next if defn

    record_fix(FixCodes::NUMBERING_REFERENCED, "Numbering instance numId=#{inst.num_id} references " \
                     "missing abstractNumId=#{abs_id}")
  end
end

#reconcile_settingsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/uniword/docx/reconciler/parts.rb', line 12

def reconcile_settings
  return unless profile

  new_settings = package.settings.nil?
  settings = package.settings
  settings ||= begin
    package.settings = Wordprocessingml::Settings.new
    package.settings
  end

  rsid = generate_rsid

  settings.zoom ||= Wordprocessingml::Zoom.new(percent: 100)
  if new_settings && settings.do_not_display_page_boundaries.nil?
    settings.do_not_display_page_boundaries =
      Wordprocessingml::DoNotDisplayPageBoundaries.new
    ensure_element_in_order(settings, "doNotDisplayPageBoundaries",
                            after: "zoom")
  end
  settings.proof_state ||= Wordprocessingml::ProofState.new(
    spelling: "clean", grammar: "clean",
  )
  settings.default_tab_stop ||= Wordprocessingml::DefaultTabStop.new(val: "720")
  settings.character_spacing_control ||=
    Wordprocessingml::CharacterSpacingControl.new(val: "doNotCompress")

  settings.compat ||= build_compat
  settings.rsids ||= build_rsids(rsid)
  settings.math_pr ||= build_math_pr
  settings.theme_font_lang ||= Wordprocessingml::ThemeFontLang.new(
    val: profile.lang,
    east_asia: profile.east_asia_lang,
  )
  settings.clr_scheme_mapping ||= build_clr_scheme_mapping
  settings.decimal_symbol ||= Wordprocessingml::DecimalSymbol.new(
    val: profile.decimal_symbol,
  )
  settings.list_separator ||= Wordprocessingml::ListSeparator.new(
    val: profile.list_separator,
  )

  unless settings.w14_doc_id
    settings.w14_doc_id = Wordprocessingml::W14DocId.new(
      val: hex_derive("w14_doc_id", 4),
    )
    record_fix(FixCodes::DOC_ID_GENERATED, "Generated w14:docId")
  end
  unless settings.w15_doc_id
    raw = hex_derive("w15_doc_id", 16)
    formatted = "#{raw[0..7]}-#{raw[8..11]}-#{raw[12..15]}-" \
                "#{raw[16..19]}-#{raw[20..31]}"
    settings.w15_doc_id = Wordprocessingml::W15DocId.new(
      val: "{#{formatted.upcase}}",
    )
    record_fix(FixCodes::DOC_ID_GENERATED, "Generated w15:docId in GUID format")
  end

  set_mc_ignorable(settings)
end

#reconcile_stylesObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/uniword/docx/reconciler/parts.rb', line 117

def reconcile_styles
  return unless profile

  styles = package.styles
  styles ||= begin
    package.styles = Wordprocessingml::StylesConfiguration.new(include_defaults: false)
    package.styles
  end

  styles.doc_defaults ||= build_doc_defaults
  styles.latent_styles ||= build_latent_styles

  ensure_default_styles(styles)

  set_mc_ignorable(styles)
  record_fix(FixCodes::STYLE_DEFAULTS_ADDED,
             "Ensured styles have docDefaults, latentStyles, and default styles")
end

#reconcile_web_settingsObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/uniword/docx/reconciler/parts.rb', line 170

def reconcile_web_settings
  return unless profile

  ws = package.web_settings
  ws ||= begin
    package.web_settings = Wordprocessingml::WebSettings.new
    package.web_settings
  end

  set_mc_ignorable(ws)
  record_fix(FixCodes::MC_IGNORABLE, "Cleared mc:Ignorable on webSettings")
end